Browse Source

cmFileSys.h/c: Added cmFileSysGenFn().

master
kevin 10 years ago
parent
commit
67f4fddcd6
2 changed files with 54 additions and 1 deletions
  1. 43
    0
      cmFileSys.c
  2. 11
    1
      cmFileSys.h

+ 43
- 0
cmFileSys.c View File

@@ -7,6 +7,7 @@
7 7
 #include "cmMallocDebug.h"
8 8
 #include "cmLinkedHeap.h"
9 9
 #include "cmFileSys.h"
10
+#include "cmText.h"
10 11
 
11 12
 #include <sys/stat.h>
12 13
 #include <errno.h>
@@ -467,6 +468,45 @@ void cmFileSysFreeFn( cmFileSysH_t h, const cmChar_t* fn )
467 468
   cmLHeapFree(p->heapH, (void*)fn); 
468 469
 }
469 470
 
471
+cmFsRC_t cmFileSysGenFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* prefixStr, const cmChar_t* extStr, const cmChar_t** fnPtr )
472
+{
473
+  cmFsRC_t rc            = kOkFsRC;
474
+  cmFs_t*  p             = _cmFileSysHandleToPtr(h);
475
+  unsigned maxAttemptCnt = 0xffff;
476
+
477
+  *fnPtr = NULL;
478
+
479
+  assert(dir != NULL);
480
+
481
+  if( prefixStr == NULL )
482
+    prefixStr = "";
483
+
484
+  if( extStr == NULL )
485
+    extStr = "";
486
+
487
+  if( !cmFileSysIsDir(h,dir) )
488
+    return cmErrMsg(&p->err,kOpenDirFailFsRC,"File name generation failed because the directory '%s' does not exist.",cmStringNullGuard(dir));
489
+
490
+  unsigned i;
491
+  for(i=0; *fnPtr==NULL; ++i)
492
+  {
493
+    cmChar_t* fn = cmTsPrintfP(NULL,"%s%i",prefixStr,i);
494
+
495
+    const cmChar_t* path = cmFileSysMakeFn(h,dir,fn,extStr,NULL );
496
+    
497
+    if( !cmFileSysIsFile(h,path) )
498
+      *fnPtr = cmMemAllocStr(path);
499
+
500
+    cmFileSysFreeFn(h,path);
501
+    cmMemFree(fn);
502
+
503
+    if( i == maxAttemptCnt )
504
+      return cmErrMsg(&p->err,kGenFileFailFsRC,"File name generation failed because a suitable file name could not be found after %i attempts.",maxAttemptCnt);
505
+  };
506
+
507
+  return rc;
508
+}
509
+
470 510
 cmFsRC_t    cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir )
471 511
 {
472 512
   cmFs_t* p = _cmFileSysHandleToPtr(h);
@@ -1113,6 +1153,9 @@ const cmChar_t*      cmFsMakeFn(  const cmChar_t* dirPrefix, const cmChar_t* fn,
1113 1153
 void                 cmFsFreeFn(  const cmChar_t* fn )
1114 1154
 { cmFileSysFreeFn(_cmFsH, fn); }
1115 1155
 
1156
+cmFsRC_t             cmFsGenFn( const cmChar_t* dir, const cmChar_t* prefixStr, const cmChar_t* extStr, const cmChar_t** fnPtr )
1157
+{ return cmFileSysGenFn(_cmFsH,dir,prefixStr,extStr,fnPtr); }
1158
+
1116 1159
 cmFsRC_t             cmFsMkDir( const cmChar_t* dir )
1117 1160
 { return cmFileSysMkDir(_cmFsH,dir); }
1118 1161
 

+ 11
- 1
cmFileSys.h View File

@@ -34,7 +34,8 @@ extern "C" {
34 34
     kSysErrFsRC,
35 35
     kOsxFailFsRC,
36 36
     kLinuxFailFsRC,
37
-    kInvalidDirFsRC
37
+    kInvalidDirFsRC,
38
+    kGenFileFailFsRC
38 39
   };
39 40
 
40 41
 
@@ -89,6 +90,12 @@ extern "C" {
89 90
   // Release the file name created through an earlier call to cmFileSysMakeFn().
90 91
   void            cmFileSysFreeFn( cmFileSysH_t h, const cmChar_t* fn );
91 92
 
93
+  // Generate an unused filename in the directory 'dir' beginning with the prefix 'prefixStr'.
94
+  // The returned file name will have the format: <dir>/<prefixStr>nnnn.<extStr> where
95
+  // nnn represents 1 or more digits.  The returned string must be released with a 
96
+  // call to cmMemFree().
97
+  cmFsRC_t cmFileSysGenFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* prefixStr, const cmChar_t* extStr, const cmChar_t** fnPtr );
98
+
92 99
   // Create a directory - where the entire path already exists except for the 
93 100
   // final directory.
94 101
   cmFsRC_t    cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir );
@@ -178,6 +185,7 @@ extern "C" {
178 185
   // Release the memory assoicated with a cmFileSysDirEntry_t array returned from an earlier call to cmFileSysDirEntries().
179 186
   void cmFileSysDirFreeEntries( cmFileSysH_t h, cmFileSysDirEntry_t* p );
180 187
 
188
+
181 189
   // Return the last error code generated by the file system.
182 190
   cmFsRC_t cmFileSysErrorCode( cmFileSysH_t h );
183 191
 
@@ -202,6 +210,8 @@ extern "C" {
202 210
   const cmChar_t* cmFsVMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
203 211
   const cmChar_t* cmFsMakeFn(  const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
204 212
   void            cmFsFreeFn(  const cmChar_t* fn );
213
+  cmFsRC_t        cmFsGenFn(  const cmChar_t* dir, const cmChar_t* prefixStr, const cmChar_t* extStr, const cmChar_t** fnPtr );
214
+
205 215
 
206 216
   cmFsRC_t        cmFsMkDir( const cmChar_t* dir );
207 217
   cmFsRC_t        cmFsMkDirAll( const cmChar_t* dir );

Loading…
Cancel
Save