File: cccc_itm.cc

package info (click to toggle)
cccc 3.pre81-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,820 kB
  • ctags: 4,972
  • sloc: ansic: 33,244; cpp: 10,691; java: 618; makefile: 165; sh: 11
file content (124 lines) | stat: -rw-r--r-- 1,968 bytes parent folder | download | duplicates (2)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// cccc_itm.cc

#include "cccc.h"
#include <fstream>

#include "cccc_itm.h"
#include <cstdio>
#include <cmath>

CCCC_Item::CCCC_Item(const string& s, char c)
{
  buffer=s;
  delimiter=c;
  good=true;
}

bool CCCC_Item::Insert(const string& s)
{
  buffer+=s;
  buffer+=delimiter;
#if 0
  cerr << buffer << endl;
#endif
  return good;
}

bool CCCC_Item::Insert(const char* cptr)
{
  string s(cptr);
  return Insert(s);
}
 
bool CCCC_Item::Extract(string& s)
{
  size_t delimiter_position=buffer.find(delimiter);
  if(delimiter_position!=string::npos)
    {
      good=true;
      s=buffer.substr(0,delimiter_position);
      string tempBuffer=buffer.substr(delimiter_position+1);
	  buffer=tempBuffer;
    }
  else
    { 
      good=false;
    }
  return good;
}

bool CCCC_Item::Insert(int n)
{
  char numbuf[64];
  sprintf(numbuf,"%d",n);
  return Insert(numbuf);
}

bool CCCC_Item::Extract(int& n)
{
  string numstr;
  bool retval=Extract(numstr);
  n=atoi(numstr.c_str());
  return retval;
}

bool CCCC_Item::Insert(char c)
{
  char charbuf[2];
  sprintf(charbuf,"%c",c);
  return Insert(charbuf);
}

bool CCCC_Item::Extract(char& c)
{
  string charstr;
  bool retval=Extract(charstr);
  if(charstr.size()==1)
    {
      c=charstr[0];
    }
  return retval;
}

bool CCCC_Item::Insert(float f)
{
  char numbuf[64];
  sprintf(numbuf,"%f",f);
  return Insert(numbuf);
}

bool CCCC_Item::Extract(float& f)
{
  string numstr;
  bool retval=Extract(numstr);
  f=atof(numstr.c_str());
  return retval;
}

bool CCCC_Item::ToFile(ofstream& ofstr)
{
  ofstr << buffer << endl;
  good=ofstr.good();
  return good;
}

bool CCCC_Item::FromFile(ifstream& ifstr)
{
  good=false;
  char line_buffer[1024];
  ifstr.getline(line_buffer,1023);
  buffer=line_buffer;
  if(ifstr.good() && buffer.size()>0 && buffer.size()<1023)
    {
      delimiter=buffer[buffer.size()-1];
      good=true;
#if 0
      cerr << "Delimiter is " << delimiter << endl;
#endif
    }
  return good;
}