~ubuntu-branches/ubuntu/lucid/codelite/lucid

« back to all changes in this revision

Viewing changes to sdk/ctags/string_util.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-02-10 02:27:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090210022755-m5692nfc1t5uf1w9
Tags: 1.0.2759+dfsg-0ubuntu1
* New upstream release (LP: #327216).
* debian/patches/series, debian/patches/00_fix-ia64-build.patch:
  + Dropped, applied upstream already.
* debian/patches/02_fix-desktop.patch,
  debian/patches/03_fix-sh.patch:
  + Refreshed to patch cleanly.
* debian/rules:
  + Make get-orig-source honour UPSTREAM_VERSION if set.
* debian/ctags-le.1,
  debian/codelite_indexer.1,
  debian/codelite.manpages:
  + Dropped ctags-le manpage, since ctags-le was replaced by
    codelite_indexer.
  + Added codelite_indexer manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "string_util.h"
2
 
 
3
 
#include <sys/types.h>
4
 
#include <string.h>
5
 
#include <stdlib.h>
6
 
 
7
 
char* string_replace(const char* src, const char* from, const char* to)
8
 
{
9
 
        /*
10
 
          * Find out the lengths of the source string, text to replace, and
11
 
          * the replacement text.
12
 
          */
13
 
 
14
 
        size_t size = strlen(src) + 1;
15
 
        size_t fromlen = strlen(from);
16
 
        size_t tolen = strlen(to);
17
 
        int whole_word = 0;
18
 
        
19
 
        /*
20
 
         * Allocate the first chunk with enough for the original string.
21
 
         */
22
 
        char *value = malloc(size);
23
 
        /*
24
 
         * We need to return 'value', so let's make a copy to mess around with.
25
 
         */
26
 
        char *dst = value;
27
 
 
28
 
        /*
29
 
         * Before we begin, let's see if malloc was successful.
30
 
         */
31
 
        if ( value != NULL ) {
32
 
                /*
33
 
                 * Loop until no matches are found.
34
 
                 */
35
 
 
36
 
                for ( ;; ) {
37
 
                        /*
38
 
                         * Try to find the search text.
39
 
                         */
40
 
                        whole_word = 0;
41
 
                        static const char word_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_";
42
 
                        
43
 
                        const char *match = strstr(src, from);
44
 
                        if(match) {
45
 
                                
46
 
                                /* make sure there is a complete word */
47
 
                                char ch_before = ' ';
48
 
                                char ch_after = ' ';
49
 
                                
50
 
                                if(match > src) {
51
 
                                        ch_before = *(match-1);
52
 
                                }
53
 
                                
54
 
                                if(src+strlen(src) >= match + fromlen){
55
 
                                        ch_after = *(match + fromlen);
56
 
                                }
57
 
                                
58
 
                                whole_word = 1;
59
 
                                if(strchr(word_chars, ch_before) || strchr(word_chars, ch_after)){
60
 
                                        whole_word = 0;
61
 
                                }
62
 
                        }
63
 
                        
64
 
                        if ( match != NULL && whole_word ) {
65
 
                                /*
66
 
                                 * Found search text at location 'match'. :)
67
 
                                 * Find out how many characters to copy up to the 'match'.
68
 
                                 */
69
 
                                size_t count = match - src;
70
 
 
71
 
                                /*
72
 
                                 * We are going to realloc, and for that we will need a
73
 
                                 * temporary pointer for safe usage.
74
 
                                 */
75
 
                                char *temp;
76
 
 
77
 
                                /*
78
 
                                 * Calculate the total size the string will be after the
79
 
                                 * replacement is performed.
80
 
                                 */
81
 
                                size += tolen - fromlen;
82
 
 
83
 
                                /*
84
 
                                 * Attempt to realloc memory for the new size.
85
 
                                 */
86
 
                                temp = realloc(value, size);
87
 
 
88
 
                                if ( temp == NULL ) {
89
 
                                        /*
90
 
                                         * Attempt to realloc failed. Free the previously malloc'd
91
 
                                         * memory and return with our tail between our legs. :(
92
 
                                         */
93
 
                                        free(value);
94
 
                                        return NULL;
95
 
                                }
96
 
 
97
 
                                /*
98
 
                                  * The call to realloc was successful. :) But we'll want to
99
 
                                  * return 'value' eventually, so let's point it to the memory
100
 
                                  * that we are now working with. And let's not forget to point
101
 
                                  * to the right location in the destination as well.
102
 
                                  */
103
 
                                dst = temp + (dst - value);
104
 
                                value = temp;
105
 
 
106
 
                                /*
107
 
                                  * Copy from the source to the point where we matched. Then
108
 
                                  * move the source pointer ahead by the amount we copied. And
109
 
                                  * move the destination pointer ahead by the same amount.
110
 
                                  */
111
 
                                memmove(dst, src, count);
112
 
                                src += count;
113
 
                                dst += count;
114
 
 
115
 
                                /*
116
 
                                  * Now copy in the replacement text 'to' at the position of
117
 
                                  * the match. Adjust the source pointer by the text we replaced.
118
 
                                  * Adjust the destination pointer by the amount of replacement
119
 
                                  * text.
120
 
                                  */
121
 
 
122
 
                                memmove(dst, to, tolen);
123
 
                                src += fromlen;
124
 
                                dst += tolen;
125
 
                        } else { /* No match found. */
126
 
                                /*
127
 
                                  * Copy any remaining part of the string. This includes the null
128
 
                                  * termination character.
129
 
                                  */
130
 
 
131
 
                                strcpy(dst, src);
132
 
                                break;
133
 
                        }
134
 
                }
135
 
        }
136
 
 
137
 
        return value;
138
 
}
139
 
 
140
 
list_t* string_split(const char* src, const char* delim)
141
 
{
142
 
        list_t *l;
143
 
        char *line = (char*)0;
144
 
        char *ptr = (char*)src;
145
 
        char *v;
146
 
 
147
 
        list_init(&l);
148
 
 
149
 
        /* read the string as line by line */
150
 
        line = strtok(ptr, "\n");
151
 
        while (line) {
152
 
                char *key, *value;
153
 
                v = strstr(line, delim);
154
 
                if (v) {
155
 
                        value = (char*)malloc(strlen(v+1)+1);
156
 
                        strcpy(value, v+1);
157
 
                        
158
 
                        key = (char*)malloc(v - line + 1);
159
 
                        memcpy(key, line, v-line);
160
 
                        key[v-line] = 0;
161
 
                        
162
 
                        /* allocate string pair and append it to the list */
163
 
                        string_pair_t *pair = (string_pair_t*)malloc(sizeof(string_pair_t));
164
 
                        pair->key = key;
165
 
                        pair->data = value;
166
 
                        list_append(l, (void*)pair);
167
 
 
168
 
                }
169
 
                line = strtok((char*)0, "\n");
170
 
        }
171
 
        return l;
172
 
}
173