Changeset 3193 in MondoRescue for branches/3.1/mondo/src/lib/mr_str.c


Ignore:
Timestamp:
Sep 29, 2013, 9:31:34 AM (11 years ago)
Author:
Bruno Cornec
Message:
  • Finish with backports from 3.1 for now. Still some work to do, but we will now make that version compile and work again and serve as a base

so the gettext patch can be added

File:
1 edited

Legend:

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

    r3147 r3193  
    1212
    1313#include "mr_mem.h"
     14
     15// to get bool type
     16#define _MY_STUFF_H_
     17#include "my-stuff.h"
    1418 
    1519/**
     
    6670 * @note this function allocates memory that needs to be freed by caller
    6771 **/
    68 char *mr_stresc(char *instr, char *toesc, const char escchr) {
     72char *mr_stresc(char *instr, char *toesc, const char escchr, const char specialchr) {
    6973    char *inptr = NULL;
    7074    char *retstr = NULL;
     
    7276    char *escptr = NULL;
    7377    int cnt = 0;
     78    bool found = FALSE;
    7479
    7580    inptr = instr;
    76 
    7781    // Count how many characters need escaping.
    7882    while (*inptr != '\0') {
     
    8286                // Found it, skip the rest.
    8387                cnt++;
     88                // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
     89                if (*inptr == specialchr) {
     90                    cnt += 2;
     91                }
    8492                break;
    8593            }
     
    8896        inptr++;
    8997    }
     98
    9099    inptr = instr;
    91 
    92100    retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
    93101    retptr = retstr;
     
    99107            if (*inptr == *escptr) {
    100108                // Found it, skip the rest.
    101                 *retptr = escchr;
    102                 retptr++;
     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                }
    103120                break;
    104121            }
     
    108125        retptr++;
    109126        inptr++;
     127        if (found) {
     128            // finish to put the remaining specialchr
     129            *retptr = specialchr;
     130            retptr++;
     131            found = FALSE;
     132        }
    110133    }
    111134    *retptr = '\0';
     
    114137}
    115138
    116 /**
    117  * Remove all characters in caracs from begining and end of string @p in_out
    118  * @param in_out The string to strip char characters from (modified).
     139/* Return a string containing the date */
     140char *mr_date(void) {
     141   
     142    time_t tcurr;
     143
     144    tcurr = time(NULL);
     145    return(ctime(&tcurr));
     146}
     147
     148/* Return an allocated string containing the date
     149char *mr_date(void) {
     150   
     151    time_t tcurr;
     152    char *tmp = NULL;
     153
     154    tcurr = time(NULL);
     155    mr_asprintf(tmp, "%s", ctime(&tcurr));
     156    mr_chomp(tmp);
     157    return(tmp);
     158}
    119159 */
    120 void mr_strip_char(char *in_out, char *caracs) {
    121     int i = 0;
    122     int j = 0;
    123     int length = 0;
    124 
    125     if (caracs == NULL) {
    126         return;
    127     }
    128     if (in_out == NULL) {
    129         return;
    130     }
    131     length = (int)strlen(in_out);
    132 
    133     /* Skip initial caracs */
    134     for (i = 0; index(caracs, in_out[i]) != NULL && i < length ; i++);
    135 
    136     /* Shift the string to the begining if needed */
    137     if (i != 0) {
    138         for (j = 0; i < length ; i++, j++) {
    139             in_out[j] = in_out[i];
    140         }
    141         /* Erase the end of the string if needed */
    142         j++;
    143         in_out[j] = '\0';
    144     }
    145 
    146     /* Skip final spaces and special chars */
    147     for (i = (int)strlen(in_out) - 1; i >= 0  && index(caracs, in_out[i]) != NULL; i--);
    148 
    149     /* The string now ends after that char */
    150     i++;
    151     in_out[i] = '\0';
    152 }
    153160
    154161/**
     
    158165 */
    159166void mr_strip_spaces(char *in_out) {
    160 
    161     int i;
    162     char *tmp = NULL;
    163 
    164     mr_asprintf(tmp, "");
    165 
    166     /* Build the string of char to avoid: ctrl char up to space*/
    167     for (i = 0; i <= ' '; i++) {
    168         mr_strcat(tmp, "%c", i);
    169     }
    170    
    171     mr_strip_char(in_out, tmp);
    172     mr_free(tmp);
    173 }
     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        j++;
     185        in_out[j] = '\0';
     186    }
     187
     188    /* Skip final spaces and special chars */
     189    for (i = (int)strlen(in_out) - 1; i >= 0  && in_out[i] <= ' '; i--);
     190
     191    /* The string now ends after that char */
     192    i++;
     193    in_out[i] = '\0';
     194}
     195
    174196
    175197/*
    176198 * Remove '\n' char from both sides of @p in_out.
    177199 * @param in_out The string to strip characters from (modified).
    178  */
     200
    179201void mr_chomp(char *in_out) {
    180202
    181203    mr_strip_char(in_out, "\n");
    182204}
    183 
    184 /* Return an allocated string containing the date */
    185 char *mr_date(void) {
    186    
    187     time_t tcurr;
    188     char *tmp = NULL;
    189 
    190     tcurr = time(NULL);
    191     mr_asprintf(tmp, "%s", ctime(&tcurr));
    192     mr_chomp(tmp);
    193     return(tmp);
    194 }
    195 
    196 
     205*/
     206
     207/**
     208 * Remove all characters in caracs from begining and end of string @p in_out
     209 * @param in_out The string to strip char characters from (modified).
     210
     211void mr_strip_char(char *in_out, char *caracs) {
     212    int i = 0;
     213    int j = 0;
     214    size_t length = 0;
     215
     216    if (caracs == NULL) {
     217        return;
     218    }
     219    if (in_out == NULL) {
     220        return;
     221    }
     222    length = strlen(in_out);
     223
     224    /* Skip initial caracs */
     225    for (i = 0; index(caracs, in_out[i]) != NULL && i < (int)length ; i++);
     226
     227    /* Shift the string to the begining if needed */
     228    if (i != 0) {
     229        for (j = 0; i < (int)length ; i++, j++) {
     230            in_out[j] = in_out[i];
     231        }
     232        /* Erase the end of the string if needed */
     233        j++;
     234        in_out[j] = '\0';
     235    }
     236
     237    /* Skip final caracs */
     238    for (i = (int)strlen(in_out) - 1; i >= 0  && index(caracs, in_out[i]) != NULL; i--);
     239
     240    /* The string now ends after that char */
     241    i++;
     242    in_out[i] = '\0';
     243}
     244*/
     245
Note: See TracChangeset for help on using the changeset viewer.