libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmFileSys.c 33KB

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