Changeset 2024


Ignore:
Timestamp:
07/30/12 12:00:06 (11 months ago)
Author:
tsv
Message:

tsvstar-ui:

  • Stability fixes in some core functions
  • "Show HELP" fix - no more hangup if no file
  • More rewrite of script to extendability/readability
  • Format & name convention of data files are reverted to back compatibility. For a while
Location:
branches/tsvstar-uitest
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • branches/tsvstar-uitest/core/gui.c

    r2007 r2024  
    797797        strcpy( tgt, str ); 
    798798 
    799         // replce extension 
     799        // replace extension 
    800800        str = strrchr( tgt, '.' ); 
    801801        if ( !str ) { str = tgt+strlen(tgt); } 
    802802        strcpy(  str, ext ); 
    803  
    804                  
    805803} 
    806804 
     
    824822                copy_fname_w_new_ext( helpfile_name, conf.lang_file, ".hlp"); 
    825823 
     824                if ( is_file_exists( helpfile_name) > 0 ) 
     825                        if ( module_run("txtread.flt", 0, sizeof(argv)/sizeof(argv[0]), argv, UNLOAD_IF_ERR) == 0 ) 
     826                                return 1; 
     827        } 
     828 
     829        // if not success, try to load "_base_language_.hlp" 
     830        copy_fname_w_new_ext( helpfile_name, gui_lang_source_filename, ".hlp"); 
     831        if ( is_file_exists( helpfile_name) > 0 ) 
     832                if ( module_run("txtread.flt", 0, sizeof(argv)/sizeof(argv[0]), argv, UNLOAD_IF_ERR) == 0 ) 
     833                        return 1; 
     834 
     835        //if not success, try to load "english.hlp" 
     836        argv[0] = (unsigned int)"A/CHDK/HELP/ENGLISH.HLP"; 
     837        if ( is_file_exists( (const char*)argv[0] ) >0 ) { 
    826838                if ( module_run("txtread.flt", 0, sizeof(argv)/sizeof(argv[0]), argv, UNLOAD_IF_ERR) == 0 ) 
    827839                        return 1; 
    828840        } 
    829  
    830         // if not success, try to load "_base_language_.hlp" 
    831         copy_fname_w_new_ext( helpfile_name, gui_lang_source_filename, ".hlp"); 
    832         if ( module_run("txtread.flt", 0, sizeof(argv)/sizeof(argv[0]), argv, UNLOAD_IF_ERR) == 0 ) 
    833                 return 1; 
    834  
    835         //if not success, try to load "english.hlp" 
    836         argv[0] = (unsigned int)"A/CHDK/HELP/ENGLISH.HLP"; 
    837         if ( module_run("txtread.flt", 0, sizeof(argv)/sizeof(argv[0]), argv, UNLOAD_IF_ERR) == 0 ) 
    838                 return 1; 
    839841 
    840842        return 0; 
  • branches/tsvstar-uitest/core/module_wrappers.c

    r1975 r2024  
    2828 
    2929  if ( pStat==0 ) return 1; 
     30 
     31  // sanity check. canon firmware hangup if start not from 'A/' 
     32  if ( !name || (name[0] | 0x20)!='a' || name[1]!='/' ) return 1; 
     33 
    3034  memset( pStat, 0, sizeof(struct STD_stat)); 
    3135  rv =  stat( name, &st ); 
  • branches/tsvstar-uitest/core/script.c

    r2007 r2024  
    1 // TODO: 
    2 //   check how _set function works and improve it 
    3 //   temporary script runing 
    4 //  script_params_update -> mean is such value listed in valuelist 
    5 //      script_named_strings[n]/script_named_count[n] -> this is for enum 
    6  
    71#include "stdlib.h" 
    82#include "keyboard.h" 
     
    2721#endif 
    2822 
     23// Requested filename 
     24enum FilenameMakeModeEnum  
     25        {       MAKE_PARAMSETNUM_FILENAME,              // "DATA/scriptname.cfg" -> cfg_name 
     26                MAKE_PARAM_FILENAME };                  // "DATA/scriptname_%d" -> cfg_param_name 
     27 
    2928//------------------------------------------------------------------- 
    3029 
    3130long kbd_last_clicked; 
    3231const char *script_source_str=NULL; // ptr to content of script 
    33 char cfg_name[100] = "\0";          // buffer to make string "DATAPATH/scriptname.cfg" 
    34 char cfg_set_name[100] = "\0";      // buffer to make string "DATAPATH/scriptname_PARAMSET" 
     32char cfg_name[100] = "\0";          // buffer to make cfg files name (paramsetnum, param_names) 
     33char cfg_param_name[100] = "\0";    // buffer to make cfg param files name (params, state_before_tmprun) 
    3534 
    3635static const char *lua_script_default = 
     
    131130const char* skip_whitespace(const char* p)  { while (*p==' ' || *p=='\t') p++; return p; }  // Skip past whitespace 
    132131const char* skip_token(const char* p)       { while (*p && *p!='\r' && *p!='\n' && *p!=' ' && *p!='\t') p++; return p; }  // Skip past current token value 
    133 const char* skip_toeol(const char* p)       { while (*p!='\r' && *p!='\n') p++; return p; } // Skip to end of line 
     132const char* skip_toeol(const char* p)       { while (*p && *p!='\r' && *p!='\n') p++; return p; } // Skip to end of line 
    134133 
    135134//======================================================= 
     
    365364 
    366365//------------------------------------------------------------------- 
    367 // PURPOSE:     Create filename of paramset 
    368 // PARAMETERS:  fn - full path of script 
    369 //              param_set - >=0 = num of paramset, <0 = made .cfg 
    370 // RESULT:  name at cfg_set_name or cfg_name (depending on param_set) 
    371 //------------------------------------------------------------------- 
    372 void set_params_values_name(const char *fn, int param_set) 
    373 { 
    374     int shift; 
    375     register char *ptr = (param_set >= 0 ? cfg_set_name : cfg_name); 
    376     const char *name; 
    377      
    378     if (fn == NULL || fn[0] == 0) { ptr[0] = 0; return; } 
    379      
    380     strncpy(ptr, SCRIPT_DATA_PATH, 100); ptr[99]=0; 
    381     shift = strlen(SCRIPT_DATA_PATH); 
    382     name = strrchr(fn, '/');  
    383     if (name) name++; else name=fn; 
    384     strncpy(ptr+shift, name, 100-shift); ptr[99]=0; 
    385     shift = strlen(ptr); if (shift >= 100) shift=99; 
    386  
    387     if (param_set >= 0)  
    388         sprintf(ptr+shift-3, "_%d\0", param_set); 
    389     else 
    390         strcpy(ptr+shift-3, "cfg\0"); 
     366// PURPOSE:     Create cfg filename in buffer. 
     367// PARAMETERS:  mode - what exact kind of cfg file name required 
     368//                              fn - full path of script  (optional. have no matter for some modes) 
     369//                              paramset - target paramset (optional) 
     370// RESULT:  name at cfg_param_name or cfg_name (depending on mode) 
     371//------------------------------------------------------------------- 
     372void make_param_filename( enum FilenameMakeModeEnum mode, const char* fn, int paramset ) 
     373{ 
     374        char extbuf[5]; 
     375        char* tgt_buf; 
     376        char* base_path; 
     377        char* name = 0; 
     378         
     379        // find name of script 
     380    if (fn && fn[0] )  
     381{ 
     382            name = strrchr( fn, '/' );  
     383        if (name) name++; else name=(char*)fn; 
     384        } 
     385 
     386        // prepare base data to make 
     387        switch ( mode ) { 
     388                case MAKE_PARAMSETNUM_FILENAME: 
     389                        tgt_buf = cfg_name; 
     390                        base_path = SCRIPT_DATA_PATH; 
     391                        strcpy(extbuf,".set"); 
     392                        break;                   
     393                case MAKE_PARAM_FILENAME: 
     394                        tgt_buf   = cfg_param_name; 
     395                        base_path = SCRIPT_DATA_PATH; 
     396                        sprintf(extbuf,"_%d",paramset); 
     397                        break;                   
     398                default:                // unknown mode 
     399                        return; 
     400        } 
     401     
     402        // make path 
     403        strcpy(tgt_buf, base_path); 
     404     
     405        // add script filename 
     406        char* tgt_name=tgt_buf+strlen(tgt_buf); 
     407        strncpy( tgt_name, name, 12 ); 
     408        tgt_name[12] = 0; 
     409 
     410        // find where extension start and replace it 
     411        char* ext = strrchr(tgt_name, '.'); 
     412        if (!ext) ext=tgt_name+strlen(tgt_name); 
     413        strcpy ( ext, extbuf ); 
    391414} 
    392415 
     
    399422    if (fn == NULL || fn[0] == 0) return; 
    400423 
    401     set_params_values_name(fn, -1); 
     424    make_param_filename( MAKE_PARAMSETNUM_FILENAME, fn, 0); 
    402425        if ( !load_int_value_file( cfg_name, &conf.script_param_set ) ) 
    403426           conf.script_param_set = 0; 
    404     set_params_values_name(fn, conf.script_param_set); 
     427        make_param_filename( MAKE_PARAM_FILENAME, fn, conf.script_param_set); 
    405428} 
    406429 
     
    414437int load_params_values(const char *fn, int paramset) 
    415438{ 
     439        if ( paramset>=0 ) { 
    416440        // skip if internal script used 
    417441    if (fn == NULL || fn[0] == 0) return 0; 
    418442     
    419         if ( paramset>=0 ) 
    420             set_params_values_name(fn, paramset); 
     443                conf.script_param_set = paramset; 
     444                make_param_filename( MAKE_PARAM_FILENAME, fn, paramset ); 
     445        } 
    421446 
    422447        int size; 
    423         char* buf = load_file(cfg_set_name, &size); 
     448        char* buf = load_file( cfg_param_name, &size); 
    424449    if(!buf) 
    425450        return 0; 
    426451 
    427         script_apply_param_string(buf,0); 
     452        int i; 
     453        char *ptr; 
     454    for(i = 0; i < SCRIPT_NUM_PARAMS; ++i) script_params_update[i]=0; 
     455    ptr = buf; 
     456 
     457    while (ptr[0])  
     458    { 
     459        while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces 
     460        if (ptr[0]=='@') 
     461        { 
     462            if (strncmp("@param", ptr, 6) == 0)  
     463            { 
     464                ptr+=6; 
     465                process_param(ptr, 1); 
     466            } else if (strncmp("@default", ptr, 8)==0) { 
     467                ptr+=8; 
     468                process_default(ptr, 1); 
     469            } else if (strncmp("@range", ptr, 6)==0) { 
     470                ptr+=6; 
     471                process_range(ptr, 1); 
     472            } else if (strncmp("@values", ptr, 7)==0) { 
     473                ptr+=7; 
     474                process_values(ptr, 0); 
     475            } 
     476                        /*      // this will reqruired to temporary scripts infrastructure 
     477                                // only in data files (never in script) 
     478                                // not implemented yet 
     479                                else if (strncmp("@load_script", ptr, 12)==0) { 1;} 
     480                                else if (strncmp("@load_paramset", ptr, 14)==0) { 1;} 
     481                        */ 
     482        } 
     483        while (ptr[0] && ptr[0]!='\n') ++ptr; // unless end of line 
     484        if (ptr[0]) ++ptr; 
     485    } 
     486 
    428487    ufree(buf); 
    429488 
    430489    return 1; 
     490} 
     491 
     492 
     493//------------------------------------------------------------------- 
     494// PURPOSE:     Auxilary function. 
     495//                              Actually save param file 
     496// PARAMETERS:  fn = name of target file (actually always cfg_param_name) 
     497//                              script_file = name if need to restore path 
     498//                              paramset = num of paramset, if it should be restored 
     499//------------------------------------------------------------------- 
     500static void do_save_param_file( char* fn, char* script_file, int paramset ) 
     501{ 
     502    int i,n, fd; 
     503    char *buf; 
     504 
     505    buf=umalloc(250); 
     506    if( buf ) 
     507        { 
     508        fd = open(fn, O_WRONLY|O_CREAT, 0777); 
     509        if (fd >=0) 
     510                { 
     511                        ///// OLD FORMAT OF DATA FILE//// 
     512 
     513                        // store filename and current paramset 
     514                        if (script_file) { 
     515                                sprintf( buf, "@script_file %s\n", script_file ); 
     516                                write( fd, buf, strlen(buf) ); 
     517                        } 
     518                        if ( paramset >0 ) { 
     519                                sprintf( buf, "@load_paramset %d\n", paramset ); 
     520                                write( fd, buf, strlen(buf) ); 
     521                        } 
     522 
     523                for(n = 0; n < SCRIPT_NUM_PARAMS; ++n) 
     524                { 
     525                    if (script_params[n][0] != 0) 
     526                    { 
     527                        sprintf(buf,"@param %c %s\n@default %c %d\n",'a'+n,script_params[n],'a'+n,conf.script_vars[n]); 
     528                        if (script_range_values[n] != 0) 
     529                            sprintf(buf+strlen(buf),"@range %c %d %d\n",'a'+n,(short)(script_range_values[n]&0xFFFF),(short)(script_range_values[n]>>16)); 
     530                        if (script_named_counts[n] != 0) 
     531                        { 
     532                            sprintf(buf+strlen(buf),"@values %c",'a'+n); 
     533                            for (i=0; i<script_named_counts[n]; i++) 
     534                                sprintf(buf+strlen(buf)," %s",script_named_values[n][i]); 
     535                            strcat(buf,"\n"); 
     536                        } 
     537                                write(fd, buf, strlen(buf)); 
     538                                } 
     539                } 
     540                 
     541                close(fd); 
     542                } 
     543                         
     544                ufree(buf); 
     545        } 
    431546} 
    432547 
     
    445560void save_params_values( int enforce ) 
    446561{ 
    447     int i, fd, changed=0; 
    448     char *buf; 
     562    int i, changed=0; 
    449563 
    450564        // Check is anything changed since last time 
     
    457571    if (!enforce && !changed) return; 
    458572 
     573        // Write paramsetnum file 
    459574    if (cfg_name[0] == 0) 
    460                 set_params_values_name(conf.script_file, -1); 
    461  
    462         // Write paramsetnum file 
    463  
     575            make_param_filename( MAKE_PARAMSETNUM_FILENAME, conf.script_file, 0); 
    464576    save_int_value_file( cfg_name, conf.script_param_set ); 
    465577     
    466578    // Write parameters file 
    467579     
    468     set_params_values_name(conf.script_file, conf.script_param_set); 
    469  
    470     buf=umalloc(50); 
    471     if( buf ) 
    472         { 
    473         fd = open(cfg_set_name, O_WRONLY|O_CREAT, 0777); 
    474         if (fd >=0) 
    475                 { 
    476                     for(i = 0; i < SCRIPT_NUM_PARAMS; ++i) 
    477                 { 
    478                         if (script_params[i][0] != 0) 
    479                         { 
    480                                         sprintf( buf, ",%c=%d", ('a'+i), conf.script_vars[i] ); 
    481                                         write( fd, buf, strlen(buf) ); 
    482                         } 
    483                 } 
    484                 close(fd); 
    485                 } 
    486  
    487         ufree(buf); 
    488         } 
     580        make_param_filename( MAKE_PARAM_FILENAME, conf.script_file, conf.script_param_set); 
     581        do_save_param_file( cfg_param_name, 0, -1); 
    489582} 
    490583 
     
    498591//     allow_paramset_load = 1-apply paramset load if exists, 0-ignore it 
    499592//-------------------------------------------------------------------------- 
    500 void script_apply_param_string( char* param_str, int allow_load_paramset ) 
     593void script_apply_paramstr( char* param_str, int allow_load_paramset ) 
    501594{ 
    502595        int n; 
     
    509602                if (*ptr>='0' && *ptr<='9' ) { 
    510603                        load_params_values( conf.script_file, (*ptr-'0') ); 
    511                 set_params_values_name( conf.script_file, conf.script_param_set ); 
     604                        make_param_filename( MAKE_PARAM_FILENAME, conf.script_file, conf.script_param_set); 
    512605                } 
    513606        } 
    514607 
     608        // main list parsing 
    515609        for( ; *ptr; ptr++ ) { 
    516610 
     
    590684    } 
    591685 
    592  
    593686        fn = conf.script_file; 
    594  
    595687 
    596688        switch ( saved_params ) 
    597689        { 
    598       /* This is obsolete - use reset_to_default_values() 
    599                 case 0: 
     690                case SCRIPT_LOAD_DEFAULT_VALUES: 
    600691                        // reset to default 
    601692                    script_scan( fn, 1 ); 
    602693                        break; 
    603        */ 
    604694 
    605695                case SCRIPT_LOAD_LAST_PARAMSET:         // load last paramset ( if script was changed ) 
     
    702792    // needs to finish. So keep the script marked as running, but don't 
    703793    // call any more scripting functions. 
    704     state_kbd_script_run = 3; 
     794    state_kbd_script_run = SCRIPT_STATE_PENDING; 
    705795} 
    706796 
     
    708798{   // Note: This function is called from an action stack for AS_SCRIPT_RUN. 
    709799 
    710     if (state_kbd_script_run != 3) { 
     800    if (state_kbd_script_run != SCRIPT_STATE_PENDING) { 
    711801#ifdef OPT_LUA 
    712802        if( L ) { 
     
    819909#endif 
    820910        default: 
    821             if (!action_stack_standard(p) && !state_kbd_script_run) 
     911            if (!action_stack_standard(p) && state_kbd_script_run==SCRIPT_STATE_INACTIVE ) 
    822912            { 
    823913                /*finished();*/ 
     
    859949    if (kbd_is_key_clicked(KEY_SHOOT_FULL)) 
    860950    { 
    861         if (state_kbd_script_run == 2 || state_kbd_script_run == 3) 
     951        if (state_kbd_script_run == SCRIPT_STATE_INTERRUPTED || state_kbd_script_run == SCRIPT_STATE_PENDING ) 
    862952            interrupt_script(); 
    863953#ifdef OPT_LUA 
    864954        else if (L) 
    865955        { 
    866             state_kbd_script_run = 2; 
     956            state_kbd_script_run = SCRIPT_STATE_INTERRUPTED; 
    867957            lua_run_restore(); 
    868958            interrupt_script(); 
     
    872962        else 
    873963        { 
    874             state_kbd_script_run = 2; 
     964            state_kbd_script_run = SCRIPT_STATE_INTERRUPTED; 
    875965            if (jump_label("restore") == 0) 
    876966                interrupt_script(); 
     
    9301020        shot_histogram_set(0); 
    9311021    kbd_key_release_all(); 
    932     state_kbd_script_run = 0; 
     1022    state_kbd_script_run = SCRIPT_STATE_INACTIVE; 
    9331023 
    9341024    conf_update_prevent_shutdown(); 
     
    10031093    } 
    10041094 
    1005     state_kbd_script_run = 1; 
     1095    state_kbd_script_run = SCRIPT_STATE_RAN; 
    10061096 
    10071097    conf_update_prevent_shutdown(); 
     
    10161106{ 
    10171107  if (!lua_script_start(script,1)) return -1; 
    1018   state_kbd_script_run = 1; 
     1108  state_kbd_script_run = SCRIPT_STATE_RAN; 
    10191109  kbd_set_block(1); 
    10201110  auto_started = 0; 
  • branches/tsvstar-uitest/include/conf.h

    r1999 r2024  
    400400extern void user_menu_restore(); 
    401401 
     402// Current stage of script processing 
    402403extern int state_kbd_script_run; 
     404 
     405enum {  SCRIPT_STATE_INACTIVE=0,  // 0 - script is inactive now 
     406                SCRIPT_STATE_RAN,             // 1 - script works now 
     407                SCRIPT_STATE_INTERRUPTED, // 2 - shutter button was pressed. wait for second press 
     408                SCRIPT_STATE_PENDING };   // 3 - final housekeep processing 
     409 
     410 
    403411extern int state_shooting_progress; 
    404412extern int state_save_raw_nth_only; 
  • branches/tsvstar-uitest/include/lang.h

    r2007 r2024  
    88extern void lang_map_preparsed_from_mem( char* gui_lang_default, int num ); 
    99 
     10extern char* lang_str(int str); 
     11extern unsigned lang_strhash31(int langid); 
     12 
     13//------------------------------------------------------------------- 
     14 
     15// fileutil.c functions 
     16 
    1017typedef int (*callback_process_file)(char* buf, int size); 
    1118extern int load_from_file(const char *filename, callback_process_file callback) ; 
     
    1320extern char* load_file_to_cached( const char* name, int* rv_size ); 
    1421 
    15 int load_int_value_file( char* filename, int* value_p ); 
    16 void save_int_value_file( char* filename, int value ); 
     22extern int load_int_value_file( char* filename, int* value_p ); 
     23extern void save_int_value_file( char* filename, int value ); 
    1724 
    18 extern char* lang_str(int str); 
    19 extern unsigned lang_strhash31(int langid); 
     25extern int is_file_exists(const char* fn);                  // 0-not exist, otherwise exist (=-1 mean empty, =1 mean nonempty) 
    2026 
    2127//------------------------------------------------------------------- 
     28 
     29 
    2230#endif 
  • branches/tsvstar-uitest/include/script.h

    r2007 r2024  
    2828 
    2929//------------------------------------------------------------------- 
    30 enum ScriptLoad_Mode_ { SCRIPT_LOAD_LAST_PARAMSET = 1, SCRIPT_UPDATE_PARAMS }; 
     30enum ScriptLoad_Mode_ { SCRIPT_LOAD_DEFAULT_VALUES = 0, 
     31                                                SCRIPT_LOAD_LAST_PARAMSET, 
     32                                                SCRIPT_UPDATE_PARAMS }; 
    3133 
    3234extern void script_load(const char *fn, enum ScriptLoad_Mode_ saved_params); 
    33 extern void script_apply_param_string( char* param_str, int allow_paramset_load ); 
     35extern void script_apply_paramstr( char* param_str, int allow_load_paramset ); 
    3436extern int load_params_values(const char *fn, int paramset ); 
    3537extern void save_params_values(int enforce); 
    36 void script_reset_to_default_params_values(); 
     38extern void script_reset_to_default_params_values(); 
    3739 
    3840extern void script_console_add_line(const char *str); 
  • branches/tsvstar-uitest/include/stdlib_unified.h

    r1975 r2024  
    11#ifndef STDLIB_UNIFIED_H 
    2 #define STDLIB_UNIFIEDH 
     2#define STDLIB_UNIFIED_H 
    33 
    44// This is header of unified wrapper platform-dependend io functions 
  • branches/tsvstar-uitest/lib/lang/Makefile

    r1975 r2024  
    55CFLAGS+=$(CTHUMB) 
    66 
    7 OBJS = lang.o 
     7OBJS = lang.o fileutil.o 
    88 
    99all: liblang.a 
  • branches/tsvstar-uitest/lib/lang/lang.c

    r2007 r2024  
    123123} 
    124124 
    125 // PURPOSE: 
    126 // Universal file loader: alloc space and load file 
    127 // RETURN: 
    128 //  pointer to loaded file (0 if fail) and loaded size  
    129 //                      into rv_size (-1 if file not exists) 
    130 //------------------------------------------------------------------- 
    131 char* load_file( const char* name, int* rv_size ) 
    132 { 
    133         int fd; 
    134         int size = -1; 
    135         char* buf; 
    136  
    137     if ( strlen(name) ) { 
    138                 struct stat st; 
    139         if ( stat(name,&st) == 0 ) 
    140                          size = st.st_size; 
    141     } 
    142  
    143         if ( rv_size ) 
    144                 *rv_size = size; 
    145  
    146         if ( size<=0 ) 
    147         return 0; 
    148  
    149         fd = open( name, O_RDONLY, 0777 ); 
    150         if ( fd <=0 ) 
    151         return 0; 
    152  
    153         buf = umalloc(size+1); 
    154         if ( buf==0 ) { 
    155                 close(fd); 
    156                 return 0; 
    157         } 
    158      
    159     size = read(fd, buf, size ); 
    160         buf[size+1]=0; 
    161         close(fd); 
    162  
    163         if ( rv_size ) 
    164                 *rv_size = size; 
    165         return buf; 
    166 } 
    167  
    168  
    169 // Completely same as load_file but place result to cached memory 
    170 //------------------------------------------------------------------- 
    171 char* load_file_to_cached( const char* name, int* rv_size ) 
    172 { 
    173         int size; 
    174  
    175         char* buf = load_file( name, &size ); 
    176         if ( rv_size ) *rv_size = size; 
    177  
    178         if ( !buf ) 
    179                 return 0; 
    180  
    181         char* buf_cached = malloc( size+1 ); 
    182         if ( !buf_cached ) 
    183         { 
    184                 if (rv_size) *rv_size=-1; 
    185                 ufree(buf); 
    186                 return 0; 
    187         } 
    188  
    189         memcpy( buf_cached, buf, size+1 ); 
    190         ufree(buf); 
    191  
    192         return buf_cached; 
    193 } 
    194  
    195 // PURPOSE: 
    196 // Universal file processor 
    197 // Load file, process by callback, unalloc/close file 
    198 // RETURN: 
    199 //       Transfer return value from callback 
    200 // NOTE: 
    201 //       Call callback even if fail to load/malloc (size=-1 if no file, size=0 if empty)  
    202 //------------------------------------------------------------------- 
    203 int load_from_file(const char *filename, callback_process_file callback) 
    204 { 
    205     int size; 
    206  
    207     char *buf = load_file( filename, &size); 
    208  
    209         size = callback( buf, size ); 
    210  
    211         if ( buf ) 
    212                 ufree(buf); 
    213  
    214         return size; 
    215 } 
    216  
    217125//------------------------------------------------------------------- 
    218126void lang_load_from_file(const char *filename) { 
     
    245153        return hash; 
    246154} 
    247  
    248 //============================================ 
    249 //              DIFFERENT UTILITY FUNCTIONS 
    250 //============================================ 
    251  
    252 //------------------------------------------------------------------- 
    253  
    254 // load content to *value_p if file exist and contain number 
    255 // return: 0-file_not_exist_or_failed (value is not changed), 1-ok 
    256  
    257 int load_int_value_file( char* filename, int* value_p ) 
    258 { 
    259         int tmp; 
    260         char *buf; 
    261  
    262         buf=load_file( filename, &tmp ); 
    263         if ( !buf ) 
    264            return 0; 
    265          
    266         *value_p = strtol(buf, NULL, 10 /*dec*/); 
    267         ufree(buf); 
    268  
    269         return 1; 
    270 } 
    271  
    272 //------------------------------------------------------------------- 
    273  
    274 // save integer "value" to text file with name "filename" 
    275  
    276 void save_int_value_file( char* filename, int value ) 
    277 { 
    278         char* buf = umalloc(20); 
    279         if ( !buf ) 
    280                 return; 
    281  
    282         sprintf(buf,"%d", value); 
    283  
    284         int fd = open( filename, O_WRONLY|O_CREAT, 0777); 
    285         if (fd>=0)  
    286         { 
    287                 write(fd, buf, strlen(buf)); 
    288                 close(fd); 
    289         }  
    290         ufree(buf); 
    291 } 
Note: See TracChangeset for help on using the changeset viewer.