File: qvf.cc

package info (click to toggle)
quelcom 0.4.0-16
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 1,544 kB
  • sloc: cpp: 4,148; makefile: 158; sh: 15
file content (98 lines) | stat: -rw-r--r-- 1,897 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*! \file
 * implementation functions for class qvf
 */

# include <stdio.h>	// sscanf

using namespace std;

#ifdef NLS
  # include <locale.h>
  # include <libintl.h>
  # define _(s) gettext (s)
#else
  # define _(s) (s)
#endif

# include "qvf.hh"
# include "qexception.hh"
# include "qmisc.hh"

char qvf::format2char(enum format f) {
 
  switch (f) {
    case BYTES: return 'b';
    case KBYTES: return 'k';
    case MBYTES: return 'M';
    case MINUTES: return 'm';
    case SECONDS: return 's';
    case MILLISECONDS: return 'j';
    case SPECIFIC: return ' ';
    case UNDEFINED: return 'X';
  }
  return '?';
}


enum qvf::format qvf::char2format(char c) {

  switch (c) {
    case 'b': return BYTES;
    case 'k': return KBYTES;
    case 'M': return MBYTES;
    case 'm': return MINUTES;
    case 's': return SECONDS;
    case 'j': return MILLISECONDS;
    case ' ': return SPECIFIC;
  }

  throw qexception(__PRETTY_FUNCTION__,
                   string(_("invalid format specifier: "))+char2string(c));  
}

qvf::qvf() { 

  format=UNDEFINED; 
} 


qvf::qvf(u_int32_t v, enum format f):value(v),format(f) { }


qvf::qvf(char *s) {

  char c;

 // valors <0 es llegeixen com a MAX-valor ...
  switch (sscanf(s,"%u%c",&value,&c)) {
    case 1:
      c = ' ';
    case 2:
      format = char2format(c);
    break;
    default: 
      throw qexception(__PRETTY_FUNCTION__,
                       string(_("error reading: "))+string(s));
  }
}


unsigned int qvf::getValue() { 

  if (format==UNDEFINED)
    throw qexception(__PRETTY_FUNCTION__,_("format is undefined"));

  return value; 
}


enum qvf::format qvf::getFormat() { return format; }
void qvf::set(unsigned int v, enum format f=SPECIFIC) { value=v; format=f; }

void qvf::print (ostream *os) {
  *os << "{" << value << "," << format2char(format) << "}";
}
ostream &operator<<(ostream &os, qvf &q) {
  q.print(&os);
  return os;
}