Bool OpenReadOnlyTest() { Bool result = true; File* file; String text; UInt fileSize; //File does not exists, open it and it will fail File::Delete(_fileName); file = File::OpenReadOnly(_fileName); CHECK file == 0; //File exists, open it and it will work, write to it and it will fail CHECK (file = File::Create(_fileName)); file->Write((VoidPtr)_fileName, String::CStrByteSize(_fileName)); DeletePtr(file); CHECK (file = File::OpenReadOnly(_fileName)); fileSize = ToUInt(file->GetFileSize()); text.Reserve(fileSize / sizeof(TChar)); file->Read(text.DrivePointer(fileSize / sizeof(TChar)), fileSize); CHECK text == _fileName; DeletePtr(file); File::Delete(_fileName); return result; }
bool ProcessInstance::CanExecuteOn( const ImageVariant& image, String& whyNot ) const { if ( !image.IsSharedImage() || !image.IsCompletelySelected() ) { ImageVariant tmp; tmp.CreateSharedImageAs( image ).AssignImage( image ); return CanExecuteOn( tmp, whyNot ); } image.PushSelections(); image.ResetSelections(); ImageInfo info1( *image ); image.PopSelections(); whyNot.Clear(); whyNot.Reserve( WHYNOT_MAXLENGTH ); bool ok = (*API->Process->ValidateImageExecution)( handle, image.SharedImageHandle(), whyNot.c_str(), WHYNOT_MAXLENGTH ) != api_false; image.PushSelections(); image.ResetSelections(); ImageInfo info2( *image ); image.PopSelections(); if ( info1 != info2 ) APIHackingAttempt( "ValidateImageExecution" ); return ok; }
Bool TextFile::ReadAll(CStr fileName, String& text, Exception* out_ex) { ASSERT_PARAMETER(fileName); Bool result = false; File* file = File::Open(fileName, File::Disposition::OpenExisting, File::Access::AccessRead, File::Flags::ShareRead | File::Flags::OptimizeForSequentialAccess, out_ex); if(file) { UInt fileSize = ToUInt(file->GetFileSize()); UInt length = fileSize / sizeof(TChar); //If fileSize is odd then +1 will compensate the division rounding if(fileSize % 2) ++fileSize; text.Reserve(length); TChar* buffer = text.DrivePointer(length); result = file->Read(buffer, fileSize); GALATEA_DELETE_PTR(file); if(!result) { text.Clear(); text.Shrink(); } } return result; }
/// Convert the path separator characters in the given object path to valid directory delimiters for the current /// platform. /// /// Note that objects with instance indices are not supported for file paths. Instance index delimiters will not be /// converted, and may be invalid file name characters on certain platforms. /// /// @param[out] rFilePath Converted file path. /// @param[in] rPackagePath Asset path string to convert (can be the same as the output file path). void AssetPath::ConvertStringToFilePath( String& rFilePath, const String& rPackagePath ) { #if HELIUM_PACKAGE_PATH_CHAR != HELIUM_PATH_SEPARATOR_CHAR && HELIUM_PACKAGE_PATH_CHAR != HELIUM_ALT_PATH_SEPARATOR_CHAR size_t pathLength = rPackagePath.GetSize(); if( &rFilePath == &rPackagePath ) { for( size_t characterIndex = 0; characterIndex < pathLength; ++characterIndex ) { char& rCharacter = rFilePath[ characterIndex ]; if( rCharacter == HELIUM_PACKAGE_PATH_CHAR || rCharacter == HELIUM_OBJECT_PATH_CHAR ) { rCharacter = Helium::s_InternalPathSeparator; } } } else { rFilePath.Remove( 0, rFilePath.GetSize() ); rFilePath.Reserve( rPackagePath.GetSize() ); for( size_t characterIndex = 0; characterIndex < pathLength; ++characterIndex ) { char character = rPackagePath[ characterIndex ]; if( character == HELIUM_PACKAGE_PATH_CHAR || character == HELIUM_OBJECT_PATH_CHAR ) { character = Helium::s_InternalPathSeparator; } rFilePath.Add( character ); } } #else rFilePath = rPackagePath; #endif }
String StringStream::ToString() const { String string; string.Reserve(m_bufferSize); for (const String& str : m_strings) string += str; return string; }
String XPathQuery::EvaluateToString(XMLElement element) const { if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode())) return String::EMPTY; const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode()); String result; result.Reserve(query_->evaluate_string(0, 0, node)); // First call get the size query_->evaluate_string(const_cast<pugi::char_t*>(result.CString()), result.Capacity(), node); // Second call get the actual string return result; }
tistream& SimpleSortedSetData< KeyT, CompareKeyT, AllocatorT >::operator<<( tistream& stream ) { m_Data->Clear(); String str; std::streamsize size = stream.rdbuf()->in_avail(); str.Reserve( static_cast< size_t >( size ) ); str.Resize( static_cast< size_t >( size ) ); stream.read( &str[ 0 ], size ); Tokenize< KeyT, CompareKeyT, AllocatorT >( str, *m_Data, s_ContainerItemDelimiter ); return stream; }
String ProcessInstance::Description() const { size_type len = 0; (*API->Process->GetProcessInstanceDescription)( handle, 0, &len ); String description; if ( len != 0 ) { description.Reserve( len ); if ( (*API->Process->GetProcessInstanceDescription)( handle, description.c_str(), &len ) == api_false ) throw APIFunctionError( "GetProcessInstanceDescription" ); } return description; }
String Label::Text() const { size_type len = 0; (*API->Label->GetLabelText)( handle, 0, &len ); String text; if ( len != 0 ) { text.Reserve( len ); if ( (*API->Label->GetLabelText)( handle, text.c_str(), &len ) == api_false ) throw APIFunctionError( "GetLabelText" ); } return text; }
bool OpenFileDialog::Execute() { q->fileNames.Clear(); String apiFilters = p->MakeAPIFilters(); String fileName; fileName.Reserve( 8192 ); *fileName.c_str() = CharTraits::Null(); if ( q->multipleSelections ) { if ( (*API->Dialog->ExecuteOpenMultipleFilesDialog)( fileName.c_str(), AddFileNameToList, &q->fileNames, p->caption.c_str(), p->initialPath.c_str(), apiFilters.c_str(), p->fileExtension.c_str() ) == api_false ) { return false; } } else { if ( (*API->Dialog->ExecuteOpenFileDialog)( fileName.c_str(), p->caption.c_str(), p->initialPath.c_str(), apiFilters.c_str(), p->fileExtension.c_str() ) == api_false ) { return false; } fileName.ResizeToNullTerminated(); if ( !fileName.IsEmpty() ) q->fileNames.Add( fileName ); } if ( !q->fileNames.IsEmpty() ) { p->fileExtension = File::ExtractExtension( *q->fileNames ); return true; } return false; }
String TextFile::ReadAll(CStr fileName) { Assert(fileName); File* file; String text; UInt fileSize; UInt length; TChar* buffer; file = File::OpenReadOnly(fileName); if(file) { fileSize = ToUInt(file->GetFileSize()); length = fileSize / sizeof(TChar); text.Reserve(length); buffer = text.DrivePointer(length); file->Read(buffer, fileSize); DeletePtr(file); } return text; }
void Calendar::Format(String & string, const char * format) { string.Reserve(64); if (Year && Month) { struct tm local; memset(&local,0,sizeof(struct tm)); local.tm_mday = Day; local.tm_mon = Month-1; local.tm_year = Year-1900; local.tm_hour = Hour; local.tm_min = Minute; local.tm_sec = Second; time_t seconds = mktime(&local); if (Times::Gmtime(&seconds,&local)) string.Size = strftime(string.Data,string.Allocated-1,format,&local); } string.Terminate(); }
bool ProcessInstance::CanExecuteGlobal( String& whyNot ) const { whyNot.Clear(); whyNot.Reserve( WHYNOT_MAXLENGTH ); return (*API->Process->ValidateGlobalExecution)( handle, whyNot.c_str(), WHYNOT_MAXLENGTH ) != api_false; }
bool ProcessInstance::Validate( String& whyNot ) { whyNot.Clear(); whyNot.Reserve( WHYNOT_MAXLENGTH ); return (*API->Process->ValidateProcessInstance)( handle, whyNot.c_str(), WHYNOT_MAXLENGTH ) != api_false; }
bool PluginManager::Mount(const String& pluginPath, bool appendExtension) { if (!Initialize()) { NazaraError("Failed to initialize PluginManager"); return false; } String path = pluginPath; if (appendExtension && !path.EndsWith(NAZARA_DYNLIB_EXTENSION)) path += NAZARA_DYNLIB_EXTENSION; bool exists = false; if (!File::IsAbsolute(path)) { for (const String& dir : s_directories) { String testPath; testPath.Reserve(dir.GetSize() + path.GetSize() + 10); testPath = dir; testPath += NAZARA_DIRECTORY_SEPARATOR; testPath += path; if (File::Exists(testPath)) { path = testPath; exists = true; break; } } } else if (File::Exists(path)) exists = true; if (!exists) { NazaraError("Failed to find plugin file"); return false; } std::unique_ptr<DynLib> library(new DynLib); if (!library->Load(path)) { NazaraError("Failed to load plugin"); return false; } PluginLoad func = reinterpret_cast<PluginLoad>(library->GetSymbol("PluginLoad")); if (!func) { NazaraError("Failed to get symbol PluginLoad"); return false; } if (!func()) { NazaraError("Plugin failed to load"); return false; } s_plugins[pluginPath] = library.release(); return true; }
Variant ProcessInstance::ParameterValue( const ProcessParameter& parameter, size_type rowIndex ) const { if ( parameter.IsNull() ) return Variant(); // First call to get the parameter type and length uint32 apiType = 0; size_type len = 0; (*API->Process->GetParameterValue)( handle, parameter.Handle(), rowIndex, &apiType, 0, &len ); apiType &= PTYPE_TYPE_MASK; if ( apiType == 0 ) throw APIFunctionError( "GetParameterValue" ); if ( apiType == PTYPE_TABLE ) throw Error( "ProcessInstance::ParameterValue(): Invoked for a table parameter" ); if ( apiType == PTYPE_STRING ) { String value; if ( len > 0 ) { value.Reserve( len ); if ( (*API->Process->GetParameterValue)( handle, parameter.Handle(), rowIndex, &apiType, value.c_str(), &len ) == api_false ) throw APIFunctionError( "GetParameterValue" ); } return value; } if ( apiType == PTYPE_BLOCK ) { ByteArray value( len ); if ( len > 0 ) { if ( (*API->Process->GetParameterValue)( handle, parameter.Handle(), rowIndex, &apiType, value.Begin(), &len ) == api_false ) throw APIFunctionError( "GetParameterValue" ); } return value; } union { uint8 u8; int8 i8; uint16 u16; int16 i16; uint32 u32; int32 i32; uint64 u64; int64 i64; float f; double d; api_bool b; api_enum e; } value; if ( (*API->Process->GetParameterValue)( handle, parameter.Handle(), rowIndex, &apiType, &value, 0 ) == api_false ) throw APIFunctionError( "GetParameterValue" ); switch ( apiType ) { case PTYPE_UINT8: return value.u8; case PTYPE_INT8: return value.i8; case PTYPE_UINT16: return value.u16; case PTYPE_INT16: return value.i16; case PTYPE_UINT32: return value.u32; case PTYPE_INT32: return value.i32; case PTYPE_UINT64: return value.u64; case PTYPE_INT64: return value.i64; case PTYPE_FLOAT: return value.f; case PTYPE_DOUBLE: return value.d; case PTYPE_BOOL: return value.b; case PTYPE_ENUM: return value.e; default: throw Error( "ProcessParameter::ParameterValue(): Internal error: Unknown parameter type" ); } }
void Lex::Next() { grounding = false; while((byte)*ptr <= ' ') { if(*ptr == '\2') grounding = true; if(*ptr == '\0') return; ptr++; } pos = ptr; int c = (byte)*ptr++; if(c == '\0') return; switch(c) { case_id: { String x; x.Reserve(12); x.Cat(c); while(iscid(*ptr)) x.Cat(*ptr++); int q = id.FindAdd(x); if(q == tk_rval_ - 256) { // simple hack for transitionary macro AddCode('&'); AddCode('&'); } else AddCode(q + 256); break; } case ':': AddCode(Char(':') ? t_dblcolon : ':'); break; case '*': AssOp('*', t_mulass); break; case '/': AssOp('/', t_divass); break; case '%': AssOp('%', t_modass); break; case '^': AssOp('^', t_xorass); break; case '!': AssOp('!', t_neq); break; case '.': if(Char('*')) AddCode(t_dot_asteriks); else if(*ptr == '.' && ptr[1] == '.') { AddCode(t_elipsis); ptr += 2; } else AddCode('.'); break; case '+': if(Char('+')) AddCode(t_inc); else AssOp('+', t_addass); return; case '-': if(Char('-')) AddCode(t_dec); else if(Char('>')) AddCode(Char('*') ? t_arrow_asteriks : t_arrow); else AssOp('-', t_subass); break; case '&': if(Char('&')) AddCode(t_and); else AssOp('&', t_andass); break; case '|': if(Char('|')) AddCode(t_or); else AssOp('|', t_orass); break; case '=': AssOp('=', t_eq); break; case '<': if(Char('<')) AssOp(t_shl, t_shlass); else AssOp('<', t_le); break; case '>': if(Char('>')) AssOp(t_shr, t_shrass); else AssOp('>', t_ge); break; case '0': { dword w = 0; if(Char('x') || Char('X')) { for(;;) { int d; if(*ptr >= '0' && *ptr <= '9') d = *ptr - '0'; else if(*ptr >= 'A' && *ptr <= 'F') d = *ptr - 'A' + 10; else if(*ptr >= 'a' && *ptr <= 'f') d = *ptr - 'a' + 10; else break; if(w >= 0x8000000u - d) { AddCode(te_integeroverflow); return; } w = w * 16 + d - '0'; ptr++; } } else while(*ptr >= '0' && *ptr <= '7') { int d = *ptr++ - '0'; if(w >= 0x1000000u - d) { AddCode(te_integeroverflow); return; } w = w * 8 + d - '0'; } Term& tm = term.AddTail(); tm.code = t_integer; tm.ptr = pos; tm.number = w; } break; case_nonzero_digit: { double w = c - '0'; bool fp = false; while(*ptr >= '0' && *ptr <= '9') w = w * 10 + *ptr++ - '0'; if(*ptr == '.') { //TODO TO BE Completed !!! fp = true; ptr++; double x = 0.1; while(*ptr >= '0' && *ptr <= '9') { w += x * (*ptr++ - '0'); x /= 10; } } Term& tm = term.AddTail(); if(fp || w < INT_MIN || w > INT_MAX) tm.code = t_double; else tm.code = t_integer; tm.ptr = pos; tm.number = w; } break; case '\'': { Term& tm = term.AddTail(); tm.code = t_character; tm.ptr = pos; tm.text = String(GetCharacter(), 1); if(*ptr == '\'') ptr++; else tm.code = te_badcharacter; } break; case '\"': { Term& tm = term.AddTail(); tm.code = t_string; tm.ptr = pos; for(;;) { while(*ptr != '\"') { if((byte)*ptr < ' ' && *ptr != 9) { tm.code = te_badstring; return; } tm.text.Cat(GetCharacter()); } ptr++; while(*ptr && (byte)*ptr <= ' ') ptr++; if(*ptr != '\"') break; ptr++; } } break; default: AddCode(c); return; } }