Browse Source

cmFile.h/c Added cmFileByteCountFn(), cmFileCompare().

master
kevin 11 years ago
parent
commit
79eb363343
2 changed files with 63 additions and 0 deletions
  1. 59
    0
      cmFile.c
  2. 4
    0
      cmFile.h

+ 59
- 0
cmFile.c View File

@@ -193,6 +193,65 @@ unsigned   cmFileByteCount(  cmFileH_t h )
193 193
   return sr.st_size;
194 194
 }
195 195
 
196
+cmFileRC_t   cmFileByteCountFn( const cmChar_t* fn, cmRpt_t* rpt, unsigned* fileByteCntPtr )
197
+{
198
+  assert( fileByteCntPtr != NULL );
199
+  cmFileRC_t rc;
200
+  cmFileH_t h = cmFileNullHandle;
201
+  if((rc = cmFileOpen(&h,fn,kReadFileFl,rpt)) != kOkFileRC )
202
+    return rc;
203
+
204
+  if( fileByteCntPtr != NULL)
205
+    *fileByteCntPtr = cmFileByteCount(h);
206
+
207
+  cmFileClose(&h);
208
+
209
+  return rc;    
210
+}
211
+
212
+cmFileRC_t cmFileCompare( const cmChar_t* fn0, const cmChar_t* fn1, cmRpt_t* rpt, bool* isEqualPtr )
213
+{
214
+  cmFileRC_t rc         = kOkFileRC;
215
+  unsigned   bufByteCnt = 2048;
216
+  cmFileH_t  h0         = cmFileNullHandle;
217
+  cmFileH_t  h1         = cmFileNullHandle;
218
+
219
+  char       b0[ bufByteCnt ];
220
+  char       b1[ bufByteCnt ];
221
+
222
+  assert(isEqualPtr != NULL );
223
+  *isEqualPtr = true;
224
+
225
+  if((rc = cmFileOpen(&h0,fn0,kReadFileFl,rpt)) != kOkFileRC )
226
+    goto errLabel;
227
+
228
+  if((rc = cmFileOpen(&h1,fn1,kReadFileFl,rpt)) != kOkFileRC )
229
+    goto errLabel;
230
+
231
+  cmFile_t*   p0 = _cmFileHandleToPtr(h0);
232
+  cmFile_t*   p1 = _cmFileHandleToPtr(h1);
233
+
234
+  while(1)
235
+  {
236
+    size_t n0 = fread(b0,1,bufByteCnt,p0->fp);
237
+    size_t n1 = fread(b1,1,bufByteCnt,p1->fp);
238
+    if( n0 != n1 || memcmp(b0,b1,n0)!=0 )
239
+    {
240
+      *isEqualPtr = false;
241
+      break;
242
+    }
243
+
244
+    if( n0 != bufByteCnt || n1 != bufByteCnt )
245
+      break;
246
+  }
247
+
248
+ errLabel:
249
+  cmFileClose(&h0);
250
+  cmFileClose(&h1);
251
+  return rc;
252
+}
253
+
254
+
196 255
 const cmChar_t* cmFileName( cmFileH_t h )
197 256
 {
198 257
   cmFile_t* p = _cmFileHandleToPtr(h);

+ 4
- 0
cmFile.h View File

@@ -89,6 +89,10 @@ extern "C" {
89 89
 
90 90
   // Return the length of the file in bytes
91 91
   unsigned   cmFileByteCount(  cmFileH_t h );
92
+  cmFileRC_t cmFileByteCountFn( const cmChar_t* fn, cmRpt_t* rpt, unsigned* fileByteCntPtr );
93
+
94
+  // Set *isEqualPtr=true if the two files are identical.
95
+  cmFileRC_t cmFileCompare( const cmChar_t* fn0, const cmChar_t* fn1, cmRpt_t* rpt, bool* isEqualFlPtr );
92 96
 
93 97
   // Return the file name associated with a file handle.
94 98
   const cmChar_t* cmFileName( cmFileH_t h );

Loading…
Cancel
Save