~ubuntu-branches/ubuntu/karmic/xarchiver/karmic

« back to all changes in this revision

Viewing changes to src/string_utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-11-07 14:54:00 UTC
  • mfrom: (1.1.7 upstream) (2.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20081107145400-z0j3jmgads8coae2
Tags: 0.5.1-1
MergingĀ upstreamĀ versionĀ 0.5.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2006 Giuseppe Torelli - <colossus73@gmail.com>
 
2
 *  Copyright (C) 2008 Giuseppe Torelli - <colossus73@gmail.com>
3
3
 *
4
4
 *  This program is free software; you can redistribute it and/or modify
5
5
 *  it under the terms of the GNU General Public License as published by
16
16
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
17
17
 */
18
18
 
19
 
#ifdef HAVE_CONFIG_H
20
 
#include "config.h"
21
 
#endif
22
 
 
23
 
#include <gtk/gtk.h>
24
 
#include <glib.h>
25
 
#include <string.h>
26
19
#include "string_utils.h"
27
 
#include "errno.h"
28
20
 
29
21
#ifndef HAVE_MKDTEMP
30
 
static char *mkdtemp (gchar *tmpl)
 
22
char *mkdtemp (gchar *tmpl)
31
23
{
32
24
        static const gchar LETTERS[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
33
25
        static guint64     value = 0;
90
82
}
91
83
#endif /* !HAVE_STRCASESTR */
92
84
 
93
 
gchar *EscapeBadChars ( gchar *string , gchar *pattern)
94
 
{
95
 
        return escape_str_common (string, pattern, '\\', 0);
96
 
}
97
 
 
98
 
gchar *JoinPathArchiveName ( const gchar *extract_path , gchar *path )
99
 
{
100
 
        return g_strconcat (extract_path , path , NULL);
101
 
}
102
 
 
103
 
int CountCharacter ( gchar *string , int chr )
104
 
{
105
 
    int n = 0;
106
 
    while ( *string )
107
 
    {
108
 
        if ( *string == chr ) n++;
109
 
        string++;
110
 
    }
111
 
    return n;
112
 
}
113
 
 
114
 
gchar *RemoveBackSlashes ( gchar *name)
115
 
{
116
 
    gchar *nome, *q;
117
 
    int x = CountCharacter ( name , '\\' );
118
 
    nome = (char *) g_malloc (strlen(name) - x + 1);
119
 
    q = nome;
120
 
    while ( *name )
121
 
    {
122
 
        if ( *name == '\\' ) name++;
123
 
        *q++ = *name++;
124
 
    }
125
 
    *q = '\000';
126
 
    return nome;
 
85
gchar *xa_escape_bad_chars ( gchar *string , gchar *pattern)
 
86
{
 
87
        return xa_escape_common_chars (string, pattern, '\\', 0);
127
88
}
128
89
 
129
90
/* These functions are from File-Roller code */
130
 
char *get_last_field (char *line,int last_field)
131
 
{
132
 
        char *field;
133
 
        int i;
134
 
 
135
 
        if (line == NULL)
136
 
                return NULL;
137
 
 
138
 
        last_field--;
139
 
        field = eat_spaces (line);
140
 
        for (i = 0; i < last_field; i++) {
141
 
                if (field == NULL)
142
 
                        return NULL;
143
 
                field = strchr (field, ' ');
144
 
                field = eat_spaces (field);
145
 
        }
146
 
        //The following line is mine, I replace the \n with the null terminated
147
 
    if (field != NULL) field [ strlen(field) -1 ] = '\000';
148
 
        return field;
149
 
}
150
 
 
151
 
char **split_line (char *line,int n_fields)
152
 
{
153
 
        char **fields;
154
 
        char *scan, *field_end;
155
 
        int i;
156
 
 
157
 
        fields = g_new0 (char *, n_fields + 1);
158
 
        fields[n_fields] = NULL;
159
 
 
160
 
        scan = eat_spaces (line);
161
 
        for (i = 0; i < n_fields; i++)
162
 
        {
163
 
                if (scan == NULL)
164
 
                {
165
 
                        fields[i] = NULL;
166
 
                        continue;
167
 
                }
168
 
                field_end = strchr (scan, ' ');
169
 
                //The following line is mine, I added the case when the last field ends with a newline
170
 
                if (field_end == NULL) field_end = strchr (scan, '\n');
171
 
                if (field_end != NULL)
172
 
                {
173
 
                        fields[i] = g_strndup (scan, field_end - scan);
174
 
                        scan = eat_spaces (field_end);
175
 
                }
176
 
        }
177
 
        return fields;
178
 
}
179
91
 
180
92
static int count_chars_to_escape (const char *str, const char *meta_chars)
181
93
{
182
 
        int         meta_chars_n = strlen (meta_chars);
183
 
        const char *s;
184
 
        int         n = 0;
 
94
        int meta_chars_n = strlen (meta_chars);
 
95
        const char *s;
 
96
        int n = 0;
185
97
 
186
 
        for (s = str; *s != 0; s++) {
187
 
                int i;
188
 
                for (i = 0; i < meta_chars_n; i++)
189
 
                        if (*s == meta_chars[i]) {
190
 
                                n++;
191
 
                                break;
192
 
                        }
193
 
        }
194
 
        return n;
 
98
        for (s = str; *s != 0; s++)
 
99
        {
 
100
                int i;
 
101
                for (i = 0; i < meta_chars_n; i++)
 
102
                        if (*s == meta_chars[i])
 
103
                        {
 
104
                        n++;
 
105
                break;
 
106
                        }
 
107
                }
 
108
        return n;
195
109
}
196
 
char *escape_str_common (const char *str, const char *meta_chars, const char  prefix, const char  postfix)
 
110
char *xa_escape_common_chars (const char *str, const char *meta_chars, const char  prefix, const char postfix)
197
111
{
198
112
        int         meta_chars_n = strlen (meta_chars);
199
113
        char       *escaped;
210
124
                extra_chars++;
211
125
 
212
126
        new_l = strlen (str) + (count_chars_to_escape (str, meta_chars) * extra_chars);
213
 
        escaped = g_malloc (new_l + 1);
 
127
        escaped = g_malloc0 (new_l + 1);
214
128
 
215
129
        s = str;
216
130
        t = escaped;
229
143
        return escaped;
230
144
}
231
145
 
232
 
char *eat_spaces (char *line)
 
146
gchar *xa_remove_level_from_path (const gchar *path)
233
147
{
234
 
        if (line == NULL)
235
 
                return NULL;
236
 
        while ((*line == ' ') && (*line != 0))
237
 
                line++;
238
 
        return line;
239
 
}
 
148
        gchar *local_path;
 
149
        gchar *_local_path;
240
150
 
241
 
gchar *remove_level_from_path (const gchar *path)
242
 
{
243
 
    const gchar *ptr = path;
244
 
    gint p;
245
 
    if (! path) return NULL;
246
 
    p = strlen (path) - 1;
247
 
    if (p < 0) return NULL;
248
 
    while ((ptr[p] != '/') && (p > 0))
249
 
        p--;
250
 
    if ((p == 0) && (ptr[p] == '/')) p++;
251
 
    return g_strndup (path, (guint)p);
 
151
        if (path[strlen(path)-1] == '/')
 
152
        {
 
153
                _local_path = g_strndup(path,strlen(path)-1);
 
154
                local_path  = g_path_get_dirname (_local_path);
 
155
                g_free(_local_path);
 
156
        }
 
157
        else
 
158
        local_path = g_path_get_dirname (path);
 
159
    return local_path;
252
160
}
253
161
 
254
162
gboolean file_extension_is (const char *filename, const char *ext)
264
172
}
265
173
/* End code from File-Roller */
266
174
 
267
 
gchar *extract_local_path (gchar *path)
268
 
{
269
 
    gchar *local_path;
270
 
    gchar *local_escaped_path;
271
 
 
272
 
    local_path = g_path_get_dirname (path);
273
 
    local_escaped_path = EscapeBadChars ( local_path ,"$\'`\"\\!?* ()[]&|@#:;");
274
 
    g_free (local_path);
275
 
    return local_escaped_path;
276
 
}
277
 
 
278
 
void xa_set_window_title ( GtkWidget *window , gchar *title)
 
175
void xa_set_window_title (GtkWidget *window,gchar *title)
279
176
{
280
177
        gchar *x        = NULL;
281
178
        gchar *slash= NULL;
282
179
 
 
180
        if (title)
 
181
        {
 
182
                slash = g_strrstr (title , "/");
 
183
                if (slash)
 
184
                        slash++;
 
185
        }
 
186
        if (!slash)
 
187
                slash = title;
 
188
 
283
189
        if (title == NULL)
284
 
                gtk_window_set_title ( GTK_WINDOW (window) , "Xarchiver " VERSION );
285
 
        else
286
 
        {
287
 
                slash = g_strrstr (title , "/");
288
 
                if (slash == NULL)
289
 
                {
290
 
                        x = g_strconcat (  title , " - " , "Xarchiver " , VERSION , NULL);
291
 
                        gtk_window_set_title ( GTK_WINDOW (window) , x);
292
 
                        g_free (x);
 
190
                x = g_strconcat ("Xarchiver ",VERSION,NULL);
 
191
        else
 
192
                x = g_strconcat (slash, " - Xarchiver ",VERSION,NULL);
 
193
        gtk_window_set_title (GTK_WINDOW (window),x);
 
194
        g_free (x);
 
195
}
 
196
 
 
197
gboolean match_patterns (char **patterns,const char *string,int flags)
 
198
{
 
199
        int i;
 
200
        int result;
 
201
 
 
202
        if (patterns[0] == NULL)
 
203
                return TRUE;
 
204
 
 
205
        if (string == NULL)
 
206
                return FALSE;
 
207
 
 
208
        result = FNM_NOMATCH;
 
209
        i = 0;
 
210
        while ((result != 0) && (patterns[i] != NULL))
 
211
        {
 
212
                result = g_utf8_fnmatch (patterns[i],string,flags);
 
213
                i++;
 
214
        }
 
215
        return (result == 0);
 
216
}
 
217
 
 
218
gchar *xa_remove_path_from_archive_name(gchar *name)
 
219
{
 
220
        gchar *utf8_string,*text;
 
221
 
 
222
        text = g_strrstr (name,"/");
 
223
        if (text != NULL)
 
224
        {
 
225
                text++;
 
226
                utf8_string = g_filename_display_name (text);
 
227
        }
 
228
        else
 
229
                utf8_string = g_filename_display_name (name);
 
230
 
 
231
        return utf8_string;
 
232
}
 
233
 
 
234
gchar *xa_escape_filename (gchar *filename,gchar *meta_chars)
 
235
{
 
236
        return xa_escape_common_chars (filename,meta_chars,'\\',0);
 
237
}
 
238
 
 
239
gchar *xa_strip_current_working_dir_from_path(gchar *working_dir,gchar *filename)
 
240
{
 
241
        gchar *slash;
 
242
        int len = 0;
 
243
 
 
244
        if (working_dir == NULL)
 
245
                return filename;
 
246
        len = strlen(working_dir)+1;
 
247
        slash = g_strrstr(filename,"/");
 
248
        if (slash == NULL || ! g_path_is_absolute(filename))
 
249
                return filename;
 
250
 
 
251
        return filename+len;
 
252
}
 
253
 
 
254
void xa_cat_filenames (XArchive *archive,GSList *list,GString *data)
 
255
{
 
256
        gchar *basename, *name, *e_filename;
 
257
        GSList *slist = list;
 
258
 
 
259
        while (slist)
 
260
        {
 
261
                if (archive->location_entry_path != NULL)
 
262
                {
 
263
                        if (archive->full_path == 0)
 
264
                        {
 
265
                                basename = xa_strip_current_working_dir_from_path(archive->working_dir ? archive->working_dir : archive->tmp,slist->data);
 
266
                                name = g_strconcat(archive->location_entry_path,basename,NULL);
 
267
                                e_filename = xa_escape_filename(name,"$'`\"\\!?* ()[]&|:;<>#");
 
268
                                g_string_prepend (data,e_filename);
 
269
                                g_string_prepend_c (data,' ');
 
270
                                g_free(name);
 
271
                        }
 
272
                        else
 
273
                        {
 
274
                                name = g_strconcat(archive->location_entry_path,slist->data,NULL);
 
275
                                e_filename = xa_escape_filename(name,"$'`\"\\!?* ()[]&|:;<>#");
 
276
                                g_string_prepend (data,e_filename);
 
277
                                g_string_prepend_c (data,' ');
 
278
                                g_free(name);
 
279
                        }
 
280
                }
 
281
                else
 
282
                {
 
283
                        if (archive->full_path == 0)
 
284
                        {
 
285
                                basename = xa_strip_current_working_dir_from_path(archive->working_dir ? archive->working_dir : archive->tmp,slist->data);
 
286
                                e_filename = xa_escape_filename(basename,"$'`\"\\!?* ()[]&|:;<>#");
 
287
                                g_string_prepend (data,e_filename);
 
288
                                g_string_prepend_c (data,' ');
 
289
                        }
 
290
                        else
 
291
                        {
 
292
                                e_filename = xa_escape_filename(slist->data,"$'`\"\\!?* ()[]&|:;<>#");
 
293
                                g_string_prepend (data,e_filename);
 
294
                                g_string_prepend_c (data,' ');
 
295
                        }
 
296
                }
 
297
                slist = slist->next;
 
298
        }
 
299
}
 
300
 
 
301
GSList *xa_slist_copy(GSList *list)
 
302
{
 
303
        GSList *x,*y = NULL;
 
304
        x = list;
 
305
 
 
306
        while (x)
 
307
        {
 
308
                y = g_slist_prepend(y,g_strdup(x->data));
 
309
                x = x->next;
 
310
        }
 
311
        return g_slist_reverse(y);
 
312
}
 
313
 
 
314
void xa_recurse_local_directory(gchar *path,GSList **list,gboolean recurse,gint type)
 
315
{
 
316
        DIR *dir;
 
317
        struct dirent *dirlist;
 
318
        gchar *fullname = NULL,*basename = NULL;
 
319
        gboolean is_dir;
 
320
 
 
321
        dir = opendir(path);
 
322
        is_dir = g_file_test(path,G_FILE_TEST_IS_DIR);
 
323
        if (is_dir && type != XARCHIVETYPE_ARJ && is_tar_compressed(type) == FALSE)
 
324
                *list = g_slist_prepend(*list,g_strdup(path));
 
325
        if (dir == NULL)
 
326
        {
 
327
                if (is_dir == FALSE)
 
328
                {
 
329
                        basename = g_path_get_basename(path);
 
330
                        *list = g_slist_prepend(*list,basename);
293
331
                        return;
294
332
                }
295
 
                else
296
 
                {
297
 
                        x = g_strconcat (  slash , " - " , "Xarchiver " , VERSION , NULL);
298
 
                        x++;
299
 
                        gtk_window_set_title ( GTK_WINDOW (window) , x);
300
 
                        x--;
301
 
                        g_free (x);
302
 
                }
 
333
        }       
 
334
 
 
335
        while ((dirlist = readdir(dir)))
 
336
        {
 
337
                if (strcmp(dirlist->d_name,".") == 0 || strcmp(dirlist->d_name,"..") == 0)
 
338
                        continue;
 
339
                fullname = g_strconcat (path,"/",dirlist->d_name,NULL);
 
340
                is_dir = g_file_test(fullname,G_FILE_TEST_IS_DIR);
 
341
                if ( ! is_dir)
 
342
                        *list = g_slist_prepend(*list,fullname);
 
343
                if (recurse && is_dir)
 
344
                        xa_recurse_local_directory(fullname,list,recurse,type);
303
345
        }
 
346
        closedir(dir);
304
347
}