filestream.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. {$MACRO ON}
  2. (******************************************************************************
  3. *
  4. * Copyright (c) 1994-2000 Palm, Inc. or its subsidiaries.
  5. * All rights reserved.
  6. *
  7. * File: FileStream.h
  8. *
  9. * Release: Palm OS SDK 4.0 (63220)
  10. *
  11. * Description:
  12. * Pilot File Stream equates -- File Streams were initially implemented
  13. * in PalmOS v3.0 (not available in earlier versions)
  14. *
  15. * History:
  16. * 11/24/97 vmk - Created by Vitaly Kruglikov
  17. *
  18. *****************************************************************************)
  19. unit filestream;
  20. interface
  21. uses palmos, coretraps, errorbase;
  22. (************************************************************
  23. * File Stream error codes
  24. * the constant dmErrorClass is defined in ErrorBase.h
  25. *************************************************************)
  26. const
  27. fileErrMemError = fileErrorClass or 1; // out of memory error
  28. fileErrInvalidParam = fileErrorClass or 2; // invalid parameter value passed
  29. fileErrCorruptFile = fileErrorClass or 3; // the file is corrupted/invalid/not a stream file
  30. fileErrNotFound = fileErrorClass or 4; // couldn't find the file
  31. fileErrTypeCreatorMismatch = fileErrorClass or 5; // file's type and creator didn't match those expected
  32. fileErrReplaceError = fileErrorClass or 6; // couldn't replace an existing file
  33. fileErrCreateError = fileErrorClass or 7; // couldn't create a new file
  34. fileErrOpenError = fileErrorClass or 8; // generic open error
  35. fileErrInUse = fileErrorClass or 9; // file couldn't be opened or deleted because it is in use
  36. fileErrReadOnly = fileErrorClass or 10; // couldn't open in write mode because db is read-only
  37. fileErrInvalidDescriptor = fileErrorClass or 11; // invalid file descriptor (FileHandle)
  38. fileErrCloseError = fileErrorClass or 12; // error closing the database
  39. fileErrOutOfBounds = fileErrorClass or 13; // attempted operation went out of bounds of the file
  40. fileErrPermissionDenied = fileErrorClass or 14; // couldn't write to a file open for read-only access
  41. fileErrIOError = fileErrorClass or 15; // general I/O error
  42. fileErrEOF = fileErrorClass or 16; // end-of-file error
  43. fileErrNotStream = fileErrorClass or 17; // attempted to open a file that is not a stream
  44. (************************************************************
  45. * File Stream handle type
  46. *************************************************************)
  47. type
  48. FileHand = MemHandle;
  49. const
  50. fileNullHandle = FileHand(0);
  51. (************************************************************
  52. * Mode flags passed to FileOpen
  53. *************************************************************)
  54. // fileModeReadOnly, fileModeReadWrite, fileModeUpdate, and fileModeAppend are mutually exclusive - only
  55. // pass one of them to FileOpen!
  56. const
  57. fileModeReadOnly = $80000000; // open for read access
  58. fileModeReadWrite = $40000000; // create for read/write access, discarding previous if any */
  59. fileModeUpdate = $20000000; // open/create for read/write, preserving previous if any
  60. fileModeAppend = $10000000; // open/create for read/write, always writing at the end
  61. fileModeLeaveOpen = $08000000; // leave open when app quits
  62. fileModeExclusive = $04000000; // don't let anyone else open it
  63. fileModeAnyTypeCreator = $02000000; // if set, skip type/creator validation when
  64. // opening or replacing an existing file
  65. fileModeTemporary = $01000000; // will automatically delete the file when it is closed;
  66. // if this bit is set and the file type passed to FileOpen is zero,
  67. // FileOpen will use sysFileTTemp (defined in SystemResources.h for the file
  68. // type (recommended) - this will enable automatic cleanup of undeleted
  69. // temp files following a system crash in future PalmOS versions
  70. // (post-crash cleanup will likely come after 3.0)
  71. fileModeDontOverwrite = $00800000; // if set, will prevent fileModeReadWrite from discarding an existing file
  72. // with the same name; may only be specified together with fileModeReadWrite
  73. // For debugging/validation
  74. const
  75. fileModeAllFlags = fileModeReadOnly or fileModeReadWrite or fileModeUpdate or
  76. fileModeAppend or fileModeLeaveOpen or fileModeExclusive or
  77. fileModeAnyTypeCreator or fileModeTemporary or fileModeDontOverwrite;
  78. (************************************************************
  79. * Origin passed to FileSetPos
  80. *************************************************************)
  81. type
  82. FileOriginEnum = Enum;
  83. const
  84. fileOriginBeginning = 1; // from the beginning (first data byte of file)
  85. fileOriginCurrent = Succ(fileOriginBeginning); // from the current position
  86. fileOriginEnd = Succ(fileOriginCurrent); // from the end of file (one position beyond last data byte)
  87. (************************************************************
  88. * Operation passed to FileControl
  89. *************************************************************)
  90. type
  91. FileOpEnum = Enum;
  92. const
  93. fileOpNone = 0; // no-op
  94. fileOpDestructiveReadMode = Succ(fileOpNone); // switch to destructive read mode (there is no turning back);
  95. // implicitly rewinds the file to the beginning;
  96. // destructive read mode deletes file stream data blocks as
  97. // data is being read, thus freeing up storage automatically;
  98. // once in destructive read mode, FileWrite, FileSeek and FileTruncate
  99. // are not allowed; stream's contents after closing (or crash)
  100. // are undefined.
  101. // ARGUMENTS:
  102. // stream = open stream handle
  103. // valueP = NULL
  104. // valueLenP = NULL
  105. // RETURNS:
  106. // zero on success; fileErr... on error
  107. fileOpGetEOFStatus = Succ(fileOpDestructiveReadMode); // get end-of-file status (err = fileErrEOF indicates end of file condition);
  108. // use FileClearerr to clear this error status
  109. // ARGUMENTS:
  110. // stream = open stream handle
  111. // valueP = NULL
  112. // valueLenP = NULL
  113. // RETURNS:
  114. // zero if _not_ end of file; non-zero if end of file
  115. fileOpGetLastError = Succ(fileOpGetEOFStatus); // get error code from last operation on file stream, and
  116. // clear the last error code value (will not change end of file
  117. // or I/O error status -- use FileClearerr to reset all error codes)
  118. // ARGUMENTS:
  119. // stream = open stream handle
  120. // valueP = NULL
  121. // valueLenP = NULL
  122. // RETURNS:
  123. // Error code from last file stream operation
  124. fileOpClearError = Succ(fileOpGetLastError); // clear I/O and end of file error status, and last error
  125. // ARGUMENTS:
  126. // stream = open stream handle
  127. // valueP = NULL
  128. // valueLenP = NULL
  129. // RETURNS:
  130. // zero on success; fileErr... on error
  131. fileOpGetIOErrorStatus = Succ(fileOpClearError); // get I/O error status (like C runtime's ferror); use FileClearerr
  132. // to clear this error status
  133. // ARGUMENTS:
  134. // stream = open stream handle
  135. // valueP = NULL
  136. // valueLenP = NULL
  137. // RETURNS:
  138. // zero if _not_ I/O error; non-zero if I/O error is pending
  139. fileOpGetCreatedStatus = Succ(fileOpGetIOErrorStatus); // find out whether the FileOpen call caused the file to
  140. // be created
  141. // ARGUMENTS:
  142. // stream = open stream handle
  143. // valueP = ptr to Boolean type variable
  144. // valueLenP = ptr to Int32 variable set to sizeof(Boolean)
  145. // RETURNS:
  146. // zero on success; fileErr... on error;
  147. // the Boolean variable will be set to non zero if the file was created.
  148. fileOpGetOpenDbRef = Succ(fileOpGetCreatedStatus); // get the open database reference (handle) of the underlying
  149. // database that implements the stream (NULL if none);
  150. // this is needed for performing PalmOS-specific operations on
  151. // the underlying database, such as changing or getting creator/type,
  152. // version, backup/reset bits, etc.
  153. // ARGUMENTS:
  154. // stream = open stream handle
  155. // valueP = ptr to DmOpenRef type variable
  156. // valueLenP = ptr to Int32 variable set to sizeof(DmOpenRef)
  157. // RETURNS:
  158. // zero on success; fileErr... on error;
  159. // the DmOpenRef variable will be set to the file's open db reference
  160. // that may be passed to Data Manager calls;
  161. // WARNING:
  162. // Do not make any changes to the data of the underlying database --
  163. // this will cause the file stream to become corrupted.
  164. fileOpFlush = Succ(fileOpGetOpenDbRef); // flush any cached data to storage
  165. // ARGUMENTS:
  166. // stream = open stream handle
  167. // valueP = NULL
  168. // valueLenP = NULL
  169. // RETURNS:
  170. // zero on success; fileErr... on error;
  171. fileOpLAST = Succ(fileOpFlush); // ***ADD NEW OPERATIONS BEFORE THIS ENTRY***
  172. // *** AND ALWAYS AFTER EXISTING ENTRIES ***
  173. // *** FOR BACKWARD COMPATIBILITY ***
  174. (************************************************************
  175. * File Stream procedures
  176. *************************************************************)
  177. // Open/create a file stream (name must all be valid -- non-null, non-empty)
  178. // (errP is optional - set to NULL to ignore)
  179. function FileOpen(cardNo: UInt16; const nameP: PChar; type_, creator, openMode: UInt32; var errP: Err): FileHand; syscall sysTrapFileOpen;
  180. // Close the file stream
  181. function FileClose(stream: FileHand): Err; syscall sysTrapFileClose;
  182. // Delete a file
  183. function FileDelete(cardNo: UInt16; const nameP: PChar): Err; syscall sysTrapFileDelete;
  184. (***********************************************************************
  185. *
  186. * MACRO: FileRead
  187. *
  188. * DESCRIPTION: Read data from a file into a buffer. If you need to read into a data storage
  189. * heap-based chunk, record or resource, you _must_ use FileDmRead instead.
  190. *
  191. * PROTOTYPE: Int32 FileRead(FileHand stream, void *bufP, Int32 objSize, Int32 numObj, Err *errP)
  192. *
  193. * PARAMETERS: stream -- handle of open file
  194. * bufP -- buffer for reading data
  195. * objSize -- size of each object to read
  196. * numObj -- number of objects to read
  197. * errP -- ptr to variable for returning the error code (fileErr...)
  198. * (OPTIONAL -- pass NULL to ignore)
  199. *
  200. * RETURNED: the number of objects that were read - this may be less than
  201. * the number of objects requested
  202. *
  203. ***********************************************************************)
  204. function FileRead(stream: FileHand; bufP: Pointer; objSize, numObj: Int32; var errP: Err): Int32;
  205. (***********************************************************************
  206. *
  207. * MACRO: FileDmRead
  208. *
  209. * DESCRIPTION: Read data from a file into a data storage heap-based chunk, record
  210. * or resource.
  211. *
  212. * PROTOTYPE: Int32 FileDmRead(FileHand stream, void *startOfDmChunkP, Int32 destOffset,
  213. * Int32 objSize, Int32 numObj, Err *errP)
  214. *
  215. * PARAMETERS: stream -- handle of open file
  216. * startOfDmChunkP
  217. * -- ptr to beginning of data storage heap-based chunk, record or resource
  218. * destOffset -- offset from base ptr to the destination area (must be >= 0)
  219. * objSize -- size of each object to read
  220. * numObj -- number of objects to read
  221. * errP -- ptr to variable for returning the error code (fileErr...)
  222. * (OPTIONAL -- pass NULL to ignore)
  223. *
  224. * RETURNED: the number of objects that were read - this may be less than
  225. * the number of objects requested
  226. *
  227. ***********************************************************************)
  228. function FileDmRead(stream: FileHand; startOfDmChunkP: Pointer; destOffset: Int32; objSize, numObj: Int32; var errP: Err): Int32;
  229. // Low-level routine for reading data from a file stream -- use helper macros FileRead and FileDmRead
  230. // instead of calling this function directly;
  231. // (errP is optional - set to NULL to ignore)
  232. function FileReadLow(stream: FileHand; baseP: Pointer; offset: Int32; dataStoreBased: Boolean;
  233. objSize, numObj: Int32; var errP: Err): Int32; syscall sysTrapFileReadLow;
  234. // Write data to a file stream
  235. // (errP is optional - set to NULL to ignore)
  236. function FileWrite(stream: FileHand; const dataP: Pointer; objSize, numObj: Int32; var errP: Err): Int32; syscall sysTrapFileWrite;
  237. // Set position within a file stream
  238. function FileSeek(stream: FileHand; offset: Int32; origin: FileOriginEnum): Err; syscall sysTrapFileSeek;
  239. function FileRewind(stream: FileHand): Err;
  240. // Get current position and filesize
  241. // (fileSizeP and errP are optional - set to NULL to ignore)
  242. function FileTell(stream: FileHand; var fileSizeP: Int32; var errP: Err): Int32; syscall sysTrapFileTell;
  243. // Truncate a file
  244. function FileTruncate(stream: FileHand; newSize: Int32): Err; syscall sysTrapFileTruncate;
  245. // Returns the error code from the last operation on this file stream;
  246. // if resetLastError is non-zero, resets the error status
  247. // function FileControl(op: FileOpEnum; stream: FileHand; valueP: Pointer; var valueLenP: Int32): Err; syscall sysTrapFileControl;
  248. function FileControl(op: FileOpEnum; stream: FileHand; valueP: Pointer; valueLenP: Pointer): Err; syscall sysTrapFileControl;
  249. function FileEOF(stream: FileHand): Boolean;
  250. function FileError(stream: FileHand): Err;
  251. function FileClearerr(stream: FileHand): Err;
  252. function FileGetLastError(stream: FileHand): Err;
  253. function FileFlush(stream: FileHand): Err;
  254. implementation
  255. function FileRead(stream: FileHand; bufP: Pointer; objSize, numObj: Int32; var errP: Err): Int32;
  256. begin
  257. FileRead := FileReadLow(stream, bufP, 0{offset}, False{dataStoreBased}, objSize, numObj, errP);
  258. end;
  259. function FileDmRead(stream: FileHand; startOfDmChunkP: Pointer; destOffset: Int32; objSize, numObj: Int32; var errP: Err): Int32;
  260. begin
  261. FileDmRead := FileReadLow(stream, startOfDmChunkP, destOffset, True{dataStoreBased}, objSize, numObj, errP);
  262. end;
  263. function FileRewind(stream: FileHand): Err;
  264. begin
  265. FileClearerr(stream);
  266. FileRewind := FileSeek(stream, 0, fileOriginBeginning);
  267. end;
  268. function FileEOF(stream: FileHand): Boolean;
  269. begin
  270. FileEOF := FileControl(fileOpGetEOFStatus, stream, nil, Longint(nil)) = fileErrEOF;
  271. end;
  272. function FileError(stream: FileHand): Err;
  273. begin
  274. FileError := FileControl(fileOpGetIOErrorStatus, stream, nil, Longint(nil));
  275. end;
  276. function FileClearerr(stream: FileHand): Err;
  277. begin
  278. FileClearerr := FileControl(fileOpClearError, stream, nil, Longint(nil));
  279. end;
  280. function FileGetLastError(stream: FileHand): Err;
  281. begin
  282. FileGetLastError := FileControl(fileOpGetLastError, stream, nil, Longint(nil));
  283. end;
  284. function FileFlush(stream: FileHand): Err;
  285. begin
  286. FileFlush := FileControl(fileOpFlush, stream, nil, Longint(nil));
  287. end;
  288. end.