Просмотр исходного кода

cmFileSys.h/c : Added 'user' directory prefixed functions for cmFileSysMakeFn() and cmFileSysMakeDir().

master
kevin 8 лет назад
Родитель
Сommit
2a6739ab0f
2 измененных файлов: 180 добавлений и 36 удалений
  1. 143
    30
      cmFileSys.c
  2. 37
    6
      cmFileSys.h

+ 143
- 30
cmFileSys.c Просмотреть файл

@@ -467,6 +467,11 @@ const cmChar_t* cmFileSysVMakeFn( cmFileSysH_t h, const cmChar_t* dir, const cmC
467 467
   return rp;
468 468
 }
469 469
 
470
+const cmChar_t* cmFileSysVMakeUserFn( cmFileSysH_t h, const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl )
471
+{
472
+  return cmFileSysMakeFn(h,cmFileSysUserDir(h),fn,ext,dirPrefix,NULL);
473
+}
474
+
470 475
 const cmChar_t* cmFileSysMakeFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* fn, const cmChar_t* ext, ... )
471 476
 {
472 477
   va_list vl;
@@ -476,6 +481,41 @@ const cmChar_t* cmFileSysMakeFn( cmFileSysH_t h, const cmChar_t* dir, const cmCh
476 481
   return retPtr;
477 482
 }
478 483
 
484
+const cmChar_t* cmFileSysMakeUserFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* fn, const cmChar_t* ext, ... )
485
+{
486
+  va_list vl;
487
+  va_start(vl,ext);
488
+  const cmChar_t* retPtr = cmFileSysVMakeUserFn(h,dir,fn,ext,vl);
489
+  va_end(vl);
490
+  return retPtr;
491
+}
492
+
493
+
494
+const cmChar_t* cmFileSysVMakeDir(     cmFileSysH_t h, const cmChar_t* dir,  va_list vl )
495
+{ return cmFileSysVMakeFn(h,dir,NULL,NULL,vl); }
496
+
497
+const cmChar_t* cmFileSysMakeDir(      cmFileSysH_t h, const cmChar_t* dir,  ... )
498
+{
499
+  va_list vl;
500
+  va_start(vl,dir);
501
+  const cmChar_t* retPtr = cmFileSysVMakeFn(h,dir,NULL,NULL,vl);
502
+  va_end(vl);
503
+  return retPtr;
504
+}
505
+
506
+const cmChar_t* cmFileSysVMakeUserDir( cmFileSysH_t h, const cmChar_t* dir,  va_list vl )
507
+{ return cmFileSysVMakeUserFn(h,dir,NULL,NULL,vl);  }
508
+
509
+const cmChar_t* cmFileSysMakeUserDir(  cmFileSysH_t h, const cmChar_t* dir,  ... )
510
+{
511
+  va_list vl;
512
+  va_start(vl,dir);
513
+  const cmChar_t* retPtr = cmFileSysVMakeUserFn(h,dir,NULL,NULL,vl);
514
+  va_end(vl);
515
+  return retPtr;
516
+}
517
+
518
+
479 519
 void cmFileSysFreeFn( cmFileSysH_t h, const cmChar_t* fn )
480 520
 {
481 521
   cmFs_t* p = _cmFileSysHandleToPtr(h);
@@ -535,6 +575,19 @@ cmFsRC_t    cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir )
535 575
   return kOkFsRC;
536 576
 }
537 577
 
578
+cmFsRC_t    cmFileSysMkUserDir( cmFileSysH_t h, const cmChar_t* dir0 )
579
+{
580
+  const cmChar_t* dir = cmFileSysMakeUserFn(h,dir0,NULL,NULL,NULL);
581
+  
582
+  if( dir == NULL )
583
+    return _cmFileSysError(_cmFileSysHandleToPtr(h),kFormFnFailFsRC,0, "The specified directory string /<user>/%s could not be formed.",cmStringNullGuard(dir0));
584
+  
585
+  cmFsRC_t rc = cmFileSysMkDir(h,dir);
586
+  cmFileSysFreeFn(h,dir);
587
+  return rc;
588
+}
589
+
590
+
538 591
 cmFsRC_t    cmFileSysMkDirAll( cmFileSysH_t h, const cmChar_t* dir )
539 592
 {
540 593
   cmFsRC_t   rc = kOkFsRC;
@@ -561,6 +614,18 @@ cmFsRC_t    cmFileSysMkDirAll( cmFileSysH_t h, const cmChar_t* dir )
561 614
   return rc;
562 615
 }
563 616
 
617
+cmFsRC_t    cmFileSysMkUserDirAll( cmFileSysH_t h, const cmChar_t* dir0 )
618
+{
619
+  const cmChar_t* dir = cmFileSysMakeUserFn(h,dir0,NULL,NULL,NULL);
620
+  
621
+  if( dir == NULL )
622
+    return _cmFileSysError(_cmFileSysHandleToPtr(h),kFormFnFailFsRC,0,"The specified directory string /<user>/%s could not be formed.",cmStringNullGuard(dir0));
623
+  
624
+  cmFsRC_t rc = cmFileSysMkDirAll(h,dir);
625
+  cmFileSysFreeFn(h,dir);
626
+  return rc;
627
+}
628
+
564 629
 cmFileSysPathPart_t* cmFileSysPathParts( cmFileSysH_t h, const cmChar_t* pathStr )
565 630
 {
566 631
 
@@ -1171,6 +1236,42 @@ const cmChar_t*      cmFsMakeFn(  const cmChar_t* dirPrefix, const cmChar_t* fn,
1171 1236
   return retPtr;
1172 1237
 }
1173 1238
 
1239
+const cmChar_t* cmFsVMakeUserFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl )
1240
+{ return cmFileSysVMakeUserFn(_cmFsH,dirPrefix,fn,ext,vl); }
1241
+
1242
+const cmChar_t* cmFsMakeUserFn(  const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... )
1243
+{
1244
+  va_list vl;
1245
+  va_start(vl,ext);
1246
+  const cmChar_t* retPtr = cmFsVMakeUserFn(dirPrefix,fn,ext,vl);
1247
+  va_end(vl);
1248
+  return retPtr;
1249
+}
1250
+
1251
+const cmChar_t* cmFsVMakeDir(     const cmChar_t* dir,  va_list vl )
1252
+{ return cmFileSysVMakeDir(_cmFsH,dir,vl); }
1253
+
1254
+const cmChar_t* cmFsMakeDir(     const cmChar_t* dir,  ... )
1255
+{
1256
+  va_list vl;
1257
+  va_start(vl,dir);
1258
+  const cmChar_t* retPtr = cmFsVMakeDir(dir,vl);
1259
+  va_end(vl);
1260
+  return retPtr;
1261
+}
1262
+
1263
+const cmChar_t* cmFsVMakeUserDir( const cmChar_t* dir,  va_list vl )
1264
+{ return cmFileSysVMakeUserDir(_cmFsH,dir,vl);  }
1265
+
1266
+const cmChar_t* cmFsMakeUserDir( const cmChar_t* dir,  ... )
1267
+{
1268
+  va_list vl;
1269
+  va_start(vl,dir);
1270
+  const cmChar_t* retPtr = cmFsVMakeUserDir(dir,vl);
1271
+  va_end(vl);
1272
+  return retPtr;
1273
+}
1274
+
1174 1275
 void                 cmFsFreeFn(  const cmChar_t* fn )
1175 1276
 { cmFileSysFreeFn(_cmFsH, fn); }
1176 1277
 
@@ -1183,6 +1284,12 @@ cmFsRC_t             cmFsMkDir( const cmChar_t* dir )
1183 1284
 cmFsRC_t             cmFsMkDirAll( const cmChar_t* dir )
1184 1285
 { return cmFileSysMkDirAll(_cmFsH,dir); }
1185 1286
 
1287
+cmFsRC_t             cmFsMkUserDir( const cmChar_t* dir )
1288
+{ return cmFileSysMkUserDir(_cmFsH,dir); }
1289
+
1290
+cmFsRC_t             cmFsMkUserDirAll( const cmChar_t* dir )
1291
+{ return cmFileSysMkUserDirAll(_cmFsH,dir); }
1292
+
1186 1293
 cmFileSysPathPart_t* cmFsPathParts(     const cmChar_t* pathNameStr )
1187 1294
 { return cmFileSysPathParts(_cmFsH,pathNameStr); }
1188 1295
 
@@ -1222,21 +1329,14 @@ cmFsRC_t             cmFsErrorCode()
1222 1329
 //(
1223 1330
 //
1224 1331
 // cmFileSysTest() function gives usage and testing examples 
1225
-// for some of the cmFileSys functions.
1226
-// Note that the HOME_DIR macro should be set to your true
1227
-// $HOME directroy and SRC_DIR should be set to any existing
1228
-// and accessible directory.
1332
+// for some of the cmFileSys functions. Note that the
1333
+// 'dir0' directory should exist and contain files and
1334
+// a shallow sub-tree in order to exercise the directory
1335
+// tree walking routine.
1229 1336
 //
1230 1337
 //)
1231 1338
 //(
1232 1339
 
1233
-#if defined(OS_OSX)
1234
-#define HOME_DIR "/Users/kevin"
1235
-#else
1236
-#define HOME_DIR "/home/kevin"
1237
-#endif
1238
-
1239
-#define SRC_DIR  HOME_DIR"/src"
1240 1340
 
1241 1341
 void _cmFileSysTestFnParser( 
1242 1342
   cmFileSysH_t    h, 
@@ -1250,10 +1350,10 @@ cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
1250 1350
 
1251 1351
   cmFsRC_t        rc      = kOkFsRC;
1252 1352
   cmFileSysH_t    h       = cmFileSysNullHandle;
1253
-  const char      dir0[]  = SRC_DIR; 
1254
-  const char      dir1[]  = HOME_DIR"/blah";
1255
-  const char      file0[] = HOME_DIR"/.emacs";
1256
-  const char      file1[] = HOME_DIR"/blah.txt";
1353
+  const char*     dir0    = cmFsMakeUserDir("src/kc",NULL); 
1354
+  const char*     dir1    = cmFsMakeUserDir("blah",NULL,NULL,NULL);
1355
+  const char*     file0   = cmFsMakeUserFn(NULL,".emacs",NULL,NULL);
1356
+  const char*     file1   = cmFsMakeUserFn(NULL,"blah","txt",NULL);
1257 1357
   const char      not[]   = " not ";
1258 1358
   const char      e[]     = " ";
1259 1359
   bool            fl      = false;
@@ -1288,15 +1388,15 @@ cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
1288 1388
   //----------------------------------------------------------
1289 1389
   // Test the file name creation functions
1290 1390
   //
1291
-  if((fn = cmFileSysMakeFn(h,HOME_DIR,"cmFileSys",
1292
-        "c","src","cm","src",NULL)) != NULL)
1391
+  if((fn = cmFileSysMakeUserFn(h,"src","cmFileSys",
1392
+        "c","cm","src",NULL)) != NULL)
1293 1393
   {
1294 1394
     printf("File:'%s'\n",fn);
1295 1395
   }
1296 1396
   cmFileSysFreeFn(h,fn);
1297 1397
 
1298
-  if((fn = cmFileSysMakeFn(h,HOME_DIR,"cmFileSys",
1299
-        ".c","/src/","/cm/","/src/",NULL)) != NULL )
1398
+  if((fn = cmFileSysMakeUserFn(h,"src","cmFileSys",
1399
+        ".c","/cm/","/src/",NULL)) != NULL )
1300 1400
   {
1301 1401
     printf("File:'%s'\n",fn);
1302 1402
   }
@@ -1305,23 +1405,26 @@ cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
1305 1405
   //----------------------------------------------------------
1306 1406
   // Test the file name parsing functions
1307 1407
   //
1308
-  _cmFileSysTestFnParser(h,&ctx->rpt,
1309
-                         HOME_DIR"/src/cm/src/cmFileSys.c");
1310
-
1311
-  _cmFileSysTestFnParser(h,&ctx->rpt,
1312
-                         HOME_DIR"/src/cm/src/cmFileSys");
1313 1408
 
1314
-  _cmFileSysTestFnParser(h,&ctx->rpt,
1315
-                         HOME_DIR"/src/cm/src/cmFileSys/");
1409
+  const char* fn0 = cmFileSysMakeUserFn(h,"src/cm/src","cmFileSys","c",NULL);
1410
+  const char* fn1 = cmFileSysMakeUserFn(h,"src/cm/src","cmFileSys",NULL,NULL);
1411
+  const char* fn2 = cmFileSysMakeUserDir(h,"src/cm/src/cmFileSys/",NULL);
1412
+  
1413
+  _cmFileSysTestFnParser(h,&ctx->rpt,fn0);
1414
+  _cmFileSysTestFnParser(h,&ctx->rpt,fn1);
1415
+  _cmFileSysTestFnParser(h,&ctx->rpt,fn2);
1316 1416
 
1317 1417
   _cmFileSysTestFnParser(h,&ctx->rpt,"cmFileSys.c");
1318 1418
   _cmFileSysTestFnParser(h,&ctx->rpt,"/");
1319 1419
   _cmFileSysTestFnParser(h,&ctx->rpt," ");
1320 1420
 
1421
+  cmFileSysFreeFn(h,fn0);
1422
+  cmFileSysFreeFn(h,fn1);
1423
+  cmFileSysFreeFn(h,fn1);
1424
+
1321 1425
   //----------------------------------------------------------
1322 1426
   // Test the directory tree walking routines.
1323 1427
   //
1324
-  cmRptPrintf(&ctx->rpt,"Dir Entry Test:\n");
1325 1428
   cmFileSysDirEntry_t* dep;
1326 1429
   unsigned             dirEntCnt;
1327 1430
   unsigned             filterFlags = kDirFsFl 
@@ -1329,8 +1432,11 @@ cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
1329 1432
                                    | kRecurseFsFl 
1330 1433
                                    | kFullPathFsFl;
1331 1434
 
1332
-  if((dep = cmFileSysDirEntries(h,SRC_DIR"/doc",filterFlags,
1333
-                                &dirEntCnt)) != NULL)
1435
+  const char* src_dir = cmFileSysMakeFn(h,dir0,"doc",NULL,NULL);
1436
+
1437
+  cmRptPrintf(&ctx->rpt,"Dir Entry Test: %s\n",src_dir);
1438
+
1439
+  if((dep = cmFileSysDirEntries(h,src_dir,filterFlags,&dirEntCnt)) != NULL)
1334 1440
   {
1335 1441
     unsigned i;
1336 1442
     for(i=0; i<dirEntCnt; ++i)
@@ -1339,6 +1445,8 @@ cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
1339 1445
     cmFileSysDirFreeEntries(h,dep);
1340 1446
   }
1341 1447
 
1448
+  cmFileSysFreeFn(h,src_dir);
1449
+
1342 1450
   //----------------------------------------------------------
1343 1451
   // Test the directory parsing/building routines.
1344 1452
   //
@@ -1372,7 +1480,7 @@ cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
1372 1480
   //----------------------------------------------------------
1373 1481
   // Test the extended mkdir routine.
1374 1482
   //
1375
-  if( cmFileSysMkDirAll(h, HOME_DIR"/temp/doc/doc" )!=kOkFsRC )
1483
+  if( cmFileSysMkUserDirAll(h, "/temp/doc/doc" )!=kOkFsRC )
1376 1484
   {
1377 1485
     cmRptPrint(&ctx->rpt,"cmFileSysMkDirAll() failed.\n");
1378 1486
   }
@@ -1383,6 +1491,11 @@ cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
1383 1491
 
1384 1492
   cmRptPrintf(&ctx->rpt,"File Test done\n");
1385 1493
 
1494
+  cmFsFreeFn(dir0);
1495
+  cmFsFreeFn(dir1);
1496
+  cmFsFreeFn(file0);
1497
+  cmFsFreeFn(file1);
1498
+
1386 1499
   return rc;    
1387 1500
 }
1388 1501
 

+ 37
- 6
cmFileSys.h Просмотреть файл

@@ -35,7 +35,8 @@ extern "C" {
35 35
     kLinuxFailFsRC,
36 36
     kInvalidDirFsRC,
37 37
     kGenFileFailFsRC,
38
-    kAccessFailFsRC
38
+    kAccessFailFsRC,
39
+    kFormFnFailFsRC
39 40
   };
40 41
 
41 42
 
@@ -86,10 +87,24 @@ extern "C" {
86 87
   // The memory used by the string will exist until it is released with cmFileSysFreeFn()
87 88
   // or the cmFileSys object is finalized.
88 89
   const cmChar_t* cmFileSysMakeFn( cmFileSysH_t h, const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
90
+
91
+  // Same as cmFileSysMakeFn but prefixes the entire file path with the current users
92
+  // home directory. (i.e. /home/me/<dirPrefix>/<var-args-dir-0>/<var-args-dir1>/<fn>.<ext>)
93
+  const cmChar_t* cmFileSysMakeUserFn( cmFileSysH_t h, const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
89 94
   
90
-  // Same as cmFileSysMakeFn with but with a va_list argument to accept the var. args. parameters.
95
+  // Same as cmFileSysMakeFn but with a va_list argument to accept the var. args. parameters.
91 96
   const cmChar_t* cmFileSysVMakeFn( cmFileSysH_t h, const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
92 97
 
98
+  // Same as cmFileSysMakeUserFn but with a va_list argument to accept the var. args parameters.
99
+  const cmChar_t* cmFileSysVMakeUserFn( cmFileSysH_t h, const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
100
+
101
+  // Equivalent to same named cmFileSysMakeFn() functions but form a directory
102
+  // path rather than a file name path.
103
+  const cmChar_t* cmFileSysVMakeDir(     cmFileSysH_t h, const cmChar_t* dir,  va_list vl );
104
+  const cmChar_t* cmFileSysMakeDir(      cmFileSysH_t h, const cmChar_t* dir,  ... );
105
+  const cmChar_t* cmFileSysVMakeUserDir( cmFileSysH_t h, const cmChar_t* dir,  va_list vl );
106
+  const cmChar_t* cmFileSysMakeUserDir(  cmFileSysH_t h, const cmChar_t* dir,  ... );
107
+  
93 108
   // Release the file name created through an earlier call to cmFileSysMakeFn().
94 109
   void            cmFileSysFreeFn( cmFileSysH_t h, const cmChar_t* fn );
95 110
 
@@ -103,10 +118,16 @@ extern "C" {
103 118
   // final directory.
104 119
   cmFsRC_t    cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir );
105 120
   
121
+  // Same as cmFileSysMkDir() but 'dir' is automatically prefixed with the users home directory.
122
+  cmFsRC_t    cmFileSysMkUserDir( cmFileSysH_t h, const cmChar_t* dir );
123
+  
106 124
   // Create a complete directory path - where any of the path segments may
107 125
   // not already exist.
108 126
   cmFsRC_t    cmFileSysMkDirAll( cmFileSysH_t h, const cmChar_t* dir );
109 127
 
128
+  // Same as cmFileSysMkDir() but 'dir' is automatically prefixed with the users home directory.
129
+  cmFsRC_t    cmFileSysMkUserDirAll( cmFileSysH_t h, const cmChar_t* dir );
130
+
110 131
   // Parse a path into its parts:
111 132
   //  
112 133
   // Return record used by cmFileSysParts()
@@ -212,14 +233,24 @@ extern "C" {
212 233
   bool            cmFsIsFile( const cmChar_t* fnStr ); 
213 234
   bool            cmFsIsLink( const cmChar_t* fnStr ); 
214 235
 
215
-  const cmChar_t* cmFsVMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
216
-  const cmChar_t* cmFsMakeFn(  const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
217
-  void            cmFsFreeFn(  const cmChar_t* fn );
218
-  cmFsRC_t        cmFsGenFn(  const cmChar_t* dir, const cmChar_t* prefixStr, const cmChar_t* extStr, const cmChar_t** fnPtr );
236
+  const cmChar_t* cmFsVMakeFn(     const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
237
+  const cmChar_t* cmFsMakeFn(      const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
238
+  const cmChar_t* cmFsVMakeUserFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
239
+  const cmChar_t* cmFsMakeUserFn(  const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
240
+  
241
+  const cmChar_t* cmFsVMakeDir(     const cmChar_t* dirPrefix,  va_list vl );
242
+  const cmChar_t* cmFsMakeDir(      const cmChar_t* dirPrefix,  ... );
243
+  const cmChar_t* cmFsVMakeUserDir( const cmChar_t* dirPrefix,  va_list vl );
244
+  const cmChar_t* cmFsMakeUserDir(  const cmChar_t* dirPrefix,  ... );
245
+  
246
+  void            cmFsFreeFn(      const cmChar_t* fn );
247
+  cmFsRC_t        cmFsGenFn(       const cmChar_t* dir, const cmChar_t* prefixStr, const cmChar_t* extStr, const cmChar_t** fnPtr );
219 248
 
220 249
 
221 250
   cmFsRC_t        cmFsMkDir( const cmChar_t* dir );
251
+  cmFsRC_t        cmFsMkUserDir( const cmChar_t* dir );
222 252
   cmFsRC_t        cmFsMkDirAll( const cmChar_t* dir );
253
+  cmFsRC_t        cmFsMkUserDirAll( const cmChar_t* dir );
223 254
 
224 255
   cmFileSysPathPart_t* cmFsPathParts(     const cmChar_t* pathNameStr );
225 256
   void   cmFsFreePathParts( cmFileSysPathPart_t* p );

Загрузка…
Отмена
Сохранить