source: branches/stable/mondo/src/lib/mr_str.c @ 1241

Last change on this file since 1241 was 1241, checked in by Bruno Cornec, 13 years ago
  • Fix a bug for tape mangement where block size was missing during extract (Michel Loiseleur)
  • Remove turn_wildcard_chars_into_literal_chars and use mr_stresc instead (based on an idea from Michel)
  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
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 */
7
8#include <stdio.h>
9#include <string.h>
10#include <time.h>
11
12#include "mr_mem.h"
13 
14/**
15 * Safe alternative to standard function strok()
16 * Build a partition name from a drive and a partition number.
17 * @param instr
18 * @param delims
19 * @param lastpos
20 * @return @p
21 * @note this function allocates memory that needs to be freed by caller
22 **/
23char *mr_strtok(char *instr, const char *delims, int *lastpos)
24{
25
26    char *token = NULL;
27    char *strptr = NULL;
28    size_t pos1 = 0;
29    size_t pos2 = 0;
30
31    if (strlen(instr) <= *lastpos) {
32        *lastpos = 0;
33        return token;
34    }
35
36    strptr = instr + *lastpos;
37    pos2 = strspn(strptr, delims);
38    strptr += pos2;
39    pos1 = strcspn(strptr, delims);
40    token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
41    strncpy(token, strptr, pos1);
42    token[pos1] = '\0';
43    *lastpos = *lastpos + pos1 + pos2 + 1;
44
45    return token;
46}
47
48
49/**
50 * Returns the string fed to it 'inptr' with all characters to escape given
51 * in 'toesc' prepended by escaping character 'escchr'.
52 * (Prepare strings for use in system() or popen() with this function.)
53 * @param instr
54 * @param toesc
55 * @param escchr
56 * @note this function allocates memory that needs to be freed by caller
57 **/
58char *mr_stresc(char *instr, char *toesc, const char escchr) {
59    char *inptr = NULL;
60    char *retstr = NULL;
61    char *retptr = NULL;
62    char *escptr = NULL;
63    int cnt = 0;
64
65    inptr = instr;
66
67    // Count how many characters need escaping.
68    while (*inptr != '\0') {
69        escptr = toesc;
70        while (*escptr != '\0') {
71            if (*inptr == *escptr) {
72                // Found it, skip the rest.
73                cnt++;
74                inptr++;
75                break;
76            }
77            inptr++;
78            escptr++;
79        }
80    }
81    inptr = instr;
82
83    retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
84    retptr = retstr;
85
86    // Prepend specified characters with escape character.
87    while (*inptr != '\0') {
88        escptr = toesc;
89        while (*escptr != '\0') {
90            if (*inptr == *escptr) {
91                // Found it, skip the rest.
92                *retptr = escchr;
93                retptr++;
94                break;
95            }
96            escptr++;
97        }
98        *retptr = *inptr;
99        retptr++;
100        inptr++;
101    }
102    *retptr = '\0';
103
104    return retstr;
105}
106
107/* Return a string containing the date */
108char *mr_date(void) {
109   
110    time_t tcurr;
111
112    tcurr = time(NULL);
113    return(ctime(&tcurr));
114}
115
116
117/**
118 * Remove all characters whose ASCII value is less than or equal to 32
119 * (spaces and control characters) from both sides of @p in_out.
120 * @param in_out The string to strip spaces/control characters from (modified).
121 */
122void mr_strip_spaces(char *in_out) {
123    int i = 0;
124    int j = 0;
125    size_t length;
126
127    if (in_out == NULL) {
128        return;
129    }
130    length = strlen(in_out);
131
132    /* Skip initial spaces and special chars */
133    for (i = 0; in_out[i] <= ' ' && i < (int)length ; i++);
134    /* Shift the string to the begining if needed */
135    if (i != 0) {
136        for (j = 0; i < (int)length ; i++, j++) {
137            in_out[j] = in_out[i];
138        }
139        /* Erase the end of the string if needed */
140        j++;
141        in_out[j] = '\0';
142    }
143
144    /* Skip final spaces and special chars */
145    for (i = (int)strlen(in_out) - 1; i >= 0  && in_out[i] <= ' '; i--);
146
147    /* The string now ends after that char */
148    i++;
149    in_out[i] = '\0';
150}
Note: See TracBrowser for help on using the repository browser.