fileObject.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/fileObject.h"
  23. #include "console/engineAPI.h"
  24. IMPLEMENT_CONOBJECT(FileObject);
  25. ConsoleDocClass( FileObject,
  26. "@brief This class is responsible opening, reading, creating, and saving file contents.\n\n"
  27. "FileObject acts as the interface with OS level files. You create a new FileObject and pass into it "
  28. "a file's path and name. The FileObject class supports three distinct operations for working with files:\n\n"
  29. "<table border='1' cellpadding='1'>"
  30. "<tr><th>Operation</th><th>%FileObject Method</th><th>Description</th></tr>"
  31. "<tr><td>Read</td><td>openForRead()</td><td>Open the file for reading</td></tr>"
  32. "<tr><td>Write</td><td>openForWrite()</td><td>Open the file for writing to and replace its contents (if any)</td></tr>"
  33. "<tr><td>Append</td><td>openForAppend()</td><td>Open the file and start writing at its end</td></tr>"
  34. "</table>\n\n"
  35. "Before you may work with a file you need to use one of the three above methods on the FileObject.\n\n"
  36. "@tsexample\n"
  37. "// Create a file object for writing\n"
  38. "%fileWrite = new FileObject();\n\n"
  39. "// Open a file to write to, if it does not exist it will be created\n"
  40. "%result = %fileWrite.OpenForWrite(\"./test.txt\");\n\n"
  41. "if ( %result )\n"
  42. "{\n"
  43. " // Write a line to the text files\n"
  44. " %fileWrite.writeLine(\"READ. READ CODE. CODE\");\n"
  45. "}\n\n"
  46. "// Close the file when finished\n"
  47. "%fileWrite.close();\n\n"
  48. "// Cleanup the file object\n"
  49. "%fileWrite.delete();\n\n\n"
  50. "// Create a file object for reading\n"
  51. "%fileRead = new FileObject();\n\n"
  52. "// Open a text file, if it exists\n"
  53. "%result = %fileRead.OpenForRead(\"./test.txt\");\n\n"
  54. "if ( %result )\n"
  55. "{\n"
  56. " // Read in the first line\n"
  57. " %line = %fileRead.readline();\n\n"
  58. " // Print the line we just read\n"
  59. " echo(%line);\n"
  60. "}\n\n"
  61. "// Close the file when finished\n"
  62. "%fileRead.close();\n\n"
  63. "// Cleanup the file object\n"
  64. "%fileRead.delete();\n"
  65. "@endtsexample\n\n"
  66. "@ingroup FileSystem\n"
  67. );
  68. bool FileObject::isEOF()
  69. {
  70. return mCurPos == mBufferSize;
  71. }
  72. FileObject::FileObject()
  73. {
  74. mFileBuffer = NULL;
  75. mBufferSize = 0;
  76. mCurPos = 0;
  77. stream = NULL;
  78. }
  79. FileObject::~FileObject()
  80. {
  81. SAFE_DELETE_ARRAY(mFileBuffer);
  82. SAFE_DELETE(stream);
  83. }
  84. void FileObject::close()
  85. {
  86. SAFE_DELETE(stream);
  87. SAFE_DELETE_ARRAY(mFileBuffer);
  88. mFileBuffer = NULL;
  89. mBufferSize = mCurPos = 0;
  90. }
  91. bool FileObject::openForWrite(const char *fileName, const bool append)
  92. {
  93. char buffer[1024];
  94. Con::expandScriptFilename( buffer, sizeof( buffer ), fileName );
  95. close();
  96. if( !buffer[ 0 ] )
  97. return false;
  98. if((stream = FileStream::createAndOpen( fileName, append ? Torque::FS::File::WriteAppend : Torque::FS::File::Write )) == NULL)
  99. return false;
  100. stream->setPosition( stream->getStreamSize() );
  101. return( true );
  102. }
  103. bool FileObject::openForRead(const char* /*fileName*/)
  104. {
  105. AssertFatal(false, "Error, not yet implemented!");
  106. return false;
  107. }
  108. bool FileObject::readMemory(const char *fileName)
  109. {
  110. char buffer[1024];
  111. Con::expandScriptFilename( buffer, sizeof( buffer ), fileName );
  112. close();
  113. void *data = NULL;
  114. U32 dataSize = 0;
  115. Torque::FS::ReadFile(buffer, data, dataSize, true);
  116. if(data == NULL)
  117. return false;
  118. mBufferSize = dataSize;
  119. mFileBuffer = (U8 *)data;
  120. mCurPos = 0;
  121. return true;
  122. }
  123. const U8 *FileObject::readLine()
  124. {
  125. if(!mFileBuffer)
  126. return (U8 *) "";
  127. U32 tokPos = mCurPos;
  128. for(;;)
  129. {
  130. if(mCurPos == mBufferSize)
  131. break;
  132. if(mFileBuffer[mCurPos] == '\r')
  133. {
  134. mFileBuffer[mCurPos++] = 0;
  135. if(mFileBuffer[mCurPos] == '\n')
  136. mCurPos++;
  137. break;
  138. }
  139. if(mFileBuffer[mCurPos] == '\n')
  140. {
  141. mFileBuffer[mCurPos++] = 0;
  142. break;
  143. }
  144. mCurPos++;
  145. }
  146. return mFileBuffer + tokPos;
  147. }
  148. void FileObject::peekLine( S32 peekLineOffset, U8* line, S32 length )
  149. {
  150. if(!mFileBuffer)
  151. {
  152. line[0] = '\0';
  153. return;
  154. }
  155. // Copy the line into the buffer. We can't do this like readLine because
  156. // we can't modify the file buffer.
  157. S32 i = 0;
  158. U32 tokPos = mCurPos;
  159. S32 lineOffset = 0;
  160. //Lets push our tokPos up until we've offset the requested number of lines
  161. while (lineOffset < peekLineOffset && tokPos <= mBufferSize)
  162. {
  163. if (mFileBuffer[tokPos] == '\r')
  164. {
  165. tokPos++;
  166. if (mFileBuffer[tokPos] == '\n')
  167. tokPos++;
  168. lineOffset++;
  169. continue;
  170. }
  171. if (mFileBuffer[tokPos] == '\n')
  172. {
  173. tokPos++;
  174. lineOffset++;
  175. continue;
  176. }
  177. tokPos++;
  178. }
  179. //now peek that line, then return the results
  180. while( ( tokPos != mBufferSize ) && ( mFileBuffer[tokPos] != '\r' ) && ( mFileBuffer[tokPos] != '\n' ) && ( i < ( length - 1 ) ) )
  181. line[i++] = mFileBuffer[tokPos++];
  182. line[i++] = '\0';
  183. //if( i == length )
  184. //Con::warnf( "FileObject::peekLine - The line contents could not fit in the buffer (size %d). Truncating.", length );
  185. }
  186. void FileObject::writeLine(const U8 *line)
  187. {
  188. stream->write(dStrlen((const char *) line), line);
  189. stream->write(2, "\r\n");
  190. }
  191. void FileObject::writeObject( SimObject* object, const U8* objectPrepend )
  192. {
  193. if( objectPrepend == NULL )
  194. stream->write(2, "\r\n");
  195. else
  196. stream->write(dStrlen((const char *) objectPrepend), objectPrepend );
  197. object->write( *stream, 0 );
  198. }
  199. DefineEngineMethod( FileObject, openForRead, bool, ( const char* filename ),,
  200. "@brief Open a specified file for reading\n\n"
  201. "There is no limit as to what kind of file you can read. Any format and data contained within is accessible, not just text\n\n"
  202. "@param filename Path, name, and extension of file to be read"
  203. "@tsexample\n"
  204. "// Create a file object for reading\n"
  205. "%fileRead = new FileObject();\n\n"
  206. "// Open a text file, if it exists\n"
  207. "%result = %fileRead.OpenForRead(\"./test.txt\");\n"
  208. "@endtsexample\n\n"
  209. "@return True if file was successfully opened, false otherwise\n")
  210. {
  211. return object->readMemory(filename);
  212. }
  213. DefineEngineMethod( FileObject, openForWrite, bool, ( const char* filename ),,
  214. "@brief Open a specified file for writing\n\n"
  215. "There is no limit as to what kind of file you can write. Any format and data is allowable, not just text\n\n"
  216. "@param filename Path, name, and extension of file to write to"
  217. "@tsexample\n"
  218. "// Create a file object for writing\n"
  219. "%fileWrite = new FileObject();\n\n"
  220. "// Open a file to write to, if it does not exist it will be created\n"
  221. "%result = %fileWrite.OpenForWrite(\"./test.txt\");\n"
  222. "@endtsexample\n\n"
  223. "@return True if file was successfully opened, false otherwise\n")
  224. {
  225. return object->openForWrite(filename);
  226. }
  227. DefineEngineMethod( FileObject, openForAppend, bool, ( const char* filename ),,
  228. "@brief Open a specified file for writing, adding data to the end of the file\n\n"
  229. "There is no limit as to what kind of file you can write. Any format and data is allowable, not just text. Unlike openForWrite(), "
  230. "which will erase an existing file if it is opened, openForAppend() preserves data in an existing file and adds to it.\n\n"
  231. "@param filename Path, name, and extension of file to append to"
  232. "@tsexample\n"
  233. "// Create a file object for writing\n"
  234. "%fileWrite = new FileObject();\n\n"
  235. "// Open a file to write to, if it does not exist it will be created\n"
  236. "// If it does exist, whatever we write will be added to the end\n"
  237. "%result = %fileWrite.OpenForAppend(\"./test.txt\");\n"
  238. "@endtsexample\n\n"
  239. "@return True if file was successfully opened, false otherwise\n")
  240. {
  241. return object->openForWrite(filename, true);
  242. }
  243. DefineEngineMethod( FileObject, isEOF, bool, (),,
  244. "@brief Determines if the parser for this FileObject has reached the end of the file\n\n"
  245. "@tsexample\n"
  246. "// Create a file object for reading\n"
  247. "%fileRead = new FileObject();\n\n"
  248. "// Open a text file, if it exists\n"
  249. "%fileRead.OpenForRead(\"./test.txt\");\n\n"
  250. "// Keep reading until we reach the end of the file\n"
  251. "while( !%fileRead.isEOF() )\n"
  252. "{\n"
  253. " %line = %fileRead.readline();\n"
  254. " echo(%line);\n"
  255. "}\n\n"
  256. "// Made it to the end\n"
  257. "echo(\"Finished reading file\");\n"
  258. "@endtsexample\n\n"
  259. "@return True if the parser has reached the end of the file, false otherwise\n")
  260. {
  261. return object->isEOF();
  262. }
  263. DefineEngineMethod( FileObject, readLine, const char*, (),,
  264. "@brief Read a line from file.\n\n"
  265. "Emphasis on *line*, as in you cannot parse individual characters or chunks of data. "
  266. "There is no limitation as to what kind of data you can read.\n\n"
  267. "@tsexample\n"
  268. "// Create a file object for reading\n"
  269. "%fileRead = new FileObject();\n\n"
  270. "// Open a text file, if it exists\n"
  271. "%fileRead.OpenForRead(\"./test.txt\");\n\n"
  272. "// Read in the first line\n"
  273. "%line = %fileRead.readline();\n\n"
  274. "// Print the line we just read\n"
  275. "echo(%line);\n"
  276. "@endtsexample\n\n"
  277. "@return String containing the line of data that was just read\n")
  278. {
  279. return (const char *) object->readLine();
  280. }
  281. DefineEngineMethod( FileObject, peekLine, const char*, (S32 peekOffset), (0),
  282. "@brief Read a line from the file without moving the stream position.\n\n"
  283. "Emphasis on *line*, as in you cannot parse individual characters or chunks of data. "
  284. "There is no limitation as to what kind of data you can read. Unlike readLine, the parser does not move forward after reading.\n\n"
  285. "@param filename Path, name, and extension of file to be read"
  286. "@tsexample\n"
  287. "// Create a file object for reading\n"
  288. "%fileRead = new FileObject();\n\n"
  289. "// Open a text file, if it exists\n"
  290. "%fileRead.OpenForRead(\"./test.txt\");\n\n"
  291. "// Peek the first line\n"
  292. "%line = %fileRead.peekLine();\n\n"
  293. "// Print the line we just peeked\n"
  294. "echo(%line);\n"
  295. "// If we peek again...\n"
  296. "%line = %fileRead.peekLine();\n\n"
  297. "// We will get the same output as the first time\n"
  298. "// since the stream did not move forward\n"
  299. "echo(%line);\n"
  300. "@endtsexample\n\n"
  301. "@return String containing the line of data that was just peeked\n")
  302. {
  303. static const U32 bufSize = 512;
  304. char *line = Con::getReturnBuffer( bufSize );
  305. object->peekLine(peekOffset, (U8*)line, bufSize );
  306. return line;
  307. }
  308. DefineEngineMethod( FileObject, writeLine, void, ( const char* text ),,
  309. "@brief Write a line to the file, if it was opened for writing.\n\n"
  310. "There is no limit as to what kind of text you can write. Any format and data is allowable, not just text. "
  311. "Be careful of what you write, as whitespace, current values, and literals will be preserved.\n\n"
  312. "@param text The data we are writing out to file."
  313. "@tsexample\n"
  314. "// Create a file object for writing\n"
  315. "%fileWrite = new FileObject();\n\n"
  316. "// Open a file to write to, if it does not exist it will be created\n"
  317. "%fileWrite.OpenForWrite(\"./test.txt\");\n\n"
  318. "// Write a line to the text files\n"
  319. "%fileWrite.writeLine(\"READ. READ CODE. CODE\");\n\n"
  320. "@endtsexample\n\n"
  321. "@return True if file was successfully opened, false otherwise\n")
  322. {
  323. object->writeLine((const U8 *) text);
  324. }
  325. DefineEngineMethod( FileObject, close, void, (),,
  326. "@brief Close the file.\n\n"
  327. "It is EXTREMELY important that you call this function when you are finished reading or writing to a file. "
  328. "Failing to do so is not only a bad programming practice, but could result in bad data or corrupt files. "
  329. "Remember: Open, Read/Write, Close, Delete...in that order!\n\n"
  330. "@tsexample\n"
  331. "// Create a file object for reading\n"
  332. "%fileRead = new FileObject();\n\n"
  333. "// Open a text file, if it exists\n"
  334. "%fileRead.OpenForRead(\"./test.txt\");\n\n"
  335. "// Peek the first line\n"
  336. "%line = %fileRead.peekLine();\n\n"
  337. "// Print the line we just peeked\n"
  338. "echo(%line);\n"
  339. "// If we peek again...\n"
  340. "%line = %fileRead.peekLine();\n\n"
  341. "// We will get the same output as the first time\n"
  342. "// since the stream did not move forward\n"
  343. "echo(%line);\n\n"
  344. "// Close the file when finished\n"
  345. "%fileWrite.close();\n\n"
  346. "// Cleanup the file object\n"
  347. "%fileWrite.delete();\n"
  348. "@endtsexample\n\n")
  349. {
  350. object->close();
  351. }
  352. static ConsoleDocFragment _FileObjectwriteObject1(
  353. "@brief Write an object to a text file.\n\n"
  354. "Unlike a simple writeLine using specified strings, this function writes an entire object "
  355. "to file, preserving its type, name, and properties. This is similar to the save() functionality of "
  356. "the SimObject class, but with a bit more control.\n\n"
  357. "@param object The SimObject being written to file, properties, name, and all.\n"
  358. "@tsexample\n"
  359. "// Let's assume this SpawnSphere was created and currently\n"
  360. "// exists in the running level\n"
  361. "new SpawnSphere(TestSphere)\n"
  362. "{\n"
  363. " spawnClass = \"Player\";\n"
  364. " spawnDatablock = \"DefaultPlayerData\";\n"
  365. " autoSpawn = \"1\";\n"
  366. " radius = \"5\";\n"
  367. " sphereWeight = \"1\";\n"
  368. " indoorWeight = \"1\";\n"
  369. " outdoorWeight = \"1\";\n"
  370. " dataBlock = \"SpawnSphereMarker\";\n"
  371. " position = \"-42.222 1.4845 4.80334\";\n"
  372. " rotation = \"0 0 -1 108\";\n"
  373. " scale = \"1 1 1\";\n"
  374. " canSaveDynamicFields = \"1\";\n"
  375. "};\n\n"
  376. "// Create a file object for writing\n"
  377. "%fileWrite = new FileObject();\n\n"
  378. "// Open a file to write to, if it does not exist it will be created\n"
  379. "%fileWrite.OpenForWrite(\"./spawnSphers.txt\");\n\n"
  380. "// Write out the TestSphere\n"
  381. "%fileWrite.writeObject(TestSphere);\n\n"
  382. "// Close the text file\n"
  383. "%fileWrite.close();\n\n"
  384. "// Cleanup\n"
  385. "%fileWrite.delete();\n"
  386. "@endtsexample\n\n\n",
  387. "FileObject",
  388. "void writeObject( SimObject* object);");
  389. static ConsoleDocFragment _FileObjectwriteObject2(
  390. "@brief Write an object to a text file, with some data added first.\n\n"
  391. "Unlike a simple writeLine using specified strings, this function writes an entire object "
  392. "to file, preserving its type, name, and properties. This is similar to the save() functionality of "
  393. "the SimObject class, but with a bit more control.\n\n"
  394. "@param object The SimObject being written to file, properties, name, and all.\n"
  395. "@param prepend Data or text that is written out before the SimObject.\n\n"
  396. "@tsexample\n"
  397. "// Let's assume this SpawnSphere was created and currently\n"
  398. "// exists in the running level\n"
  399. "new SpawnSphere(TestSphere)\n"
  400. "{\n"
  401. " spawnClass = \"Player\";\n"
  402. " spawnDatablock = \"DefaultPlayerData\";\n"
  403. " autoSpawn = \"1\";\n"
  404. " radius = \"5\";\n"
  405. " sphereWeight = \"1\";\n"
  406. " indoorWeight = \"1\";\n"
  407. " outdoorWeight = \"1\";\n"
  408. " dataBlock = \"SpawnSphereMarker\";\n"
  409. " position = \"-42.222 1.4845 4.80334\";\n"
  410. " rotation = \"0 0 -1 108\";\n"
  411. " scale = \"1 1 1\";\n"
  412. " canSaveDynamicFields = \"1\";\n"
  413. "};\n\n"
  414. "// Create a file object for writing\n"
  415. "%fileWrite = new FileObject();\n\n"
  416. "// Open a file to write to, if it does not exist it will be created\n"
  417. "%fileWrite.OpenForWrite(\"./spawnSphers.txt\");\n\n"
  418. "// Write out the TestSphere, with a prefix\n"
  419. "%fileWrite.writeObject(TestSphere, \"$mySphere = \");\n\n"
  420. "// Close the text file\n"
  421. "%fileWrite.close();\n\n"
  422. "// Cleanup\n"
  423. "%fileWrite.delete();\n"
  424. "@endtsexample\n\n\n",
  425. "FileObject",
  426. "void writeObject( SimObject* object, string prepend);");
  427. DefineEngineMethod( FileObject, writeObject, void, (const char * simName, const char * objName), (""), "FileObject.writeObject(SimObject, object prepend)"
  428. "@hide")
  429. {
  430. SimObject* obj = Sim::findObject( simName );
  431. if( !obj )
  432. {
  433. Con::printf("FileObject::writeObject - Invalid Object!");
  434. return;
  435. }
  436. if (!String::compare(objName,""))
  437. objName = NULL;
  438. object->writeObject( obj, (const U8*)objName );
  439. }