octstr.c

来自「The Kannel Open Source WAP and SMS gatew」· C语言 代码 · 共 2,586 行 · 第 1/5 页

C
2,586
字号
}int octstr_case_search(const Octstr *haystack, const Octstr *needle, long pos){    long i, j;    int c1, c2;    seems_valid(haystack);    seems_valid(needle);    gw_assert(pos >= 0);    /* Always "find" an empty string */    if (needle->len == 0)        return 0;    for (i = pos; i <= haystack->len - needle->len; ++i) {	for (j = 0; j < needle->len; ++j) {	    c1 = toupper(haystack->data[i + j]);	    c2 = toupper(needle->data[j]);	    if (c1 != c2)	    	break;	}	if (j == needle->len)	    return i;    }    return -1;    }int octstr_case_nsearch(const Octstr *haystack, const Octstr *needle, long pos, long n){    long i, j;    int c1, c2;    seems_valid(haystack);    seems_valid(needle);    gw_assert(pos >= 0);    /* Always "find" an empty string */    if (needle->len == 0)        return 0;    for (i = pos; i <= haystack->len - needle->len && i < n; ++i) {        for (j = 0; j < needle->len && j < n; ++j) {            c1 = toupper(haystack->data[i + j]);            c2 = toupper(needle->data[j]);            if (c1 != c2)                break;        }        if (j == needle->len)            return i;    }    return -1;}int octstr_print(FILE *f, Octstr *ostr){    gw_assert(f != NULL);    seems_valid(ostr);    if (ostr->len == 0)        return 0;    if (fwrite(ostr->data, ostr->len, 1, f) != 1) {        error(errno, "Couldn't write all of octet string to file.");        return -1;    }    return 0;}int octstr_pretty_print(FILE *f, Octstr *ostr){    unsigned char *p;    long i;    gw_assert(f != NULL);    seems_valid(ostr);    p = ostr->data;    for (i = 0; i < ostr->len; ++i, ++p) {        if (isprint(*p))            fprintf(f, "%c", *p);        else            fprintf(f, "\\x%02x", *p);    }    if (ferror(f))        return -1;    return 0;}int octstr_write_to_socket(int socket, Octstr *ostr){    long len;    unsigned char *data;    int ret;    gw_assert(socket >= 0);    seems_valid(ostr);    data = ostr->data;    len = ostr->len;    while (len > 0) {        ret = write(socket, data, len);        if (ret == -1) {            if (errno != EINTR) {                error(errno, "Writing to socket failed");                return -1;            }        } else {            /* ret may be less than len */            len -= ret;            data += ret;        }    }    return 0;}long octstr_write_data(Octstr *ostr, int fd, long from){    long ret;    gw_assert(fd >= 0);    gw_assert(from >= 0);    seems_valid(ostr);    if (from >= ostr->len)        return 0;    ret = write(fd, ostr->data + from, ostr->len - from);    if (ret < 0) {        if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)            return 0;        error(errno, "Error writing %ld octets to fd %d:",              ostr->len - from, fd);        return -1;    }    return ret;}int octstr_append_from_socket(Octstr *ostr, int socket){    unsigned char buf[4096];    int len;    seems_valid(ostr);    gw_assert(!ostr->immutable);again:    len = recv(socket, buf, sizeof(buf), 0);    if (len < 0 && errno == EINTR)        goto again;    if (len < 0) {        error(errno, "Could not read from socket %d", socket);        return -1;    }    octstr_append_data(ostr, buf, len);    return len;}void octstr_insert(Octstr *ostr1, const Octstr *ostr2, long pos){    if (ostr2 == NULL)        return;    seems_valid(ostr1);    seems_valid(ostr2);    gw_assert(pos <= ostr1->len);    gw_assert(!ostr1->immutable);    if (ostr2->len == 0)        return;    octstr_grow(ostr1, ostr1->len + ostr2->len);    memmove(ostr1->data + pos + ostr2->len, ostr1->data + pos,            ostr1->len - pos);    memcpy(ostr1->data + pos, ostr2->data, ostr2->len);    ostr1->len += ostr2->len;    ostr1->data[ostr1->len] = '\0';    seems_valid(ostr1);}void octstr_truncate(Octstr *ostr, int new_len){    seems_valid(ostr);    gw_assert(!ostr->immutable);    gw_assert(new_len >= 0);    if (new_len >= ostr->len)        return;    ostr->len = new_len;    ostr->data[new_len] = '\0';    seems_valid(ostr);}void octstr_strip_blanks(Octstr *text){    int start = 0, end, len = 0;    seems_valid(text);    gw_assert(!text->immutable);    /* Remove white space from the beginning of the text */    while (isspace(octstr_get_char(text, start)) && 	   start <= octstr_len(text))        start ++;    if (start > 0)        octstr_delete(text, 0, start);    /* and from the end. */    if ((len = octstr_len(text)) > 0) {        end = len = len - 1;        while (isspace(octstr_get_char(text, end)) && end >= 0)            end--;        octstr_delete(text, end + 1, len - end);    }    seems_valid(text);}static int iscrlf(unsigned char c){    return c == '\n' || c == '\r';}void octstr_strip_crlfs(Octstr *text){    int start = 0, end, len = 0;    seems_valid(text);    gw_assert(!text->immutable);    /* Remove white space from the beginning of the text */    while (iscrlf(octstr_get_char(text, start)) && 	   start <= octstr_len(text))        start ++;    if (start > 0)        octstr_delete(text, 0, start);    /* and from the end. */    if ((len = octstr_len(text)) > 0) {        end = len = len - 1;        while (iscrlf(octstr_get_char(text, end)) && end >= 0)            end--;        octstr_delete(text, end + 1, len - end);    }    seems_valid(text);}void octstr_strip_nonalphanums(Octstr *text){    int start = 0, end, len = 0;    seems_valid(text);    gw_assert(!text->immutable);    /* Remove white space from the beginning of the text */    while (!isalnum(octstr_get_char(text, start)) && 	   start <= octstr_len(text))        start ++;    if (start > 0)        octstr_delete(text, 0, start);    /* and from the end. */    if ((len = octstr_len(text)) > 0) {        end = len = len - 1;        while (!isalnum(octstr_get_char(text, end)) && end >= 0)            end--;        octstr_delete(text, end + 1, len - end);    }    seems_valid(text);}void octstr_shrink_blanks(Octstr *text){    int i, j, end;    seems_valid(text);    gw_assert(!text->immutable);    end = octstr_len(text);    /* Shrink white spaces to one  */    for (i = 0; i < end; i++) {        if (isspace(octstr_get_char(text, i))) {            /* Change the remaining space into single space. */            if (octstr_get_char(text, i) != ' ')                octstr_set_char(text, i, ' ');            j = i = i + 1;            while (isspace(octstr_get_char(text, j)))                j ++;            if (j - i > 1)                octstr_delete(text, i, j - i);        }    }    seems_valid(text);}void octstr_insert_data(Octstr *ostr, long pos, const char *data, long len){    seems_valid(ostr);    gw_assert(!ostr->immutable);    gw_assert(pos <= ostr->len);    if (len == 0)        return;    octstr_grow(ostr, ostr->len + len);    if (ostr->len > pos) {	/* only if neccessary*/        memmove(ostr->data + pos + len, ostr->data + pos, ostr->len - pos);    }    memcpy(ostr->data + pos, data, len);    ostr->len += len;    ostr->data[ostr->len] = '\0';    seems_valid(ostr);}void octstr_insert_char(Octstr *ostr, long pos, const char c){    seems_valid(ostr);    gw_assert(!ostr->immutable);    gw_assert(pos <= ostr->len);        octstr_grow(ostr, ostr->len + 1);    if (ostr->len > pos)        memmove(ostr->data + pos + 1, ostr->data + pos, ostr->len - pos);    memcpy(ostr->data + pos, &c, 1);    ostr->len += 1;    ostr->data[ostr->len] = '\0';        seems_valid(ostr);}void octstr_append_data(Octstr *ostr, const char *data, long len){    gw_assert(ostr != NULL);    octstr_insert_data(ostr, ostr->len, data, len);}void octstr_append(Octstr *ostr1, const Octstr *ostr2){    gw_assert(ostr1 != NULL);    octstr_insert(ostr1, ostr2, ostr1->len);}void octstr_append_cstr(Octstr *ostr, const char *cstr){    octstr_insert_data(ostr, ostr->len, cstr, strlen(cstr));}void octstr_append_char(Octstr *ostr, int ch){    unsigned char c = ch;    gw_assert(ch >= 0);    gw_assert(ch <= UCHAR_MAX);    octstr_insert_data(ostr, ostr->len, &c, 1);}void octstr_delete(Octstr *ostr1, long pos, long len){    seems_valid(ostr1);    gw_assert(!ostr1->immutable);    if (pos > ostr1->len)        pos = ostr1->len;    if (pos + len > ostr1->len)        len = ostr1->len - pos;    if (len > 0) {        memmove(ostr1->data + pos, ostr1->data + pos + len,                ostr1->len - pos - len);        ostr1->len -= len;        ostr1->data[ostr1->len] = '\0';    }    seems_valid(ostr1);}Octstr *octstr_read_file(const char *filename){    FILE *f;    Octstr *os;    char buf[4096];    long n;    gw_assert(filename != NULL);    f = fopen(filename, "r");    if (f == NULL) {        error(errno, "fopen failed: couldn't open `%s'", filename);        return NULL;    }    os = octstr_create("");    if (os == NULL)        goto error;    while ((n = fread(buf, 1, sizeof(buf), f)) > 0)        octstr_insert_data(os, octstr_len(os), buf, n);    (void) fclose(f);    return os;error:    (void) fclose(f);    octstr_destroy(os);    return NULL;}Octstr *octstr_read_pipe(FILE *f){    Octstr *os;    char buf[4096];    gw_assert(f != NULL);    os = octstr_create("");    if (os == NULL)        goto error;    while (fgets(buf, sizeof(buf), f) != NULL)        octstr_append_data(os, buf, strlen(buf));    return os;error:    octstr_destroy(os);    return NULL;}List *octstr_split_words(const Octstr *ostr){    unsigned char *p;    List *list;    Octstr *word;    long i, start, end;    seems_valid(ostr);    list = list_create();    p = ostr->data;    i = 0;    for (; ; ) {        while (i < ostr->len && isspace(*p)) {            ++p;            ++i;        }        start = i;        while (i < ostr->len && !isspace(*p)) {            ++p;            ++i;        }        end = i;        if (start == end)            break;        word = octstr_create_from_data(ostr->data + start,                                       end - start);        list_append(list, word);    }    return list;}List *octstr_split(const Octstr *os, const Octstr *sep){    List *list;    long next, pos, seplen;        list = list_create();    pos = 0;    seplen = octstr_len(sep);    while ((next = octstr_search(os, sep, pos)) != -1) {	list_append(list, octstr_copy(os, pos, next - pos));	pos = next + seplen;    }    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?