~ubuntu-branches/ubuntu/maverick/xosview/maverick

« back to all changes in this revision

Viewing changes to .pc/25_iostream_h_FTBFS_fixes.diff/bitfieldmeter.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2010-06-01 20:02:57 UTC
  • mfrom: (6.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100601200257-orz2mey0oilujocu
Tags: 1.8.3+debian-21
* debian/source/format:
  + Added to use source format 3.0 (quilt)
* debian/watch:
  + Fixed dversionmangle format
* Update debian/compat to 7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  Copyright (c) 1999, 2006 Thomas Waldmann ( ThomasWaldmann@gmx.de )
 
3
//  based on work of Mike Romberg ( mike.romberg@noaa.gov )
 
4
//
 
5
//  This file may be distributed under terms of the GPL
 
6
//
 
7
//
 
8
#ifdef HAVE_FSTREAM
 
9
#include <fstream>
 
10
#else
 
11
#include <fstream.h>
 
12
#endif
 
13
#include <stdlib.h>
 
14
#include "snprintf.h"
 
15
#include "general.h"
 
16
#include "bitfieldmeter.h"
 
17
#include "xosview.h"
 
18
 
 
19
 
 
20
BitFieldMeter::BitFieldMeter( XOSView *parent, int numBits, int numfields,
 
21
                        const char *title,
 
22
                        const char *bitslegend, const char *FieldLegend,
 
23
                        int docaptions, int dolegends, int dousedlegends )
 
24
: Meter(parent, title, bitslegend, docaptions, dolegends, dousedlegends){
 
25
    /*  We need to set print_ to something valid -- the meters
 
26
     *  apparently get drawn before the meters have a chance to call
 
27
     *  CheckResources() themselves.  */
 
28
  bits_ = NULL;
 
29
  lastbits_ = NULL;
 
30
  numWarnings_ = printedZeroTotalMesg_ = 0;
 
31
  print_ = PERCENT;
 
32
  used_ = 0;
 
33
  lastused_ = -1;
 
34
  fields_ = NULL;
 
35
  colors_ = NULL;
 
36
  lastvals_ = NULL;
 
37
  lastx_ = NULL;
 
38
  setNumBits(numBits);
 
39
  fieldLegend_ = NULL;
 
40
  setfieldlegend(FieldLegend);
 
41
  setNumFields(numfields);
 
42
}
 
43
 
 
44
void
 
45
BitFieldMeter::disableMeter ( )
 
46
{
 
47
  setNumFields(1);
 
48
  setfieldcolor (0, "grey");
 
49
  setfieldlegend ("Disabled");
 
50
  total_ = fields_[0] = 1.0;
 
51
  setNumBits(1);
 
52
  offColor_ = onColor_ = parent_->allocColor("grey");
 
53
}
 
54
 
 
55
 
 
56
BitFieldMeter::~BitFieldMeter( void ){
 
57
  delete[] bits_;
 
58
  delete[] lastbits_;
 
59
  delete[] fields_;
 
60
  delete[] colors_;
 
61
  delete[] lastvals_;
 
62
  delete[] lastx_;
 
63
}
 
64
 
 
65
void BitFieldMeter::checkResources( void ){
 
66
  Meter::checkResources();
 
67
  usedcolor_ = parent_->allocColor( parent_->getResource( "usedLabelColor") );
 
68
}
 
69
 
 
70
 
 
71
void BitFieldMeter::setNumBits(int n){
 
72
  numbits_ = n;
 
73
  delete[] bits_;
 
74
  delete[] lastbits_;
 
75
 
 
76
  bits_ = new char[numbits_];
 
77
  lastbits_ = new char[numbits_];
 
78
 
 
79
  for ( int i = 0 ; i < numbits_ ; i++ )
 
80
      bits_[i] = lastbits_[i] = 0;
 
81
}
 
82
 
 
83
void BitFieldMeter::SetUsedFormat ( const char * const fmt ) {
 
84
    /*  Do case-insensitive compares.  */
 
85
  if (!strncasecmp (fmt, "percent", 8))
 
86
    print_ = PERCENT;
 
87
  else if (!strncasecmp (fmt, "autoscale", 10))
 
88
    print_ = AUTOSCALE;
 
89
  else if (!strncasecmp (fmt, "float", 6))
 
90
    print_ = FLOAT;
 
91
  else
 
92
  {
 
93
    fprintf (stderr, "Error:  could not parse format of '%s'\n", fmt);
 
94
    fprintf (stderr, "  I expected one of 'percent', 'bytes', or 'float'\n");
 
95
    fprintf (stderr, "  (Case-insensitive)\n");
 
96
    exit(1);
 
97
  }
 
98
}
 
99
 
 
100
void BitFieldMeter::setUsed (float val, float total)
 
101
{
 
102
  if (print_ == FLOAT)
 
103
    used_ = val;
 
104
  else if (print_ == PERCENT)
 
105
  {
 
106
    if (total != 0.0)
 
107
      used_ = val / total * 100.0;
 
108
    else
 
109
    {
 
110
      if (!printedZeroTotalMesg_) {
 
111
        printedZeroTotalMesg_ = 1;
 
112
        fprintf(stderr, "Warning:  %s meter had a zero total "
 
113
                "field!  Would have caused a div-by-zero "
 
114
                "exception.\n", name());
 
115
      }
 
116
      used_ = 0.0;
 
117
    }
 
118
  }
 
119
  else if (print_ == AUTOSCALE)
 
120
    used_ = val;
 
121
  else {
 
122
    fprintf (stderr, "Error in %s:  I can't handle a "
 
123
                     "UsedType enum value of %d!\n", name(), print_);
 
124
    exit(1);
 
125
  }
 
126
}
 
127
 
 
128
void BitFieldMeter::reset( void ){
 
129
  for ( int i = 0 ; i < numfields_ ; i++ )
 
130
    lastvals_[i] = lastx_[i] = -1;
 
131
}
 
132
 
 
133
void BitFieldMeter::setfieldcolor( int field, const char *color ){
 
134
  colors_[field] = parent_->allocColor( color );
 
135
}
 
136
 
 
137
void BitFieldMeter::setfieldcolor( int field, unsigned long color ) {
 
138
  colors_[field] = color;
 
139
}
 
140
 
 
141
 
 
142
void BitFieldMeter::draw( void ){
 
143
    /*  Draw the outline for the fieldmeter.  */
 
144
  parent_->setForeground( parent_->foreground() );
 
145
  parent_->lineWidth( 1 );
 
146
  parent_->drawFilledRectangle( x_ - 1, y_ - 1, width_/2 + 2, height_ + 2 );
 
147
 
 
148
// ??  parent_->lineWidth( 0 );
 
149
 
 
150
  parent_->drawRectangle( x_ + width_/2 +3, y_ - 1, width_/2 - 2, height_ + 2 );
 
151
  if ( dolegends_ ){
 
152
    parent_->setForeground( textcolor_ );
 
153
 
 
154
    int offset;
 
155
    if ( dousedlegends_ )
 
156
      offset = parent_->textWidth( "XXXXXXXXX" );
 
157
    else
 
158
      offset = parent_->textWidth( "XXXXX" );
 
159
 
 
160
    parent_->drawString( x_ - offset + 1, y_ + height_, title_ );
 
161
 
 
162
    if(docaptions_){
 
163
      parent_->setForeground( onColor_ );
 
164
      parent_->drawString( x_, y_ - 5, legend_ );
 
165
      drawfieldlegend();
 
166
    }
 
167
  }
 
168
  drawBits( 1 );
 
169
  drawfields( 1 );
 
170
}
 
171
 
 
172
void BitFieldMeter::drawfieldlegend( void ){
 
173
  char *tmp1, *tmp2, buff[100];
 
174
  int n, x = x_ + width_/2 + 4;
 
175
 
 
176
  tmp1 = tmp2 = fieldLegend_;
 
177
  for ( int i = 0 ; i < numfields_ ; i++ ){
 
178
    n = 0;
 
179
    while ( (*tmp2 != '/') && (*tmp2 != '\0') ){
 
180
      tmp2++;
 
181
      n++;
 
182
    }
 
183
    tmp2++;
 
184
    strncpy( buff, tmp1, n );
 
185
    buff[n] = '\0';
 
186
    parent_->setStippleN(i%4);
 
187
    parent_->setForeground( colors_[i] );
 
188
    parent_->drawString( x, y_ - 5, buff );
 
189
    x += parent_->textWidth( buff, n );
 
190
    parent_->setForeground( parent_->foreground() );
 
191
    if ( i != numfields_ - 1 )
 
192
      parent_->drawString( x, y_ - 5, "/" );
 
193
    x += parent_->textWidth( "/", 1 );
 
194
    tmp1 = tmp2;
 
195
  }
 
196
  parent_->setStippleN(0);      /*  Restore default all-bits stipple.  */
 
197
}
 
198
 
 
199
void BitFieldMeter::drawused( int manditory ){
 
200
  if ( !manditory )
 
201
    if ( (lastused_ == used_) )
 
202
      return;
 
203
 
 
204
  parent_->setStippleN(0);      /*  Use all-bits stipple.  */
 
205
  static const int onechar = parent_->textWidth( "X" );
 
206
  static int xoffset = parent_->textWidth( "XXXXX" );
 
207
 
 
208
  char buf[10];
 
209
 
 
210
  if (print_ == PERCENT){
 
211
    snprintf( buf, 10, "%d%%", (int)used_ );
 
212
  }
 
213
  else if (print_ == AUTOSCALE){
 
214
    char scale;
 
215
    float scaled_used;
 
216
      /*  Unfortunately, we have to do our comparisons by 1000s (otherwise
 
217
       *  a value of 1020, which is smaller than 1K, could end up
 
218
       *  being printed as 1020, which is wider than what can fit)  */
 
219
      /*  However, we do divide by 1024, so a K really is a K, and not
 
220
       *  1000.  */
 
221
      /*  In addition, we need to compare against 999.5*1000, because
 
222
       *  999.5, if not rounded up to 1.0 K, will be rounded by the
 
223
       *  %.0f to be 1000, which is too wide.  So anything at or above
 
224
       *  999.5 needs to be bumped up.  */
 
225
    if (used_ >= 999.5*1000*1000*1000*1000*1000*1000)
 
226
        {scale='E'; scaled_used = used_/1024/1024/1024/1024/1024/1024;}
 
227
    else if (used_ >= 999.5*1000*1000*1000*1000)
 
228
        {scale='P'; scaled_used = used_/1024/1024/1024/1024/1024;}
 
229
    else if (used_ >= 999.5*1000*1000*1000)
 
230
        {scale='T'; scaled_used = used_/1024/1024/1024/1024;}
 
231
    else if (used_ >= 999.5*1000*1000)
 
232
        {scale='G'; scaled_used = used_/1024/1024/1024;}
 
233
    else if (used_ >= 999.5*1000)
 
234
        {scale='M'; scaled_used = used_/1024/1024;}
 
235
    else if (used_ >= 999.5)
 
236
        {scale='K'; scaled_used = used_/1024;}
 
237
    else {scale=' '; scaled_used = used_;}
 
238
      /*  For now, we can only print 3 characters, plus the optional
 
239
       *  suffix, without overprinting the legends.  Thus, we can
 
240
       *  print 965, or we can print 34, but we can't print 34.7 (the
 
241
       *  decimal point takes up one character).  bgrayson   */
 
242
    if (scaled_used == 0.0)
 
243
      snprintf (buf, 10, "0");
 
244
    else if (scaled_used < 9.95)  //  9.95 or above would get
 
245
                                  //  rounded to 10.0, which is too wide.
 
246
      snprintf (buf, 10, "%.1f%c", scaled_used, scale);
 
247
    /*  We don't need to check against 99.5 -- it all gets %.0f.  */
 
248
    /*else if (scaled_used < 99.5)*/
 
249
      /*snprintf (buf, 10, "%.0f%c", scaled_used, scale);*/
 
250
    else
 
251
      snprintf (buf, 10, "%.0f%c", scaled_used, scale);
 
252
  }
 
253
  else {
 
254
    snprintf( buf, 10, "%.1f", used_ );
 
255
  }
 
256
 
 
257
  parent_->clear( x_ - xoffset, y_ + height_ - parent_->textHeight(),
 
258
                 xoffset - onechar / 2, parent_->textHeight() + 1 );
 
259
  parent_->setForeground( usedcolor_ );
 
260
  parent_->drawString( x_ - (strlen( buf ) + 1 ) * onechar + 2,
 
261
                      y_ + height_, buf );
 
262
 
 
263
  lastused_ = used_;
 
264
}
 
265
 
 
266
void BitFieldMeter::drawBits( int manditory ){
 
267
  static int pass = 1;
 
268
 
 
269
//  pass = (pass + 1) % 2;
 
270
 
 
271
  int x1 = x_, w;
 
272
 
 
273
  w = (width_/2 - (numbits_+1)) / numbits_;
 
274
 
 
275
  for ( int i = 0 ; i < numbits_ ; i++ ){
 
276
    if ( (bits_[i] != lastbits_[i]) || manditory ){
 
277
      if ( bits_[i] && pass )
 
278
        parent_->setForeground( onColor_ );
 
279
      else
 
280
        parent_->setForeground( offColor_ );
 
281
      parent_->drawFilledRectangle( x1, y_, w, height_);
 
282
    }
 
283
 
 
284
    lastbits_[i] = bits_[i];
 
285
 
 
286
    x1 += (w + 2);
 
287
  }
 
288
}
 
289
 
 
290
void BitFieldMeter::drawfields( int manditory ){
 
291
  int twidth, x = x_ + width_/2 + 4;
 
292
 
 
293
  if ( total_ == 0 )
 
294
    return;
 
295
 
 
296
  for ( int i = 0 ; i < numfields_ ; i++ ){
 
297
    /*  Look for bogus values.  */
 
298
    if (fields_[i] < 0.0) {
 
299
      /*  Only print a warning 5 times per meter, followed by a
 
300
       *  message about no more warnings.  */
 
301
      numWarnings_ ++;
 
302
      if (numWarnings_ < 5)
 
303
        fprintf(stderr, "Warning:  meter %s had a negative "
 
304
          "value of %f for field %d\n", name(), fields_[i], i);
 
305
      if (numWarnings_ == 5)
 
306
        fprintf(stderr, "Future warnings from the %s meter "
 
307
          "will not be displayed.\n", name());
 
308
    }
 
309
 
 
310
    twidth = (int) (((width_/2 - 3) * (float) fields_[i]) / total_);
 
311
//    twidth = (int)((fields_[i] * width_) / total_);
 
312
    if ( (i == numfields_ - 1) && ((x + twidth) != (x_ + width_)) )
 
313
      twidth = width_ + x_ - x;
 
314
 
 
315
    if ( manditory || (twidth != lastvals_[i]) || (x != lastx_[i]) ){
 
316
      parent_->setForeground( colors_[i] );
 
317
      parent_->setStippleN(i%4);
 
318
      parent_->drawFilledRectangle( x, y_, twidth, height_ );
 
319
      parent_->setStippleN(0);  /*  Restore all-bits stipple.  */
 
320
      lastvals_[i] = twidth;
 
321
      lastx_[i] = x;
 
322
 
 
323
      if ( dousedlegends_ )
 
324
        drawused( manditory );
 
325
    }
 
326
    x += twidth;
 
327
  }
 
328
 
 
329
  //parent_->flush();
 
330
}
 
331
 
 
332
void BitFieldMeter::checkevent( void ){
 
333
    drawBits();
 
334
    drawfields();
 
335
}
 
336
 
 
337
void BitFieldMeter::setBits(int startbit, unsigned char values){
 
338
  unsigned char mask = 1;
 
339
  for (int i = startbit ; i < startbit + 8 ; i++){
 
340
    bits_[i] = values & mask;
 
341
    mask = mask << 1;
 
342
  }
 
343
}
 
344
 
 
345
void BitFieldMeter::setNumFields(int n){
 
346
  numfields_ = n;
 
347
  delete[] fields_;
 
348
  delete[] colors_;
 
349
  delete[] lastvals_;
 
350
  delete[] lastx_;
 
351
  fields_ = new float[numfields_];
 
352
  colors_ = new unsigned long[numfields_];
 
353
  lastvals_ = new int[numfields_];
 
354
  lastx_ = new int[numfields_];
 
355
 
 
356
  total_ = 0;
 
357
  for ( int i = 0 ; i < numfields_ ; i++ ){
 
358
    fields_[i] = 0.0;             /* egcs 2.91.66 bug !? don't do this and */
 
359
    lastvals_[i] = lastx_[i] = 0; /* that in a single statement or it'll   */
 
360
                                  /* overwrite too much with 0 ...         */
 
361
                                  /* Thomas Waldmann ( tw@com-ma.de )      */
 
362
  }
 
363
}
 
364
 
 
365
bool BitFieldMeter::checkX(int x, int width) const {
 
366
  if ((x < x_) || (x + width < x_)
 
367
      || (x > x_ + width_) || (x + width > x_ + width_)){
 
368
    std::cerr << "BitFieldMeter::checkX() : bad horiz values for meter : "
 
369
         << name() << std::endl;
 
370
 
 
371
    std::cerr <<"value "<<x<<", width "<<width<<", total_ = "<<total_<<std::endl;
 
372
 
 
373
    for (int i = 0 ; i < numfields_ ; i++)
 
374
      std::cerr <<"fields_[" <<i <<"] = " <<fields_[i] <<",";
 
375
    std::cerr <<std::endl;
 
376
 
 
377
    return false;
 
378
  }
 
379
 
 
380
  return true;
 
381
}
 
382
 
 
383
 
 
384
void BitFieldMeter::setfieldlegend( const char *fieldlegend ){
 
385
  delete[] fieldLegend_;
 
386
  int len = strlen(fieldlegend);
 
387
  fieldLegend_ = new char[len + 1];
 
388
  strncpy( fieldLegend_, fieldlegend, len );
 
389
  fieldLegend_[len] = '\0'; // strncpy() will not null terminate if s2 > len
 
390
}