Copyright (C) Kevin Larke 2009-2020

This file is part of libcm.

libcm is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

libcm is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

See the GNU General Public License distributed with the libcm package or look here: .


cmFileSysTest() function gives usage and testing examples for some of the cmFileSys functions. Note that the ‘dir0’ directory should exist and contain files and a shallow sub-tree in order to exercise the directory tree walking routine.




void _cmFileSysTestFnParser( 
cmFileSysH_t    h, 
cmRpt_t*        rpt, 
const cmChar_t* fn );

cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
{
  // The global heap manager must have been initialized 
  // via cmMdInitialize() prior to running this function.  
  
  cmFsRC_t        rc      = kOkFsRC;
  cmFileSysH_t    h       = cmFileSysNullHandle;
  const char*     dir0    = cmFsMakeUserDir("src/kc",NULL); 
  const char*     dir1    = cmFsMakeUserDir("blah",NULL,NULL,NULL);
  const char*     file0   = cmFsMakeUserFn(NULL,".emacs",NULL,NULL);
  const char*     file1   = cmFsMakeUserFn(NULL,"blah","txt",NULL);
  const char      not[]   = " not ";
  const char      e[]     = " ";
  bool            fl      = false;
  const cmChar_t* fn      = NULL;
  
  // Initialize the file system.
  if((rc = cmFileSysInitialize(&h,ctx,"fs_test")) != kOkFsRC )
    return rc;
  
  
// Print the standard directories //
printf("Prefs Dir:%s\n",cmFsPrefsDir()); printf("Rsrc Dir: %s\n",cmFsRsrcDir()); printf("User Dir: %s\n",cmFsUserDir());
// Run the file system type checker //
fl = cmFileSysIsDir(h,dir0); printf("'%s' is%sa directory.\n",dir0, (fl ? e : not)); fl = cmFileSysIsDir(h,dir1); printf("'%s' is%sa directory.\n",dir1, (fl ? e : not)); fl = cmFileSysIsFile(h,file0); printf("'%s' is%sa file.\n",file0, (fl ? e : not)); fl = cmFileSysIsFile(h,file1); printf("'%s' is%sa file.\n",file1, (fl ? e : not));
// Test the file name creation functions //
if((fn = cmFileSysMakeUserFn(h,"src","cmFileSys", "c","cm","src",NULL)) != NULL) { printf("File:'%s'\n",fn); } cmFileSysFreeFn(h,fn); if((fn = cmFileSysMakeUserFn(h,"src","cmFileSys", ".c","/cm/","/src/",NULL)) != NULL ) { printf("File:'%s'\n",fn); } cmFileSysFreeFn(h,fn);
// Test the file name parsing functions //
const char* fn0 = cmFileSysMakeUserFn(h,"src/cm/src","cmFileSys","c",NULL); const char* fn1 = cmFileSysMakeUserFn(h,"src/cm/src","cmFileSys",NULL,NULL); const char* fn2 = cmFileSysMakeUserDir(h,"src/cm/src/cmFileSys/",NULL); _cmFileSysTestFnParser(h,&ctx->rpt,fn0); _cmFileSysTestFnParser(h,&ctx->rpt,fn1); _cmFileSysTestFnParser(h,&ctx->rpt,fn2); _cmFileSysTestFnParser(h,&ctx->rpt,"cmFileSys.c"); _cmFileSysTestFnParser(h,&ctx->rpt,"/"); _cmFileSysTestFnParser(h,&ctx->rpt," "); cmFileSysFreeFn(h,fn0); cmFileSysFreeFn(h,fn1); cmFileSysFreeFn(h,fn1);
// Test the directory tree walking routines. //
cmFileSysDirEntry_t* dep; unsigned dirEntCnt; unsigned filterFlags = kDirFsFl | kFileFsFl | kRecurseFsFl | kFullPathFsFl; const char* src_dir = cmFileSysMakeFn(h,dir0,"doc",NULL,NULL); cmRptPrintf(&ctx->rpt,"Dir Entry Test: %s\n",src_dir); if((dep = cmFileSysDirEntries(h,src_dir,filterFlags,&dirEntCnt)) != NULL) { unsigned i; for(i=0; i<dirEntCnt; ++i) cmRptPrintf(&ctx->rpt,"%s\n",dep[i].name); cmFileSysDirFreeEntries(h,dep); } cmFileSysFreeFn(h,src_dir);
// Test the directory parsing/building routines. //
cmRptPrintf(&ctx->rpt,"Dir Parsing routings:\n"); cmChar_t** a; unsigned j; for(j=0; j<2; ++j) { const cmChar_t* dstr = dir0 + j; if((a = cmFileSysDirParts(h,dstr)) == NULL) cmRptPrint(&ctx->rpt,"cmFileSysDirParts() failed.\n"); else { unsigned i; cmRptPrintf(&ctx->rpt,"Input:%s\n",dstr); for(i=0; a[i]!=NULL; ++i) cmRptPrintf(&ctx->rpt,"%i : %s\n",i,a[i]); cmChar_t* d; if((d = cmFileSysFormDir(h,a, cmFileSysDirPartsCount(h,a))) != NULL ) { cmRptPrintf(&ctx->rpt,"Reformed:%s\n",d); } cmFileSysFreeDirParts(h,a); } }
// Test the extended mkdir routine. //
if( cmFileSysMkUserDirAll(h, "/temp/doc/doc" )!=kOkFsRC ) { cmRptPrint(&ctx->rpt,"cmFileSysMkDirAll() failed.\n"); } // finalize the file system if((rc = cmFileSysFinalize(&h)) != kOkFsRC ) return rc; cmRptPrintf(&ctx->rpt,"File Test done\n"); cmFsFreeFn(dir0); cmFsFreeFn(dir1); cmFsFreeFn(file0); cmFsFreeFn(file1); return rc; } // Parse a file name and print the results. // Called by cmFileSysTest(). void _cmFileSysTestFnParser( cmFileSysH_t h, cmRpt_t* rpt, const cmChar_t* fn ) { cmFileSysPathPart_t* pp; cmRptPrintf(rpt,"Fn Parse Test:%s\n",fn); if((pp = cmFileSysPathParts(h,fn)) != NULL ) { if(pp->dirStr != NULL) cmRptPrintf(rpt,"Dir:%s\n",pp->dirStr); if(pp->fnStr != NULL) cmRptPrintf(rpt,"Fn:%s\n",pp->fnStr); if(pp->extStr != NULL ) cmRptPrintf(rpt,"Ext:%s\n",pp->extStr); cmFileSysFreePathParts(h,pp); } cmRptPrintf(rpt,"\n"); }