Changeset 969 in MondoRescue


Ignore:
Timestamp:
Nov 23, 2006, 6:24:03 PM (17 years ago)
Author:
Bruno Cornec
Message:

mr_str now contains all string functions (removal of mr_string from trunk)

Location:
trunk/mondo/src
Files:
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mondo/src/lib/mr_str.c

    r900 r969  
    1 /*
    2    $Id$
    3 */
     1/*
     2 * $Id$
     3 *
     4 * New generation of string handling functions safe from a memory management point of view
     5 * Developped by Andree Leidenfrost
     6 */
    47
    58#include <stdio.h>
    69#include <string.h>
    7 
    8 /* New functions safe from a memory manageemnt point of view */
    9 /* Developped by Andree Leidenfrost */
     10 
    1011/**
     12 * Safe alternative to standard function strok()
    1113 * Build a partition name from a drive and a partition number.
    12  * @param instr 
    13  * @param delims 
    14  * @param lastpos 
    15  * @return @p 
     14 * @param instr
     15 * @param delims
     16 * @param lastpos
     17 * @return @p
    1618 * @note this function allocates memory that needs to be freed by caller
    1719 **/
     20char *mr_strtok(char *instr, const char *delims, int *lastpos)
     21{
    1822
     23    char *token = NULL;
     24    char *strptr = NULL;
     25    size_t pos1 = 0;
     26    size_t pos2 = 0;
    1927
    20 char *mr_strtok(char *instr, const char *delims, int *lastpos) {
     28    if (strlen(instr) <= *lastpos) {
     29        *lastpos = 0;
     30        return token;
     31    }
    2132
    22 char *token = NULL;
    23 char *strptr = NULL;
    24 size_t pos1 = 0;
    25 size_t pos2 = 0;
     33    strptr = instr + *lastpos;
     34    pos2 = strspn(strptr, delims);
     35    strptr += pos2;
     36    pos1 = strcspn(strptr, delims);
     37    token = malloc(sizeof(*token) * (pos1 + 1));
     38    strncpy(token, strptr, pos1);
     39    token[pos1] = '\0';
     40    *lastpos = *lastpos + pos1 + pos2 + 1;
    2641
    27 if (strlen(instr) <= *lastpos) {
    28     *lastpos = 0;
    2942    return token;
    3043}
    3144
    32 strptr = instr + *lastpos;
    33 pos2 = strspn(strptr, delims);
    34 strptr += pos2;
    35 pos1 = strcspn(strptr, delims);
    36 token = malloc(sizeof(*token)*(pos1+1));
    37 strncpy(token, strptr, pos1);
    38 token[pos1] = '\0';
    39 *lastpos = *lastpos + pos1 + pos2 + 1;
    4045
    41 return token;
     46/**
     47 * Returns the string fed to it 'inptr' with all characters to escape given
     48 * in 'toesc' prepended by escaping character 'escchr'.
     49 * (Prepare strings for use in system() or popen() with this function.)
     50 * @param instr
     51 * @param toesc
     52 * @param escchr
     53 * @note this function allocates memory that needs to be freed by caller
     54 **/
     55char *mr_stresc(char *instr, char *toesc, const char escchr)
     56{
     57
     58    char *inptr = NULL;
     59    char *retstr = NULL;
     60    char *retptr = NULL;
     61    char *escptr = NULL;
     62    int cnt = 0;
     63
     64    inptr = instr;
     65
     66    // Count how many characters need escaping.
     67    while (*inptr != '\0') {
     68        escptr = toesc;
     69        while (*escptr != '\0') {
     70            if (*inptr == *escptr) {
     71                // Found it, skip the rest.
     72                cnt++;
     73                inptr++;
     74                break;
     75            }
     76            inptr++;
     77            escptr++;
     78        }
     79    }
     80    inptr = instr;
     81
     82    retstr = (char *) malloc(strlen(inptr) + cnt + 1);
     83    retptr = retstr;
     84
     85    // Prepend specified characters with escape character.
     86    while (*inptr != '\0') {
     87        escptr = toesc;
     88        while (*escptr != '\0') {
     89            if (*inptr == *escptr) {
     90                // Found it, skip the rest.
     91                *retptr = escchr;
     92                retptr++;
     93                break;
     94            }
     95            escptr++;
     96        }
     97        *retptr = *inptr;
     98        retptr++;
     99        inptr++;
     100    }
     101    *retptr = '\0';
     102
     103    return retstr;
    42104}
Note: See TracChangeset for help on using the changeset viewer.