fileStreamObject.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "console/console.h"
  23. #include "fileStreamObject.h"
  24. //////////////////////////////////////////////////////////////////////////
  25. // Local Globals
  26. //////////////////////////////////////////////////////////////////////////
  27. static const struct
  28. {
  29. const char *strMode;
  30. FileStream::AccessMode mode;
  31. } gModeMap[]=
  32. {
  33. { "read", FileStream::Read },
  34. { "write", FileStream::Write },
  35. { "readwrite", FileStream::ReadWrite },
  36. { "writeappend", FileStream::WriteAppend },
  37. { NULL, (FileStream::AccessMode)0 }
  38. };
  39. //////////////////////////////////////////////////////////////////////////
  40. // Constructor/Destructor
  41. //////////////////////////////////////////////////////////////////////////
  42. FileStreamObject::FileStreamObject()
  43. {
  44. }
  45. FileStreamObject::~FileStreamObject()
  46. {
  47. close();
  48. }
  49. IMPLEMENT_CONOBJECT(FileStreamObject);
  50. //////////////////////////////////////////////////////////////////////////
  51. bool FileStreamObject::onAdd()
  52. {
  53. // [tom, 2/12/2007] Skip over StreamObject's onAdd() so that we can
  54. // be instantiated from script.
  55. return SimObject::onAdd();
  56. }
  57. //////////////////////////////////////////////////////////////////////////
  58. // Public Methods
  59. //////////////////////////////////////////////////////////////////////////
  60. bool FileStreamObject::open(const char *filename, FileStream::AccessMode mode)
  61. {
  62. close();
  63. if(! mFileStream.open(filename, mode))
  64. return false;
  65. mStream = &mFileStream;
  66. return true;
  67. }
  68. void FileStreamObject::close()
  69. {
  70. mFileStream.close();
  71. mStream = NULL;
  72. }
  73. //////////////////////////////////////////////////////////////////////////
  74. // Console Methods
  75. //////////////////////////////////////////////////////////////////////////
  76. ConsoleMethod(FileStreamObject, open, bool, 4, 4, "(filename, mode) Open a file. Mode can be one of Read, Write, ReadWrite or WriteAppend.")
  77. {
  78. FileStream::AccessMode mode;
  79. bool found = false;
  80. for(S32 i = 0;gModeMap[i].strMode;++i)
  81. {
  82. if(dStricmp(gModeMap[i].strMode, argv[3]) == 0)
  83. {
  84. mode = gModeMap[i].mode;
  85. found = true;
  86. break;
  87. }
  88. }
  89. if(! found)
  90. {
  91. Con::errorf("FileStreamObject::open - Mode must be one of Read, Write, ReadWrite or WriteAppend.");
  92. return false;
  93. }
  94. char buffer[1024];
  95. Con::expandPath(buffer, sizeof(buffer), argv[2]);
  96. return object->open(buffer, mode);
  97. }
  98. ConsoleMethod(FileStreamObject, close, void, 2, 2, "() Close the file.")
  99. {
  100. object->close();
  101. }