Changeset 2421 in MondoRescue for branches/2.2.10/mondo/src/lib/mr_str.c


Ignore:
Timestamp:
Sep 24, 2009, 3:50:38 PM (15 years ago)
Author:
Bruno Cornec
Message:
  • Remove some compiler warnings on main
  • Adds function mr_chomp and mr_strip_char and use them in mr_date and call_program_and_get_last_line_of_output
  • Fix call_program_and_get_last_line_of_output to return the right result using a temp file instead of popen
  • Fix an issue in mr_getline_int in case of eof (returns now an empty string)
  • Sequencing of Init of bkpinfo reviewed
File:
1 edited

Legend:

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

    r1924 r2421  
    104104}
    105105
    106 /* Return a string containing the date */
    107 char *mr_date(void) {
    108    
    109     time_t tcurr;
    110 
    111     tcurr = time(NULL);
    112     return(ctime(&tcurr));
    113 }
    114 
    115 
    116106/**
    117  * Remove all characters whose ASCII value is less than or equal to 32
    118  * (spaces and control characters) from both sides of @p in_out.
    119  * @param in_out The string to strip spaces/control characters from (modified).
     107 * Remove all characters in caracs from begining and end of string @p in_out
     108 * @param in_out The string to strip char characters from (modified).
    120109 */
    121 void mr_strip_spaces(char *in_out) {
     110void mr_strip_char(char *in_out, char *caracs) {
    122111    int i = 0;
    123112    int j = 0;
    124     size_t length;
     113    int length = 0;
    125114
     115    if (caracs == NULL) {
     116        return;
     117    }
    126118    if (in_out == NULL) {
    127119        return;
    128120    }
    129     length = strlen(in_out);
     121    length = (int)strlen(in_out);
    130122
    131     /* Skip initial spaces and special chars */
    132     for (i = 0; in_out[i] <= ' ' && i < (int)length ; i++);
     123    /* Skip initial caracs */
     124    for (i = 0; index(caracs, in_out[i]) != NULL && i < length ; i++);
     125
    133126    /* Shift the string to the begining if needed */
    134127    if (i != 0) {
    135         for (j = 0; i < (int)length ; i++, j++) {
     128        for (j = 0; i < length ; i++, j++) {
    136129            in_out[j] = in_out[i];
    137130        }
     
    142135
    143136    /* Skip final spaces and special chars */
    144     for (i = (int)strlen(in_out) - 1; i >= 0  && in_out[i] <= ' '; i--);
     137    for (i = (int)strlen(in_out) - 1; i >= 0  && index(caracs, in_out[i]) != NULL; i--);
    145138
    146139    /* The string now ends after that char */
     
    148141    in_out[i] = '\0';
    149142}
     143
     144/**
     145 * Remove all characters whose ASCII value is less than or equal to 32
     146 * (spaces and control characters) from both sides of @p in_out.
     147 * @param in_out The string to strip spaces/control characters from (modified).
     148 */
     149void mr_strip_spaces(char *in_out) {
     150
     151    int i;
     152    char *tmp = NULL;
     153
     154    mr_asprintf(tmp, "");
     155
     156    /* Build the string of char to avoid: ctrl char up to space*/
     157    for (i = 0; i <= ' '; i++) {
     158        mr_strcat(tmp, "%c", i);
     159    }
     160   
     161    mr_strip_char(in_out, tmp);
     162    mr_free(tmp);
     163}
     164
     165/*
     166 * Remove '\n' char from both sides of @p in_out.
     167 * @param in_out The string to strip characters from (modified).
     168 */
     169void mr_chomp(char *in_out) {
     170
     171    mr_strip_char(in_out, "\n");
     172}
     173
     174/* Return an allocated string containing the date */
     175char *mr_date(void) {
     176   
     177    time_t tcurr;
     178    char *tmp = NULL;
     179
     180    tcurr = time(NULL);
     181    mr_asprintf(tmp, "%s", ctime(&tcurr));
     182    mr_chomp(tmp);
     183    return(tmp);
     184}
     185
     186
Note: See TracChangeset for help on using the changeset viewer.