[ create a new paste ] login | about

Link: http://codepad.org/XslS1XIf    [ raw code | output | fork ]

dslc - C, pasted on Jun 18:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char *s = "INSERT INTO sample VALUES('1', 'Am G'haubach');";
    char *t = NULL, *targetbuf = NULL;
    size_t len = 0, padding = 32;

    len = strlen(s);
    // Destination buffer
    targetbuf = malloc(sizeof(char) * len + padding);
    if (!targetbuf) {
        perror("malloc");
        exit(1);
    }
    // Point to base of target buf ...
    int n_quotes = 0;
    t = targetbuf;
    while (*s) {
        switch (*s) {
            case '\'':
                // Examine the next character in an attempt
                // to determine if this is a 'string context'
                if (*(s + 1) != ',' && *(s + 1) != ')' && n_quotes % 2 == 1) {
                    // We are more than likely inside a string ...
                    *(t++) = '\'';
                    *(t++) = '\'';
                } else {
                    *(t++) = *s;
                    n_quotes++;
                }
                break;
            default:
                *(t++) = *s;
                break;
        }
        s++;
    }
    *t = '\0';
    puts(targetbuf);

    free(targetbuf);

    return 0;
}


Output:
1
INSERT INTO sample VALUES('1', 'Am G''haubach');


Create a new paste based on this one


Comments: