xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

meter.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
3 //
4 // This file may be distributed under terms of the GPL
5 //
6 
7 #include "meter.h"
8 #include <string.h>
9 
10 
11 Meter::Meter( XOSView *parent, const char *title, const char *legend,
12  int docaptions, int dolegends, int dousedlegends ) {
13  title_ = legend_ = NULL;
16  parent_ = parent;
20  priority_ = 1;
21  counter_ = 0;
22  resize( parent->xoff(), parent->newypos(), parent->width() - 10, 10 );
23 
24 }
25 
26 Meter::~Meter( void ){
27  if ( title_ )
28  delete[] title_;
29  if ( legend_ )
30  delete[] legend_;
31 }
32 
33 void Meter::checkResources( void ){
34  textcolor_ = parent_->allocColor( parent_->getResource( "meterLabelColor") );
35 }
36 
37 void Meter::title( const char *title ){
38 
39  if ( title_ )
40  delete[] title_;
41 
42  int len = strlen(title);
43  title_ = new char[len + 1];
44  strncpy( title_, title, len );
45  title_[len] = '\0'; // strncpy() will not null terminate if s2 > len
46 }
47 
48 void Meter::legend( const char *legend ){
49 
50  if ( legend_ )
51  delete[] legend_;
52 
53  int len = strlen(legend);
54  legend_ = new char[len + 1];
55  strncpy( legend_, legend, len );
56  legend_[len] = '\0'; // strncpy() will not null terminate if s2 > len
57 }
58 
59 void Meter::resize( int x, int y, int width, int height ){
60  x_ = x;
61  y_ = y;
62  width_ = (width>=0) ? width : 0; // fix for cosmetical bug:
63  height_ = (height>=0) ? height : 0; // beware of values < 0 !
64  width_ &= ~1; // only allow even width_ values
65 }
66 
67 double Meter::scaleValue( double value, char *scale, bool metric ){
68  double scaled = ( value < 0 ? -value : value );
69 
70  if (scaled >= 999.5*1e15){
71  scale[0] = 'E';
72  scaled = value / ( metric ? 1e18 : 1ULL<<60 );
73  }
74  else if (scaled >= 999.5*1e12){
75  scale[0] = 'P';
76  scaled = value / ( metric ? 1e15 : 1ULL<<50 );
77  }
78  else if (scaled >= 999.5*1e9){
79  scale[0] = 'T';
80  scaled = value / ( metric ? 1e12 : 1ULL<<40 );
81  }
82  else if (scaled >= 999.5*1e6){
83  scale[0] = 'G';
84  scaled = value / ( metric ? 1e9 : 1UL<<30 );
85  }
86  else if (scaled >= 999.5*1e3){
87  scale[0] = 'M';
88  scaled = value / ( metric ? 1e6 : 1UL<<20 );
89  }
90  else if (scaled >= 999.5){
91  scale[0] = ( metric ? 'k' : 'K' );
92  scaled = value / ( metric ? 1e3 : 1UL<<10 );
93  }
94  else if (scaled < 0.9995 && metric){
95  if (scaled >= 0.9995/1e3){
96  scale[0] = 'm';
97  scaled = value * 1e3;
98  }
99  else if (scaled >= 0.9995/1e6){
100  scale[0] = '\265';
101  scaled = value * 1e6;
102  }
103  else {
104  scale[0] = 'n';
105  scaled = value * 1e9;
106  }
107  // add more if needed
108  }
109  else {
110  scale[0] = '\0';
111  scaled = value;
112  }
113  scale[1] = '\0';
114  return scaled;
115 }
int x_
Definition: meter.h:53
int priority_
Definition: meter.h:54
const char * title(void)
Definition: meter.h:25
int docaptions_
Definition: meter.h:53
int dolegends_
Definition: meter.h:53
void resize(int x, int y, int width, int height)
Definition: meter.cc:59
virtual void checkResources(void)
Definition: meter.cc:33
int y_
Definition: meter.h:53
void dolegends(int val)
Definition: meter.h:29
Meter(XOSView *parent, const char *title="", const char *legend="", int docaptions=0, int dolegends=0, int dousedlegends=0)
Definition: meter.cc:11
int dousedlegends_
Definition: meter.h:53
int width_
Definition: meter.h:53
char * legend_
Definition: meter.h:55
static double scaleValue(double value, char *scale, bool metric)
Definition: meter.cc:67
unsigned long textcolor_
Definition: meter.h:56
int counter_
Definition: meter.h:54
const char * legend(void)
Definition: meter.h:27
void docaptions(int val)
Definition: meter.h:28
char * title_
Definition: meter.h:55
void dousedlegends(int val)
Definition: meter.h:30
XOSView * parent_
Definition: meter.h:52
int height_
Definition: meter.h:53
virtual ~Meter(void)
Definition: meter.cc:26
int xoff(void) const
Definition: xosview.h:31
int newypos(void)
Definition: xosview.cc:189
unsigned long allocColor(const char *name)
Definition: xwin.cc:383
const char * getResource(const char *name)
Definition: xwin.cc:362
int width(void)
Definition: xwin.h:23