Subversion Repositories DevTools

Rev

Rev 313 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
313 dpurdie 1
/* -*- mode: c; tabs: 4 -*- */
2
/*****************************************************************************
3
* Module name   : util.c
4
* Module type   : CMDFILE source file
5
* Environment(s): n/a
6
*
7
* Description:
8
*
9
    Util - Misc functions
10
*
11
* Version   Who     Date        Description
12
            APY     12/99       Created
13
                    23/06/04    StringPath
14
*
15
* $Source: /cvsroot/device/DEVL/UTILS/CMDFILE/util.c,v $
16
* $Revision: 1.6 $ $Date: 2004/10/11 08:59:27 $ $State: Exp $
17
* $Author: ayoung $ $Locker:  $
18
*...........................................................................*/
19
 
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include "cmdfile.h"
23
 
24
/*  ************************** String functions  ************************   */
25
 
26
int
27
StringInit(
28
    String_t *pStr, char *pBuf, unsigned len)
29
{
30
    pStr->s_len = pStr->s_left = len;
31
    if (pBuf == NULL)
32
        if ((pBuf = malloc(len)) == NULL)
33
             return (-1);
34
    pStr->s_ptr = pStr->s_data = pBuf;
35
    return (0);
36
}
37
 
38
 
39
int
40
StringZero(
41
    String_t *pStr)
42
{
43
    pStr->s_left = pStr->s_len;
44
    pStr->s_ptr = pStr->s_data;
45
    return (0);
46
}
47
 
48
 
49
int
50
StringCat(
51
    String_t *pStr, const char *pBuf)
52
{
53
    return StringCatn(pStr, pBuf, strlen(pBuf));
54
}
55
 
56
 
57
int
58
StringCatn(
59
    String_t *pStr, const char *pBuf, unsigned len)
60
{
61
    if (len > pStr->s_left) {
62
        verbose( "macro buffer overflow" );
63
        return (ENOSPC);
64
    }
65
    pStr->s_left -= len;
66
    (void) memcpy(pStr->s_ptr, pBuf, len);
67
    pStr->s_ptr += len;
68
    return (0);
69
}
70
 
71
 
72
static int
73
CopyPath( const char *str, char *buf )
74
{
75
    int     len = 0;
76
 
77
    if (whitespace == WS_ESCAPE) {
78
        const char *c;                          /* escape spaces */
79
        char *b = buf;
80
 
81
        for (c = str; *c; *c++) {
82
            if (*c == ' ')
83
                *b++ = '\\', len++;
84
            *b++ = *c;
85
        }
86
        *b = '\0';
87
 
88
    } else if (whitespace == WS_QUOTE) {
89
        sprintf( buf, "\"%s\"", str );
90
        len = 2;
91
 
92
    } else if (whitespace == WS_URL) {
93
        const char *c;
94
        char *b = buf;
95
 
96
        for (c = str; *c; *c++) {
97
            if ( *c >= 127 ||
98
                strchr( "$&+,;=?@ \"<>{}|^~[]`", *c ) )
99
            {                                   /* convert */
100
                *b++ = '%';
101
                (void) sprintf( b, "%-2.2X", *c );
102
                b += 2;
103
                len += 2;
104
            } else {                            /* quote */
105
                *b++ = *c;
106
            }
107
        }
108
        *b = '\0';
109
 
110
    } else {                                    /* unknown/ignore */
111
        static int spacewarning;
112
 
113
        if (whitespace == WS_UNKNOWN && !spacewarning) {
114
            warning( "embedded spaces encountered within path, specify -W option\n" );
115
            spacewarning++;
116
        }
117
        strcpy(buf, str);
118
    }
119
    return (len);
120
}
121
 
122
 
123
int
124
StringPath(
125
    String_t *pStr, const char *pPath)
126
{
127
    unsigned len, white;
128
    const char *p;
129
 
130
    if (pPath == NULL)
131
        return 0;                               /* NULL string */
132
 
133
    for (len = 0, white = 0, p = pPath; *p; *p++) {
134
        if (*p == ' ') white++;
135
        len++;
136
    }
137
 
138
    if (!white) {
139
        StringCatn(pStr, pPath, len);
140
 
141
    } else {
142
        if (len + white > pStr->s_left) {       /* worst case */
143
            verbose( "macro buffer overflow" );
144
            return (ENOSPC);
145
        }
146
        white = CopyPath( pPath, pStr->s_ptr );
147
        pStr->s_left -= (len + white);
148
        pStr->s_ptr += (len + white);
149
    }
150
 
151
    if (len > PATH_MAX) {
152
        static int lengthwarning;
153
 
154
        if (!lengthwarning) {
155
            warning( "encountered path greater then %d in length\n", PATH_MAX );
156
            lengthwarning++;
157
        }
158
 
159
    }
160
    return (len);
161
}
162
 
163
 
164
 
165
int
166
StringPut(
167
    String_t *pStr, char c)
168
{
169
    if (pStr->s_left == 0) {
170
        verbose( "macro buffer overflow" );
171
        return (ENOSPC);
172
    }
173
    *pStr->s_ptr++ = c;
174
    pStr->s_left--;
175
    return (0);
176
}
177
 
178
 
179
char *
180
StringData(
181
    String_t *pStr)
182
{
183
    if (pStr->s_left <= 0)
184
        return (char *)"!! OVERFLOW !!";
185
    if (pStr->s_left > pStr->s_len)
186
        return (char *)"!! UNDERFLOW !!";
187
    *pStr->s_ptr = '\0';
188
    return (pStr->s_data);
189
}
190