source: trunk/mondo/src/lib/mr_str.c @ 1106

Last change on this file since 1106 was 1106, checked in by bruno, 13 years ago

merge -r1082:1105 $SVN_M/branches/stable

  • Property svn:eol-style set to native
File size: 2.3 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{
60
61    char *inptr = NULL;
62    char *retstr = NULL;
63    char *retptr = NULL;
64    char *escptr = NULL;
65    int cnt = 0;
66
67    inptr = instr;
68
69    // Count how many characters need escaping.
70    while (*inptr != '\0') {
71        escptr = toesc;
72        while (*escptr != '\0') {
73            if (*inptr == *escptr) {
74                // Found it, skip the rest.
75                cnt++;
76                inptr++;
77                break;
78            }
79            inptr++;
80            escptr++;
81        }
82    }
83    inptr = instr;
84
85    retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
86    retptr = retstr;
87
88    // Prepend specified characters with escape character.
89    while (*inptr != '\0') {
90        escptr = toesc;
91        while (*escptr != '\0') {
92            if (*inptr == *escptr) {
93                // Found it, skip the rest.
94                *retptr = escchr;
95                retptr++;
96                break;
97            }
98            escptr++;
99        }
100        *retptr = *inptr;
101        retptr++;
102        inptr++;
103    }
104    *retptr = '\0';
105
106    return retstr;
107}
108
109/* Return a string containing the date */
110char *mr_date(void) {
111   
112    time_t tcurr;
113
114    tcurr = time(NULL);
115    return(ctime(&tcurr));
116}
117
Note: See TracBrowser for help on using the repository browser.