~ubuntu-branches/ubuntu/natty/file-roller/natty

« back to all changes in this revision

Viewing changes to src/glib-utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette
  • Date: 2008-12-31 16:44:14 UTC
  • mfrom: (1.5.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 93.
  • Revision ID: james.westby@ubuntu.com-20081231164414-op9c0bo27snmq5yg
Tags: 2.24.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <glib/gi18n.h>
27
27
#include <glib/gprintf.h>
28
28
#include "glib-utils.h"
29
 
#include "utf8-fnmatch.h"
30
29
 
31
30
 
32
31
#define MAX_PATTERNS 128
160
159
}
161
160
 
162
161
 
163
 
/* remove backslashes from a string. */
164
 
char*
165
 
unescape_str (const char  *str)
166
 
{
167
 
        char       *new_str;
168
 
        const char *s;
169
 
        char       *t;
170
 
 
171
 
        if (str == NULL)
172
 
                return NULL;
173
 
 
174
 
        new_str = g_malloc (strlen (str) + 1);
175
 
 
176
 
        s = str;
177
 
        t = new_str;
178
 
        while (*s) {
179
 
                if (*s == '\\')
180
 
                        s++;
181
 
                *t++ = *s++;
182
 
        }
183
 
        *t = 0;
184
 
 
185
 
        return new_str;
186
 
}
187
 
 
188
 
 
189
162
/* escape with backslash the file name. */
190
163
char*
191
164
shell_escape (const char *filename)
317
290
}
318
291
 
319
292
 
 
293
#define g_utf8_strstrip(string)    g_utf8_strchomp (g_utf8_strchug (string))
 
294
 
 
295
 
320
296
gboolean
321
 
match_patterns (char       **patterns,
322
 
                const char  *string,
323
 
                int          flags)
 
297
match_regexps (GRegex           **regexps,
 
298
               const char        *string,
 
299
               GRegexMatchFlags   match_options)
324
300
{
325
 
        int i;
326
 
        int result;
327
 
 
328
 
        if (patterns[0] == NULL)
 
301
        gboolean matched;
 
302
        int      i;
 
303
        
 
304
        if ((regexps == NULL) || (regexps[0] == NULL))
329
305
                return TRUE;
330
306
 
331
307
        if (string == NULL)
332
308
                return FALSE;
333
 
 
334
 
        result = FNM_NOMATCH;
335
 
        i = 0;
336
 
        while ((result != 0) && (patterns[i] != NULL)) {
337
 
                result = g_utf8_fnmatch (patterns[i], string, flags);
338
 
                i++;
339
 
        }
340
 
 
341
 
        return (result == 0);
342
 
}
343
 
 
344
 
 
345
 
#define g_utf8_strstrip(string)    g_utf8_strchomp (g_utf8_strchug (string))
 
309
        
 
310
        matched = FALSE;
 
311
        for (i = 0; regexps[i] != NULL; i++)
 
312
                if (g_regex_match (regexps[i], string, match_options, NULL)) {
 
313
                        matched = TRUE;
 
314
                        break;
 
315
                }
 
316
                
 
317
        return matched;
 
318
}
 
319
 
 
320
 
 
321
void
 
322
free_regexps (GRegex **regexps)
 
323
{
 
324
        int i;
 
325
        
 
326
        if (regexps == NULL) 
 
327
                return;
 
328
                
 
329
        for (i = 0; regexps[i] != NULL; i++)
 
330
                g_regex_unref (regexps[i]);
 
331
        g_free (regexps);
 
332
}
346
333
 
347
334
 
348
335
char **
351
338
        char **patterns;
352
339
        int    i;
353
340
 
 
341
        if (pattern_string == NULL)
 
342
                return NULL;
 
343
 
354
344
        patterns = g_utf8_strsplit (pattern_string, ";", MAX_PATTERNS);
355
 
        for (i = 0; patterns[i] != NULL; i++)
356
 
                patterns[i] = g_utf8_strstrip (patterns[i]);
 
345
        for (i = 0; patterns[i] != NULL; i++) {
 
346
                char *p1, *p2;
 
347
                
 
348
                p1 = g_utf8_strstrip (patterns[i]);
 
349
                p2 = str_substitute (p1, ".", "\\.");
 
350
                patterns[i] = str_substitute (p2, "*", ".*");
 
351
                
 
352
                g_free (p2);
 
353
                g_free (p1);
 
354
        }
357
355
 
358
356
        return patterns;
359
357
}
360
358
 
361
359
 
 
360
GRegex **
 
361
search_util_get_regexps (const char         *pattern_string,
 
362
                         GRegexCompileFlags  compile_options)
 
363
{
 
364
        char   **patterns;
 
365
        GRegex **regexps;
 
366
        int      i;
 
367
                
 
368
        patterns = search_util_get_patterns (pattern_string);
 
369
        if (patterns == NULL)
 
370
                return NULL;
 
371
                
 
372
        regexps = g_new0 (GRegex*, n_fields (patterns) + 1);
 
373
        for (i = 0; patterns[i] != NULL; i++) 
 
374
                regexps[i] = g_regex_new (patterns[i], 
 
375
                                          G_REGEX_OPTIMIZE | compile_options, 
 
376
                                          G_REGEX_MATCH_NOTEMPTY, 
 
377
                                          NULL);
 
378
        g_strfreev (patterns);
 
379
        
 
380
        return regexps;
 
381
}
 
382
 
 
383
 
362
384
char *
363
385
_g_strdup_with_max_size (const char *s,
364
386
                         int         max_size)
559
581
                g_ptr_array_index (array, j) = tmp;
560
582
        }
561
583
}
 
584
 
 
585
 
 
586
GHashTable *static_strings = NULL;
 
587
 
 
588
 
 
589
const char *
 
590
get_static_string (const char *s)
 
591
{
 
592
        const char *result;
 
593
 
 
594
        if (s == NULL)
 
595
                return NULL;
 
596
 
 
597
        if (static_strings == NULL)
 
598
                static_strings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
 
599
 
 
600
        if (! g_hash_table_lookup_extended (static_strings, s, (gpointer*) &result, NULL)) {
 
601
                result = g_strdup (s);
 
602
                g_hash_table_insert (static_strings,
 
603
                                     (gpointer) result,
 
604
                                     GINT_TO_POINTER (1));
 
605
        }
 
606
 
 
607
        return result;
 
608
}
 
609
 
 
610
 
 
611
char*
 
612
g_uri_display_basename (const char  *uri)
 
613
{
 
614
        char *e_name, *name;
 
615
        
 
616
        e_name = g_filename_display_basename (uri);
 
617
        name = g_uri_unescape_string (e_name, "");
 
618
        g_free (e_name);
 
619
        
 
620
        return name;
 
621
}