Changeset 525


Ignore:
Timestamp:
09/25/08 04:19:39 (5 years ago)
Author:
reyalp
Message:
  • lua print: truncate messages longer than 127, don't use tostring for numbers, use spaces instead of tabs between values
  • make script console a circular buffer.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/script.c

    r524 r525  
    7474char script_params[SCRIPT_NUM_PARAMS][28]; 
    7575int script_param_order[SCRIPT_NUM_PARAMS]; 
    76 char script_params_update[SCRIPT_NUM_PARAMS]; 
    77 int script_loaded_params[SCRIPT_NUM_PARAMS]; 
    78 char script_console_buf[SCRIPT_CONSOLE_NUM_LINES][SCRIPT_CONSOLE_LINE_LENGTH+1]; 
    79 static int script_console_lines=0; 
     76static char script_params_update[SCRIPT_NUM_PARAMS]; 
     77static int script_loaded_params[SCRIPT_NUM_PARAMS]; 
     78static char script_console_buf[SCRIPT_CONSOLE_NUM_LINES][SCRIPT_CONSOLE_LINE_LENGTH+1]; 
     79static int script_con_start_line=0; // oldest valid line in console 
     80static int script_con_num_lines=0; // number of valid lines 
     81 
    8082//------------------------------------------------------------------- 
    8183static void process_title(const char *title) { 
     
    393395        script_console_buf[i][0]=0; 
    394396    } 
    395     script_console_lines=0; 
     397    script_con_num_lines=script_con_start_line=0; 
    396398    draw_restore(); 
    397399} 
    398400 
    399401//------------------------------------------------------------------- 
     402static inline int script_con_line_index(int i) { 
     403    return i%SCRIPT_CONSOLE_NUM_LINES; 
     404} 
     405 
    400406void script_console_draw() { 
    401     register int i, l; 
    402     static char buf[8]; 
    403  
    404     for (i=0; i<script_console_lines; ++i) { 
     407    int i,c,l; 
     408    for(c = 0; c < script_con_num_lines; ++c) { 
     409        i=script_con_line_index(script_con_start_line+c); 
    405410        l=strlen(script_console_buf[i]); 
    406         draw_txt_string(SCRIPT_CONSOLE_X, SCRIPT_CONSOLE_Y+SCRIPT_CONSOLE_NUM_LINES-script_console_lines+i, script_console_buf[i], MAKE_COLOR(COLOR_BG, COLOR_FG)); 
     411        draw_txt_string(SCRIPT_CONSOLE_X, SCRIPT_CONSOLE_Y+SCRIPT_CONSOLE_NUM_LINES-script_con_num_lines+c, script_console_buf[i], MAKE_COLOR(COLOR_BG, COLOR_FG)); 
    407412        for (; l<SCRIPT_CONSOLE_LINE_LENGTH; ++l) 
    408             draw_txt_char(SCRIPT_CONSOLE_X+l, SCRIPT_CONSOLE_Y+SCRIPT_CONSOLE_NUM_LINES-script_console_lines+i, ' ', MAKE_COLOR(COLOR_BG, COLOR_FG)); 
    409     } 
    410  
     413            draw_txt_char(SCRIPT_CONSOLE_X+l, SCRIPT_CONSOLE_Y+SCRIPT_CONSOLE_NUM_LINES-script_con_num_lines+c, ' ', MAKE_COLOR(COLOR_BG, COLOR_FG)); 
     414    } 
    411415} 
    412416 
     
    457461 
    458462//------------------------------------------------------------------- 
    459  
    460 static void script_console_add_impl(const char *str) { 
    461     register int i; 
    462  
    463     if (script_console_lines == SCRIPT_CONSOLE_NUM_LINES ) { 
    464         for (i=1; i<SCRIPT_CONSOLE_NUM_LINES; ++i) { 
    465             strcpy(script_console_buf[i-1], script_console_buf[i]); 
    466         } 
    467         --script_console_lines; 
    468     } 
    469  
    470     if (strlen(str) > SCRIPT_CONSOLE_LINE_LENGTH) { 
    471       // let overlong lines wrap to the next line 
    472       memcpy(script_console_buf[script_console_lines], str, SCRIPT_CONSOLE_LINE_LENGTH); 
    473       script_console_buf[script_console_lines][SCRIPT_CONSOLE_LINE_LENGTH]=0; 
    474       ++script_console_lines; 
    475       script_console_add_impl(str+SCRIPT_CONSOLE_LINE_LENGTH); 
    476       return; 
    477     } 
    478     else 
    479       strcpy(script_console_buf[script_console_lines], str); 
    480      
    481     ++script_console_lines; 
     463void script_console_start_line() { 
     464    if (SCRIPT_CONSOLE_NUM_LINES==script_con_num_lines) { 
     465        script_con_start_line=script_con_line_index(script_con_start_line+1); 
     466    } 
     467    else { 
     468        ++script_con_num_lines; 
     469    } 
     470     script_console_buf[script_con_line_index(script_con_start_line+script_con_num_lines-1)][0]=0; 
     471} 
     472 
     473void script_console_add_text(const char *str) { 
     474    char *cur; 
     475    int curlen; 
     476    int left; 
     477 
     478    do { 
     479        cur = script_console_buf[script_con_line_index(script_con_start_line+script_con_num_lines-1)]; 
     480        curlen = strlen(cur); 
     481        left = SCRIPT_CONSOLE_LINE_LENGTH-curlen; 
     482        if(strlen(str) > left) { 
     483            strncpy(cur+curlen,str,left); 
     484            cur[SCRIPT_CONSOLE_LINE_LENGTH]=0; 
     485            script_console_start_line(); 
     486            str+=left; 
     487        } 
     488        else { 
     489            strcat(cur,str); 
     490            break; 
     491        } 
     492    } while(1); 
    482493} 
    483494 
    484495void script_console_add_line(const char *str) { 
    485     script_console_add_impl(str); 
    486  
     496    script_console_start_line(); 
     497    script_console_add_text(str); 
    487498    if (print_screen_p && print_screen_d>=0) { 
    488       char nl = '\n'; 
    489       write(print_screen_d, str, strlen(str) ); 
    490       write(print_screen_d, &nl, 1); 
    491     } 
    492  
     499        char nl = '\n'; 
     500        write(print_screen_d, str, strlen(str) ); 
     501        write(print_screen_d, &nl, 1); 
     502    } 
    493503    script_console_draw(); 
    494504} 
    495  
    496 //------------------------------------------------------------------- 
    497 //------------------------------------------------------------------- 
    498 //------------------------------------------------------------------- 
     505//------------------------------------------------------------------- 
     506//------------------------------------------------------------------- 
     507//------------------------------------------------------------------- 
  • trunk/lib/lua/lbaselib.c

    r515 r525  
    3434  lua_getglobal(L, "tostring"); 
    3535  char buf[128]; 
     36  const int max_buf_chars=sizeof(buf)-1; 
     37  char numbuf[16]; 
    3638  buf[0] = 0; 
     39  int buf_chars = 0; 
    3740  for (i=1; i<=n; i++) { 
    3841    const char *s; 
    39     lua_pushvalue(L, -1);  /* function to be called */ 
    40     lua_pushvalue(L, i);   /* value to print */ 
    41     lua_call(L, 1, 1); 
    42     s = lua_tostring(L, -1);  /* get result */ 
    43     if (s == NULL) 
    44       return luaL_error(L, LUA_QL("tostring") " must return a string to " 
    45                            LUA_QL("print")); 
    46     if (i>1) strcat( buf, "\t" ); 
    47     strcat( buf, s ); 
    48     lua_pop(L, 1);  /* pop result */ 
     42    /* avoid calling tostring on numbers, so we don't 
     43     generate new string for each unique number */ 
     44    if(lua_type(L,i) == LUA_TNUMBER) { 
     45      sprintf(numbuf,LUA_NUMBER_FMT,lua_tonumber(L,i)); 
     46      s=numbuf; 
     47    } 
     48    else { 
     49      lua_pushvalue(L, -1);  /* function to be called */ 
     50      lua_pushvalue(L, i);   /* value to print */ 
     51      lua_call(L, 1, 1); 
     52      s = lua_tostring(L, -1);  /* get result */ 
     53      if (s == NULL) 
     54        return luaL_error(L, LUA_QL("tostring") " must return a string to " 
     55                          LUA_QL("print")); 
     56      lua_pop(L, 1);  /* pop result */ 
     57    } 
     58    if(i>1) { 
     59      strcpy(buf+buf_chars," "); 
     60      ++buf_chars; 
     61    } 
     62    if(buf_chars+strlen(s) >= max_buf_chars) { 
     63      strncpy(buf+buf_chars,s,max_buf_chars-buf_chars); 
     64      buf[max_buf_chars]=0; 
     65      break; 
     66    } 
     67    strcpy(buf+buf_chars,s); 
     68    buf_chars = strlen(buf); 
     69    if(buf_chars >= max_buf_chars-1) // -1 allow for space 
     70       break; 
     71 
    4972  } 
    5073  script_console_add_line(buf); 
     74 
    5175  return 0; 
    5276} 
Note: See TracChangeset for help on using the changeset viewer.