summaryrefslogtreecommitdiffstats
path: root/kxsldbg/kxsldbgpart/libxsldbg/utils.cpp
blob: c63c07fc60fe4a2ea3d8fb773f7340f8e05652f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

/**************************************************************************
                          utils.c  -  misc utils

                             -------------------
    begin                : Thur Jan 31 2002
    copyright            : (C) 2001 by Keith Isdale
    email                : k_isdale@tpg.com.au
 **************************************************************************/

/**************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 **************************************************************************/

#include "utils.h"

/**
 * trimString:
 * @text : A valid string with leading or trailing spaces
 *
 * Remove leading and trailing spaces off @text
 *         stores result back into @text
 *
 * Returns 1 on success,
 *         0 otherwise
 */
int
trimString(xmlChar * text)
{
    int result = 0;
    xmlChar *start, *end;

    if (text && xmlStrlen(text)) {
        start = text;
        end = text + xmlStrLen(text) - 1;
        while (_IS_BLANK(*start) && (start <= end))
            start++;

        while (_IS_BLANK(*end) && (end >= start))
            end--;

        /* copy  to @text */
        while (start <= end) {
            *text = *start;
            text++;
            start++;
        }

        *text = '\0';
        result = 1;
    }
    return result;
}


/**
 * splitString:
 * @textIn: The string to split
 * @maxStrings: The max number of strings to put into @out
 * @out: Is valid and at least the size of @maxStrings
 *
 * Split string by white space and put into @out
 *
 * Returns 1 on success,
 *         0 otherwise
 */
int
splitString(xmlChar * textIn, int maxStrings, xmlChar ** out)
{
    int wordCount = 0;
    int foundQuote = 0;

    if (!textIn || !out)
        return wordCount;


    while ((*textIn != '\0') && (wordCount < maxStrings)) {
        /*skip the first spaces ? */
        while (_IS_BLANK(*textIn))
            textIn++;

        if (*textIn == '\"') {
            textIn++;
            foundQuote = 1;
        }
        out[wordCount] = textIn;

        /* look for end of word */
        if (foundQuote == 0) {
            while (!_IS_BLANK(*textIn) && (*textIn != '\0'))
                textIn++;

            if (*textIn != '\0') {
                *textIn = '\0';
                textIn++;
            }

            if (xmlStrLen(out[wordCount]) > 0) {
                wordCount++;
            }
        } else {
            /* look for ending quotation mark */
            while ((*textIn != '\0') && (*textIn != '\"'))
                textIn++;
            if (*textIn == '\0') {
                xsldbgGenericErrorFunc(i18n("Error: Unmatched quotes in input.\n"));
                wordCount = 0;
                break;
            }
            *textIn = '\0';
            textIn++;           /* skip the '"' which is now a '\0' */
            foundQuote = 0;
            wordCount++;
        }

    }

    if (*textIn != '\0')
        wordCount = 0;          /* We have not processed all the text givent to us */
    return wordCount;
}



/**
 * lookupName:
 * @name : Is valid
 * @matchList : A NULL terminated list of names to use as lookup table
 *
 * Lookup and name in a list
 *
 * Returns The id of name found in @matchList,
 *         0 otherwise
*/
int
lookupName(xmlChar * name, xmlChar ** matchList)
{
    int result = -1, nameIndex;

    if (!name || !matchList)
        return result;

    for (nameIndex = 0; matchList[nameIndex]; nameIndex++) {
        if (xmlStrEqual(name, matchList[nameIndex])) {
            result = nameIndex;
            break;
        }
    }

    return result;
}

/**
 * fullQName:
 * @nameURI : QName part of name
 * @name : Local part of name 
 *
 * Join nameURI to name
 *
 * Returns a copy of "nameURI:name"
 *
 */

xmlChar * fullQName(const xmlChar* nameURI, const xmlChar * name)
{
  xmlChar *result = NULL;
  if (!nameURI && !name)
    result =  xmlStrdup((xmlChar*)"");
  else{
    if (nameURI == NULL){
      result = xmlStrdup(name);
    }else{
      result = (xmlChar*) xmlMalloc(sizeof(char) * (
			  xmlStrLen(name) +
			  xmlStrLen(nameURI) + 3));
      if (result)
	sprintf((char*)result, "%s:%s",  (char*)nameURI, (char*)name);
    }
  }
  return result;
}