Changeset 3614 in MondoRescue for branches/3.2/mondo/src/lib/mr_str.c


Ignore:
Timestamp:
Nov 18, 2016, 5:31:43 PM (7 years ago)
Author:
Bruno Cornec
Message:

mr_strip_spaces now returns a dynamically allocated string

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mondo/src/lib/mr_str.c

    r3566 r3614  
    2525 * @note this function allocates memory that needs to be freed by caller
    2626 **/
    27 char *mr_strtok(char *instr, const char *delims, int *lastpos)
    28 {
    29 
    30     char *token = NULL;
    31     char *strptr = NULL;
    32     size_t pos1 = 0;
    33     size_t pos2 = 0;
    34 
    35     if (instr == NULL) {
    36         *lastpos = 0;
    37         return token;
    38     }
    39 
    40     if (delims == NULL) {
    41         *lastpos = 0;
    42         return token;
    43     }
    44 
    45     if (strlen(instr) <= *lastpos) {
    46         *lastpos = 0;
    47         return token;
    48     }
    49 
    50     strptr = instr + *lastpos;
    51     pos2 = strspn(strptr, delims);
    52     strptr += pos2;
    53     pos1 = strcspn(strptr, delims);
    54     token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
    55     strncpy(token, strptr, pos1);
    56     token[pos1] = '\0';
    57     *lastpos = *lastpos + pos1 + pos2 + 1;
    58 
     27char *mr_strtok(char *instr, const char *delims, int *lastpos) {
     28
     29char *token = NULL;
     30char *strptr = NULL;
     31size_t pos1 = 0;
     32size_t pos2 = 0;
     33
     34if (instr == NULL) {
     35    *lastpos = 0;
    5936    return token;
     37}
     38
     39if (delims == NULL) {
     40    *lastpos = 0;
     41    return token;
     42}
     43
     44if (strlen(instr) <= *lastpos) {
     45    *lastpos = 0;
     46    return token;
     47}
     48
     49strptr = instr + *lastpos;
     50pos2 = strspn(strptr, delims);
     51strptr += pos2;
     52pos1 = strcspn(strptr, delims);
     53token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
     54strncpy(token, strptr, pos1);
     55token[pos1] = '\0';
     56*lastpos = *lastpos + pos1 + pos2 + 1;
     57
     58return token;
    6059}
    6160
     
    7170 **/
    7271char *mr_stresc(char *instr, char *toesc, const char escchr, const char specialchr) {
    73     char *inptr = NULL;
    74     char *retstr = NULL;
    75     char *retptr = NULL;
    76     char *escptr = NULL;
    77     int cnt = 0;
    78     bool found = FALSE;
    79 
    80     inptr = instr;
    81     // Count how many characters need escaping.
    82     while (*inptr != '\0') {
    83         escptr = toesc;
    84         while (*escptr != '\0') {
    85             if (*inptr == *escptr) {
    86                 // Found it, skip the rest.
    87                 cnt++;
    88                 // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
    89                 if (*inptr == specialchr) {
    90                     cnt += 2;
    91                 }
    92                 break;
     72
     73char *inptr = NULL;
     74char *retstr = NULL;
     75char *retptr = NULL;
     76char *escptr = NULL;
     77int cnt = 0;
     78bool found = FALSE;
     79
     80inptr = instr;
     81// Count how many characters need escaping.
     82while (*inptr != '\0') {
     83    escptr = toesc;
     84    while (*escptr != '\0') {
     85        if (*inptr == *escptr) {
     86            // Found it, skip the rest.
     87            cnt++;
     88            // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
     89            if (*inptr == specialchr) {
     90                cnt += 2;
    9391            }
    94             escptr++;
     92            break;
    9593        }
    96         inptr++;
    97     }
    98 
    99     inptr = instr;
    100     retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
    101     retptr = retstr;
    102 
    103     // Prepend specified characters with escape character.
    104     while (*inptr != '\0') {
    105         escptr = toesc;
    106         while (*escptr != '\0') {
    107             if (*inptr == *escptr) {
    108                 // Found it, skip the rest.
    109                 // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
    110                 if (*inptr == specialchr) {
    111                     *retptr = specialchr;
    112                     retptr++;
    113                     *retptr = escchr;
    114                     retptr++;
    115                     found = TRUE;
    116                 } else {
    117                     *retptr = escchr;
    118                     retptr++;
    119                 }
    120                 break;
     94        escptr++;
     95    }
     96    inptr++;
     97}
     98
     99inptr = instr;
     100retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
     101retptr = retstr;
     102
     103// Prepend specified characters with escape character.
     104while (*inptr != '\0') {
     105    escptr = toesc;
     106    while (*escptr != '\0') {
     107        if (*inptr == *escptr) {
     108            // Found it, skip the rest.
     109            // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
     110            if (*inptr == specialchr) {
     111                *retptr = specialchr;
     112                retptr++;
     113                *retptr = escchr;
     114                retptr++;
     115                found = TRUE;
     116            } else {
     117                *retptr = escchr;
     118                retptr++;
    121119            }
    122             escptr++;
     120            break;
    123121        }
    124         *retptr = *inptr;
     122        escptr++;
     123    }
     124    *retptr = *inptr;
     125    retptr++;
     126    inptr++;
     127    if (found) {
     128        // finish to put the remaining specialchr
     129        *retptr = specialchr;
    125130        retptr++;
    126         inptr++;
    127         if (found) {
    128             // finish to put the remaining specialchr
    129             *retptr = specialchr;
    130             retptr++;
    131             found = FALSE;
    132         }
    133     }
    134     *retptr = '\0';
    135 
    136     return retstr;
     131        found = FALSE;
     132    }
     133}
     134*retptr = '\0';
     135
     136return(retstr);
    137137}
    138138
    139139/* Return a string containing the date */
    140140char *mr_date(void) {
    141    
    142     time_t tcurr;
    143 
    144     tcurr = time(NULL);
    145     return(ctime(&tcurr));
     141
     142time_t tcurr;
     143
     144tcurr = time(NULL);
     145return(ctime(&tcurr));
    146146}
    147147
     
    161161/**
    162162 * Remove all characters whose ASCII value is less than or equal to 32
    163  * (spaces and control characters) from both sides of @p in_out.
    164  * @param in_out The string to strip spaces/control characters from (modified).
     163 * (spaces and control characters) from both sides of @p instr.
     164 * @param instr The string to strip spaces/control characters from
     165 * returns a newly allocated string without the spaces that needs to be freed
     166 * by the caller
    165167 */
    166 void mr_strip_spaces(char *in_out) {
    167     int i = 0;
    168     int j = 0;
    169     size_t length;
    170 
    171     if (in_out == NULL) {
    172         return;
    173     }
    174     length = strlen(in_out);
    175 
    176     /* Skip initial spaces and special chars */
    177     for (i = 0; in_out[i] <= ' ' && i < (int)length ; i++);
    178     /* Shift the string to the begining if needed */
    179     if (i != 0) {
    180         for (j = 0; i < (int)length ; i++, j++) {
    181             in_out[j] = in_out[i];
    182         }
    183         /* Erase the end of the string if needed */
    184         in_out[j] = '\0';
    185     }
    186 
    187     /* Skip final spaces and special chars */
    188     for (i = (int)strlen(in_out) - 1; i >= 0  && in_out[i] <= ' '; i--);
    189 
    190     /* The string now ends after that char */
    191     i++;
    192     in_out[i] = '\0';
     168char *mr_strip_spaces(const char *instr) {
     169
     170char *p = NULL;
     171char *q = NULL;
     172char *outstr = NULL;
     173
     174if (instr == NULL) {
     175    return(NULL);
     176}
     177p = instr;
     178
     179/* Skip initial spaces and special chars */
     180while (*p <= ' ' && *p != '\0') {
     181    p++;
     182}
     183mr_asprintf(outstr, "%s", p);
     184q = outstr + strlen(outstr) -1;
     185
     186/* Skip final spaces and special chars */
     187while (*q <= ' ' && q != outstr) {
     188    q--;
     189}
     190
     191/* The string now ends after that char */
     192q++;
     193*q = '\0';
     194return(outstr);
    193195}
    194196
Note: See TracChangeset for help on using the changeset viewer.