Changeset 8b5001b in mainline


Ignore:
Timestamp:
2009-12-01T20:07:06Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
fd34f4e
Parents:
da2bd08 (diff), e866806 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/string.h

    rda2bd08 r8b5001b  
    8787extern void str_cpy(char *dest, size_t size, const char *src);
    8888extern void str_ncpy(char *dest, size_t size, const char *src, size_t n);
    89 extern void wstr_nstr(char *dst, const wchar_t *src, size_t size);
     89extern void wstr_to_str(char *dest, size_t size, const wchar_t *src);
    9090
    9191extern char *str_chr(const char *str, wchar_t ch);
  • kernel/generic/src/console/kconsole.c

    rda2bd08 r8b5001b  
    289289                       
    290290                        char tmp[STR_BOUNDS(MAX_CMDLINE)];
    291                         wstr_nstr(tmp, current + beg, position - beg + 1);
     291                        wstr_to_str(tmp, position - beg + 1, current + beg);
    292292                       
    293293                        int found;
     
    665665               
    666666                char cmdline[STR_BOUNDS(MAX_CMDLINE)];
    667                 wstr_nstr(cmdline, tmp, STR_BOUNDS(MAX_CMDLINE));
     667                wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp);
    668668               
    669669                if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
  • kernel/generic/src/lib/string.c

    rda2bd08 r8b5001b  
    537537 * null-terminated and containing only complete characters.
    538538 *
    539  * @param dst   Destination buffer.
     539 * @param dest   Destination buffer.
    540540 * @param count Size of the destination buffer (must be > 0).
    541541 * @param src   Source string.
     
    571571 * have to be null-terminated.
    572572 *
    573  * @param dst   Destination buffer.
     573 * @param dest   Destination buffer.
    574574 * @param count Size of the destination buffer (must be > 0).
    575575 * @param src   Source string.
     
    596596}
    597597
    598 /** Copy NULL-terminated wide string to string
    599  *
    600  * Copy source wide string @a src to destination buffer @a dst.
    601  * No more than @a size bytes are written. NULL-terminator is always
    602  * written after the last succesfully copied character (i.e. if the
    603  * destination buffer is has at least 1 byte, it will be always
    604  * NULL-terminated).
    605  *
    606  * @param src   Source wide string.
    607  * @param dst   Destination buffer.
    608  * @param count Size of the destination buffer.
    609  *
    610  */
    611 void wstr_nstr(char *dst, const wchar_t *src, size_t size)
    612 {
    613         /* No space for the NULL-terminator in the buffer */
    614         if (size == 0)
    615                 return;
    616        
     598/** Convert wide string to string.
     599 *
     600 * Convert wide string @a src to string. The output is written to the buffer
     601 * specified by @a dest and @a size. @a size must be non-zero and the string
     602 * written will always be well-formed.
     603 *
     604 * @param dest  Destination buffer.
     605 * @param size  Size of the destination buffer.
     606 * @param src   Source wide string.
     607 */
     608void wstr_to_str(char *dest, size_t size, const wchar_t *src)
     609{
    617610        wchar_t ch;
    618         size_t src_idx = 0;
    619         size_t dst_off = 0;
     611        size_t src_idx;
     612        size_t dest_off;
     613
     614        /* There must be space for a null terminator in the buffer. */
     615        ASSERT(size > 0);
     616
     617        src_idx = 0;
     618        dest_off = 0;
    620619       
    621620        while ((ch = src[src_idx++]) != 0) {
    622                 if (chr_encode(ch, dst, &dst_off, size) != EOK)
     621                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    623622                        break;
    624623        }
    625        
    626         if (dst_off >= size)
    627                 dst[size - 1] = 0;
    628         else
    629                 dst[dst_off] = 0;
     624
     625        dest[dest_off] = '\0';
    630626}
    631627
  • uspace/app/bdsh/input.c

    rda2bd08 r8b5001b  
    134134static char *tinput_get_str(tinput_t *ti)
    135135{
    136         char *str;
    137 
    138         str = malloc(STR_BOUNDS(ti->nc) + 1);
    139         if (str == NULL)
    140                 return NULL;
    141 
    142         wstr_nstr(str, ti->buffer, STR_BOUNDS(ti->nc) + 1);
    143 
    144         return str;
     136        return wstr_to_astr(ti->buffer);
    145137}
    146138
  • uspace/app/edit/edit.c

    rda2bd08 r8b5001b  
    3636
    3737#include <stdio.h>
     38#include <stdlib.h>
    3839#include <sys/types.h>
    3940#include <vfs/vfs.h>
     
    101102#define ED_INFTY 65536
    102103
     104/** Maximum filename length that can be entered. */
     105#define INFNAME_MAX_LEN 128
     106
    103107static void key_handle_unmod(console_event_t const *ev);
    104108static void key_handle_ctrl(console_event_t const *ev);
    105109static int file_save(char const *fname);
     110static void file_save_as(void);
    106111static int file_insert(char *fname);
    107112static int file_save_range(char const *fname, spt_t const *spos,
    108113    spt_t const *epos);
     114static char *filename_prompt(char const *prompt, char const *init_value);
    109115static void pane_text_display(void);
    110116static void pane_row_display(void);
     
    150156
    151157        if (argc == 2) {
    152                 doc.file_name = argv[1];
     158                doc.file_name = str_dup(argv[1]);
    153159        } else if (argc > 1) {
    154160                printf("Invalid arguments.\n");
    155161                return -2;
    156162        } else {
    157                 doc.file_name = "/edit.txt";
     163                doc.file_name = NULL;
    158164        }
    159165
    160166        new_file = false;
    161167
    162         if (file_insert(doc.file_name) != EOK)
     168        if (doc.file_name == NULL || file_insert(doc.file_name) != EOK)
    163169                new_file = true;
    164170
     
    170176        pane_text_display();
    171177        pane_status_display();
    172         if (new_file)
    173                 status_display("File not found. Created empty file.");
     178        if (new_file && doc.file_name != NULL)
     179                status_display("File not found. Starting empty file.");
    174180        pane_caret_display();
    175181
     
    266272                break;
    267273        case KC_S:
    268                 (void) file_save(doc.file_name);
     274                if (doc.file_name != NULL)
     275                        file_save(doc.file_name);
     276                else
     277                        file_save_as();
     278                break;
     279        case KC_E:
     280                file_save_as();
    269281                break;
    270282        default:
     
    272284        }
    273285}
    274 
    275286
    276287/** Save the document. */
     
    285296
    286297        rc = file_save_range(fname, &sp, &ep);
    287         status_display("File saved.");
     298
     299        switch (rc) {
     300        case EINVAL:
     301                status_display("Error opening file!");
     302                break;
     303        case EIO:
     304                status_display("Error writing data!");
     305                break;
     306        default:
     307                status_display("File saved.");
     308                break;
     309        }
    288310
    289311        return rc;
     312}
     313
     314/** Change document name and save. */
     315static void file_save_as(void)
     316{
     317        char *old_fname, *fname;
     318        int rc;
     319
     320        old_fname = (doc.file_name != NULL) ? doc.file_name : "";
     321        fname = filename_prompt("Save As", old_fname);
     322        if (fname == NULL) {
     323                status_display("Save cancelled.");
     324                return;
     325        }
     326
     327        rc = file_save(fname);
     328        if (rc != EOK)
     329                return;
     330
     331        if (doc.file_name != NULL)
     332                free(doc.file_name);
     333        doc.file_name = fname;
     334}
     335
     336/** Ask for a file name. */
     337static char *filename_prompt(char const *prompt, char const *init_value)
     338{
     339        console_event_t ev;
     340        char *str;
     341        wchar_t buffer[INFNAME_MAX_LEN + 1];
     342        int max_len;
     343        int nc;
     344        bool done;
     345
     346        asprintf(&str, "%s: %s", prompt, init_value);
     347        status_display(str);
     348        console_goto(con, 1 + str_length(str), scr_rows - 1);
     349        free(str);
     350
     351        console_set_color(con, COLOR_WHITE, COLOR_BLACK, 0);
     352
     353        max_len = min(INFNAME_MAX_LEN, scr_columns - 4 - str_length(prompt));
     354        str_to_wstr(buffer, max_len + 1, init_value);
     355        nc = wstr_length(buffer);
     356        done = false;
     357
     358        while (!done) {
     359                console_get_event(con, &ev);
     360
     361                if (ev.type == KEY_PRESS) {
     362                        /* Handle key press. */
     363                        if (((ev.mods & KM_ALT) == 0) &&
     364                             (ev.mods & KM_CTRL) != 0) {
     365                                ;
     366                        } else if ((ev.mods & (KM_CTRL | KM_ALT)) == 0) {
     367                                switch (ev.key) {
     368                                case KC_ESCAPE:
     369                                        return NULL;
     370                                case KC_BACKSPACE:
     371                                        if (nc > 0) {
     372                                                putchar('\b');
     373                                                fflush(stdout);
     374                                                --nc;
     375                                        }
     376                                        break;
     377                                case KC_ENTER:
     378                                        done = true;
     379                                        break;
     380                                default:
     381                                        if (ev.c >= 32 && nc < max_len) {
     382                                                putchar(ev.c);
     383                                                fflush(stdout);
     384                                                buffer[nc++] = ev.c;
     385                                        }
     386                                        break;
     387                                }
     388                        }
     389                }
     390        }
     391
     392        buffer[nc] = '\0';
     393        str = wstr_to_astr(buffer);
     394
     395        console_set_color(con, COLOR_BLACK, COLOR_WHITE, 0);
     396
     397        return str;
    290398}
    291399
     
    359467        } while (!spt_equal(&bep, epos));
    360468
    361         fclose(f);
     469        if (fclose(f) != EOK)
     470                return EIO;
    362471
    363472        return EOK;
     
    473582        spt_t caret_pt;
    474583        coord_t coord;
     584        char *fname;
    475585        int n;
    476586
     
    478588        spt_get_coord(&caret_pt, &coord);
    479589
     590        fname = (doc.file_name != NULL) ? doc.file_name : "<unnamed>";
     591
    480592        console_goto(con, 0, scr_rows - 1);
    481593        console_set_color(con, COLOR_WHITE, COLOR_BLACK, 0);
    482         n = printf(" %d, %d: File '%s'. Ctrl-S Save  Ctrl-Q Quit",
    483             coord.row, coord.column, doc.file_name);
     594        n = printf(" %d, %d: File '%s'. Ctrl-Q Quit  Ctrl-S Save  "
     595            "Ctrl-E Save As", coord.row, coord.column, fname);
    484596        printf("%*s", scr_columns - 1 - n, "");
    485597        fflush(stdout);
  • uspace/lib/libc/generic/string.c

    rda2bd08 r8b5001b  
    471471 * null-terminated and containing only complete characters.
    472472 *
    473  * @param dst   Destination buffer.
     473 * @param dest   Destination buffer.
    474474 * @param count Size of the destination buffer (must be > 0).
    475475 * @param src   Source string.
     
    505505 * have to be null-terminated.
    506506 *
    507  * @param dst   Destination buffer.
     507 * @param dest   Destination buffer.
    508508 * @param count Size of the destination buffer (must be > 0).
    509509 * @param src   Source string.
     
    537537 * null-terminated and containing only complete characters.
    538538 *
    539  * @param dst   Destination buffer.
     539 * @param dest   Destination buffer.
    540540 * @param count Size of the destination buffer.
    541541 * @param src   Source string.
     
    549549}
    550550
    551 /** Copy NULL-terminated wide string to string
    552  *
    553  * Copy source wide string @a src to destination buffer @a dst.
    554  * No more than @a size bytes are written. NULL-terminator is always
    555  * written after the last succesfully copied character (i.e. if the
    556  * destination buffer is has at least 1 byte, it will be always
    557  * NULL-terminated).
    558  *
    559  * @param src   Source wide string.
    560  * @param dst   Destination buffer.
    561  * @param count Size of the destination buffer.
    562  *
    563  */
    564 void wstr_nstr(char *dst, const wchar_t *src, size_t size)
    565 {
    566         /* No space for the NULL-terminator in the buffer */
    567         if (size == 0)
    568                 return;
    569        
     551/** Convert wide string to string.
     552 *
     553 * Convert wide string @a src to string. The output is written to the buffer
     554 * specified by @a dest and @a size. @a size must be non-zero and the string
     555 * written will always be well-formed.
     556 *
     557 * @param dest  Destination buffer.
     558 * @param size  Size of the destination buffer.
     559 * @param src   Source wide string.
     560 */
     561void wstr_to_str(char *dest, size_t size, const wchar_t *src)
     562{
    570563        wchar_t ch;
    571         size_t src_idx = 0;
    572         size_t dst_off = 0;
    573        
     564        size_t src_idx;
     565        size_t dest_off;
     566
     567        /* There must be space for a null terminator in the buffer. */
     568        assert(size > 0);
     569       
     570        src_idx = 0;
     571        dest_off = 0;
     572
    574573        while ((ch = src[src_idx++]) != 0) {
    575                 if (chr_encode(ch, dst, &dst_off, size) != EOK)
     574                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    576575                        break;
    577576        }
    578        
    579         if (dst_off >= size)
    580                 dst[size - 1] = 0;
    581         else
    582                 dst[dst_off] = 0;
    583 }
     577
     578        dest[dest_off] = '\0';
     579}
     580
     581/** Convert wide string to new string.
     582 *
     583 * Convert wide string @a src to string. Space for the new string is allocated
     584 * on the heap.
     585 *
     586 * @param src   Source wide string.
     587 * @return      New string.
     588 */
     589char *wstr_to_astr(const wchar_t *src)
     590{
     591        char dbuf[STR_BOUNDS(1)];
     592        char *str;
     593        wchar_t ch;
     594
     595        size_t src_idx;
     596        size_t dest_off;
     597        size_t dest_size;
     598
     599        /* Compute size of encoded string. */
     600
     601        src_idx = 0;
     602        dest_size = 0;
     603
     604        while ((ch = src[src_idx++]) != 0) {
     605                dest_off = 0;
     606                if (chr_encode(ch, dbuf, &dest_off, STR_BOUNDS(1)) != EOK)
     607                        break;
     608                dest_size += dest_off;
     609        }
     610
     611        str = malloc(dest_size + 1);
     612        if (str == NULL)
     613                return NULL;
     614
     615        /* Encode string. */
     616
     617        src_idx = 0;
     618        dest_off = 0;
     619
     620        while ((ch = src[src_idx++]) != 0) {
     621                if (chr_encode(ch, str, &dest_off, dest_size) != EOK)
     622                        break;
     623        }
     624
     625        str[dest_size] = '\0';
     626        return str;
     627}
     628
    584629
    585630/** Convert string to wide string.
    586631 *
    587632 * Convert string @a src to wide string. The output is written to the
    588  * buffer specified by @a dest and @a size, which must have non-zero
    589  * size. The output will always be null-terminated.
     633 * buffer specified by @a dest and @a dlen. @a dlen must be non-zero
     634 * and the wide string written will always be null-terminated.
    590635 *
    591636 * @param dest  Destination buffer.
  • uspace/lib/libc/include/string.h

    rda2bd08 r8b5001b  
    7373extern void str_append(char *dest, size_t size, const char *src);
    7474
    75 extern void wstr_nstr(char *dst, const wchar_t *src, size_t size);
     75extern void wstr_to_str(char *dest, size_t size, const wchar_t *src);
     76extern char *wstr_to_astr(const wchar_t *src);
    7677extern void str_to_wstr(wchar_t *dest, size_t dlen, const char *src);
    7778
Note: See TracChangeset for help on using the changeset viewer.