TRF Language Model
wb-file.cpp
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 #include "wb-file.h"
20 
21 namespace wb
22 {
23  bool File::Open(const char *path, const char *mode, bool bHardOpen/* = true*/)
24  {
25  Close();
26  if (path == NULL || mode == NULL)
27  return false;
28 
29  if (bHardOpen) {
30  SAFE_FOPEN(fp, path, mode);
31  }
32  else {
33  fp = fopen(path, mode);
34  }
35 
36  strFileName = path;
37  nLine = 0;
38  nBuf = 1;
39  bOver = false;
40  return(fp != NULL);
41  }
42  bool File::Reopen(const char* model)
43  {
44  return Open(strFileName.c_str(), model);
45  }
46 
47  char *File::GetLine(bool bPrecent/* = false*/)
48  {
49  if (!Good()) {
50  lout_error("file open failed, can't GetLine!!");
51  }
52 
53  if (bPrecent && nLine == 0)
54  lout.Progress(fp, true, "Read File:");
55 
56  if (!pStrLine) {
57  nBuf = 1;
58  pStrLine = new char[MAX_SENTENCE_LEN];
59  }
60 
61  bool bOk = GetLine(pStrLine, MAX_SENTENCE_LEN);
62  if (bPrecent)
63  lout.Progress(fp);
64 
65  while (bOver) {
66  // re-alloc memory
67  nBuf++;
68  char *pBuf = new char[MAX_SENTENCE_LEN*nBuf];
69  if (pBuf == NULL) {
70  lout_error("[FILE GetLine] [Open Memory ERROR! (" << nBuf << "*" << MAX_SENTENCE_LEN << ")")
71  }
72 
73  memcpy(pBuf, pStrLine, MAX_SENTENCE_LEN*(nBuf - 1)); //copy the buffer
74  delete[]pStrLine;
75  pStrLine = pBuf;
76  bOk = GetLine(pStrLine + MAX_SENTENCE_LEN*(nBuf - 1) - 1, MAX_SENTENCE_LEN + 1);
77  if (bPrecent)
78  lout.Progress(fp);
79  }
80 
81 
82  if (bOk)
83  return pStrLine;
84  else
85  return NULL;
86  }
87 
88  bool File::GetLine(char *str, int maxLen/* = GS_SENTENCE_LEN*/)
89  {
90  bOver = false;
91  char *p = fgets(str, maxLen, fp);
92  if (p == NULL)
93  return false;
94 
95  nLine++;
96  int l = strlen(str);
97  if (l >= maxLen - 1)
98  {
99  bOver = true;
100  return false;
101  }
102  if (str[l - 1] == '\n')
103  str[l - 1] = '\0';
104 
105 #ifdef __linux
106  // In linux, there may exist '\r\n' at the end of the line.
107  // remove '\r'
108  if (str[l-2] == '\r')
109  str[l-2] = '\0';
110 #endif
111 
112  return true;
113  }
114 
115  void File::Print(const char* p_pMessage, ...)
116  {
117  if (fp == NULL)
118  return;
119 
120  char strBuffer[MAX_SENTENCE_LEN];
121  va_list vaParams;
122  va_start(vaParams, p_pMessage);
123  //_vsnprintf(strBuffer,GS_SENTENCE_LEN,p_pMessage,vaParams);
124  vfprintf(fp, p_pMessage, vaParams);
125  va_end(vaParams);
126 
127  //fprintf(fp, "%s", strBuffer);
128 
129  fflush(fp);
130  }
132  int File::Scanf(const char* p_pMessage, ...)
133  {
134  if (fp == NULL)
135  return 0;
136 
137  va_list vaParams;
138 
139  va_start(vaParams, p_pMessage);
140  int retval = vfscanf(fp, p_pMessage, vaParams);
141  va_end(vaParams);
142 
143 
144  return retval;
145  }
146 
149  {
150  Print("nTotalNum = %d\n\n", m_nTotalNum);
151  m_nCurNum = 0;
152  }
155  {
156  fscanf(File::fp, "nTotalNum = %d\n\n", &m_nTotalNum);
157  m_nCurNum = 0;
158  }
159 
162  {
163  Print("nObj: %d\n", m_nCurNum);
164  m_pObj->WriteT(*this);
165  Print("\n");
166  m_nCurNum++;
167  }
170  {
171  if (feof(fp))
172  return false;
173  if (m_nCurNum >= m_nTotalNum)
174  return false;
175 
176  fscanf(File::fp, "nObj: %d\n", &m_nCurNum);
177 
178  m_pObj->ReadT(*this);
179  fscanf(File::fp, "\n");
180  return true;
181  }
182 
185  {
186  fwrite(&m_nTotalNum, sizeof(m_nTotalNum), 1, fp);
187  m_nCurNum = 0;
188  }
191  {
192  fread(&m_nTotalNum, sizeof(m_nTotalNum), 1, fp);
193  m_nCurNum = 0;
194  }
197  {
198  fwrite(&m_nCurNum, sizeof(m_nCurNum), 1, fp);
199  m_pObj->WriteB(*this);
200  m_nCurNum++;
201  }
204  {
205  if (feof(fp))
206  return false;
207  if (m_nCurNum >= m_nTotalNum)
208  return false;
209 
210  fread(&m_nCurNum, sizeof(m_nCurNum), 1, fp);
211  m_pObj->ReadB(*this);
212  return true;
213  }
214 }
void ReadHeadT()
��txt��read the head
Definition: wb-file.cpp:154
short nBuf
record the buffer is nBuf times of GS_SENTENCE_LEN
Definition: wb-file.h:104
void WriteHeadT()
��txt��write the head
Definition: wb-file.cpp:148
#define lout_error(x)
Definition: wb-log.h:183
bool ReadObjT()
��txt��read the object
Definition: wb-file.cpp:169
void ReadHeadB()
��bin��read the head
Definition: wb-file.cpp:190
virtual int Scanf(const char *p_pMessage,...)
scanf
Definition: wb-file.cpp:132
FILE * fp
file pointer
Definition: wb-file.h:97
virtual void Close()
close the file
Definition: wb-file.h:124
#define SAFE_FOPEN(pfile, path, mode)
file open
Definition: wb-file.h:41
virtual void Print(const char *p_pMessage,...)
print
Definition: wb-file.cpp:115
virtual bool Open(const char *path, const char *mode, bool bHardOpen=true)
Open file.
Definition: wb-file.cpp:23
void WriteHeadB()
��bin��write the head
Definition: wb-file.cpp:184
virtual bool Reopen(const char *model)
re-open the file
Definition: wb-file.cpp:42
virtual char * GetLine(bool bPrecent=false)
Read a line into the buffer.
Definition: wb-file.cpp:47
void Progress(long long n=-1, bool bInit=false, long long total=100, const char *head="")
progress bar
Definition: wb-log.cpp:146
string strFileName
stroe the file name
Definition: wb-file.h:101
bool ReadObjB()
��bin��read the object
Definition: wb-file.cpp:203
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
Log lout
the defination is in wb-log.cpp
Definition: wb-log.cpp:22
bool Good() const
return if the file is accessible.
Definition: wb-file.h:145
int nLine
the number of reading from file
Definition: wb-file.h:98
define the file class
#define MAX_SENTENCE_LEN
the maximum length of string read from file once
Definition: wb-file.h:53
void WriteObjB()
��bin��write the object
Definition: wb-file.cpp:196
void WriteObjT()
��txt��write the object
Definition: wb-file.cpp:161
define all the code written by Bin Wang.
Definition: wb-file.cpp:21