bool CheckIfFileExists(const Path& p) { FILE *tempf = POV_UCS2_FOPEN(p().c_str(), "r"); if(tempf != NULL) fclose(tempf); else return false; return true; }
POV_LONG GetFileLength(const Path& p) { FILE *tempf = POV_UCS2_FOPEN(p().c_str(), "rb"); POV_LONG result = -1; if(tempf != NULL) { fseek(tempf, 0, SEEK_END); result = ftell(tempf); fclose(tempf); } return result; }
IFileStream::IFileStream(const UCS2String& name) : IStream(name), f(NULL) { if(pov_stricmp(UCS2toASCIIString(name).c_str(), "stdin") == 0) { f = stdin; } else if((pov_stricmp(UCS2toASCIIString(name).c_str(), "stdout") == 0) || (pov_stricmp(UCS2toASCIIString(name).c_str(), "stderr") == 0)) { f = NULL; } else { f = POV_UCS2_FOPEN(name, "rb"); } fail = (f == NULL); }
OStream::OStream(const UCS2String& name, unsigned int Flags) : IOBase(name), f(NULL) { const char* mode; if((Flags & append) == 0) { mode = "wb"; } else { // we cannot use append mode here, since "a" mode is totally incompatible with any // output file format that requires in-place updates(i.e. writing to any location // other than the end of the file). BMP files are in this category. In theory, "r+" // can do anything "a" can do(with appropriate use of seek()) so append mode should // not be needed. mode = "r+b"; } if(pov_stricmp(UCS2toASCIIString(name).c_str(), "stdin") == 0) { f = NULL; } else if(pov_stricmp(UCS2toASCIIString(name).c_str(), "stdout") == 0) { if((Flags & append) != 0) f = NULL; else f = stdout; } else if(pov_stricmp(UCS2toASCIIString(name).c_str(), "stderr") == 0) { if((Flags & append) != 0) f = NULL; else f = stdout; } else { f = POV_UCS2_FOPEN(name, mode); if (f == NULL) { if((Flags & append) == 0) f = NULL; else { // to maintain traditional POV +c(continue) mode compatibility, if // the open for append of an existing file fails, we allow a new file // to be created. mode = "wb"; f = POV_UCS2_FOPEN(name, mode); } } if (f != NULL) { if((Flags & append) != 0) { if(!seekg(0, seek_end)) { fclose(f); f = NULL; } } } } fail = (f == NULL); }
bool IOBase::open(const UCS2String& name, unsigned int Flags /* = 0 */) { char mode[8]; close(); filename = name; if((Flags & append) == 0) { switch(direction) { case input: strcpy(mode, "r"); break; case output: strcpy(mode, "w"); break; case io: strcpy(mode, "w+"); break; default: return false; } } else { // we cannot use append mode here, since "a" mode is totally incompatible with any // output file format that requires in-place updates(i.e. writing to any location // other than the end of the file). BMP files are in this category. In theory, "r+" // can do anything "a" can do(with appropriate use of seek()) so append mode should // not be needed. strcpy(mode, "r+"); } strcat(mode, "b"); f = NULL; if(pov_stricmp(UCS2toASCIIString(name).c_str(), "stdin") == 0) { if(direction != input ||(Flags & append) != 0) return false; f = stdin; } else if(pov_stricmp(UCS2toASCIIString(name).c_str(), "stdout") == 0) { if(direction != output ||(Flags & append) != 0) return false; f = stdout; } else if(pov_stricmp(UCS2toASCIIString(name).c_str(), "stderr") == 0) { if(direction != output ||(Flags & append) != 0) return false; f = stderr; } else { if((f = POV_UCS2_FOPEN(name, mode)) == NULL) { if((Flags & append) == 0) return false; // to maintain traditional POV +c(continue) mode compatibility, if // the open for append of an existing file fails, we allow a new file // to be created. mode [0] = 'w'; if((f = POV_UCS2_FOPEN(name, mode)) == NULL) return false; } } fail = false; if((Flags & append) != 0) { if(!seekg(0, seek_end)) { close(); return false; } } return true; }