TRF Language Model
wb-file.h
Go to the documentation of this file.
1 // You may obtain a copy of the License at
2 //
3 // http://www.apache.org/licenses/LICENSE-2.0
4 //
5 // Unless required by applicable law or agreed to in writing, software
6 // distributed under the License is distributed on an "AS IS" BASIS,
7 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8 // See the License for the specific language governing permissions and
9 // limitations under the License.
10 //
11 // Copyright 2014-2015 Tsinghua University
12 // Author: wb.th08@gmail.com (Bin Wang), ozj@tsinghua.edu.cn (Zhijian Ou)
13 //
14 // All h, cpp, cc, and script files (e.g. bat, sh, pl, py) should include the above
15 // license declaration. Different coding language may use different comment styles.
16 
17 
18 
19 
28 #pragma once
29 #include "iostream"
30 #include "fstream"
31 using namespace std;
32 
33 #include <stdio.h>
34 #include <time.h>
35 #include <cstdarg> //include va_list
36 
37 #include "wb-win.h"
38 #include "wb-log.h"
39 
41 #define SAFE_FOPEN(pfile, path, mode) {\
42  if ( !path || !(pfile = fopen(path, mode)) ) {cout<<"File Open Failed: "<<path<<endl; exit(0);}}
43 #define SAFE_FMOPEN(file, path, mode) {\
45  file.open(path, mode); if (!file.good()) {cout<<"File Open Failed: "<<path<<endl; exit(0);}}
46 #define SAFE_FSOPEN(file, path) {\
48  file.open(path); if (!file.good()) {cout<<"File Open Failed: "<<path<<endl; exit(0);}}
49 #define SAFE_FCLOSE(fp) { if(fp) {fclose(fp); fp=NULL;} }
51 
53 #define MAX_SENTENCE_LEN 32*1024
54 
56 #define WRITE_VALUE(x,fp) fwrite(&(x),sizeof(x),1,fp)
57 #define READ_VALUE(x,fp) fread(&(x),sizeof(x),1,fp)
59 #define FPRINT_VALUE(x, fp) fprintf(fp, #x"=%d\n", (x))
61 #define FSCANF_VALUE(x, fp) fscanf(fp, #x"=%d\n", &(x))
63 
64 namespace wb
65 {
94  class File
95  {
96  public:
97  FILE *fp;
98  int nLine;
99 
100  char *pStrLine;
101  string strFileName;
102 
103  bool bOver;
104  short nBuf;
105 
106  public:
108  File() :fp(NULL), pStrLine(NULL) { fp = NULL; bOver = false; nBuf = 1; }
110  File(const char *path, const char *mode, bool bHardOpen = true) :fp(NULL), pStrLine(NULL) { Open(path, mode, bHardOpen); }
112  ~File() { SAFE_FCLOSE(fp); delete[]pStrLine; }
114 
120  virtual bool Open(const char *path, const char *mode, bool bHardOpen = true);
122  virtual bool Reopen(const char* model);
124  virtual void Close() { SAFE_FCLOSE(fp); }
126  operator FILE* () { return fp; }
128 
132  virtual char *GetLine(bool bPrecent = false);
134  virtual bool GetLine(char *str, int maxLen = MAX_SENTENCE_LEN);
136  virtual void Print(const char* p_pMessage, ...);
138  virtual int Scanf(const char* p_pMessage, ...);
140  void Flush() { fflush(fp); }
142 
143  void Reset() { _fseeki64(fp, 0, SEEK_SET); nLine = 0; }
145  bool Good() const { return !(fp == NULL); }
147  template <typename TYPE>
148  void PrintArray(const char* pformat, TYPE* pbuf, int num)
149  {
150  for (int i = 0; i<num; i++) {
151  Print(pformat, *pbuf);
152  putc(' ', fp);
153  pbuf++;
154  }
155  putc('\n', fp);
156  }
157  };
158 
160  class IO_Obj
161  {
162  public:
164  virtual void WriteT(File &file) {};
166  virtual void ReadT(File &file) {};
168  virtual void WriteB(File &file) {};
170  virtual void ReadB(File &file) {};
171  };
172 
174  class ObjFile : public File
175  {
176  public:
178  int m_nCurNum;
180  public:
182  ObjFile() :File(), m_nTotalNum(0), m_nCurNum(-1){}
184  ObjFile(const char *path, const char *mode) :File(path, mode), m_nTotalNum(0), m_nCurNum(-1){}
186  void WriteHeadT();
188  void ReadHeadT();
190  void WriteObjT();
192  bool ReadObjT();
194  void WriteHeadB();
196  void ReadHeadB();
198  void WriteObjB();
200  bool ReadObjB();
201  };
202 
206 }
207 
208 
209 
210 
int m_nTotalNum
object number
Definition: wb-file.h:177
short nBuf
record the buffer is nBuf times of GS_SENTENCE_LEN
Definition: wb-file.h:104
virtual void ReadT(File &file)
read from the txt file
Definition: wb-file.h:166
void Reset()
reset position
Definition: wb-file.h:143
~File()
destructor
Definition: wb-file.h:112
FILE * fp
file pointer
Definition: wb-file.h:97
virtual void Close()
close the file
Definition: wb-file.h:124
#define SAFE_FCLOSE(fp)
file close
Definition: wb-file.h:50
virtual void WriteB(File &file)
write to binary file
Definition: wb-file.h:168
base class used to derive. It providing the vritual function of write and read
Definition: wb-file.h:160
virtual void ReadB(File &file)
read from binary file
Definition: wb-file.h:170
a definition of a class Log, which can output to the cmd window and the log file simultaneously. In wb-log.cpp, there are a Log variable "lout", which can be directly used just like "cout". For example:
Provide the toolkits for cmd window of window platform.
int m_nCurNum
current object number
Definition: wb-file.h:178
ObjFile(const char *path, const char *mode)
constructor
Definition: wb-file.h:184
file class.
Definition: wb-file.h:94
File()
constructor
Definition: wb-file.h:108
string strFileName
stroe the file name
Definition: wb-file.h:101
used to read more than one objects
Definition: wb-file.h:174
IO_Obj * m_pObj
object pointer
Definition: wb-file.h:179
bool bOver
record the if the buffer is overflow
Definition: wb-file.h:103
char * pStrLine
store the string get from file
Definition: wb-file.h:100
bool Good() const
return if the file is accessible.
Definition: wb-file.h:145
void PrintArray(const char *pformat, TYPE *pbuf, int num)
print a array into file
Definition: wb-file.h:148
int nLine
the number of reading from file
Definition: wb-file.h:98
ObjFile()
constructor
Definition: wb-file.h:182
#define MAX_SENTENCE_LEN
the maximum length of string read from file once
Definition: wb-file.h:53
File(const char *path, const char *mode, bool bHardOpen=true)
constructor
Definition: wb-file.h:110
void Flush()
clean buffer
Definition: wb-file.h:140
define all the code written by Bin Wang.
Definition: wb-file.cpp:21
virtual void WriteT(File &file)
write to the txt file
Definition: wb-file.h:164