const char* environment_current_working_directory( void ) { if( _environment_current_working_dir[0] ) return _environment_current_working_dir; #if FOUNDATION_PLATFORM_WINDOWS { char* path; wchar_t* wd = memory_allocate_zero( sizeof( wchar_t ) * FOUNDATION_MAX_PATHLEN, 0, MEMORY_TEMPORARY ); GetCurrentDirectoryW( FOUNDATION_MAX_PATHLEN-1, wd ); path = path_clean( string_allocate_from_wstring( wd, 0 ), true ); string_copy( _environment_current_working_dir, path, FOUNDATION_MAX_PATHLEN ); string_copy( _environment_current_working_dir, path, FOUNDATION_MAX_PATHLEN ); string_deallocate( path ); memory_deallocate( wd ); } #elif FOUNDATION_PLATFORM_POSIX char* path = memory_allocate_zero( FOUNDATION_MAX_PATHLEN, 0, MEMORY_TEMPORARY ); if( !getcwd( path, FOUNDATION_MAX_PATHLEN ) ) { log_errorf( ERROR_SYSTEM_CALL_FAIL, "Unable to get cwd: %s", system_error_message( 0 ) ); return ""; } path = path_clean( path, true ); string_copy( _environment_current_working_dir, path, FOUNDATION_MAX_PATHLEN ); memory_deallocate( path ); #else # error Not implemented #endif return _environment_current_working_dir; }
char* path_prepend( char* tail, const char* base ) { if( !tail ) return path_clean( string_clone( base ), path_is_absolute( base ) ); tail = string_prepend( tail, "/" ); tail = string_prepend( tail, base ); tail = path_clean( tail, path_is_absolute( tail ) ); return tail; }
char* path_append( char* base, const char* tail ) { if( !base ) return path_clean( string_clone( tail ), path_is_absolute( tail ) ); base = string_append( base, "/" ); base = string_append( base, tail ); base = path_clean( base, path_is_absolute( base ) ); return base; }
int bin2hex_process_files( char const* const* input, char const* const* output, int columns ) { int result = BIN2HEX_RESULT_OK; unsigned int ifile, files_size; for( ifile = 0, files_size = array_size( input ); ( result == BIN2HEX_RESULT_OK ) && ( ifile < files_size ); ++ifile ) { char* input_filename = 0; char* output_filename = 0; stream_t* input_file = 0; stream_t* output_file = 0; input_filename = path_clean( string_clone( input[ifile] ), path_is_absolute( input[ifile] ) ); error_context_push( "parsing file", input_filename ); output_filename = path_clean( string_clone( output[ifile] ), path_is_absolute( output[ifile] ) ); log_infof( 0, "bin2hex %s -> %s", input_filename, output_filename ); input_file = stream_open( input_filename, STREAM_IN | STREAM_BINARY ); if( !input_file ) { log_warnf( 0, WARNING_BAD_DATA, "Unable to open input file: %s", input_filename ); result = BIN2HEX_RESULT_MISSING_INPUT_FILE; } else { output_file = stream_open( output_filename, STREAM_OUT ); if( !output_file ) { log_warnf( 0, WARNING_BAD_DATA, "Unable to open output file: %s", output_filename ); result = BIN2HEX_RESULT_UNABLE_TO_OPEN_OUTPUT_FILE; } } if( input_file && output_file ) result = bin2hex_process_file( input_file, output_file, columns ); stream_deallocate( input_file ); stream_deallocate( output_file ); string_deallocate( output_filename ); error_context_pop(); string_deallocate( input_filename ); } if( ( result == BIN2HEX_RESULT_OK ) && ( files_size > 0 ) ) log_info( 0, "All files generated" ); return result; }
const char* environment_temporary_directory( void ) { if( _environment_temp_dir[0] ) return _environment_temp_dir; #if FOUNDATION_PLATFORM_WINDOWS { char* path; wchar_t* wpath = memory_allocate_zero( sizeof( wchar_t ) * FOUNDATION_MAX_PATHLEN, 0, MEMORY_TEMPORARY ); GetTempPathW( FOUNDATION_MAX_PATHLEN, wpath ); path = path_clean( string_allocate_from_wstring( wpath, 0 ), true ); string_copy( _environment_temp_dir, path, FOUNDATION_MAX_PATHLEN ); string_deallocate( path ); memory_deallocate( wpath ); } #elif FOUNDATION_PLATFORM_POSIX string_copy( _environment_temp_dir, P_tmpdir, FOUNDATION_MAX_PATHLEN ); unsigned int len = string_length( _environment_temp_dir ); if( ( len > 1 ) && ( _environment_temp_dir[ len - 1 ] == '/' ) ) _environment_temp_dir[ len - 1 ] = 0; #else # error Not implemented #endif if( _environment_app.config_dir ) { unsigned int curlen = string_length( _environment_temp_dir ); unsigned int cfglen = string_length( _environment_app.config_dir ); if( ( curlen + cfglen + 2 ) < FOUNDATION_MAX_PATHLEN ) { _environment_temp_dir[curlen] = '/'; memcpy( _environment_temp_dir + curlen + 1, _environment_app.config_dir, cfglen + 1 ); } } return _environment_temp_dir; }
char* path_subdirectory_name( const char* path, const char* root ) { char* subpath; char* testpath; char* testroot; char* pathofpath; unsigned int pathprotocol, rootprotocol; char* cpath = string_clone( path ); char* croot = string_clone( root ); cpath = path_clean( cpath, path_is_absolute( cpath ) ); croot = path_clean( croot, path_is_absolute( croot ) ); pathofpath = path_directory_name( cpath ); testpath = pathofpath; pathprotocol = string_find_string( testpath, "://", 0 ); if( pathprotocol != STRING_NPOS ) testpath += pathprotocol + 2; // add two to treat as absolute path testroot = croot; rootprotocol = string_find_string( testroot, "://", 0 ); if( rootprotocol != STRING_NPOS ) testroot += rootprotocol + 2; if( ( rootprotocol != STRING_NPOS ) && ( ( pathprotocol == STRING_NPOS ) || ( pathprotocol != rootprotocol ) || !string_equal_substr( cpath, croot, rootprotocol ) ) ) subpath = string_allocate( 0 ); else if( !string_equal_substr( testpath, testroot, string_length( testroot ) ) ) subpath = string_allocate( 0 ); else { char* filename = path_file_name( cpath ); subpath = string_substr( testpath, string_length( testroot ), STRING_NPOS ); subpath = path_clean( path_append( subpath, filename ), false ); string_deallocate( filename ); } string_deallocate( pathofpath ); string_deallocate( cpath ); string_deallocate( croot ); return subpath; }
int main_run(void* main_arg) { int result = RENDERCOMPILE_RESULT_OK; rendercompile_input_t input = rendercompile_parse_command_line(environment_command_line()); FOUNDATION_UNUSED(main_arg); if (input.display_help) { rendercompile_print_usage(); goto exit; } resource_source_set_path(STRING_ARGS(input.source_path)); resource_compile_register(render_compile); size_t ifile, fsize; for (ifile = 0, fsize = array_size(input.input_files); ifile < fsize; ++ifile) { uuid_t uuid = string_to_uuid(STRING_ARGS(input.input_files[ifile])); if (uuid_is_null(uuid)) { char buffer[BUILD_MAX_PATHLEN]; string_t pathstr = string_copy(buffer, sizeof(buffer), STRING_ARGS(input.input_files[ifile])); pathstr = path_clean(STRING_ARGS(pathstr), sizeof(buffer)); pathstr = path_absolute(STRING_ARGS(pathstr), sizeof(buffer)); uuid = resource_import_map_lookup(STRING_ARGS(pathstr)); } if (uuid_is_null(uuid)) { log_warnf(HASH_RESOURCE, WARNING_INVALID_VALUE, STRING_CONST("Failed to lookup: %.*s"), STRING_FORMAT(input.input_files[ifile])); result = RENDERCOMPILE_RESULT_INVALID_INPUT; break; } if (resource_compile(uuid, RESOURCE_PLATFORM_ALL)) { string_const_t uuidstr = string_from_uuid_static(uuid); log_infof(HASH_RESOURCE, STRING_CONST("Successfully compiled: %.*s (%.*s)"), STRING_FORMAT(uuidstr), STRING_FORMAT(input.input_files[ifile])); } else { string_const_t uuidstr = string_from_uuid_static(uuid); log_warnf(HASH_RESOURCE, WARNING_UNSUPPORTED, STRING_CONST("Failed to compile: %.*s (%.*s)"), STRING_FORMAT(uuidstr), STRING_FORMAT(input.input_files[ifile])); } } exit: array_deallocate(input.input_files); return result; }
char* path_merge( const char* first, const char* second ) { char* merged; if( !first ) merged = string_clone( second ); else if( !second ) merged = string_clone( first ); else { merged = string_format( "%s/", first ); merged = string_append( merged, second ); } merged = path_clean( merged, path_is_absolute( first ) ); return merged; }
char* path_base_file_name_with_path( const char* path ) { unsigned int start, end; char* base; if( !path ) return string_allocate( 0 ); start = string_find_last_of( path, "/\\", STRING_NPOS ); end = string_rfind( path, '.', STRING_NPOS ); //For "dot" files, i.e files with names like "/path/to/.file", return the dot name ".file" if( !end || ( end == ( start + 1 ) ) || ( ( start != STRING_NPOS ) && ( end < start ) ) ) end = STRING_NPOS; base = string_substr( path, 0, ( end != STRING_NPOS ) ? end : STRING_NPOS ); base = path_clean( base, path_is_absolute( base ) ); return base; }
char* path_directory_name( const char* path ) { char* pathname; unsigned int pathprotocol; unsigned int pathstart = 0; unsigned int end = string_find_last_of( path , "/\\", STRING_NPOS ); if( end == 0 ) return string_clone( "/" ); if( end == STRING_NPOS ) return string_allocate( 0 ); pathprotocol = string_find_string( path, "://", 0 ); if( pathprotocol != STRING_NPOS ) pathstart = pathprotocol +=2; // add two to treat as absolute path pathname = string_substr( path, pathstart, end - pathstart ); pathname = path_clean( pathname, path_is_absolute( pathname ) ); return pathname; }
const char* environment_home_directory( void ) { if( _environment_home_dir[0] ) return _environment_home_dir; #if FOUNDATION_PLATFORM_WINDOWS { char* path; wchar_t* wpath = memory_allocate_zero( sizeof( wchar_t ) * FOUNDATION_MAX_PATHLEN, 0, MEMORY_TEMPORARY ); SHGetFolderPathW( 0, CSIDL_LOCAL_APPDATA, 0, 0, wpath ); path = path_clean( string_allocate_from_wstring( wpath, 0 ), true ); string_copy( _environment_home_dir, path, FOUNDATION_MAX_PATHLEN ); string_deallocate( path ); memory_deallocate( wpath ); } #elif FOUNDATION_PLATFORM_LINUX string_copy( _environment_home_dir, environment_variable( "HOME" ), FOUNDATION_MAX_PATHLEN ); #elif FOUNDATION_PLATFORM_APPLE if( environment_application()->flags & APPLICATION_UTILITY ) { CFStringRef home = NSHomeDirectory(); CFStringGetCString( home, _environment_home_dir, FOUNDATION_MAX_PATHLEN, kCFStringEncodingUTF8 ); } else { char bundle_identifier[FOUNDATION_MAX_PATHLEN+1]; environment_bundle_identifier( bundle_identifier ); char* path = path_append( path_merge( _environment_home_dir, "/Library/Application Support" ), bundle_identifier ); string_copy( _environment_home_dir, path, FOUNDATION_MAX_PATHLEN ); string_deallocate( path ); } #elif FOUNDATION_PLATFORM_ANDROID string_copy( _environment_home_dir, android_app()->activity->internalDataPath, FOUNDATION_MAX_PATHLEN ); #else # error Not implemented #endif return _environment_home_dir; }
DECLARE_TEST( string, utility ) { { char* path1 = string_clone( "" ); char* path2 = string_clone( "/" ); char* path3 = string_clone( "/." ); char* path4 = string_clone( "./" ); char* path5 = string_clone( "./." ); char* path6 = string_clone( "././" ); char* path7 = string_clone( "././//" ); char* path8 = string_clone( "././//./////././////.//////.//." ); char* path9 = string_clone( "http://././//./////././////.//////.//." ); char* path10 = string_clone( "" ); char* path11 = string_clone( "\\" ); char* path12 = string_clone( "/\\." ); char* path13 = string_clone( ".\\/" ); char* path14 = string_clone( "./\\." ); char* path15 = string_clone( ".\\.//\\" ); char* path16 = string_clone( ".\\.\\\\\\" ); char* path17 = string_clone( ".\\.\\\\\\.\\\\////\\///\\\\.\\.\\\\\\\\\\.\\\\\\\\\\\\.\\\\." ); char* path18 = string_clone( "http://\\.\\.\\\\\\.\\\\\\\\//\\.\\.\\\\\\\\//\\.\\\\\\\\\\\\.\\\\." ); char* path19 = string_clone( "testing/path/ext" ); char* path20 = string_clone( "testing/path/extend" ); char* path21 = string_clone( "testing/path/extend/dyn" ); char* path22 = string_clone( "testing/./\\\\/\\/./path/././//./extend/\\\\" ); path1 = path_clean( path1, true ); path2 = path_clean( path2, true ); path3 = path_clean( path3, true ); path4 = path_clean( path4, true ); path5 = path_clean( path5, true ); path6 = path_clean( path6, true ); path7 = path_clean( path7, true ); path8 = path_clean( path8, true ); path9 = path_clean( path9, true ); path10 = path_clean( path10, true ); path11 = path_clean( path11, true ); path12 = path_clean( path12, true ); path13 = path_clean( path13, true ); path14 = path_clean( path14, true ); path15 = path_clean( path15, true ); path16 = path_clean( path16, true ); path17 = path_clean( path17, true ); path18 = path_clean( path18, true ); path19 = path_clean( path19, true ); path20 = path_clean( path20, true ); path21 = path_clean( path21, true ); path22 = path_clean( path22, true ); EXPECT_TRUE( string_equal( path1, "/" ) ); EXPECT_TRUE( string_equal( path2, "/" ) ); EXPECT_TRUE( string_equal( path3, "/" ) ); EXPECT_TRUE( string_equal( path4, "/" ) ); EXPECT_TRUE( string_equal( path5, "/" ) ); EXPECT_TRUE( string_equal( path6, "/" ) ); EXPECT_TRUE( string_equal( path7, "/" ) ); EXPECT_TRUE( string_equal( path8, "/" ) ); EXPECT_TRUE( string_equal( path9, "http://" ) ); EXPECT_TRUE( string_equal( path10, "/" ) ); EXPECT_TRUE( string_equal( path11, "/" ) ); EXPECT_TRUE( string_equal( path12, "/" ) ); EXPECT_TRUE( string_equal( path13, "/" ) ); EXPECT_TRUE( string_equal( path14, "/" ) ); EXPECT_TRUE( string_equal( path15, "/" ) ); EXPECT_TRUE( string_equal( path16, "/" ) ); EXPECT_TRUE( string_equal( path17, "/" ) ); EXPECT_TRUE( string_equal( path18, "http://" ) ); EXPECT_TRUE( string_equal( path19, "/testing/path/ext" ) ); EXPECT_TRUE( string_equal( path20, "/testing/path/extend" ) ); EXPECT_TRUE( string_equal( path21, "/testing/path/extend/dyn" ) ); EXPECT_TRUE( string_equal( path22, "/testing/path/extend" ) ); string_deallocate( path1 ); string_deallocate( path2 ); string_deallocate( path3 ); string_deallocate( path4 ); string_deallocate( path5 ); string_deallocate( path6 ); string_deallocate( path7 ); string_deallocate( path8 ); string_deallocate( path9 ); string_deallocate( path10 ); string_deallocate( path11 ); string_deallocate( path12 ); string_deallocate( path13 ); string_deallocate( path14 ); string_deallocate( path15 ); string_deallocate( path16 ); string_deallocate( path17 ); string_deallocate( path18 ); string_deallocate( path19 ); string_deallocate( path20 ); string_deallocate( path21 ); string_deallocate( path22 ); } { char** explodearr = 0; char* explodestr = string_clone( " .,testing, .,utility.,string methods ..., like,,,finds split..merge .,.explode.and. .., ., similar .,,,. " ); char* mergestr = string_clone( " testing merge string " ); char* mergestr2 = string_clone( " ., testing, .merge.string,. " ); char* merged = 0; char** mergearr, **mergearr2, **mergearr3; char* splitstr = string_clone( " testing split" ); char* splitright = 0, *splitleft = 0; char* splitright2 = 0, *splitleft2 = 0; char* substrtest = string_clone( "testing substr" ); char* substr = 0; explodearr = string_explode( explodestr, " ,.", false ); mergearr = string_explode( mergestr, " .,", true ); mergearr2 = string_explode( mergestr, " .,", false ); mergearr3 = string_explode( mergestr, " .,", true ); merged = string_merge( (const char* const*)mergearr, array_size( mergearr ), " " ); string_split( splitstr, " ", &splitleft, &splitright, false ); string_split( splitstr, " ", &splitleft2, &splitright2, true ); EXPECT_EQ( array_size( explodearr ), 11 ); EXPECT_TRUE( string_equal( explodearr[0], "testing" ) ); EXPECT_TRUE( string_equal( explodearr[1], "utility" ) ); EXPECT_TRUE( string_equal( explodearr[2], "string" ) ); EXPECT_TRUE( string_equal( explodearr[3], "methods" ) ); EXPECT_TRUE( string_equal( explodearr[4], "like" ) ); EXPECT_TRUE( string_equal( explodearr[5], "finds" ) ); EXPECT_TRUE( string_equal( explodearr[6], "split" ) ); EXPECT_TRUE( string_equal( explodearr[7], "merge" ) ); EXPECT_TRUE( string_equal( explodearr[8], "explode" ) ); EXPECT_TRUE( string_equal( explodearr[9], "and" ) ); EXPECT_TRUE( string_equal( explodearr[10], "similar" ) ); EXPECT_EQ( array_size( mergearr ), 12 ); EXPECT_TRUE( string_equal( mergearr[0], "" ) ); EXPECT_TRUE( string_equal( mergearr[1], "" ) ); EXPECT_TRUE( string_equal( mergearr[2], "" ) ); EXPECT_TRUE( string_equal( mergearr[3], "" ) ); EXPECT_TRUE( string_equal( mergearr[4], "testing" ) ); EXPECT_TRUE( string_equal( mergearr[5], "" ) ); EXPECT_TRUE( string_equal( mergearr[6], "" ) ); EXPECT_TRUE( string_equal( mergearr[7], "merge" ) ); EXPECT_TRUE( string_equal( mergearr[8], "string" ) ); EXPECT_TRUE( string_equal( mergearr[9], "" ) ); EXPECT_TRUE( string_equal( mergearr[10], "" ) ); EXPECT_TRUE( string_equal( mergearr[11], "" ) ); EXPECT_EQ( array_size( mergearr2 ), 3 ); EXPECT_TRUE( string_equal( mergearr2[0], "testing" ) ); EXPECT_TRUE( string_equal( mergearr2[1], "merge" ) ); EXPECT_TRUE( string_equal( mergearr2[2], "string" ) ); EXPECT_TRUE( string_equal( merged, mergestr ) ); EXPECT_EQ( array_size( mergearr3 ), 12 ); EXPECT_TRUE( string_equal( mergearr3[0], "" ) ); EXPECT_TRUE( string_equal( mergearr3[1], "" ) ); EXPECT_TRUE( string_equal( mergearr3[2], "" ) ); EXPECT_TRUE( string_equal( mergearr3[3], "" ) ); EXPECT_TRUE( string_equal( mergearr3[4], "testing" ) ); EXPECT_TRUE( string_equal( mergearr3[5], "" ) ); EXPECT_TRUE( string_equal( mergearr3[6], "" ) ); EXPECT_TRUE( string_equal( mergearr3[7], "merge" ) ); EXPECT_TRUE( string_equal( mergearr3[8], "string" ) ); EXPECT_TRUE( string_equal( mergearr3[9], "" ) ); EXPECT_TRUE( string_equal( mergearr3[10], "" ) ); EXPECT_TRUE( string_equal( mergearr3[11], "" ) ); EXPECT_TRUE( string_equal( substr = string_substr( substrtest, 0, 4 ), "test" ) ); string_deallocate( substr ); EXPECT_TRUE( string_equal( substr = string_substr( substrtest, 0, 14 ), "testing substr" ) ); string_deallocate( substr ); EXPECT_TRUE( string_equal( substr = string_substr( substrtest, 0, 20 ), "testing substr" ) ); string_deallocate( substr ); EXPECT_TRUE( string_equal( substr = string_substr( substrtest, 3, 20 ), "ting substr" ) ); string_deallocate( substr ); EXPECT_TRUE( string_equal( substr = string_substr( substrtest, 3, 11 ), "ting substr" ) ); string_deallocate( substr ); EXPECT_TRUE( string_equal( substr = string_substr( substrtest, 3, 1 ), "t" ) ); string_deallocate( substr ); EXPECT_TRUE( string_equal( substr = string_substr( substrtest, 3, 0 ), "" ) ); string_deallocate( substr ); EXPECT_TRUE( string_equal( substr = string_substr( substrtest, 20, 0 ), "" ) ); string_deallocate( substr ); EXPECT_TRUE( string_equal( substr = string_substr( substrtest, 20, 20 ), "" ) ); string_deallocate( substr ); EXPECT_TRUE( string_equal( splitleft, "testing" ) ); EXPECT_TRUE( string_equal( splitright, "split" ) ); EXPECT_TRUE( string_equal( splitleft2, "" ) ); EXPECT_TRUE( string_equal( splitright2, "testing split" ) ); { char* replacestr = string_clone( "testing replace" ); char* replacestr2 = string_clone( "testing replace" ); char* replacestr3 = string_clone( "testing replacelace" ); char* replacestr4 = string_clone( "" ); char* replacestr5 = string_clone( "repppppppppp" ); replacestr = string_replace( replacestr, "rep", "testrep", false ); replacestr2 = string_replace( replacestr2, "rep", "testrep", true ); replacestr3 = string_replace( replacestr3, "replace", "testrep", true ); replacestr4 = string_replace( replacestr4, "foo", "bar", true ); replacestr5 = string_replace( replacestr5, "rep", "re", true ); EXPECT_TRUE( string_equal( replacestr, "testing testreplace" ) ); EXPECT_TRUE( string_equal( replacestr2, "testing testreplace" ) ); EXPECT_TRUE( string_equal( replacestr3, "testing testtestrep" ) ); EXPECT_TRUE( string_equal( replacestr4, "" ) ); EXPECT_TRUE( string_equal( replacestr5, "re" ) ); string_deallocate( replacestr ); string_deallocate( replacestr2 ); string_deallocate( replacestr3 ); string_deallocate( replacestr4 ); string_deallocate( replacestr5 ); } { char* stripstr = string_clone( " testing strip : " ); char* stripstr2 = string_clone( " testing strip : " ); char* stripstr3 = string_clone( " testing strip : " ); stripstr = string_strip( stripstr, " tp: " ); stripstr2 = string_strip( stripstr2, "" ); stripstr3 = string_strip( stripstr3, " tesingrp:" ); EXPECT_TRUE( string_equal( stripstr, "esting stri" ) ); EXPECT_TRUE( string_equal( stripstr2, " testing strip : " ) ); EXPECT_TRUE( string_equal( stripstr3, "" ) ); string_deallocate( stripstr ); string_deallocate( stripstr2 ); string_deallocate( stripstr3 ); } string_array_deallocate( explodearr ); string_deallocate( explodestr ); string_deallocate( mergestr ); string_deallocate( mergestr2 ); string_deallocate( merged ); string_array_deallocate( mergearr ); string_array_deallocate( mergearr2 ); string_array_deallocate( mergearr3 ); string_deallocate( splitstr ); string_deallocate( splitright ); string_deallocate( splitleft ); string_deallocate( splitright2 ); string_deallocate( splitleft2 ); string_deallocate( substrtest ); } { #define SHORTSTRING "short" #define LONGSTRING "long string with dynamic buffer storage but with no real useful data" char* clonestr = string_clone( "" ); char* clonestr2 = string_clone( SHORTSTRING ); char* clonestr3 = string_clone( LONGSTRING ); char* teststr = string_clone( clonestr ); char* teststr2 = string_clone( clonestr2 ); char* teststr3 = string_clone( clonestr3 ); char* concatstr = string_concat( clonestr, teststr ); char* concatstr2 = string_concat( clonestr, teststr2 ); char* concatstr3 = string_concat( teststr2, clonestr ); char* concatstr4 = string_concat( clonestr2, teststr2 ); char* concatstr5 = string_concat( clonestr, teststr3 ); char* concatstr6 = string_concat( teststr3, clonestr ); char* concatstr7 = string_concat( clonestr2, teststr3 ); char* concatstr8 = string_concat( teststr3, clonestr2 ); char* concatstr9 = string_concat( clonestr3, teststr3 ); char* concatstr10 = string_concat( teststr3, clonestr3 ); EXPECT_NE( teststr, clonestr ); EXPECT_TRUE( string_equal( teststr, clonestr ) ); EXPECT_NE( teststr2, clonestr2 ); EXPECT_TRUE( string_equal( teststr2, clonestr2 ) ); EXPECT_NE( teststr3, clonestr3 ); EXPECT_TRUE( string_equal( teststr3, clonestr3 ) ); EXPECT_TRUE( string_equal( concatstr, "" ) ); EXPECT_TRUE( string_equal( concatstr2, SHORTSTRING ) ); EXPECT_TRUE( string_equal( concatstr3, SHORTSTRING ) ); EXPECT_TRUE( string_equal( concatstr4, SHORTSTRING SHORTSTRING ) ); EXPECT_TRUE( string_equal( concatstr5, LONGSTRING ) ); EXPECT_TRUE( string_equal( concatstr6, LONGSTRING ) ); EXPECT_TRUE( string_equal( concatstr7, SHORTSTRING LONGSTRING ) ); EXPECT_TRUE( string_equal( concatstr8, LONGSTRING SHORTSTRING ) ); EXPECT_TRUE( string_equal( concatstr9, LONGSTRING LONGSTRING ) ); EXPECT_TRUE( string_equal( concatstr10, LONGSTRING LONGSTRING ) ); string_deallocate( teststr ); string_deallocate( clonestr ); string_deallocate( teststr2 ); string_deallocate( clonestr2 ); string_deallocate( teststr3 ); string_deallocate( clonestr3 ); string_deallocate( concatstr ); string_deallocate( concatstr2 ); string_deallocate( concatstr3 ); string_deallocate( concatstr4 ); string_deallocate( concatstr5 ); string_deallocate( concatstr6 ); string_deallocate( concatstr7 ); string_deallocate( concatstr8 ); string_deallocate( concatstr9 ); string_deallocate( concatstr10 ); #undef SHORTSTRING #undef LONGSTRING } return 0; }
int _environment_initialize( const application_t application ) { #if FOUNDATION_PLATFORM_WINDOWS int ia; int num_args = 0; DWORD ret = 0; wchar_t module_filename[FOUNDATION_MAX_PATHLEN]; LPWSTR* arg_list = CommandLineToArgvW( GetCommandLineW(), &num_args ); if( !arg_list ) return -1; for( ia = 0; ia < num_args; ++ia ) array_push( _environment_argv, string_allocate_from_wstring( arg_list[ia], 0 ) ); LocalFree( arg_list ); if( GetModuleFileNameW( 0, module_filename, FOUNDATION_MAX_PATHLEN ) ) { char* exe_path = string_allocate_from_wstring( module_filename, 0 ); char* dir_path = path_make_absolute( exe_path ); _environment_set_executable_paths( dir_path ); string_deallocate( dir_path ); string_deallocate( exe_path ); } else { log_errorf( ERROR_SYSTEM_CALL_FAIL, "Unable to get module filename" ); return -1; } #elif FOUNDATION_PLATFORM_APPLE int ia; int* argc_ptr = _NSGetArgc(); char*** argv_ptr = _NSGetArgv(); for( ia = 0; ia < *argc_ptr; ++ia ) array_push( _environment_argv, string_clone( (*argv_ptr)[ia] ) ); FOUNDATION_ASSERT( *argc_ptr > 0 ); char* exe_path = path_make_absolute( (*argv_ptr)[0] ); _environment_set_executable_paths( exe_path ); string_deallocate( exe_path ); #elif FOUNDATION_PLATFORM_POSIX stream_t* cmdline = fs_open_file( "/proc/self/cmdline", STREAM_IN | STREAM_BINARY ); if( !cmdline ) { log_errorf( ERROR_SYSTEM_CALL_FAIL, "Unable to read /proc/self/cmdline" ); return -1; } while( true ) { char* arg = stream_read_string( cmdline ); if( !string_length( arg ) ) { string_deallocate( arg ); break; } array_push( _environment_argv, arg ); } char exelink[FOUNDATION_MAX_PATHLEN] = {0}; if( readlink( "/proc/self/exe", exelink, FOUNDATION_MAX_PATHLEN ) < 0 ) { log_errorf( ERROR_SYSTEM_CALL_FAIL, "Unable to read /proc/self/exe link" ); return -1; } char* exe_path; char* dir_path; exe_path = path_clean( string_clone( exelink ), path_is_absolute( exelink ) ); dir_path = path_make_absolute( exe_path ); _environment_set_executable_paths( dir_path ); string_deallocate( dir_path ); string_deallocate( exe_path ); #else # error Not implemented /*if( array_size( _environment_argv ) > 0 ) { char* exe_path = path_clean( string_clone( _environment_argv[0] ), path_is_absolute( _environment_argv[0] ) ); char* dir_path = path_make_absolute( exe_path ); _environment_set_executable_paths( dir_path ); string_deallocate( dir_path ); string_deallocate( exe_path ); } else if( !string_length( _environment_executable_dir ) ) string_copy( _environment_executable_dir, environment_current_working_directory(), FOUNDATION_MAX_PATHLEN ); */ #endif _environment_app = application; if( uuid_is_null( _environment_app.instance ) ) _environment_app.instance = uuid_generate_random(); string_copy( _environment_initial_working_dir, environment_current_working_directory(), FOUNDATION_MAX_PATHLEN ); environment_temporary_directory(); return 0; }
int hashify_process_files(string_t* files, bool check_only) { int result = HASHIFY_RESULT_OK; hashify_string_t* history = 0; size_t ifile, files_size; for (ifile = 0, files_size = array_size(files); (result == HASHIFY_RESULT_OK) && (ifile < files_size); ++ifile) { string_t input_filename; string_t output_filename; string_const_t base_filename; stream_t* input_file; stream_t* output_file; input_filename = string_allocate(0, BUILD_MAX_PATHLEN); input_filename = string_copy(input_filename.str, BUILD_MAX_PATHLEN, STRING_ARGS(files[ifile])); input_filename = path_clean(STRING_ARGS(input_filename), BUILD_MAX_PATHLEN); error_context_push(STRING_CONST("parsing file"), STRING_ARGS(input_filename)); base_filename = path_base_file_name_with_directory(STRING_ARGS(input_filename)); output_filename = string_allocate_format(STRING_CONST("%.*s.h"), STRING_FORMAT(base_filename)); log_infof(0, STRING_CONST("Hashifying %.*s -> %.*s"), STRING_FORMAT(input_filename), STRING_FORMAT(output_filename)); input_file = stream_open(STRING_ARGS(input_filename), STREAM_IN); //If only validating, open the final output file. If generating, make a memory buffer as intermediate storage if (check_only) output_file = stream_open(STRING_ARGS(output_filename), STREAM_IN); else output_file = buffer_stream_allocate(memory_allocate(0, 65536, 0, MEMORY_PERSISTENT), STREAM_IN | STREAM_OUT, 0, 65536, true, true); if (!input_file) { log_warnf(0, WARNING_INVALID_VALUE, STRING_CONST("Unable to open input file: %.*s"), STRING_FORMAT(input_filename)); result = HASHIFY_RESULT_MISSING_INPUT_FILE; } else if (!output_file) { log_warnf(0, WARNING_INVALID_VALUE, STRING_CONST("Unable to open output file: %.*s"), STRING_FORMAT(output_filename)); result = HASHIFY_RESULT_MISSING_OUTPUT_FILE; } if (input_file && output_file) { result = hashify_process_file(input_file, output_file, output_filename, check_only, &history); if ((result == HASHIFY_RESULT_OK) && !check_only) result = hashify_write_file(output_file, output_filename); } stream_deallocate(input_file); stream_deallocate(output_file); error_context_pop(); string_deallocate(input_filename.str); string_deallocate(output_filename.str); } if ((result == HASHIFY_RESULT_OK) && (files_size > 0)) { if (check_only) log_info(0, STRING_CONST("All hashes validated")); else log_info(0, STRING_CONST("All hashes generated")); } array_deallocate(history); return result; }
int hashify_process_files( const char* const* files, bool check_only ) { int result = HASHIFY_RESULT_OK; hashify_string_t* history = 0; unsigned int ifile, files_size; for( ifile = 0, files_size = array_size( files ); ( result == HASHIFY_RESULT_OK ) && ( ifile < files_size ); ++ifile ) { char* input_filename; char* output_filename; stream_t* input_file; stream_t* output_file; input_filename = path_clean( string_clone( files[ifile] ), path_is_absolute( files[ifile] ) ); error_context_push( "parsing file", input_filename ); output_filename = string_append( path_base_file_name_with_path( input_filename ), ".h" ); log_infof( "Hashifying %s -> %s", input_filename, output_filename ); input_file = stream_open( input_filename, STREAM_IN ); //If only validating, open the final output file. If generating, make a memory buffer as intermediate storage if( check_only ) output_file = stream_open( output_filename, STREAM_IN ); else output_file = buffer_stream_allocate( memory_allocate( 65536, 0, MEMORY_PERSISTENT ), STREAM_IN | STREAM_OUT, 0, 65536, true, true ); if( !input_file ) { log_warnf( WARNING_BAD_DATA, "Unable to open input file: %s", input_filename ); result = HASHIFY_RESULT_MISSING_INPUT_FILE; } else if( !output_file ) { log_warnf( WARNING_BAD_DATA, "Unable to open output file: %s", output_filename ); result = HASHIFY_RESULT_MISSING_OUTPUT_FILE; } if( input_file && output_file ) { result = hashify_process_file( input_file, output_file, check_only, &history ); if( ( result == HASHIFY_RESULT_OK ) && !check_only ) result = hashify_write_file( output_file, output_filename ); } stream_deallocate( input_file ); stream_deallocate( output_file ); error_context_pop(); string_deallocate( input_filename ); string_deallocate( output_filename ); } if( ( result == HASHIFY_RESULT_OK ) && ( files_size > 0 ) ) { if( check_only ) log_infof( "All hashes validated" ); else log_infof( "All hashes generated" ); } array_deallocate( history ); return result; }
char* path_make_absolute( const char* path ) { unsigned int up, last, length, protocollen; char* abspath = string_clone( path ); if( !path_is_absolute( abspath ) ) { abspath = string_prepend( abspath, "/" ); abspath = string_prepend( abspath, environment_current_working_directory() ); abspath = path_clean( abspath, true ); } else { abspath = path_clean( abspath, true ); } protocollen = string_find_string( abspath, "://", 0 ); if( protocollen != STRING_NPOS ) protocollen += 3; //Also skip the "://" separator else protocollen = 0; //Deal with .. references while( ( up = string_find_string( abspath, "/../", 0 ) ) != STRING_NPOS ) { char* subpath; if( ( protocollen && ( up == ( protocollen - 1 ) ) ) || ( !protocollen && ( up == 0 ) ) ) { //This moves mem so "prot://../path" ends up as "prot://path" memmove( abspath + protocollen, abspath + 3 + protocollen, string_length( abspath ) + 1 - ( 3 + protocollen ) ); continue; } last = string_rfind( abspath, '/', up - 1 ); if( last == STRING_NPOS ) { //Must be a path like C:/../something since other absolute paths last = up; } subpath = string_substr( abspath, 0, last ); subpath = string_append( subpath, abspath + up + 3 ); // +3 will include the / of the later part of the path string_deallocate( abspath ); abspath = subpath; } length = string_length( abspath ); if( length >= 3 ) { while( ( length >= 3 ) && ( abspath[length-3] == '/' ) && ( abspath[length-2] == '.' ) && ( abspath[length-1] == L'.' ) ) { //Step up if( length == 3 ) { abspath[1] = 0; length = 1; } else { length = string_rfind( abspath, '/', length - 4 ); abspath[length] = 0; } } } return abspath; }