pvoid STDCALL os_filefullname( pubyte filename, pvoid buf ) { pubyte ptr; #ifdef LINUX char cur[512]; if ( filename[0] != '/' ) { getcwd( cur, 512 ); chdir( filename ); basename( filename ); getcwd( str_ptr( ( pstr )buf ), 512 ); str_setlen( ( pstr )buf, strlen( str_ptr( ( pstr )buf ) ) ); str_appendb( buf, '/' ); str_appendp( buf, filename ); chdir( cur ); } else { //Ìîæåò ðàáîòàòü íåêîððåêòíî åñëè â àáñîëþòíîì ïóòè âñòðå÷àþòñÿ '.' '..' str_appendp( buf, filename ); } #else DWORD temp; temp = GetFullPathName( filename, 512, str_ptr( ( pstr )buf ), &ptr ); // äëÿ Windows CharLowerBuff( str_ptr( ( pstr )buf ), temp ); str_setlen( ( pstr )buf, temp ); #endif return buf; }
pstr STDCALL os_dirgetcur( pstr name ) { #ifdef LINUX return str_setlen( name,getcwd( str_ptr( name), 512 )); #else return str_setlen( name, GetCurrentDirectory( 512,str_ptr( str_reserve( name, 512 )))); #endif }
pstr STDCALL gettempdir( pstr name ) { if ( !str_len( &_gentee.tempdir )) { uint id = 0; str path; pstr ps; ps = &_gentee.tempdir; os_tempdir( str_init( &path )); while ( 1 ) { str_clear( ps ); str_printf( ps, (pubyte)("%s%cgentee%02X.tmp"), str_ptr( &path ), SLASH, id ); _gentee.tempfile = os_fileopen( ps, FOP_CREATE | FOP_EXCLUSIVE ); if ( !_gentee.tempfile ) { if ( id++ > 0xFFFF ) msg( MFileopen | MSG_STR | MSG_EXIT, ps ); } else break; } os_dircreate( str_setlen( ps, str_len( ps ) - 4 )); str_delete( &path ); } return str_copy( name, &_gentee.tempdir ); //return str_copy( name, os_gettemp()); }
pstr STDCALL str_sprintf( pstr ps, pstr output, pcollect pclt ) { uint args[32]; uint len = ps->use - 1; uint i, k = 0, itype; str_expand( ps, 1024 ); for ( i = 0; i < collect_count( pclt ); i++ ) { itype = collect_gettype( pclt, i ); if ( itype == TDouble || itype == TLong || itype == TUlong ) { args[k++] = *( puint )collect_index( pclt, i ); args[k++] = *(( puint )collect_index( pclt, i ) + 1 ); } else { args[k++] = *( puint )collect_index( pclt, i ); if ( itype == TStr ) args[k - 1] = (uint)(( pstr )args[k - 1])->data; } } str_setlen( ps, len + vsprintf( ps->data + len, output->data, (pubyte)args )); return ps; }
pstr STDCALL os_tempdir( pstr name ) { #ifdef LINUX pstr stemp = os_gettemp(); uint uLenTempStr = str_len(stemp); str_copy(name,stemp); #else str_setlen( name, GetTempPath( 1024, str_reserve( name, 1024 )->data )); #endif return str_trim( name, SLASH, TRIM_ONE | TRIM_RIGHT ); }
pstr STDCALL getmodulename( pstr name ) { #ifdef WINDOWS uint i; str_reserve( name, 512 ); i = GetModuleFileName( 0, str_ptr( name ), 511 ); str_setlen( name, i ); #endif return name; }
pstr CDECLCALL str_printf( pstr ps, pubyte output, ... ) { va_list args; uint len = ps->use - 1; str_expand( ps, 512 ); va_start( args, output ); str_setlen( ps, len + vsprintf( ps->data + len, output, args )); va_end( args ); return ps; }
pstr STDCALL str_trim( pstr ps, uint symbol, uint flag ) { uint len = str_len( ps ); /* uint rsymbol = symbol if flag & $TRIM_PAIR { switch symbol { case '(' : rsymbol = ')' case '{' : rsymbol = '}' case '[' : rsymbol = ']' case '<' : rsymbol = '>' } }*/ if ( flag & TRIM_RIGHT ) { uint i = len; while ( i && ps->data[ i - 1 ] == symbol ) { i--; if ( flag & TRIM_ONE ) break; } if ( i < len ) str_setlen( ps, i ); } /* if flag & $TRIM_LEFT { uint cur = this.ptr() uint end = cur + *this while cur < end && cur->byte == symbol { cur++ if flag & $TRIM_ONE : break } if cur != this.ptr() : this.del( 0, cur - this.ptr()) } */ return ps; }
/** * Create a new formatting context for a header line. * * @param `field' is the header field name, without trailing ':'. * * @param `separator' is the optional default separator to emit between * the values added via header_fmd_append_value(). To supersede the * default separator, use header_fmd_append() and specify another separator * explicitly. If set to NULL, there will be no default separator and * values will be simply concatenated together. The value given must * NOT be freed before the header_fmt_end() call (usually it will just * be a static string). Trailing spaces in the separator will be stripped * if it is emitted at the end of a line before a continuation. * * @param `len_hint' is the expected line size, for pre-sizing purposes. * (0 to guess). * * @param `max_size' is the maximum header size, including the final "\r\n" * and the trailing NUL. If the initial field name is larger than the * configured maximum size, the header field will remain completely empty. * * @return pointer to the formatting object. */ header_fmt_t * header_fmt_make(const char *field, const char *separator, size_t len_hint, size_t max_size) { struct header_fmt *hf; g_assert(size_is_non_negative(len_hint)); WALLOC(hf); hf->magic = HEADER_FMT_MAGIC; hf->header = str_new(len_hint ? len_hint : HEADER_FMT_DFLT_LEN); hf->maxlen = HEADER_FMT_LINE_LEN; hf->data_emitted = FALSE; hf->frozen = FALSE; hf->max_size = max_size; hf->sep = atom_str_get(separator ? separator : ""); hf->seplen = strlen(hf->sep); hf->stripped_seplen = stripped_strlen(hf->sep, hf->seplen); str_cat(hf->header, field); STR_CAT(hf->header, ": "); hf->current_len = str_len(hf->header); /* * If right from the start the header would be larger than the configured * size, force it to stay empty. That means, the final string returned * will be "", the empty string. */ if (str_len(hf->header) + sizeof("\r\n") > hf->max_size) { hf->empty = TRUE; str_setlen(hf->header, 0); } else { hf->empty = FALSE; } header_fmt_check(hf); return hf; }
pstr STDCALL os_gettemp( void ) { uint diskc = 0; pstr temp; pstr ret; // ubyte temp[ 512 ]; // ubyte stemp[ 512 ]; // str_lenset( dir, 0 ); // str_isfree( dir, 512 ); /*#ifdef LINUX if ( !ggentee.tempfile ) { while( 1 ) { wsprintf( stemp, "/temp/gentee%02X.tmp", ggentee.tempid ); ggentee.tempfile = file_open( stemp, FOP_CREATE | FOP_EXCLUSIVE ); if ( ggentee.tempfile = -1 ) ggentee.tempid++; else break; } stemp[ mem_len( stemp ) - 4 ] = 0; // wsprintf( stemp, "%s\\gentee%02X", temp, ggentee.tempid ); mkdir( stemp, 700 ); ggentee.tempdir = str_new( 0, stemp ); } // str_appendp( dir, str_ptr( ggentee.tempdir )); #else*/ if ( !_gentee.tempfile ) { temp = str_new( NULL ); ret = str_new( NULL ); os_tempdir( temp ); while ( 1 ) { str_clear( ret ); str_printf( ret, "%s\\gentee%02X.tmp", str_ptr( temp ), _gentee.tempid ); _gentee.tempfile = os_fileopen( ret, FOP_CREATE | FOP_EXCLUSIVE ); if ( !_gentee.tempfile ) { if ( os_getattrib( ret ) == 0xFFFFFFFF ) if ( !diskc ) { str_copyzero( temp, "c:\\temp" ); os_dircreate( temp ); diskc = 1; } else msg( MFileopen | MSG_STR, ret ); _gentee.tempid++; } else break; } str_setlen( ret, str_len( ret ) - 4 ); os_dircreate( ret ); str_copy( &_gentee.tempdir, ret ); str_destroy( temp ); str_destroy( ret ); } //#endif return &_gentee.tempdir; }
/** * Append data `str' to the header line, atomically. * * @param `hf' no brief description. * @param `str' no brief description. * @param `separator' is an optional separator string that will be emitted * BEFORE outputting the data, and only when nothing has been emitted * already. * @param `slen' is the separator length, 0 if empty. * @param `sslen' is the stripped separator length, (size_t)-1 if unknown yet. * * @return TRUE if we were able to fit the string, FALSE if it would have * resulted in the header being larger than the configured max size (the * header line is left in the state it was in upon entry, in that case). */ static bool header_fmt_append_full(header_fmt_t *hf, const char *str, const char *separator, size_t slen, size_t sslen) { size_t len, curlen; gsize gslen; bool success; header_fmt_check(hf); g_assert(size_is_non_negative(slen)); g_assert((size_t)-1 == sslen || size_is_non_negative(sslen)); if (hf->empty) return FALSE; gslen = str_len(hf->header); len = strlen(str); curlen = hf->current_len; g_assert(size_is_non_negative(curlen)); g_assert(len <= INT_MAX); /* Legacy bug */ if ( size_saturate_add(curlen, size_saturate_add(len, slen)) > UNSIGNED(hf->maxlen) ) { /* * Emit sperator, if any and data was already emitted. */ if (separator != NULL && hf->data_emitted) { sslen = (size_t)-1 != sslen ? sslen : stripped_strlen(separator, slen); str_cat_len(hf->header, separator, sslen); } STR_CAT(hf->header, "\r\n\t"); /* Includes continuation */ curlen = 1; /* One tab */ } else if (hf->data_emitted) { str_cat(hf->header, separator); curlen += slen; } str_cat(hf->header, str); /* * Check for overflows, undoing string changes if needed. */ if (str_len(hf->header) + sizeof("\r\n") > hf->max_size) { success = FALSE; str_setlen(hf->header, gslen); /* Undo! */ } else { success = TRUE; hf->data_emitted = TRUE; hf->current_len = curlen + len; } g_assert(str_len(hf->header) + sizeof("\r\n") <= hf->max_size); return success; }
pstr STDCALL os_filefullname( pstr filename, pstr result ) { pubyte ptr; #ifdef LINUX //ubyte exename[512]; //ubyte proname[ 512 ]; pstr p_drive,p_path,p_fname,p_fext,p_slesh; ubyte ffn_drive[FILENAME_MAX+1]; ubyte ffn_path[PATH_MAX+1]; ubyte ffn_fname[FILENAME_MAX+1]; ubyte ffn_fext[FILENAME_MAX+1]; os_splitpath(str_ptr(filename),ffn_drive,ffn_path,ffn_fname,ffn_fext); p_slesh = str_new((pubyte)"/"); if (strlen(ffn_path)==0) { char cur[512]; getcwd( cur, 512 ); p_path = str_new(cur); str_add(p_path,p_slesh); } else p_path = str_new(ffn_path); p_fname = str_new(ffn_fname); p_fext = str_new(ffn_fext); str_add(result,p_path); str_add(result,p_fname); str_add(result,p_fext); str_destroy(p_path); str_destroy(p_slesh); str_destroy(p_fname); str_destroy(p_fext); /* char cur[512]; if ( filename[0] != '/' ) { getcwd( cur, 512 ); chdir( filename ); basename( filename ); getcwd( str_ptr( ( pstr )buf ), 512 ); str_setlen( ( pstr )buf, strlen( str_ptr( ( pstr )buf ) ) ); str_appendb( buf, '/' ); str_appendp( buf, filename ); chdir( cur ); } else { //Ìîæåò ðàáîòàòü íåêîððåêòíî åñëè â àáñîëþòíîì ïóòè âñòðå÷àþòñÿ '.' '..' str_appendp( buf, filename ); } */ #else uint len; str_reserve( result, 512 ); len = GetFullPathName(str_ptr(filename), 512, str_ptr(result), &ptr ); // äëÿ Windows str_setlen(result, len); #endif return result; }