Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
Frm_IO.h
Go to the documentation of this file.
1 // FRM_IO.h
3 // provides a load/save base class to be derived
4 //
6 
7 #ifndef FRM_IO_H
8 #define FRM_IO_H
9 
10 #include "framework\frm_types.h"
11 #include <vector>
12 #include <fstream>
13 #include <iomanip>
14 
15 namespace Frm {
16 template <class T>
17 class IO {
18  public:
19  IO(void):_Type(IO_NOTYPE){};
20  virtual bool Load(std::string pFilename, T &pT)=0;
21  virtual bool Save(std::string pFilename, T &pT)=0;
22  virtual bool Load(std::string pFilename, std::vector<T> &pVT)=0;
23  virtual bool Save(std::string pFilename, std::vector<T> &pVT)=0;
24  void LoadInBuffer(std::string pFilename)
25  {int length;
26  ifstream fin(pFilename.c_str());
27  fin.seekg (0, ios::end);
28  length = fin.tellg();
29  fin.seekg (0, ios::beg);
30 
31  Buffer = new char[length];
32  // read data as a block:
33  fin.read (Buffer,length);
34 
35  fin.close();
36  }
37 
39  protected:
40  char* Buffer;
41  float TextToNum(char* pText)
42  {
43  float test=0, num = 10;
44  bool sign;
45 
46  int textsize = strlen(pText);
47  unsigned char i = 0;
48 
49  sign = false;
50  while((sign == false) && (i < textsize)){
51  switch(pText[i]) {
52  case '0':
53  case '1':
54  case '2':
55  case '3':
56  case '4':
57  case '5':
58  case '6':
59  case '7':
60  case '8':
61  case '9':
62  case '-':
63  case '.': sign = true; break;
64  default: i++; break;
65  }
66  }
67 
68  if (i>= textsize)
69  return 0.0f;
70 
71  if(pText[i]=='-')
72  {sign = true; i++;}
73  else sign = false;
74 
75  while ((pText[i]>='0') && (pText[i]<='9'))
76  {
77  test*=num;
78  test+=(pText[i++]-'0');
79  }
80  num = 0.1f;
81  if ((i<textsize) && (pText[i++]=='.'))
82  while ((pText[i]>='0') && (pText[i]<='9'))
83  {
84  test += (pText[i++]-'0')*num;
85  num *= 0.1f;
86  }
87  if (sign) test = -test;
88  return test;
89  };
90  void Remove(char pDelimiter, char* &pText)
91  {
92  char result[255];
93  uint32 i, j = 0;
94  for (i = 0; i < strlen(pText); i++)
95  if (pText[i] != pDelimiter)
96  result[j++] = pText[i];
97  result[j++] = '\0';
98  memcpy(pText, result, j);
99  };
100 };
101 };
102 #endif
Definition: Frm_IO.h:17
virtual bool Load(std::string pFilename, std::vector< T > &pVT)=0
char * Buffer
Definition: Frm_IO.h:40
virtual bool Save(std::string pFilename, T &pT)=0
uchar _Type
Definition: Frm_IO.h:38
void Remove(char pDelimiter, char *&pText)
Definition: Frm_IO.h:90
virtual bool Save(std::string pFilename, std::vector< T > &pVT)=0
IO(void)
Definition: Frm_IO.h:19
void LoadInBuffer(std::string pFilename)
Definition: Frm_IO.h:24
virtual bool Load(std::string pFilename, T &pT)=0
float TextToNum(char *pText)
Definition: Frm_IO.h:41
Definition: Frm.h:12
#define IO_NOTYPE
Definition: Frm_types.h:28
unsigned char uchar
Definition: Frm_types.h:16
unsigned long int uint32
Definition: Frm_types.h:15