CVS: libraries/mspgcc escape_decode.c,NONE,1.1 escape_encode.c,NONE,1.1 hex_encode.c,1.1,1.2 makefile,1.1,1.2

Chris Liechti <[email protected]>
Newsgroups gmane.comp.hardware.texas-instruments.msp430.gcc.cvs
Message-ID <[email protected]>
Update of /cvsroot/mspgcc/libraries/mspgcc
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27929/libraries/mspgcc

Modified Files:
	hex_encode.c makefile 
Added Files:
	escape_decode.c escape_encode.c 
Log Message:
- add string escape encode and decode functions
- fix fo hex_encode signature

--- NEW FILE: escape_decode.c ---
#include "mspgcc/util.h"

unsigned int escape_decode(void *dst, unsigned int maxsize, const char *src) {
    unsigned int output_size = 0;
    enum {M_PRINT, M_ESCAPE, M_HEX_DIGIT_1, M_HEX_DIGIT_0} mode = M_PRINT;
    for (unsigned int pos = 0; ((unsigned char*)src)[pos] && output_size < maxsize; pos++) {
        unsigned character = ((unsigned char*)src)[pos];
        switch (mode) {
            case M_PRINT:
                if (((unsigned char*)src)[pos] == '\\') {
                    mode = M_ESCAPE;
                } else {
                    ((unsigned char *)dst)[output_size++] = character;
                }
                break;
            case M_ESCAPE:
                switch (character) {
                    case '0':
                        ((unsigned char *)dst)[output_size++] = '\0';
                        break;
                    case 'n':
                        ((unsigned char *)dst)[output_size++] = '\n';
                        break;
                    case 'r':
                        ((unsigned char *)dst)[output_size++] = '\r';
                        break;
                    case 'a':
                        ((unsigned char *)dst)[output_size++] = '\a';
                        break;
                    case 't':
                        ((unsigned char *)dst)[output_size++] = '\t';
                        break;
                    case 'b':
                        ((unsigned char *)dst)[output_size++] = '\b';
                        break;
                    case '\\':
                        ((unsigned char *)dst)[output_size++] = '\\';
                        break;
                    case 'x':
                        mode = M_HEX_DIGIT_1;
                        break;
                    default:
                        mode = M_PRINT;
                        break;
                }
                break;
            case M_HEX_DIGIT_1:
                ((unsigned char *)dst)[output_size] = hex_fromdigit(character) << 4;
                mode = M_HEX_DIGIT_0;
                break;
            case M_HEX_DIGIT_0:
                ((unsigned char *)dst)[output_size++] |= hex_fromdigit(character);
                mode = M_PRINT;
                break;
        }
    }
    if (output_size < maxsize) ((unsigned char *)dst)[output_size] = '\0';
    return output_size;
}

--- NEW FILE: escape_encode.c ---
static const unsigned char HEX_DIGITS[16] = "0123456789ABCDEF";

unsigned int escape_encode(char *dst, unsigned int maxsize, const void *src, unsigned int size) {
    unsigned int output_size = 0;
    for (unsigned int pos = 0; pos < size && output_size < maxsize; pos++) {
        switch (((unsigned char*)src)[pos]) {
            case '\0':
                if (2+output_size >= maxsize) break;
                ((unsigned char *)dst)[output_size++] = '\\';
                ((unsigned char *)dst)[output_size++] = '0';
                break;
            case '\n':
                if (2+output_size >= maxsize) break;
                ((unsigned char *)dst)[output_size++] = '\\';
                ((unsigned char *)dst)[output_size++] = 'n';
                break;
            case '\r':
                if (2+output_size >= maxsize) break;
                ((unsigned char *)dst)[output_size++] = '\\';
                ((unsigned char *)dst)[output_size++] = 'r';
                break;
            case '\a':
                if (2+output_size >= maxsize) break;
                ((unsigned char *)dst)[output_size++] = '\\';
                ((unsigned char *)dst)[output_size++] = 'a';
                break;
            case '\t':
                if (2+output_size >= maxsize) break;
                ((unsigned char *)dst)[output_size++] = '\\';
                ((unsigned char *)dst)[output_size++] = 't';
                break;
            case '\b':
                if (2+output_size >= maxsize) break;
                ((unsigned char *)dst)[output_size++] = '\\';
                ((unsigned char *)dst)[output_size++] = 'b';
                break;
            case '\\':
                if (2+output_size >= maxsize) break;
                ((unsigned char *)dst)[output_size++] = '\\';
                ((unsigned char *)dst)[output_size++] = '\\';
                break;
                
            default:
                if (((unsigned char*)src)[pos] < 0x1f           //other control characters
                || ((unsigned char*)src)[pos] == 127) {         //DEL
                    if (4+output_size >= maxsize) break;
                    ((unsigned char *)dst)[output_size++] = '\\';
                    ((unsigned char *)dst)[output_size++] = 'x';
                    ((unsigned char *)dst)[output_size++] = HEX_DIGITS[((unsigned char*)src)[pos] >> 4];
                    ((unsigned char *)dst)[output_size++] = HEX_DIGITS[((unsigned char*)src)[pos] & 0xf];
                    } else {
                    ((unsigned char *)dst)[output_size++] = ((unsigned char*)src)[pos];
                }
                break;
        }
    }
    if (output_size < maxsize) ((unsigned char *)dst)[output_size++] = '\0';
    return output_size;
}

Index: hex_encode.c
===================================================================
RCS file: /cvsroot/mspgcc/libraries/mspgcc/hex_encode.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -d -r1.1 -r1.2
--- hex_encode.c	22 Aug 2005 11:23:20 -0000	1.1
+++ hex_encode.c	13 Sep 2005 14:09:43 -0000	1.2
@@ -1,7 +1,7 @@
 
 const unsigned char HEX_DIGITS[16] = "0123456789ABCDEF";
 
-unsigned int hex_encode(char *dst, unsigned int maxsize, const void *src, int size) {
+unsigned int hex_encode(char *dst, unsigned int maxsize, const void *src, unsigned int size) {
     unsigned int pos = 0;
     unsigned int count = 0;
     while ((maxsize > 2) && (pos < size)) {

Index: makefile
===================================================================
RCS file: /cvsroot/mspgcc/libraries/mspgcc/makefile,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -d -r1.1 -r1.2
--- makefile	22 Aug 2005 11:23:20 -0000	1.1
+++ makefile	13 Sep 2005 14:09:43 -0000	1.2
@@ -4,6 +4,7 @@
                   ringbuffer_put.c ringbuffer_get.c ringbuffer_clear.c ringbuffer_len.c \
                   flash_erase_segment.c flash_write_byte.c flash_write_word.c \
                   hex_decode_inline.c hex_decode.c hex_encode.c hex_fromdigit.c \
+                  escape_encode.c escape_decode.c \
                   event_scheduler.c
 ASOURCES        = eventhandler.S eventhandler_idle.S delay.S
 CPU             = msp1



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.