UCS2 *Parser::Convert_UTF8_To_UCS2(const unsigned char *text_array, int *char_array_size) { POV_PARSER_ASSERT(text_array); POV_PARSER_ASSERT(char_array_size); UCS2String s = UTF8toUCS2String(UTF8String(reinterpret_cast<const char*>(text_array))); UCS2String::size_type len = s.length(); *char_array_size = len; if (len == 0) return nullptr; size_t size = (len+1)*sizeof(UCS2); UCS2 *char_array = reinterpret_cast<UCS2 *>(POV_MALLOC(size, "Character Array")); if (char_array == nullptr) throw POV_EXCEPTION_CODE(kOutOfMemoryErr); memcpy(char_array, s.c_str(), size); return char_array; }
/* TODO FIXME - this is the correct code but it has a bug. The code above is just a hack [trf] IStream *BackendSceneData::ReadFile(POVMSContext ctx, const UCS2String& filename, unsigned int stype) { UCS2String scenefile(filename); UCS2String localfile; UCS2String fileurl; // 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); // 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; 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; } */ OStream *BackendSceneData::CreateFile(POVMSContext ctx, const UCS2String& filename, unsigned int stype, bool append) { UCS2String scenefile(filename); #ifdef USE_SCENE_FILE_MAPPING // 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 NewOStream(itempfile->second.c_str(), stype, append); // otherwise, create a temporary file ... UCS2String tempname = POV_PLATFORM_BASE.CreateTemporaryFile(); OStream *tempfile = NewOStream(tempname.c_str(), stype, append); // failed to open file if(tempfile == NULL) return NULL; // add the temporary file to the map scene2TempFiles[scenefile] = tempname; temp2SceneFiles[tempname] = scenefile; #else // this is a workaround for the incomplete scene temp file support // until someone has time to finish it. OStream *tempfile = NewOStream(scenefile.c_str(), stype, append); if (tempfile == NULL) return NULL; #endif // let the frontend know that a new file was created RenderBackend::SendCreatedFile(ctx, sceneId, frontendAddress, scenefile); return tempfile; }
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 }