ITextStream::ITextStream(const UCS2 *sname, unsigned int stype) { if(sname == NULL) throw POV_EXCEPTION_CODE(kParamErr); stream = NewIStream(sname, stype); if(stream == NULL) throw POV_EXCEPTION(kCannotOpenFileErr, string("Cannot open file '") + UCS2toASCIIString(sname) + "' for input."); filename = UCS2String(sname); lineno = 1; bufferoffset = 0; maxbufferoffset = 0; filelength = 0; ungetbuffer = EOF; curpos = 0 ; stream->seekg(0, IOBase::seek_end); filelength = stream->tellg(); stream->seekg(0, IOBase::seek_set); RefillBuffer(); }
IStream *BackendSceneData::ReadFile(POVMSContext ctx, const UCS2String& origname, const UCS2String& filename, unsigned int stype) { UCS2String scenefile(filename); UCS2String localfile; UCS2String fileurl; #ifdef USE_SCENE_FILE_MAPPING // see if the file is available locally FilenameToFilenameMap::iterator ilocalfile(scene2LocalFiles.find(scenefile)); // if available locally, open it end return if(ilocalfile != scene2LocalFiles.end()) return NewIStream(ilocalfile->second.c_str(), stype); // now try the original name as given in the scene if((ilocalfile = scene2LocalFiles.find(origname)) != scene2LocalFiles.end()) return NewIStream(ilocalfile->second.c_str(), stype); // see if the file is available as temporary file FilenameToFilenameMap::iterator itempfile(scene2TempFiles.find(scenefile)); // if available as temporary file, open it end return if(itempfile != scene2TempFiles.end()) return NewIStream(itempfile->second.c_str(), stype); // otherwise, request the file RenderBackend::SendReadFile(ctx, sceneId, frontendAddress, scenefile, localfile, fileurl); // if it is available locally, add it to the map and then open it if(localfile.length() > 0) { scene2LocalFiles[scenefile] = localfile; local2SceneFiles[localfile] = scenefile; // yes this is a hack scene2LocalFiles[origname] = localfile; return NewIStream(localfile.c_str(), stype); } // if it is available remotely ... if(fileurl.length() > 0) { // create a temporary file UCS2String tempname = POV_PLATFORM_BASE.CreateTemporaryFile(); OStream *tempfile = NewOStream(tempname.c_str(), stype, false); if(tempfile == NULL) { POV_PLATFORM_BASE.DeleteTemporaryFile(tempname); throw POV_EXCEPTION_CODE(kCannotOpenFileErr); } // download the file from the URL // TODO - handle referrer if(POV_PLATFORM_BASE.ReadFileFromURL(tempfile, fileurl) == false) { delete tempfile; POV_PLATFORM_BASE.DeleteTemporaryFile(tempname); throw POV_EXCEPTION_CODE(kNetworkConnectionErr); } delete tempfile; // add the temporary file to the map scene2TempFiles[scenefile] = tempname; temp2SceneFiles[tempname] = scenefile; return NewIStream(tempname.c_str(), stype); } // file not found return NULL; #else return NewIStream(filename.c_str(), stype); #endif }