Browse Source

cmFileSys.h/c: Added cmFileSysGenFn().

master
kevin 11 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
 #include "cmMallocDebug.h"
7
 #include "cmMallocDebug.h"
8
 #include "cmLinkedHeap.h"
8
 #include "cmLinkedHeap.h"
9
 #include "cmFileSys.h"
9
 #include "cmFileSys.h"
10
+#include "cmText.h"
10
 
11
 
11
 #include <sys/stat.h>
12
 #include <sys/stat.h>
12
 #include <errno.h>
13
 #include <errno.h>
467
   cmLHeapFree(p->heapH, (void*)fn); 
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
 cmFsRC_t    cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir )
510
 cmFsRC_t    cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir )
471
 {
511
 {
472
   cmFs_t* p = _cmFileSysHandleToPtr(h);
512
   cmFs_t* p = _cmFileSysHandleToPtr(h);
1113
 void                 cmFsFreeFn(  const cmChar_t* fn )
1153
 void                 cmFsFreeFn(  const cmChar_t* fn )
1114
 { cmFileSysFreeFn(_cmFsH, fn); }
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
 cmFsRC_t             cmFsMkDir( const cmChar_t* dir )
1159
 cmFsRC_t             cmFsMkDir( const cmChar_t* dir )
1117
 { return cmFileSysMkDir(_cmFsH,dir); }
1160
 { return cmFileSysMkDir(_cmFsH,dir); }
1118
 
1161
 

+ 11
- 1
cmFileSys.h View File

34
     kSysErrFsRC,
34
     kSysErrFsRC,
35
     kOsxFailFsRC,
35
     kOsxFailFsRC,
36
     kLinuxFailFsRC,
36
     kLinuxFailFsRC,
37
-    kInvalidDirFsRC
37
+    kInvalidDirFsRC,
38
+    kGenFileFailFsRC
38
   };
39
   };
39
 
40
 
40
 
41
 
89
   // Release the file name created through an earlier call to cmFileSysMakeFn().
90
   // Release the file name created through an earlier call to cmFileSysMakeFn().
90
   void            cmFileSysFreeFn( cmFileSysH_t h, const cmChar_t* fn );
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
   // Create a directory - where the entire path already exists except for the 
99
   // Create a directory - where the entire path already exists except for the 
93
   // final directory.
100
   // final directory.
94
   cmFsRC_t    cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir );
101
   cmFsRC_t    cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir );
178
   // Release the memory assoicated with a cmFileSysDirEntry_t array returned from an earlier call to cmFileSysDirEntries().
185
   // Release the memory assoicated with a cmFileSysDirEntry_t array returned from an earlier call to cmFileSysDirEntries().
179
   void cmFileSysDirFreeEntries( cmFileSysH_t h, cmFileSysDirEntry_t* p );
186
   void cmFileSysDirFreeEntries( cmFileSysH_t h, cmFileSysDirEntry_t* p );
180
 
187
 
188
+
181
   // Return the last error code generated by the file system.
189
   // Return the last error code generated by the file system.
182
   cmFsRC_t cmFileSysErrorCode( cmFileSysH_t h );
190
   cmFsRC_t cmFileSysErrorCode( cmFileSysH_t h );
183
 
191
 
202
   const cmChar_t* cmFsVMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
210
   const cmChar_t* cmFsVMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
203
   const cmChar_t* cmFsMakeFn(  const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
211
   const cmChar_t* cmFsMakeFn(  const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
204
   void            cmFsFreeFn(  const cmChar_t* fn );
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
   cmFsRC_t        cmFsMkDir( const cmChar_t* dir );
216
   cmFsRC_t        cmFsMkDir( const cmChar_t* dir );
207
   cmFsRC_t        cmFsMkDirAll( const cmChar_t* dir );
217
   cmFsRC_t        cmFsMkDirAll( const cmChar_t* dir );

Loading…
Cancel
Save