Ignore:
Timestamp:
Aug 18, 2009, 2:29:18 PM (15 years ago)
Author:
Bruno Cornec
Message:

r3326@localhost: bruno | 2009-08-01 23:53:09 +0200
resolve_naff_tokens now returns an allocated char

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.10/mondo/src/common/libmondo-string.c

    r2314 r2315  
    501501/**
    502502 * Replace all occurences of @p token with @p value while copying @p ip to @p output.
    503  * @param ip The input string containing zero or more <tt>token</tt>s.
     503 * @param ip The input string containing zero or more <tt>token</tt>s. The string will be altered by this function
    504504 * @param output The output string written with the <tt>token</tt>s replaced by @p value.
    505505 * @param token The token to be relaced with @p value.
    506506 * @param value The value to replace @p token.
    507507 */
    508 void resolve_naff_tokens(char *output, char *ip, char *value, char *token)
     508char *resolve_naff_tokens(char *ip, char *value, char *token)
    509509{
    510510    /*@ buffers *** */
    511     char *input;
     511    char *output = NULL;
    512512
    513513    /*@ pointers * */
    514     char *p;
    515 
    516     input = malloc(2000);
    517     // BERLIOS: seems to cause invalid errors
    518     //assert_string_is_neither_NULL_nor_zerolength(ip);
     514    char *p = NULL; /* points to token to modify */
     515    char *q = NULL; /* points to start of string/new string */
     516
    519517    assert_string_is_neither_NULL_nor_zerolength(token);
    520518    assert(value != NULL);
    521519
    522     strcpy(output, ip);         /* just in case the token doesn't appear in string at all */
    523     for (strcpy(input, ip); strstr(input, token); strcpy(input, output)) {
    524         strcpy(output, input);
    525         p = strstr(output, token);
     520    q = ip;
     521    while ((p = strstr(q, token) != NULL)) {
    526522        *p = '\0';
    527         strcat(output, value);
    528         p = strstr(input, token) + strlen(token);
    529         strcat(output, p);
    530     }
    531     paranoid_free(input);
     523        if (output) {
     524            mr_strcat(output, "%s%s", q, value);
     525        } else {
     526            mr_asprintf(&output, "%s%s", q, value);
     527        }
     528        p++;
     529        q = p;
     530    }
     531    if (output) {
     532        mr_strcat(output, "%s", q);
     533    } else {
     534        /* just in case the token doesn't appear in string at all */
     535        mr_asprintf(&output, "%s", q);
     536    }
     537    return(output);
    532538}
    533539
     
    10851091    /*@ int *********************************************** */
    10861092    int percentage;
     1093    int j;
    10871094
    10881095    /*@ buffers ******************************************* */
Note: See TracChangeset for help on using the changeset viewer.