Ver código fonte

cmFile.h/c : Added cmWriteStr() and cmReadStr().

master
Kevin Larke 9 anos atrás
pai
commit
d0393c623c
2 arquivos alterados com 80 adições e 1 exclusões
  1. 63
    1
      cmFile.c
  2. 17
    0
      cmFile.h

+ 63
- 1
cmFile.c Ver arquivo

@@ -36,7 +36,7 @@ cmFileRC_t _cmFileError( cmFile_t* p, cmFileRC_t rc, int errNumb, const cmChar_t
36 36
   return rc;
37 37
 }
38 38
 
39
-cmFileRC_t cmFileOpen(    cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlags_t flags, cmRpt_t* rpt )
39
+cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlags_t flags, cmRpt_t* rpt )
40 40
 {
41 41
   char mode[]  = "/0/0/0";
42 42
   cmFile_t*  p = NULL;
@@ -731,6 +731,57 @@ cmFileRC_t cmFileWriteBool(   cmFileH_t h, const bool*           buf, unsigned c
731 731
 { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); }
732 732
 
733 733
 
734
+cmFileRC_t cmFileWriteStr( cmFileH_t h, const cmChar_t* s )
735
+{
736
+  cmFileRC_t rc;
737
+  
738
+  unsigned n = cmTextLength(s);
739
+
740
+  if((rc = cmFileWriteUInt(h,&n,1)) != kOkFileRC )
741
+    return rc;
742
+
743
+  if( n > 0 )
744
+    rc = cmFileWriteChar(h,s,n);
745
+  return rc;
746
+}
747
+
748
+
749
+cmFileRC_t cmFileReadStr(  cmFileH_t h, cmChar_t** sRef, unsigned maxCharN )
750
+{
751
+  unsigned n;
752
+  cmFileRC_t rc;
753
+
754
+  assert(sRef != NULL );
755
+
756
+  *sRef = NULL;
757
+  
758
+  if( maxCharN == 0 )
759
+    maxCharN = 16384;
760
+
761
+  // read the string length
762
+  if((rc = cmFileReadUInt(h,&n,1)) != kOkFileRC )
763
+    return rc;
764
+
765
+  // verify that string isn't too long
766
+  if( n > maxCharN  )
767
+  {
768
+    cmFile_t* p = _cmFileHandleToPtr(h);
769
+    return cmErrMsg(&p->err,kBufAllocFailFileRC,"The stored string is larger than the maximum allowable size.");    
770
+  }
771
+
772
+  // allocate a read buffer
773
+  cmChar_t* s = cmMemAllocZ(cmChar_t,n+1);
774
+
775
+  // fill the buffer from the file
776
+  if((rc = cmFileReadChar(h,s,n)) != kOkFileRC )
777
+    return rc;
778
+
779
+  s[n] = 0; // terminate the string
780
+
781
+  *sRef = s;
782
+
783
+  return rc;
784
+}
734 785
 
735 786
 
736 787
 cmFileRC_t cmFilePrint(   cmFileH_t h, const cmChar_t* text )
@@ -765,3 +816,14 @@ cmFileRC_t cmFilePrintf(  cmFileH_t h, const cmChar_t* fmt, ... )
765 816
 }
766 817
 
767 818
 
819
+cmFileRC_t cmFileLastRC( cmFileH_t h )
820
+{
821
+  cmFile_t* p = _cmFileHandleToPtr(h);
822
+  return cmErrLastRC(&p->err);
823
+}
824
+
825
+cmFileRC_t cmFileSetRC( cmFileH_t h, cmFileRC_t rc )
826
+{
827
+  cmFile_t* p = _cmFileHandleToPtr(h);
828
+  return cmErrSetRC(&p->err,rc);
829
+}

+ 17
- 0
cmFile.h Ver arquivo

@@ -221,11 +221,28 @@ extern "C" {
221 221
   cmFileRC_t cmFileWriteDouble( cmFileH_t h, const double*         buf, unsigned cnt );
222 222
   cmFileRC_t cmFileWriteBool(   cmFileH_t h, const bool*           buf, unsigned cnt );
223 223
 
224
+  // Write a string to a file as <N> <char0> <char1> ... <char(N-1)>
225
+  // where N is the count of characters in the string.
226
+  cmFileRC_t cmFileWriteStr( cmFileH_t h, const cmChar_t* s );
227
+
228
+  // Read a string back from a file as written by cmFileWriteStr().
229
+  // Note that the string will by string will be dynamically allocated
230
+  // and threfore must eventually be released via cmMemFree().
231
+  // If maxCharN is set to zero then the default maximum string
232
+  // length is 16384.  Note that this limit is used to prevent
233
+  // corrupt files from generating excessively long strings.
234
+  cmFileRC_t cmFileReadStr(  cmFileH_t h, cmChar_t** sRef, unsigned maxCharN );
235
+
224 236
   // Formatted Text Output Functions:
225 237
   // Print formatted text to a file.
226 238
   cmFileRC_t cmFilePrint(   cmFileH_t h, const cmChar_t* text );
227 239
   cmFileRC_t cmFilePrintf(  cmFileH_t h, const cmChar_t* fmt, ... );
228 240
   cmFileRC_t cmFileVPrintf( cmFileH_t h, const cmChar_t* fmt, va_list vl );
241
+
242
+
243
+  cmFileRC_t cmFileLastRC( cmFileH_t h );
244
+  cmFileRC_t cmFileSetRC( cmFileH_t h, cmFileRC_t rc );
245
+  
229 246
   //)
230 247
   //}
231 248
 

Carregando…
Cancelar
Salvar