libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmFileSys.c 33KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLinkedHeap.h"
  9. #include "cmFileSys.h"
  10. #include "cmText.h"
  11. #include <sys/stat.h>
  12. #include <errno.h>
  13. #include <libgen.h> // basename(), dirname()
  14. #include <dirent.h> // opendir()/readdir()
  15. #include <limits.h> // PATH_MAX
  16. #include <sys/types.h> // mkdir
  17. #ifdef OS_OSX
  18. #include "osx/cmFileSysOsx.h"
  19. #endif
  20. #ifdef OS_LINUX
  21. #include "linux/cmFileSysLinux.h"
  22. #endif
  23. cmFileSysH_t cmFileSysNullHandle = {NULL};
  24. typedef struct
  25. {
  26. cmErr_t err;
  27. cmLHeapH_t heapH;
  28. const cmChar_t* appNameStr;
  29. #ifdef OS_OSX
  30. _cmFsOsx_t* p;
  31. cmChar_t* prefDir;
  32. #endif
  33. #ifdef OS_LINUX
  34. _cmFsLinux_t* p;
  35. #endif
  36. } cmFs_t;
  37. cmFsRC_t _cmFileSysError( cmFs_t* p, cmFsRC_t rc, int sysErr, const cmChar_t* fmt, ... )
  38. {
  39. va_list vl;
  40. va_start(vl,fmt);
  41. if( sysErr == 0 )
  42. rc = cmErrVMsg(&p->err,rc,fmt,vl);
  43. else
  44. {
  45. const unsigned bufCharCnt = 511;
  46. cmChar_t buf[bufCharCnt+1];
  47. vsnprintf(buf,bufCharCnt,fmt,vl);
  48. rc = cmErrMsg(&p->err,rc,"%s\nSystem Msg:%s",buf,strerror(sysErr));
  49. }
  50. va_end(vl);
  51. return rc;
  52. }
  53. cmFs_t* _cmFileSysHandleToPtr( cmFileSysH_t h )
  54. {
  55. cmFs_t* p = (cmFs_t*)h.h;
  56. assert( p != NULL);
  57. return p;
  58. }
  59. cmFsRC_t _cmFileSysFinalize( cmFs_t* p )
  60. {
  61. cmFsRC_t rc = kOkFsRC;
  62. if( cmLHeapIsValid(p->heapH) )
  63. cmLHeapDestroy(&p->heapH);
  64. #ifdef OS_OSX
  65. if( p->p != NULL )
  66. if((rc = _cmOsxFileSysFinalize(p->p) ) != kOkFsRC )
  67. {
  68. _cmFileSysError(p,kOsxFailFsRC,0,"The OSX file system finalization failed.");
  69. return rc;
  70. }
  71. #endif
  72. #ifdef OS_LINUX
  73. if( p->p != NULL )
  74. if((rc = _cmLinuxFileSysFinalize(p->p) ) != kOkFsRC )
  75. {
  76. _cmFileSysError(p,kLinuxFailFsRC,0,"The Linux file system finalization failed.");
  77. return rc;
  78. }
  79. #endif
  80. cmMemPtrFree(&p);
  81. return rc;
  82. }
  83. cmFsRC_t cmFileSysInitialize( cmFileSysH_t* hp, cmCtx_t* ctx, const cmChar_t* appNameStr )
  84. {
  85. cmFs_t* p;
  86. cmFsRC_t rc;
  87. cmErr_t err;
  88. if((rc = cmFileSysFinalize(hp)) != kOkFsRC )
  89. return rc;
  90. cmErrSetup(&err,&ctx->rpt,"File System");
  91. if((p = cmMemAllocZ( cmFs_t, 1 )) == NULL )
  92. return cmErrMsg(&err,kMemAllocErrFsRC,"Unable to allocate the file system object.");
  93. cmErrClone(&p->err,&err);
  94. if(cmLHeapIsValid( p->heapH = cmLHeapCreate(1024,ctx)) == false )
  95. {
  96. rc = _cmFileSysError(p,kLHeapAllocErrFsRC,0,"Unable to allocate the linked heap.");
  97. goto errLabel;
  98. }
  99. p->appNameStr = cmLhAllocStr(p->heapH,appNameStr);
  100. hp->h = p;
  101. #ifdef OS_OSX
  102. if( (rc = _cmOsxFileSysInit(&p->p, p->heapH, &p->err)) != kOkFsRC )
  103. {
  104. rc = _cmFileSysError(p,kOsxFailFsRC,0,"OSX file system initialization failed.");
  105. goto errLabel;
  106. }
  107. const cmChar_t* dir = cmFsMakeFn(p->p->prefDir,appNameStr,NULL,NULL);
  108. // BUG?
  109. // we reuse p->p->prefDir here because the one returned by the platform
  110. // specific code is never released ... which isn't quite right either
  111. // See osx/cmFileSysOsx.c.
  112. p->p->prefDir = cmLhAllocStr(p->heapH,dir);
  113. cmFsFreeFn(dir);
  114. #endif
  115. #ifdef OS_LINUX
  116. if( (rc = _cmLinuxFileSysInit(&p->p, p->heapH, &p->err)) != kOkFsRC )
  117. {
  118. rc = _cmFileSysError(p,kLinuxFailFsRC,0,"Linux file system initialization failed.");
  119. goto errLabel;
  120. }
  121. else
  122. {
  123. #endif
  124. #ifdef OS_LINUX
  125. cmChar_t hidAppNameStr[ strlen(appNameStr) + 2 ];
  126. strcpy(hidAppNameStr,".");
  127. strcat(hidAppNameStr,appNameStr);
  128. p->p->prefDir = cmFileSysMakeFn( *hp, p->p->prefDir, hidAppNameStr, NULL, NULL );
  129. // the resource directory must exist before the program can start
  130. p->p->rsrcDir = cmFileSysMakeFn( *hp, p->p->rsrcDir, appNameStr, NULL, NULL );
  131. }
  132. #endif
  133. errLabel:
  134. if( rc != kOkFsRC )
  135. return _cmFileSysFinalize(p);
  136. return kOkFsRC;
  137. }
  138. cmFsRC_t cmFileSysFinalize( cmFileSysH_t* hp )
  139. {
  140. cmFsRC_t rc;
  141. if( hp==NULL || cmFileSysIsValid(*hp) == false )
  142. return kOkFsRC;
  143. cmFs_t* p = _cmFileSysHandleToPtr(*hp);
  144. if((rc = _cmFileSysFinalize(p)) != kOkFsRC )
  145. return rc;
  146. hp->h = NULL;
  147. return rc;
  148. }
  149. const cmChar_t* cmFileSysAppName( cmFileSysH_t h )
  150. {
  151. cmFs_t* p = _cmFileSysHandleToPtr(h);
  152. return p->appNameStr;
  153. }
  154. const cmChar_t* cmFileSysPrefsDir( cmFileSysH_t h )
  155. {
  156. cmFs_t* p = _cmFileSysHandleToPtr(h);
  157. #if defined OS_OSX || defined OS_LINUX
  158. return p->p->prefDir;
  159. #else
  160. return NULL;
  161. #endif
  162. }
  163. const cmChar_t* cmFileSysRsrcDir( cmFileSysH_t h )
  164. {
  165. cmFs_t* p = _cmFileSysHandleToPtr(h);
  166. #if defined OS_OSX || defined OS_LINUX
  167. return p->p->rsrcDir;
  168. #else
  169. return NULL;
  170. #endif
  171. }
  172. const cmChar_t* cmFileSysUserDir( cmFileSysH_t h )
  173. {
  174. cmFs_t* p = _cmFileSysHandleToPtr(h);
  175. #if defined OS_OSX || defined OS_LINUX
  176. return p->p->userDir;
  177. #else
  178. return NULL;
  179. #endif
  180. }
  181. bool cmFileSysIsValid( cmFileSysH_t h )
  182. { return h.h != NULL; }
  183. bool _cmFileSysIsDir( cmFs_t* p, const cmChar_t* dirStr )
  184. {
  185. struct stat s;
  186. errno = 0;
  187. if( stat(dirStr,&s) != 0 )
  188. {
  189. // if the dir does not exist
  190. if( errno == ENOENT )
  191. return false;
  192. _cmFileSysError( p, kStatFailFsRC, errno, "'stat' failed on '%s'",dirStr);
  193. return false;
  194. }
  195. return S_ISDIR(s.st_mode);
  196. }
  197. bool cmFileSysIsDir( cmFileSysH_t h, const cmChar_t* dirStr )
  198. {
  199. cmFs_t* p = _cmFileSysHandleToPtr(h);
  200. return _cmFileSysIsDir(p,dirStr);
  201. }
  202. bool _cmFileSysIsFile( cmFs_t* p, const cmChar_t* fnStr )
  203. {
  204. struct stat s;
  205. errno = 0;
  206. if( stat(fnStr,&s) != 0 )
  207. {
  208. // if the file does not exist
  209. if( errno == ENOENT )
  210. return false;
  211. _cmFileSysError( p, kStatFailFsRC, errno, "'stat' failed on '%s'.",fnStr);
  212. return false;
  213. }
  214. return S_ISREG(s.st_mode);
  215. }
  216. bool cmFileSysIsFile( cmFileSysH_t h, const cmChar_t* fnStr )
  217. {
  218. cmFs_t* p = _cmFileSysHandleToPtr(h);
  219. return _cmFileSysIsFile(p,fnStr);
  220. }
  221. bool _cmFileSysIsLink( cmFs_t* p, const cmChar_t* fnStr )
  222. {
  223. struct stat s;
  224. errno = 0;
  225. if( lstat(fnStr,&s) != 0 )
  226. {
  227. // if the file does not exist
  228. if( errno == ENOENT )
  229. return false;
  230. _cmFileSysError( p, kStatFailFsRC, errno, "'stat' failed on '%s'.",fnStr);
  231. return false;
  232. }
  233. return S_ISLNK(s.st_mode);
  234. }
  235. bool cmFileSysIsLink( cmFileSysH_t h, const cmChar_t* fnStr )
  236. {
  237. cmFs_t* p = _cmFileSysHandleToPtr(h);
  238. return _cmFileSysIsLink(p,fnStr);
  239. }
  240. bool _cmFileSysIsSocket( cmFs_t* p, const cmChar_t* fnStr )
  241. {
  242. struct stat s;
  243. errno = 0;
  244. if( stat(fnStr,&s) != 0 )
  245. {
  246. // if the file does not exist
  247. if( errno == ENOENT )
  248. return false;
  249. _cmFileSysError( p, kStatFailFsRC, errno, "'stat' failed on '%s'.",fnStr);
  250. return false;
  251. }
  252. return S_ISSOCK(s.st_mode);
  253. }
  254. bool _cmFileSysConcat( cmChar_t* rp, unsigned rn, char sepChar, const cmChar_t* suffixStr )
  255. {
  256. unsigned m = strlen(rp);
  257. // m==0 if no sep needs to be inserted or removed
  258. //if( m == 0 )
  259. // return false;
  260. if( m != 0 )
  261. {
  262. // if a sep char needs to be inserted
  263. if( rp[m-1] != sepChar && suffixStr[0] != sepChar )
  264. {
  265. assert((m+1)<rn);
  266. if((m+1)>=rn)
  267. return false;
  268. rp[m] = sepChar;
  269. rp[m+1]= 0;
  270. ++m;
  271. }
  272. else
  273. // if a sep char needs to be removed
  274. if( rp[m-1] == sepChar && suffixStr[0] == sepChar )
  275. {
  276. rp[m-1] = 0;
  277. --m;
  278. }
  279. }
  280. assert( rn>=m && strlen(rp)+strlen(suffixStr) <= rn );
  281. strncat(rp,suffixStr,rn-m);
  282. return true;
  283. }
  284. const cmChar_t* cmFileSysVMakeFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* fn, const cmChar_t* ext, va_list vl )
  285. {
  286. cmFsRC_t rc = kOkFsRC;
  287. cmChar_t* rp = NULL;
  288. const cmChar_t* dp = NULL;
  289. unsigned n = 0;
  290. cmFs_t* p = _cmFileSysHandleToPtr(h);
  291. char pathSep = cmPathSeparatorChar[0];
  292. char extSep = '.';
  293. va_list vl_t;
  294. va_copy(vl_t,vl);
  295. assert( fn != NULL );
  296. // get prefix directory length
  297. if( dir != NULL )
  298. n += strlen(dir) + 1; // add 1 for ending sep
  299. // get file name length
  300. n += strlen(fn);
  301. // get extension length
  302. if( ext != NULL )
  303. n += strlen(ext) + 1; // add 1 for period
  304. // get length of all var args dir's
  305. while( (dp = va_arg(vl,const cmChar_t*)) != NULL )
  306. n += strlen(dp) + 1; // add 1 for ending sep
  307. // add 1 for terminating zero and allocate memory
  308. if((rp = cmLHeapAllocZ( p->heapH, n+1 )) == NULL )
  309. {
  310. rc = _cmFileSysError(p,kMemAllocErrFsRC,0,"Unable to allocate file name memory.");
  311. goto errLabel;
  312. }
  313. va_copy(vl,vl_t);
  314. rp[n] = 0;
  315. rp[0] = 0;
  316. // copy out the prefix dir
  317. if( dir != NULL )
  318. strncat(rp,dir,n-strlen(rp));
  319. // copy out ecmh of the var arg's directories
  320. while((dp = va_arg(vl,const cmChar_t*)) != NULL )
  321. if(!_cmFileSysConcat(rp,n,pathSep,dp) )
  322. {
  323. assert(0);
  324. rc = _cmFileSysError(p,kAssertFailFsRC,0,"Assert failed.");
  325. goto errLabel;
  326. }
  327. // copy out the file name
  328. if(!_cmFileSysConcat(rp,n,pathSep,fn))
  329. {
  330. assert(0);
  331. rc = _cmFileSysError(p,kAssertFailFsRC,0,"Assert failed.");
  332. goto errLabel;
  333. }
  334. // copy out the extension
  335. if( ext != NULL )
  336. if(!_cmFileSysConcat(rp,n,extSep,ext))
  337. {
  338. assert(0);
  339. rc = _cmFileSysError(p,kAssertFailFsRC,0,"Assert failed.");
  340. goto errLabel;
  341. }
  342. assert(strlen(rp)<=n);
  343. errLabel:
  344. if( rc != kOkFsRC && rp != NULL )
  345. cmLHeapFree(p->heapH, rp );
  346. return rp;
  347. }
  348. const cmChar_t* cmFileSysMakeFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* fn, const cmChar_t* ext, ... )
  349. {
  350. va_list vl;
  351. va_start(vl,ext);
  352. const cmChar_t* retPtr = cmFileSysVMakeFn(h,dir,fn,ext,vl);
  353. va_end(vl);
  354. return retPtr;
  355. }
  356. void cmFileSysFreeFn( cmFileSysH_t h, const cmChar_t* fn )
  357. {
  358. cmFs_t* p = _cmFileSysHandleToPtr(h);
  359. if( fn == NULL )
  360. return;
  361. cmLHeapFree(p->heapH, (void*)fn);
  362. }
  363. cmFsRC_t cmFileSysGenFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* prefixStr, const cmChar_t* extStr, const cmChar_t** fnPtr )
  364. {
  365. cmFsRC_t rc = kOkFsRC;
  366. cmFs_t* p = _cmFileSysHandleToPtr(h);
  367. unsigned maxAttemptCnt = 0xffff;
  368. *fnPtr = NULL;
  369. assert(dir != NULL);
  370. if( prefixStr == NULL )
  371. prefixStr = "";
  372. if( extStr == NULL )
  373. extStr = "";
  374. if( !cmFileSysIsDir(h,dir) )
  375. return cmErrMsg(&p->err,kOpenDirFailFsRC,"File name generation failed because the directory '%s' does not exist.",cmStringNullGuard(dir));
  376. unsigned i;
  377. for(i=0; *fnPtr==NULL; ++i)
  378. {
  379. cmChar_t* fn = cmTsPrintfP(NULL,"%s%i",prefixStr,i);
  380. const cmChar_t* path = cmFileSysMakeFn(h,dir,fn,extStr,NULL );
  381. if( !cmFileSysIsFile(h,path) )
  382. *fnPtr = cmMemAllocStr(path);
  383. cmFileSysFreeFn(h,path);
  384. cmMemFree(fn);
  385. if( i == maxAttemptCnt )
  386. return cmErrMsg(&p->err,kGenFileFailFsRC,"File name generation failed because a suitable file name could not be found after %i attempts.",maxAttemptCnt);
  387. };
  388. return rc;
  389. }
  390. cmFsRC_t cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir )
  391. {
  392. cmFs_t* p = _cmFileSysHandleToPtr(h);
  393. if( mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0 )
  394. return _cmFileSysError( p, kMkDirFailFsRC, errno, "The attempt to create the directory '%s' failed.",dir);
  395. return kOkFsRC;
  396. }
  397. cmFsRC_t cmFileSysMkDirAll( cmFileSysH_t h, const cmChar_t* dir )
  398. {
  399. cmFsRC_t rc = kOkFsRC;
  400. cmFs_t* p = _cmFileSysHandleToPtr(h);
  401. cmChar_t** a = NULL;
  402. unsigned i;
  403. if((a = cmFileSysDirParts(h,dir)) == NULL )
  404. return _cmFileSysError(p, kInvalidDirFsRC, 0, "The directory '%s' could not be parsed.",cmStringNullGuard(dir));
  405. for(i=0; rc==kOkFsRC && a[i]!=NULL; ++i)
  406. {
  407. cmChar_t* d = cmFileSysFormDir(h,a,i+1);
  408. if( cmFileSysIsDir(h,d)==false )
  409. if((rc = cmFileSysMkDir(h,d)) != kOkFsRC )
  410. break;
  411. cmFileSysFreeDir(h,d);
  412. }
  413. cmFileSysFreeDirParts(h,a);
  414. return rc;
  415. }
  416. cmFileSysPathPart_t* cmFileSysPathParts( cmFileSysH_t h, const cmChar_t* pathStr )
  417. {
  418. int n = 0; // char's in pathStr
  419. int dn = 0; // char's in the dir part
  420. int fn = 0; // char's in the name part
  421. int en = 0; // char's in the ext part
  422. cmChar_t* cp = NULL;
  423. cmFileSysPathPart_t* rp = NULL;
  424. cmFs_t* p = _cmFileSysHandleToPtr(h);
  425. if( pathStr==NULL )
  426. return NULL;
  427. // skip leading white space
  428. while( *pathStr )
  429. {
  430. if( isspace(*pathStr ) )
  431. ++pathStr;
  432. else
  433. break;
  434. }
  435. // get the length of pathStr
  436. if((n = strlen(pathStr)) == 0 )
  437. return NULL;
  438. // remove trailing spaces
  439. for(; n>0; --n)
  440. {
  441. if( isspace(pathStr[n-1]) )
  442. --n;
  443. else
  444. break;
  445. }
  446. //
  447. if( n == 0 )
  448. return NULL;
  449. char buf[n+1];
  450. buf[n] = 0;
  451. // Get the last word (which may be a file name) from pathStr.
  452. // (pathStr must be copied into a buf because basename()
  453. // is allowed to change the values in its arg.)
  454. strncpy(buf,pathStr,n);
  455. cp = basename(buf);
  456. if( cp != NULL )
  457. {
  458. cmChar_t* ep;
  459. // does the last word have a period in it
  460. if( (ep = strchr(cp,'.')) != NULL )
  461. {
  462. *ep = 0; // end the file name at the period
  463. ++ep; // set the ext marker
  464. en = strlen(ep); // get the length of the ext
  465. }
  466. fn = strlen(cp); //get the length of the file name
  467. }
  468. // Get the directory part.
  469. // ( pathStr must be copied into a buf because dirname()
  470. // is allowed to change the values in its arg.)
  471. strncpy(buf,pathStr,n);
  472. // if the last char in pathStr[] is '/' then the whole string is a dir.
  473. // (this is not the answer that dirname() and basename() would give).
  474. if( pathStr[n-1] == cmPathSeparatorChar[0] )
  475. {
  476. fn = 0;
  477. en = 0;
  478. dn = n;
  479. cp = buf;
  480. }
  481. else
  482. {
  483. cp = dirname(buf);
  484. }
  485. if( cp != NULL )
  486. dn = strlen(cp);
  487. // get the total size of the returned memory. (add 3 for ecmh possible terminating zero)
  488. n = sizeof(cmFileSysPathPart_t) + dn + fn + en + 3;
  489. // alloc memory
  490. if((rp = (cmFileSysPathPart_t*)cmLHeapAllocZ( p->heapH, n )) == NULL )
  491. {
  492. _cmFileSysError( p, kLHeapAllocErrFsRC, 0, "Unable to allocate the file system path part record for '%s'.",pathStr);
  493. return NULL;
  494. }
  495. // set the return pointers for ecmh of the parts
  496. rp->dirStr = (const cmChar_t* )(rp + 1);
  497. rp->fnStr = rp->dirStr + dn + 1;
  498. rp->extStr = rp->fnStr + fn + 1;
  499. // if there is a directory part
  500. if( dn>0 )
  501. strcpy((cmChar_t*)rp->dirStr,cp);
  502. else
  503. rp->dirStr = NULL;
  504. if( fn || en )
  505. {
  506. // Get the trailing word again.
  507. // pathStr must be copied into a buf because basename() may
  508. // is allowed to change the values in its arg.
  509. strncpy(buf,pathStr,n);
  510. cp = basename(buf);
  511. if( cp != NULL )
  512. {
  513. cmChar_t* ep;
  514. if( (ep = strchr(cp,'.')) != NULL )
  515. {
  516. *ep = 0;
  517. ++ep;
  518. assert( strlen(ep) == en );
  519. strcpy((cmChar_t*)rp->extStr,ep);
  520. }
  521. assert( strlen(cp) == fn );
  522. if(fn)
  523. strcpy((cmChar_t*)rp->fnStr,cp);
  524. }
  525. }
  526. if( fn == 0 )
  527. rp->fnStr = NULL;
  528. if( en == 0 )
  529. rp->extStr = NULL;
  530. return rp;
  531. }
  532. void cmFileSysFreePathParts( cmFileSysH_t h, cmFileSysPathPart_t* pp )
  533. {
  534. if( pp == NULL )
  535. return;
  536. cmFs_t* p = _cmFileSysHandleToPtr(h);
  537. cmLHeapFree(p->heapH, (void*)pp );
  538. }
  539. cmChar_t** cmFileSysDirParts( cmFileSysH_t h, const cmChar_t* dirStr )
  540. {
  541. cmFs_t* p = _cmFileSysHandleToPtr(h);
  542. const cmChar_t* s = dirStr;
  543. const cmChar_t* ep = dirStr + strlen(dirStr);
  544. char pathSep = cmPathSeparatorChar[0];
  545. unsigned n = 2;
  546. unsigned i = 0;
  547. // skip leading white space or pathsep
  548. while( isspace(*s) && s < ep )
  549. ++s;
  550. // if the directory string is empty
  551. if( s >= ep )
  552. return NULL;
  553. // set the beginning of the input dirStr past any leading white space
  554. dirStr = s;
  555. // count the number of dir segments - this might overcount the
  556. // number of segments if there are multiple repeated path seperator
  557. // char's - but this is ok because 'n' is only used to determine
  558. // the size of the returned array - which will simply have some
  559. // NULL entries at the end.
  560. for(; s < ep; ++s )
  561. if( *s == pathSep )
  562. ++n;
  563. // allocate the array
  564. cmChar_t** a = cmLhAllocZ(p->heapH,cmChar_t*,n);
  565. // reset the cur location to the begin of the buf
  566. s = dirStr;
  567. // if the path starts at the root
  568. if( *s == pathSep )
  569. {
  570. a[0] = cmPathSeparatorChar; // cmPathSeparatorChar is a static string in cmGlobal.h
  571. i = 1;
  572. ++s;
  573. }
  574. for(; i<n && s<ep; ++i)
  575. {
  576. const cmChar_t* s1;
  577. if(( s1 = strchr(s,pathSep)) == NULL )
  578. s1 = ep;
  579. if( s1!=s )
  580. {
  581. unsigned sn = (s1 - s)+1;
  582. a[i] = cmLhAlloc(p->heapH,cmChar_t,sn);
  583. strncpy(a[i],s,sn-1);
  584. a[i][sn-1] = 0;
  585. }
  586. s = s1+1;
  587. }
  588. return a;
  589. }
  590. void cmFileSysFreeDirParts( cmFileSysH_t h, cmChar_t** dirStrArray )
  591. {
  592. if( dirStrArray == NULL )
  593. return;
  594. cmFs_t* p = _cmFileSysHandleToPtr(h);
  595. unsigned i;
  596. for(i=0; dirStrArray[i]!=NULL; ++i)
  597. {
  598. // cmPathSeparatorChar is statically alloc'd in cmGlobal.h
  599. if( dirStrArray[i] != cmPathSeparatorChar )
  600. cmLHeapFree(p->heapH,dirStrArray[i]);
  601. }
  602. cmLHeapFree(p->heapH, (void*)dirStrArray );
  603. }
  604. unsigned cmFileSysDirPartsCount(cmFileSysH_t h, cmChar_t** dirStrArray )
  605. {
  606. unsigned i = 0;
  607. if( dirStrArray == NULL )
  608. return 0;
  609. while(dirStrArray[i] != NULL )
  610. ++i;
  611. return i;
  612. }
  613. cmChar_t* cmFileSysFormDir( cmFileSysH_t h, cmChar_t** a, unsigned m )
  614. {
  615. cmFs_t* p = _cmFileSysHandleToPtr(h);
  616. unsigned n;
  617. unsigned i;
  618. // determine the length of the return string
  619. for(i=0,n=0; a[i]!=NULL && i<m; ++i)
  620. n += strlen(a[i]) + strlen(cmPathSeparatorChar);
  621. if( i<m && a[i] == NULL )
  622. {
  623. _cmFileSysError(p,kInvalidDirFsRC,0,"cmFileSysFormDir() cannot form a directory string from %i parts when only %i exist.",m,i);
  624. return NULL;
  625. }
  626. n += 1;
  627. // allocate the return string
  628. cmChar_t* r = cmLhAllocZ(p->heapH,cmChar_t,n);
  629. const cmChar_t* ep = r + n;
  630. // form the return string
  631. for(i=0; a[i]!=NULL && i<m; ++i)
  632. {
  633. strcat(r,a[i]);
  634. if( strcmp(a[i],cmPathSeparatorChar)!=0 && a[i+1]!=NULL )
  635. strcat(r,cmPathSeparatorChar);
  636. assert( r + strlen(r) <= ep );
  637. }
  638. return r;
  639. }
  640. void cmFileSysFreeDir( cmFileSysH_t h, const cmChar_t* dir )
  641. {
  642. if( dir == NULL )
  643. return;
  644. cmFs_t* p = _cmFileSysHandleToPtr(h);
  645. cmLHeapFree(p->heapH,(void*)dir);
  646. }
  647. typedef struct
  648. {
  649. cmFs_t* p;
  650. unsigned filterFlags;
  651. cmFileSysDirEntry_t* rp;
  652. cmChar_t* dataPtr;
  653. cmChar_t* endPtr;
  654. unsigned entryCnt;
  655. unsigned entryIdx;
  656. unsigned dataByteCnt;
  657. unsigned passIdx;
  658. } cmFileSysDeRecd_t;
  659. cmFsRC_t _cmFileSysDirGetEntries( cmFileSysDeRecd_t* drp, const cmChar_t* dirStr )
  660. {
  661. cmFsRC_t rc = kOkFsRC;
  662. DIR* dirp = NULL;
  663. struct dirent* dp = NULL;
  664. char curDirPtr[] = "./";
  665. unsigned dn = 0;
  666. if( dirStr == NULL || strlen(dirStr) == 0 )
  667. dirStr = curDirPtr;
  668. if( _cmFileSysIsDir(drp->p,dirStr) == false )
  669. return rc;
  670. unsigned fnCharCnt= strlen(dirStr) + PATH_MAX;
  671. char fn[ fnCharCnt + 1 ];
  672. // copy the directory into fn[] ...
  673. fn[0] =0;
  674. fn[fnCharCnt] = 0;
  675. strcpy(fn,dirStr);
  676. assert( strlen(fn)+2 < fnCharCnt );
  677. // ... and be sure that it is terminated with a path sep char
  678. if( fn[ strlen(fn)-1 ] != cmPathSeparatorChar[0] )
  679. strcat(fn,cmPathSeparatorChar);
  680. // file names will be appended to the path at this location
  681. unsigned fni = strlen(fn);
  682. // open the directory
  683. if((dirp = opendir(dirStr)) == NULL)
  684. {
  685. rc = _cmFileSysError(drp->p,kOpenDirFailFsRC,errno,"Unable to open the directory:'%s'.",dirStr);
  686. goto errLabel;
  687. }
  688. // get the next directory entry
  689. while((dp = readdir(dirp)) != NULL )
  690. {
  691. // validate d_name
  692. if( (dp->d_name != NULL) && ((dn = strlen(dp->d_name)) > 0) )
  693. {
  694. unsigned flags = 0;
  695. // handle cases where d_name begins with '.'
  696. if( dp->d_name[0] == '.' )
  697. {
  698. if( strcmp(dp->d_name,".") == 0 )
  699. {
  700. if( cmIsFlag(drp->filterFlags,kCurDirFsFl) == false )
  701. continue;
  702. flags |= kCurDirFsFl;
  703. }
  704. if( strcmp(dp->d_name,"..") == 0 )
  705. {
  706. if( cmIsFlag(drp->filterFlags,kParentDirFsFl) == false )
  707. continue;
  708. flags |= kParentDirFsFl;
  709. }
  710. if( flags == 0 )
  711. {
  712. if( cmIsFlag(drp->filterFlags,kInvisibleFsFl) == false )
  713. continue;
  714. flags |= kInvisibleFsFl;
  715. }
  716. }
  717. fn[fni] = 0;
  718. strncat( fn, dp->d_name, fnCharCnt-fni );
  719. unsigned fnN = strlen(fn);
  720. // if the filename is too long for the buffer
  721. if( fnN > fnCharCnt )
  722. {
  723. rc = _cmFileSysError(drp->p, kFnTooLongFsRC, errno, "The directory entry:'%s' was too long to be processed.",dp->d_name);
  724. goto errLabel;
  725. }
  726. // is a link
  727. if( _cmFileSysIsLink(drp->p,fn) )
  728. {
  729. if( cmIsFlag(drp->filterFlags,kLinkFsFl) == false )
  730. continue;
  731. flags |= kLinkFsFl;
  732. if( cmIsFlag(drp->filterFlags,kRecurseLinksFsFl) )
  733. if((rc = _cmFileSysDirGetEntries(drp,fn)) != kOkFsRC )
  734. goto errLabel;
  735. }
  736. else
  737. {
  738. // is the entry a file
  739. if( _cmFileSysIsFile(drp->p,fn) )
  740. {
  741. if( cmIsFlag(drp->filterFlags,kFileFsFl)==false )
  742. continue;
  743. flags |= kFileFsFl;
  744. }
  745. else
  746. {
  747. // is the entry a dir
  748. if( _cmFileSysIsDir(drp->p,fn) )
  749. {
  750. if( cmIsFlag(drp->filterFlags,kDirFsFl) == false)
  751. continue;
  752. flags |= kDirFsFl;
  753. if( cmIsFlag(drp->filterFlags,kRecurseFsFl) )
  754. if((rc = _cmFileSysDirGetEntries(drp,fn)) != kOkFsRC )
  755. goto errLabel;
  756. }
  757. else
  758. {
  759. continue;
  760. }
  761. }
  762. }
  763. //assert(flags != 0);
  764. if( drp->passIdx == 0 )
  765. {
  766. ++drp->entryCnt;
  767. // add 1 for the name terminating zero
  768. drp->dataByteCnt += sizeof(cmFileSysDirEntry_t) + 1;
  769. if( cmIsFlag(drp->filterFlags,kFullPathFsFl) )
  770. drp->dataByteCnt += fnN;
  771. else
  772. drp->dataByteCnt += dn;
  773. }
  774. else
  775. {
  776. assert( drp->passIdx == 1 );
  777. assert( drp->entryIdx < drp->entryCnt );
  778. unsigned n = 0;
  779. if( cmIsFlag(drp->filterFlags,kFullPathFsFl) )
  780. {
  781. n = fnN+1;
  782. assert( drp->dataPtr + n <= drp->endPtr );
  783. strcpy(drp->dataPtr,fn);
  784. }
  785. else
  786. {
  787. n = dn+1;
  788. assert( drp->dataPtr + n <= drp->endPtr );
  789. strcpy(drp->dataPtr,dp->d_name);
  790. }
  791. drp->rp[ drp->entryIdx ].flags = flags;
  792. drp->rp[ drp->entryIdx ].name = drp->dataPtr;
  793. drp->dataPtr += n;
  794. assert( drp->dataPtr <= drp->endPtr);
  795. ++drp->entryIdx;
  796. }
  797. }
  798. }
  799. errLabel:
  800. if( dirp != NULL )
  801. closedir(dirp);
  802. return rc;
  803. }
  804. cmFileSysDirEntry_t* cmFileSysDirEntries( cmFileSysH_t h, const cmChar_t* dirStr, unsigned filterFlags, unsigned* dirEntryCntPtr )
  805. {
  806. cmFsRC_t rc = kOkFsRC;
  807. cmFileSysDeRecd_t r;
  808. memset(&r,0,sizeof(r));
  809. r.p = _cmFileSysHandleToPtr(h);
  810. r.filterFlags = filterFlags;
  811. assert( dirEntryCntPtr != NULL );
  812. *dirEntryCntPtr = 0;
  813. for(r.passIdx=0; r.passIdx<2; ++r.passIdx)
  814. {
  815. if((rc = _cmFileSysDirGetEntries( &r, dirStr )) != kOkFsRC )
  816. goto errLabel;
  817. if( r.passIdx == 0 && r.dataByteCnt>0 )
  818. {
  819. // allocate memory to hold the return values
  820. if(( r.rp = (cmFileSysDirEntry_t *)cmLHeapAllocZ( r.p->heapH, r.dataByteCnt )) == NULL )
  821. {
  822. rc= _cmFileSysError( r.p, kMemAllocErrFsRC, 0, "Unable to allocate %i bytes of dir entry memory.",r.dataByteCnt);
  823. goto errLabel;
  824. }
  825. r.dataPtr = (cmChar_t*)(r.rp + r.entryCnt);
  826. r.endPtr = ((cmChar_t*)r.rp) + r.dataByteCnt;
  827. }
  828. }
  829. errLabel:
  830. if( rc == kOkFsRC )
  831. {
  832. assert( r.entryIdx == r.entryCnt );
  833. *dirEntryCntPtr = r.entryCnt;
  834. }
  835. else
  836. {
  837. if( r.rp != NULL )
  838. cmLHeapFree(r.p->heapH,r.rp);
  839. r.rp = NULL;
  840. }
  841. return r.rp;
  842. }
  843. void cmFileSysDirFreeEntries( cmFileSysH_t h, cmFileSysDirEntry_t* dp )
  844. {
  845. cmFs_t* p = _cmFileSysHandleToPtr(h);
  846. if( dp != NULL )
  847. cmLHeapFree(p->heapH,dp);
  848. }
  849. cmFsRC_t cmFileSysErrorCode( cmFileSysH_t h )
  850. {
  851. cmFs_t* p = _cmFileSysHandleToPtr(h);
  852. return cmErrLastRC(&p->err);
  853. }
  854. //
  855. //======================================================================================================
  856. // Begin global versions
  857. //
  858. cmFileSysH_t _cmFsH = { NULL };
  859. cmFsRC_t cmFsInitialize( cmCtx_t* ctx, const cmChar_t* appNameStr )
  860. { return cmFileSysInitialize(&_cmFsH,ctx,appNameStr); }
  861. cmFsRC_t cmFsFinalize()
  862. { return cmFileSysFinalize(&_cmFsH); }
  863. const cmChar_t* cmFsAppName()
  864. { return cmFileSysAppName(_cmFsH); }
  865. const cmChar_t* cmFsPrefsDir()
  866. { return cmFileSysPrefsDir(_cmFsH); }
  867. const cmChar_t* cmFsRsrcDir()
  868. { return cmFileSysRsrcDir(_cmFsH); }
  869. const cmChar_t* cmFsUserDir()
  870. { return cmFileSysUserDir(_cmFsH); }
  871. bool cmFsIsDir( const cmChar_t* dirStr )
  872. { return cmFileSysIsDir(_cmFsH,dirStr); }
  873. bool cmFsIsFile( const cmChar_t* fnStr )
  874. { return cmFileSysIsFile(_cmFsH,fnStr); }
  875. bool cmFsIsLink( const cmChar_t* fnStr )
  876. { return cmFileSysIsLink(_cmFsH,fnStr); }
  877. const cmChar_t* cmFsVMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl )
  878. { return cmFileSysVMakeFn(_cmFsH,dirPrefix,fn,ext,vl); }
  879. const cmChar_t* cmFsMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... )
  880. {
  881. va_list vl;
  882. va_start(vl,ext);
  883. const cmChar_t* retPtr = cmFsVMakeFn(dirPrefix,fn,ext,vl);
  884. va_end(vl);
  885. return retPtr;
  886. }
  887. void cmFsFreeFn( const cmChar_t* fn )
  888. { cmFileSysFreeFn(_cmFsH, fn); }
  889. cmFsRC_t cmFsGenFn( const cmChar_t* dir, const cmChar_t* prefixStr, const cmChar_t* extStr, const cmChar_t** fnPtr )
  890. { return cmFileSysGenFn(_cmFsH,dir,prefixStr,extStr,fnPtr); }
  891. cmFsRC_t cmFsMkDir( const cmChar_t* dir )
  892. { return cmFileSysMkDir(_cmFsH,dir); }
  893. cmFsRC_t cmFsMkDirAll( const cmChar_t* dir )
  894. { return cmFileSysMkDirAll(_cmFsH,dir); }
  895. cmFileSysPathPart_t* cmFsPathParts( const cmChar_t* pathNameStr )
  896. { return cmFileSysPathParts(_cmFsH,pathNameStr); }
  897. void cmFsFreePathParts( cmFileSysPathPart_t* p )
  898. { cmFileSysFreePathParts(_cmFsH,p); }
  899. cmChar_t** cmFsDirParts( const cmChar_t* dirStr )
  900. { return cmFileSysDirParts(_cmFsH,dirStr); }
  901. void cmFsFreeDirParts( cmChar_t** dirStrArray )
  902. { cmFileSysFreeDirParts(_cmFsH,dirStrArray); }
  903. unsigned cmFsDirPartsCount( cmChar_t** dirStrArray )
  904. { return cmFileSysDirPartsCount(_cmFsH, dirStrArray); }
  905. cmChar_t* cmFsFormDir( cmChar_t** dirStrArray, unsigned n )
  906. { return cmFileSysFormDir(_cmFsH,dirStrArray,n); }
  907. void cmFsFreeDir( const cmChar_t* dir )
  908. { cmFileSysFreeDir(_cmFsH,dir); }
  909. cmFileSysDirEntry_t* cmFsDirEntries( const cmChar_t* dirStr, unsigned includeFlags, unsigned* dirEntryCntPtr )
  910. { return cmFileSysDirEntries(_cmFsH,dirStr,includeFlags,dirEntryCntPtr); }
  911. void cmFsDirFreeEntries( cmFileSysDirEntry_t* p )
  912. { cmFileSysDirFreeEntries(_cmFsH,p); }
  913. cmFsRC_t cmFsErrorCode()
  914. { return cmFileSysErrorCode(_cmFsH); }
  915. // end global version
  916. //======================================================================================================
  917. //
  918. //{ { label:cmFileSysEx }
  919. //(
  920. //
  921. // cmFileSysTest() function gives usage and testing examples
  922. // for some of the cmFileSys functions.
  923. // Note that the HOME_DIR macro should be set to your true
  924. // $HOME directroy and SRC_DIR should be set to any existing
  925. // and accessible directory.
  926. //
  927. //)
  928. //(
  929. #if defined(OS_OSX)
  930. #define HOME_DIR "/Users/kevin"
  931. #else
  932. #define HOME_DIR "/home/kevin"
  933. #endif
  934. #define SRC_DIR HOME_DIR"/src"
  935. void _cmFileSysTestFnParser(
  936. cmFileSysH_t h,
  937. cmRpt_t* rpt,
  938. const cmChar_t* fn );
  939. cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
  940. {
  941. // The global heap manager must have been initialized
  942. // via cmMdInitialize() prior to running this function.
  943. cmFsRC_t rc = kOkFsRC;
  944. cmFileSysH_t h = cmFileSysNullHandle;
  945. const char dir0[] = SRC_DIR;
  946. const char dir1[] = HOME_DIR"/blah";
  947. const char file0[] = HOME_DIR"/.emacs";
  948. const char file1[] = HOME_DIR"/blah.txt";
  949. const char not[] = " not ";
  950. const char e[] = " ";
  951. bool fl = false;
  952. const cmChar_t* fn = NULL;
  953. // Initialize the file system.
  954. if((rc = cmFileSysInitialize(&h,ctx,"fs_test")) != kOkFsRC )
  955. return rc;
  956. //----------------------------------------------------------
  957. // Print the standard directories
  958. //
  959. printf("Prefs Dir:%s\n",cmFsPrefsDir());
  960. printf("Rsrc Dir: %s\n",cmFsRsrcDir());
  961. printf("User Dir: %s\n",cmFsUserDir());
  962. //----------------------------------------------------------
  963. // Run the file system type checker
  964. //
  965. fl = cmFileSysIsDir(h,dir0);
  966. printf("'%s' is%sa directory.\n",dir0, (fl ? e : not));
  967. fl = cmFileSysIsDir(h,dir1);
  968. printf("'%s' is%sa directory.\n",dir1, (fl ? e : not));
  969. fl = cmFileSysIsFile(h,file0);
  970. printf("'%s' is%sa file.\n",file0, (fl ? e : not));
  971. fl = cmFileSysIsFile(h,file1);
  972. printf("'%s' is%sa file.\n",file1, (fl ? e : not));
  973. //----------------------------------------------------------
  974. // Test the file name creation functions
  975. //
  976. if((fn = cmFileSysMakeFn(h,HOME_DIR,"cmFileSys",
  977. "c","src","cm","src",NULL)) != NULL)
  978. {
  979. printf("File:'%s'\n",fn);
  980. }
  981. cmFileSysFreeFn(h,fn);
  982. if((fn = cmFileSysMakeFn(h,HOME_DIR,"cmFileSys",
  983. ".c","/src/","/cm/","/src/",NULL)) != NULL )
  984. {
  985. printf("File:'%s'\n",fn);
  986. }
  987. cmFileSysFreeFn(h,fn);
  988. //----------------------------------------------------------
  989. // Test the file name parsing functions
  990. //
  991. _cmFileSysTestFnParser(h,&ctx->rpt,
  992. HOME_DIR"/src/cm/src/cmFileSys.c");
  993. _cmFileSysTestFnParser(h,&ctx->rpt,
  994. HOME_DIR"/src/cm/src/cmFileSys");
  995. _cmFileSysTestFnParser(h,&ctx->rpt,
  996. HOME_DIR"/src/cm/src/cmFileSys/");
  997. _cmFileSysTestFnParser(h,&ctx->rpt,"cmFileSys.c");
  998. _cmFileSysTestFnParser(h,&ctx->rpt,"/");
  999. _cmFileSysTestFnParser(h,&ctx->rpt," ");
  1000. //----------------------------------------------------------
  1001. // Test the directory tree walking routines.
  1002. //
  1003. cmRptPrintf(&ctx->rpt,"Dir Entry Test:\n");
  1004. cmFileSysDirEntry_t* dep;
  1005. unsigned dirEntCnt;
  1006. unsigned filterFlags = kDirFsFl
  1007. | kFileFsFl
  1008. | kRecurseFsFl
  1009. | kFullPathFsFl;
  1010. if((dep = cmFileSysDirEntries(h,SRC_DIR"/doc",filterFlags,
  1011. &dirEntCnt)) != NULL)
  1012. {
  1013. unsigned i;
  1014. for(i=0; i<dirEntCnt; ++i)
  1015. cmRptPrintf(&ctx->rpt,"%s\n",dep[i].name);
  1016. cmFileSysDirFreeEntries(h,dep);
  1017. }
  1018. //----------------------------------------------------------
  1019. // Test the directory parsing/building routines.
  1020. //
  1021. cmRptPrintf(&ctx->rpt,"Dir Parsing routings:\n");
  1022. cmChar_t** a;
  1023. unsigned j;
  1024. for(j=0; j<2; ++j)
  1025. {
  1026. const cmChar_t* dstr = dir0 + j;
  1027. if((a = cmFileSysDirParts(h,dstr)) == NULL)
  1028. cmRptPrint(&ctx->rpt,"cmFileSysDirParts() failed.\n");
  1029. else
  1030. {
  1031. unsigned i;
  1032. cmRptPrintf(&ctx->rpt,"Input:%s\n",dstr);
  1033. for(i=0; a[i]!=NULL; ++i)
  1034. cmRptPrintf(&ctx->rpt,"%i : %s\n",i,a[i]);
  1035. cmChar_t* d;
  1036. if((d = cmFileSysFormDir(h,a,
  1037. cmFileSysDirPartsCount(h,a))) != NULL )
  1038. {
  1039. cmRptPrintf(&ctx->rpt,"Reformed:%s\n",d);
  1040. }
  1041. cmFileSysFreeDirParts(h,a);
  1042. }
  1043. }
  1044. //----------------------------------------------------------
  1045. // Test the extended mkdir routine.
  1046. //
  1047. if( cmFileSysMkDirAll(h, HOME_DIR"/temp/doc/doc" )!=kOkFsRC )
  1048. {
  1049. cmRptPrint(&ctx->rpt,"cmFileSysMkDirAll() failed.\n");
  1050. }
  1051. // finalize the file system
  1052. if((rc = cmFileSysFinalize(&h)) != kOkFsRC )
  1053. return rc;
  1054. cmRptPrintf(&ctx->rpt,"File Test done\n");
  1055. return rc;
  1056. }
  1057. // Parse a file name and print the results.
  1058. // Called by cmFileSysTest().
  1059. void _cmFileSysTestFnParser(
  1060. cmFileSysH_t h,
  1061. cmRpt_t* rpt,
  1062. const cmChar_t* fn )
  1063. {
  1064. cmFileSysPathPart_t* pp;
  1065. cmRptPrintf(rpt,"Fn Parse Test:%s\n",fn);
  1066. if((pp = cmFileSysPathParts(h,fn)) != NULL )
  1067. {
  1068. if(pp->dirStr != NULL)
  1069. cmRptPrintf(rpt,"Dir:%s\n",pp->dirStr);
  1070. if(pp->fnStr != NULL)
  1071. cmRptPrintf(rpt,"Fn:%s\n",pp->fnStr);
  1072. if(pp->extStr != NULL )
  1073. cmRptPrintf(rpt,"Ext:%s\n",pp->extStr);
  1074. cmFileSysFreePathParts(h,pp);
  1075. }
  1076. cmRptPrintf(rpt,"\n");
  1077. }
  1078. //)
  1079. //}