midpstring.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 725 行 · 第 1/2 页

C
725
字号
    int index = 0;    if (line.len <= 0) {        return -1;    }    while ((index < line.len) && (*(line.data+index) != c)) {        index++;    }    if (index == line.len) { /* c not found */        return -1;    }    return index;} /* end of midpStringIndexOf *//** * removes white spaces from the end of string *  * @param line   string to trim * @return Trimmed string and error code */MidpString midpStringTrimFromEnd(MidpString line) {    if (line.len <= 0) {        line.len = BAD_PARAMS;        return line;    }    while (IS_WHITE_SPACE((line.data+(line.len-1))) && (line.len > 0)) {        *(line.data+(line.len-1)) = 0x00;        line.len--;    }    return line;}/* end of midpStringTrimFromEnd *//** * Removes white spaces from the both ends of a MidpString. * <p> * Memory allocation done here, even if the result is the same as the input. * <p> * This function should not be used directly, * use the midpNewString macro. *  * @param line   MidpString to handle * @param filename provided by the midpStringTrim macro * @param line provided by the midpStringTrim macro * * @return new string */MidpString midpStringTrimImpl(MidpString in, char* filename, int line) {    int i;    int end;    int begin;    if (in.len <= 0) {        return NULL_MIDP_STRING;    }    for (i = 0; i < in.len; i++) {        if (!(IS_CHAR_WHITE_SPACE(in.data[i]))) {            break;        }    }    begin = i;    end = in.len;    for (i = in.len; i > begin; i--) {        if (!(IS_CHAR_WHITE_SPACE(in.data[i - 1]))) {            break;        }    }    end = i;    return midpSubStringImpl(in, begin, end, filename, line);}/* end of midpStringTrimImpl *//** * Converts integer to MidpString in decimal format. * Memory allocation done here. *  * @param inputInt integer to convert * @return MidpString representation of the inputInt */MidpString midpStringValueOfInt(int inputInt) {    int base = 10;    int neg = 0;    int len = 0;    jchar buffer[21];    jchar *pstr = buffer;    MidpString res = {0, NULL};    if (inputInt < 0) {        neg = 1;        inputInt*=(-1);    }    do {        *pstr++ = ((inputInt % base)+'0');        inputInt = inputInt/base;        len++;    } while (inputInt > 0);    pstr--;    if (neg) {        *++pstr = '-';        len++;    }    res.data = (jchar*)midpMalloc((len+1)*sizeof(jchar));    if (!res.data) {        return res;    }    memset(res.data,0,(len+1)*sizeof(jchar));    res.len = len;    base = 0;    while (len-- > 0) {        *(res.data+base++) = *pstr--;    }    return res;} /* midpStringValueOfInt *//** * Converts MidpString representation of decimal integer to integer. *  * @param str    MidpString representation of integer * @return integer value of str */int midpIntegerParseInt(MidpString str) {    int res = 0;    int td = 1;    jchar* p = str.data;    if (str.len <= 0) {        return NUMBER_ERROR;    }    p+=str.len-1;    while (p >= str.data) {        if ((*p >= '0') && (*p <= '9')) { /* range between 0 to 9 */            res += ((*p)-'0')*td;            td*=10;        } else {            return NUMBER_ERROR;        }        p--;    } /* end of while */    return res;} /* midpIntegerParseInt *//** * Frees a string list. * * @param pStrings point to an array of suites * @param numberOfStrings number of elements in pStrings */voidfreeStringList(MidpString* pStrings, int numberOfStrings) {    int i;    if (pStrings == NULL) {        return;    }    for (i = 0; i < numberOfStrings; i++) {        midpFree(pStrings[i].data);    }    midpFree(pStrings);}voidfree_pcsl_string_list(pcsl_string* pStrings, int numberOfStrings) {    int i;    if (pStrings == NULL) {        return;    }    for (i = 0; i < numberOfStrings; i++) {        pcsl_string_free(&pStrings[i]);    }    midpFree(pStrings);}/** * Allocate an array of pcsl_string's. * Initialize the strings with PCSL_STRING_NULL. * * @param numberOfStrings desired array size * @return address of the array, or NULL if out-of-memory error happened */pcsl_string* alloc_pcsl_string_list(int numberOfStrings){    pcsl_string* res = (pcsl_string*)midpMalloc(numberOfStrings * sizeof (pcsl_string));    if (NULL != res) {        int i;        for(i=0; i<numberOfStrings; i++) {            res[i] = PCSL_STRING_NULL;        }    }    return res;}/** * Create pcsl_string from the specified MidpString. * The caller is responsible for freeing the created string when done. * * @param midpString pointer to the MidpString instance * @param pcslString pointer to the pcsl_string instance * @return status of the operation */pcsl_string_status midp_string_to_pcsl_string(const MidpString * midpString,					      pcsl_string * pcslString) {  if (pcslString == NULL) {    return PCSL_STRING_EINVAL;  }   if (NULL == midpString) {    *pcslString = PCSL_STRING_NULL;    return PCSL_STRING_EINVAL;  } else if(NULL == midpString->data) {    *pcslString = PCSL_STRING_NULL;    return PCSL_STRING_OK;  } else {    const jsize length = midpString->len;    if (length > 0) {      return pcsl_string_convert_from_utf16(midpString->data, midpString->len, 					    pcslString);    } else if (length == 0) {      *pcslString = PCSL_STRING_EMPTY;    } else {      /* need revisit: what we should return in this case. */      /* A negative length means a run-time error. */      *pcslString = PCSL_STRING_NULL;    }    return PCSL_STRING_OK;  }}/** * Create MidpString from the specified pcsl_string. * The caller is responsible for freeing the created string when done. * * @param pcslString pointer to the pcsl_string instance * @param midpString pointer to the MidpString instance * @return status of the operation */pcsl_string_status midp_string_from_pcsl_string(const pcsl_string * pcslString,						MidpString * midpString) {  if (midpString == NULL || pcslString == NULL) {    return PCSL_STRING_EINVAL;  } else if (pcsl_string_is_null(pcslString)) {      * midpString = NULL_MIDP_STRING;      return PCSL_STRING_OK;  } else {    const jsize length = pcsl_string_utf16_length(pcslString);    if (length == 0) {      * midpString = EMPTY_MIDP_STRING;      return PCSL_STRING_OK;    } else {      pcsl_string_status status = PCSL_STRING_OK;      /* Reserve space for terminating zero */      const jsize buffer_length = length + 1;      jchar * buffer = pcsl_mem_malloc(buffer_length * sizeof(jchar));      if (buffer == NULL) {        return PCSL_STRING_ENOMEM;      }      status = pcsl_string_convert_to_utf16(pcslString,                                            buffer, buffer_length, NULL);      if (status != PCSL_STRING_OK) {        * midpString = NULL_MIDP_STRING;        pcsl_mem_free(buffer);      } else {        midpString->data = buffer;        midpString->len = length;      }      return status;    }  }}/** * Allocate a buffer of jchars with the text from the specified pcsl_string. * The caller is responsible for freeing the buffer with midpFree when done. * * @param pcslString pointer to the pcsl_string instance * @param pBuf receives the allocated buffer * @param pBufLen receives the size (in jchars) of the buffer * @return status of the operation */pcsl_string_status text_buffer_from_pcsl_string(const pcsl_string * pcslString,						jchar * * pBuf, int * pBufLen) {    *pBuf = NULL;    *pBufLen = -1;    if (pcsl_string_is_null(pcslString)) {        return PCSL_STRING_OK;    } else {        const jsize length = pcsl_string_utf16_length(pcslString);        pcsl_string_status status = PCSL_STRING_OK;        /* Reserve space for terminating zero */        const jsize buffer_length = length + 1;        jchar * buffer = midpMalloc(buffer_length * sizeof(jchar));        if (buffer == NULL) {            return PCSL_STRING_ENOMEM;        }        status = pcsl_string_convert_to_utf16(pcslString,                                            buffer, buffer_length, NULL);        if (status == PCSL_STRING_OK) {            *pBuf = buffer;            *pBufLen = length;        } else {            midpFree(buffer);        }        return status;    }}/**  An analog of midp_string_from_pcsl_string that works only with utf-16 implementation  of pcsl_string.  DO NOT FREE THE RETURNED STRING!!!!  Sometimes we need to get a MidpString for a string whose pcsl_string representation  is stored into some structure.  Such strings are not free'd by the code that accesses them.  TO BE REMOVED AS SOON AS POSSIBLE.  (mg's note: I had to choose between memory leaks, inventing a MidpString cache for  array of pcsl_strings, and accessing the internal details. I chose the latter.)*/pcsl_string_status midp_string_from_pcsl_string_hack(const pcsl_string * pcslString,						MidpString * midpString) {  if (midpString == NULL || pcslString == NULL) {    return PCSL_STRING_EINVAL;  } else if (pcsl_string_is_null(pcslString)) {      * midpString = NULL_MIDP_STRING;      return PCSL_STRING_OK;  } else if (0 == pcsl_string_length(pcslString)) {      * midpString = EMPTY_MIDP_STRING;      return PCSL_STRING_OK;  } else {    /* this is absoluteyl incorrect.... */    midpString->data = pcslString->data;    midpString->len = pcsl_string_length(pcslString);    return PCSL_STRING_OK;  }}

⌨️ 快捷键说明

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