fileObject.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "fileObject.h"
  23. #include "fileObject_ScriptBinding.h"
  24. IMPLEMENT_CONOBJECT(FileObject);
  25. bool FileObject::isEOF()
  26. {
  27. return mCurPos == mBufferSize;
  28. }
  29. FileObject::FileObject()
  30. {
  31. mFileBuffer = NULL;
  32. mBufferSize = 0;
  33. mCurPos = 0;
  34. }
  35. FileObject::~FileObject()
  36. {
  37. dFree(mFileBuffer);
  38. }
  39. void FileObject::close()
  40. {
  41. stream.close();
  42. dFree(mFileBuffer);
  43. mFileBuffer = NULL;
  44. mBufferSize = mCurPos = 0;
  45. }
  46. bool FileObject::openForWrite(const char *fileName, const bool append)
  47. {
  48. char buffer[1024];
  49. Con::expandPath(buffer, sizeof(buffer), fileName);
  50. close();
  51. if(*buffer == 0)
  52. return false;
  53. if ( !append )
  54. return( ResourceManager->openFileForWrite(stream, buffer) );
  55. // Use the WriteAppend flag so it doesn't clobber the existing file:
  56. if ( !ResourceManager->openFileForWrite(stream, buffer, File::WriteAppend) )
  57. return( false );
  58. stream.setPosition( stream.getStreamSize() );
  59. return( true );
  60. }
  61. bool FileObject::openForRead(const char* /*fileName*/)
  62. {
  63. AssertFatal(false, "Error, not yet implemented!");
  64. return false;
  65. }
  66. bool FileObject::readMemory(const char *fileName)
  67. {
  68. StringTableEntry fileToOpen = NULL;
  69. char buffer[1024];
  70. Con::expandPath( buffer, sizeof( buffer ), fileName );
  71. fileToOpen = StringTable->insert(buffer);
  72. close();
  73. Stream *s = ResourceManager->openStream(fileToOpen);
  74. if(!s)
  75. return false;
  76. mBufferSize = ResourceManager->getSize(fileToOpen);
  77. mFileBuffer = (U8 *) dMalloc(mBufferSize + 1);
  78. mFileBuffer[mBufferSize] = 0;
  79. s->read(mBufferSize, mFileBuffer);
  80. ResourceManager->closeStream(s);
  81. mCurPos = 0;
  82. return true;
  83. }
  84. const U8 *FileObject::readLine()
  85. {
  86. if(!mFileBuffer)
  87. return (U8 *) "";
  88. U32 tokPos = mCurPos;
  89. for(;;)
  90. {
  91. if(mCurPos == mBufferSize)
  92. break;
  93. if(mFileBuffer[mCurPos] == '\r')
  94. {
  95. mFileBuffer[mCurPos++] = 0;
  96. if(mFileBuffer[mCurPos] == '\n')
  97. mCurPos++;
  98. break;
  99. }
  100. if(mFileBuffer[mCurPos] == '\n')
  101. {
  102. mFileBuffer[mCurPos++] = 0;
  103. break;
  104. }
  105. mCurPos++;
  106. }
  107. return mFileBuffer + tokPos;
  108. }
  109. void FileObject::peekLine( U8* line, S32 length )
  110. {
  111. if(!mFileBuffer)
  112. {
  113. line[0] = '\0';
  114. return;
  115. }
  116. // Copy the line into the buffer. We can't do this like readLine because
  117. // we can't modify the file buffer.
  118. S32 i = 0;
  119. U32 tokPos = mCurPos;
  120. while( ( tokPos != mBufferSize ) && ( mFileBuffer[tokPos] != '\r' ) && ( mFileBuffer[tokPos] != '\n' ) && ( i < ( length - 1 ) ) )
  121. line[i++] = mFileBuffer[tokPos++];
  122. line[i++] = '\0';
  123. //if( i == length )
  124. //Con::warnf( "FileObject::peekLine - The line contents could not fit in the buffer (size %d). Truncating.", length );
  125. }
  126. void FileObject::writeLine(const U8 *line)
  127. {
  128. stream.write(dStrlen((const char *) line), line);
  129. stream.write(2, "\r\n");
  130. }
  131. void FileObject::writeObject( SimObject* object, const U8* objectPrepend )
  132. {
  133. if( objectPrepend == NULL )
  134. stream.write(2, "\r\n");
  135. else
  136. stream.write(dStrlen((const char *) objectPrepend), objectPrepend );
  137. object->write( stream, 0 );
  138. }