Source code of Windows XP (NT5)
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.

329 lines
8.7 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. /***************************************************************************/
  4. /***** Common Library Component - File Management - 1 **********************/
  5. /***************************************************************************/
  6. /*
  7. ** Purpose:
  8. ** Opens a file with the mode supplied or checks for its existance.
  9. ** Arguments:
  10. ** szFile: non-NULL, non-empty path to a file to be opened.
  11. ** ofm: mode for opening the file. Sharing modes only apply to
  12. ** other processes. Allowable modes are: ofmExistRead,
  13. ** ofmReadWrite, ofmCreate, ofmRead, ofmWrite and ofmReadWrite.
  14. ** Returns:
  15. ** NULL if a PFH could not be allocated or if the file could not be
  16. ** opened in the mode specified.
  17. ** A PFH for the successfully opened file. This PFH can be used in
  18. ** other Common Library file routines unless ofm was one of the
  19. ** existance flags.
  20. **
  21. ***************************************************************************/
  22. PFH APIENTRY PfhOpenFile(SZ szFile, OFM ofm)
  23. {
  24. PFH pfh;
  25. AssertDataSeg();
  26. ChkArg(szFile != (SZ)NULL && *szFile != '\0', 1, (PFH)NULL);
  27. ChkArg(ofm == ofmExistRead || ofm == ofmExistReadWrite || ofm == ofmRead ||
  28. ofm == ofmWrite || ofm == ofmReadWrite || ofm == ofmCreate, 2,
  29. (PFH)NULL);
  30. if ((pfh = (PFH)SAlloc((CB)sizeof(FH))) == (PFH)NULL)
  31. return((PFH)NULL);
  32. if ((pfh->iDosfh = OpenFile(szFile, &(pfh->ofstruct), ofm)) == -1)
  33. {
  34. SFree(pfh);
  35. return((PFH)NULL);
  36. }
  37. return(pfh);
  38. }
  39. /*
  40. ** Purpose:
  41. ** Closes a PFH and frees it.
  42. ** Arguments:
  43. ** pfh: a valid PFH from PfhOpenFile() of the file to be closed.
  44. ** Returns:
  45. ** fFalse if the PFH could not be closed and/or freed.
  46. ** fTrue if the PFH could be closed and freed.
  47. **
  48. ***************************************************************************/
  49. BOOL APIENTRY FCloseFile(pfh)
  50. PFH pfh;
  51. {
  52. BOOL fReturn;
  53. AssertDataSeg();
  54. ChkArg(pfh != (PFH)NULL, 1, fFalse);
  55. fReturn = (_lclose(pfh->iDosfh) != -1);
  56. SFree(pfh);
  57. return(fReturn);
  58. }
  59. /*
  60. ** Purpose:
  61. ** Frees a pfh.
  62. ** Arguments:
  63. ** pfh: a PFH, no handle to close.
  64. ** Returns:
  65. ** fFalse if the PFH is
  66. ** fTrue if the PFH could be closed and freed.
  67. **
  68. ***************************************************************************/
  69. VOID APIENTRY
  70. FreePfh(
  71. PFH pfh
  72. )
  73. {
  74. AssertDataSeg();
  75. if (pfh != (PFH)NULL) {
  76. SFree(pfh);
  77. }
  78. return;
  79. }
  80. /*
  81. ** Purpose:
  82. ** Reads bytes from a file.
  83. ** Arguments:
  84. ** pfh: valid PFH from a successful PfhOpenFile() call with a
  85. ** readable mode.
  86. ** pbBuf: non-NULL buffer to store the bytes read. This must be at
  87. ** cbMax bytes in length.
  88. ** cbMax: maximum number of bytes to read. This must be greater than
  89. ** zero but not (CB)(-1).
  90. ** Returns:
  91. ** (CB)(-1) if error occurred.
  92. ** The number of bytes actually read and stored in pbBuf. This will
  93. ** be greater than zero. It will be less than cbMax if EOF was
  94. ** encountered. If it is equal to cbMax, it might have reached
  95. ** EOF.
  96. **
  97. ***************************************************************************/
  98. CB APIENTRY CbReadFile(pfh, pbBuf, cbMax)
  99. PFH pfh;
  100. PB pbBuf;
  101. CB cbMax;
  102. {
  103. AssertDataSeg();
  104. ChkArg(pfh != (PFH)NULL, 1, (CB)(-1));
  105. ChkArg(pbBuf != (PB)NULL, 2, (CB)(-1));
  106. ChkArg(cbMax > (CB)0 && cbMax != (CB)(-1), 3, (CB)(-1));
  107. return((CB)_lread(pfh->iDosfh, pbBuf, cbMax));
  108. }
  109. /*
  110. ** Purpose:
  111. ** Write bytes from a buffer to a file.
  112. ** Arguments:
  113. ** pfh: valid PFH previously returned from PfhOpenFile() with
  114. ** write mode set.
  115. ** pbBuf: non-NULL buffer of bytes to be read. Must contain cbMax
  116. ** legitimate bytes to write.
  117. ** cbMax: number of bytes to write from pbBuf. This must be greater
  118. ** than zero and not equal to (CB)(-1).
  119. ** Returns:
  120. ** The number of bytes actually written. Anything less than cbMax
  121. ** indicates an error occurred.
  122. **
  123. ***************************************************************************/
  124. CB APIENTRY CbWriteFile(pfh, pbBuf, cbMax)
  125. PFH pfh;
  126. PB pbBuf;
  127. CB cbMax;
  128. {
  129. CB cbReturn;
  130. AssertDataSeg();
  131. ChkArg(pfh != (PFH)NULL, 1, (CB)0);
  132. ChkArg(pbBuf != (PB)NULL, 2, (CB)0);
  133. ChkArg(cbMax != (CB)0 && cbMax != (CB)(-1), 3, (CB)0);
  134. if ((cbReturn = (CB)_lwrite(pfh->iDosfh, pbBuf, cbMax)) == (CB)(-1))
  135. return(0);
  136. return(cbReturn);
  137. }
  138. /*
  139. ** Purpose:
  140. ** Moves the current read/write location within a file.
  141. ** Arguments:
  142. ** pfh: valid PFH returned from a successful PfhOpenFile() call.
  143. ** l: a signed long number of bytes to move the read/write location.
  144. ** sfm: the Seek File Mode. This can be either sfmSet, sfmCur, or
  145. ** sfmEnd. If sfmSet then l must be non-negative. If sfmEnd then
  146. ** l must be non-positive.
  147. ** Returns:
  148. ** (LFA)(-1) if an error occurred.
  149. ** The LFA of the new read/write location.
  150. **
  151. ****************************************************************************/
  152. LFA APIENTRY LfaSeekFile(PFH pfh,LONG l,SFM sfm)
  153. {
  154. AssertDataSeg();
  155. ChkArg(pfh != (PFH)NULL, 1, (LFA)(-1));
  156. ChkArg(sfm == sfmSet || sfm == sfmCur || sfm == sfmEnd, 3, (LFA)(-1));
  157. ChkArg(sfm != sfmSet || l >= 0L, 203, (LFA)(-1));
  158. ChkArg(sfm != sfmEnd || l <= 0L, 203, (LFA)(-1));
  159. return((LFA)_llseek(pfh->iDosfh, l, sfm));
  160. }
  161. /*
  162. ** Purpose:
  163. ** Determines whether the current read/write location is at the EOF.
  164. ** Arguments:
  165. ** pfh: valid PFH returned from a successful PfhOpenFile() call.
  166. ** Returns:
  167. ** fFalse if error or current read/write location is NOT at the EOF.
  168. ** fTrue if current read/write location IS at the EOF.
  169. **
  170. ****************************************************************************/
  171. BOOL APIENTRY FEndOfFile(pfh)
  172. PFH pfh;
  173. {
  174. LFA lfaCur, lfaEnd;
  175. AssertDataSeg();
  176. ChkArg(pfh != (PFH)NULL, 1, fFalse);
  177. lfaCur = LfaSeekFile(pfh, (LONG)0, sfmCur);
  178. AssertRet(lfaCur != (LFA)(-1), fFalse);
  179. lfaEnd = LfaSeekFile(pfh, (LONG)0, sfmEnd);
  180. AssertRet(lfaEnd != (LFA)(-1), fFalse);
  181. if (lfaCur == lfaEnd)
  182. return(fTrue);
  183. lfaEnd = LfaSeekFile(pfh, (LONG)lfaCur, sfmSet);
  184. Assert(lfaEnd == lfaCur);
  185. return(fFalse);
  186. }
  187. /*
  188. ** Purpose:
  189. ** Removes a file.
  190. ** Arguments:
  191. ** szFileName: valid path to a file to be removed. The file does not
  192. ** have to exist.
  193. ** Returns:
  194. ** fFalse if an error occurred and szFileName still exists after the
  195. ** operation.
  196. ** fTrue if file does not exist after the operation.
  197. **
  198. ****************************************************************************/
  199. BOOL APIENTRY FRemoveFile(szFileName)
  200. SZ szFileName;
  201. {
  202. OFSTRUCT ofs;
  203. AssertDataSeg();
  204. ChkArg(szFileName != (SZ)NULL && *szFileName != '\0', 1, fFalse);
  205. _chmod(szFileName, S_IREAD | S_IWRITE);
  206. OpenFile((LPSTR)szFileName, &ofs, OF_DELETE);
  207. return(PfhOpenFile(szFileName, ofmExistRead) == (PFH)NULL);
  208. }
  209. /*
  210. ** Purpose:
  211. ** Write a zero terminated string to a file (without writing the
  212. ** zero.
  213. ** Arguments:
  214. ** pfh: valid PFH from a successful PfhOpenFile() call.
  215. ** sz: non-NULL string to write.
  216. ** Returns:
  217. ** fFalse if the write operation fails to write all strlen(sz)
  218. ** bytes to the file.
  219. ** fTrue if the write operation writes all strlen(sz) bytes to
  220. ** the file.
  221. **
  222. **************************************************************************/
  223. BOOL APIENTRY FWriteSzToFile(PFH pfh, SZ sz)
  224. {
  225. CB cb;
  226. AssertDataSeg();
  227. ChkArg(pfh != (PFH)NULL, 1, fFalse);
  228. ChkArg(sz != (SZ)NULL, 2, fFalse);
  229. return((cb = strlen(sz)) == (CB)0 ||
  230. (cb != (CB)(-1) && CbWriteFile(pfh, (PB)sz, cb) == cb));
  231. }
  232. /*
  233. ** Purpose:
  234. ** To check for the existance of a file.
  235. ** Arguments:
  236. ** szFile: fully qualified path to filename to check for existance
  237. ** Returns:
  238. ** fFalse if File doesn't exist
  239. ** fTrue if File exists
  240. **
  241. **************************************************************************/
  242. BOOL APIENTRY FFileExists(SZ szFile)
  243. {
  244. OFSTRUCT of;
  245. AssertDataSeg();
  246. ChkArg(szFile != (SZ)NULL, 2, fFalse);
  247. return(OpenFile(szFile,&of,OF_EXIST) != -1);
  248. }
  249. /*
  250. ** Purpose:
  251. ** Take a fully qualified path and return the position of the final
  252. ** file name.
  253. ** Arguments:
  254. ** szPath:
  255. ** Returns:
  256. ** SZ return position of filename in string.
  257. **
  258. **************************************************************************/
  259. SZ APIENTRY szGetFileName(SZ szPath)
  260. {
  261. SZ sz;
  262. ChkArg(szPath != (SZ)NULL, 1, (SZ)NULL);
  263. for (sz=szPath; *sz; sz++) {
  264. }
  265. for (; (sz >= szPath) && (*sz != '\\') && (*sz !=':'); sz--) {
  266. }
  267. return ++sz;
  268. }