~ubuntu-branches/ubuntu/dapper/file-roller/dapper-updates

« back to all changes in this revision

Viewing changes to src/file-utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-12-13 00:19:53 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051213001953-0klipgnhk6vikhrt
Tags: 2.13.2-0ubuntu1
* New upstream version:
  - Fixed bug #323713: "Save as" should default to current archive filename
    (Ubuntu: #20132)
  - Fixed bug #315069: file-roller: drag 'n drop doesn't work for more
    than 1 file.
  - Fixed bug #323534: Passwords improperly escaped for zip files
  - Do not open a progress dialog when dragging files, use a progressbar
    in the statusbar instead.
  - Fixed bug #316364: Nautilus dependency should be optional
  - Fixed bug #311821: ascending and descending indicators are opposite
    from expected.
  - Fixed bug #317423: Conflicting mnemonic in "Extract" dialog.
  - Fixed bug #323068: file-roller fails in chosing extract-to directory
  - Fixed bug #322197: Improve Naming Filename when create new Archive file
  - Fixed bug #316564: needs unzip AND zip
    (Ubuntu: #15595)
  - Simplified the add dialog populating the file type combobox with the
    extensions instead of the descriptions.
  - Set progress dialog display delay to 1 second.
  - More HIG compliant progress dialog.
  - Use a single command execution to add many files and folders to an
    archive to speed up the operation.
  - Allow to stop creation of a new archive.
  - Correctly associate fr to 7zip files.
* debian/patches/03_lp-autoconf.patch:
  - updated

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include <libgnomevfs/gnome-vfs-mime.h>
40
40
#include <gconf/gconf-client.h>
41
41
#include "file-utils.h"
42
 
#include "utf8-fnmatch.h"
 
42
#include "glib-utils.h"
43
43
#include "main.h"
44
44
 
45
45
 
48
48
#endif                                        
49
49
                                            
50
50
#define BUF_SIZE 4096
51
 
#define MAX_PATTERNS 128
52
51
 
53
52
 
54
53
gboolean
462
461
}
463
462
 
464
463
 
465
 
/* counts how many characters to escape in @str. */
466
 
static int
467
 
count_chars_to_escape (const char *str, 
468
 
                       const char *meta_chars)
469
 
{
470
 
        int         meta_chars_n = strlen (meta_chars);
471
 
        const char *s;
472
 
        int         n = 0;
473
 
 
474
 
        for (s = str; *s != 0; s++) {
475
 
                int i;
476
 
                for (i = 0; i < meta_chars_n; i++) 
477
 
                        if (*s == meta_chars[i]) {
478
 
                                n++;
479
 
                                break;
480
 
                        }
481
 
        }
482
 
        return n;
483
 
}
484
 
 
485
 
 
486
 
gboolean
487
 
strchrs (const char *str,
488
 
         const char *chars)
489
 
{
490
 
        const char *c;
491
 
        for (c = chars; *c != '\0'; c++)
492
 
                if (strchr (str, *c) != NULL)
493
 
                        return TRUE;
494
 
        return FALSE;
495
 
}
496
 
 
497
 
 
498
 
char*
499
 
escape_str_common (const char *str, 
500
 
                   const char *meta_chars,
501
 
                   const char  prefix,
502
 
                   const char  postfix)
503
 
{
504
 
        int         meta_chars_n = strlen (meta_chars);
505
 
        char       *escaped;
506
 
        int         i, new_l, extra_chars = 0;
507
 
        const char *s;
508
 
        char       *t;
509
 
 
510
 
        if (str == NULL) 
511
 
                return NULL;
512
 
 
513
 
        if (prefix)
514
 
                extra_chars++;
515
 
        if (postfix)
516
 
                extra_chars++;
517
 
 
518
 
        new_l = strlen (str) + (count_chars_to_escape (str, meta_chars) * extra_chars);
519
 
        escaped = g_malloc (new_l + 1);
520
 
 
521
 
        s = str;
522
 
        t = escaped;
523
 
        while (*s) {
524
 
                gboolean is_bad = FALSE;
525
 
                for (i = 0; (i < meta_chars_n) && !is_bad; i++)
526
 
                        is_bad = (*s == meta_chars[i]);
527
 
                if (is_bad && prefix)
528
 
                        *t++ = prefix;
529
 
                *t++ = *s++;
530
 
                if (is_bad && postfix)
531
 
                        *t++ = postfix;
532
 
        }
533
 
        *t = 0;
534
 
 
535
 
        return escaped;
536
 
}
537
 
 
538
 
 
539
 
/* escape with backslash the string @str. */
540
 
char*
541
 
escape_str (const char *str, 
542
 
            const char *meta_chars)
543
 
{
544
 
        return escape_str_common (str, meta_chars, '\\', 0);
545
 
}
546
 
 
547
 
 
548
 
/* remove backslashes from a string. */
549
 
char*
550
 
unescape_str (const char  *str)
551
 
{
552
 
        char       *new_str;
553
 
        const char *s;
554
 
        char       *t;
555
 
 
556
 
        if (str == NULL)
557
 
                return NULL;
558
 
 
559
 
        new_str = g_malloc (strlen (str) + 1);
560
 
 
561
 
        s = str;
562
 
        t = new_str;
563
 
        while (*s) {
564
 
                if (*s == '\\')
565
 
                        s++;
566
 
                *t++ = *s++;
567
 
        }
568
 
        *t = 0;
569
 
 
570
 
        return new_str;
571
 
}
572
 
 
573
 
 
574
 
/* escape with backslash the file name. */
575
 
char*
576
 
shell_escape (const char *filename)
577
 
{
578
 
        return escape_str (filename, "$\'`\"\\!?* ()[]&|@#:;");
579
 
}
580
 
 
581
 
 
582
 
gboolean
583
 
match_patterns (char       **patterns, 
584
 
                const char  *string,
585
 
                int          flags)
586
 
{
587
 
        int i;
588
 
        int result;
589
 
       
590
 
        if (patterns[0] == NULL)
591
 
                return TRUE;
592
 
        
593
 
        if (string == NULL)
594
 
                return FALSE;
595
 
        
596
 
        result = FNM_NOMATCH;
597
 
        i = 0;
598
 
        while ((result != 0) && (patterns[i] != NULL)) {
599
 
                result = g_utf8_fnmatch (patterns[i], string, flags);
600
 
                i++;
601
 
        }
602
 
 
603
 
        return (result == 0);
604
 
}
605
 
 
606
 
 
607
 
static const char *
608
 
g_utf8_strstr (const char *haystack, const char *needle)
609
 
{
610
 
        const char *s;
611
 
        gsize       i;
612
 
        gsize       haystack_len = g_utf8_strlen (haystack, -1);
613
 
        gsize       needle_len = g_utf8_strlen (needle, -1);
614
 
        int         needle_size = strlen (needle);
615
 
 
616
 
        s = haystack;
617
 
        for (i = 0; i <= haystack_len - needle_len; i++) {
618
 
                if (strncmp (s, needle, needle_size) == 0)
619
 
                        return s;
620
 
                s = g_utf8_next_char(s);
621
 
        }
622
 
 
623
 
        return NULL;
624
 
}
625
 
 
626
 
 
627
 
static char**
628
 
g_utf8_strsplit (const char *string,
629
 
                 const char *delimiter,
630
 
                 int         max_tokens)
631
 
{
632
 
        GSList      *string_list = NULL, *slist;
633
 
        char       **str_array;
634
 
        const char  *s;
635
 
        guint        n = 0;
636
 
        const char  *remainder;
637
 
 
638
 
        g_return_val_if_fail (string != NULL, NULL);
639
 
        g_return_val_if_fail (delimiter != NULL, NULL);
640
 
        g_return_val_if_fail (delimiter[0] != '\0', NULL);
641
 
        
642
 
        if (max_tokens < 1)
643
 
                max_tokens = G_MAXINT;
644
 
        
645
 
        remainder = string;
646
 
        s = g_utf8_strstr (remainder, delimiter);
647
 
        if (s != NULL) {
648
 
                gsize delimiter_size = strlen (delimiter);
649
 
                
650
 
                while (--max_tokens && (s != NULL)) {
651
 
                        gsize  size = s - remainder;
652
 
                        char  *new_string;
653
 
 
654
 
                        new_string = g_new (char, size + 1);
655
 
                        strncpy (new_string, remainder, size);
656
 
                        new_string[size] = 0;
657
 
 
658
 
                        string_list = g_slist_prepend (string_list, new_string);
659
 
                        n++;
660
 
                        remainder = s + delimiter_size;
661
 
                        s = g_utf8_strstr (remainder, delimiter);
662
 
                }
663
 
        }
664
 
        if (*string) {
665
 
                n++;
666
 
                string_list = g_slist_prepend (string_list, g_strdup (remainder));
667
 
        }
668
 
        
669
 
        str_array = g_new (char*, n + 1);
670
 
        
671
 
        str_array[n--] = NULL;
672
 
        for (slist = string_list; slist; slist = slist->next)
673
 
                str_array[n--] = slist->data;
674
 
        
675
 
        g_slist_free (string_list);
676
 
        
677
 
        return str_array;
678
 
}
679
 
 
680
 
 
681
 
static char*
682
 
g_utf8_strchug (char *string)
683
 
{
684
 
        char     *scan;
685
 
        gunichar  c;
686
 
 
687
 
        g_return_val_if_fail (string != NULL, NULL);
688
 
        
689
 
        scan = string;
690
 
        c = g_utf8_get_char (scan);
691
 
        while (g_unichar_isspace (c)) {
692
 
                scan = g_utf8_next_char (scan);
693
 
                c = g_utf8_get_char (scan);
694
 
        }
695
 
 
696
 
        g_memmove (string, scan, strlen (scan) + 1);
697
 
        
698
 
        return string;
699
 
}
700
 
 
701
 
 
702
 
static char*
703
 
g_utf8_strchomp (char *string)
704
 
{
705
 
        char   *scan;
706
 
        gsize   len;
707
 
 
708
 
        g_return_val_if_fail (string != NULL, NULL);
709
 
        
710
 
        len = g_utf8_strlen (string, -1);
711
 
 
712
 
        if (len == 0)
713
 
                return string;
714
 
 
715
 
        scan = g_utf8_offset_to_pointer (string, len - 1);
716
 
 
717
 
        while (len--) {
718
 
                gunichar c = g_utf8_get_char (scan);
719
 
                if (g_unichar_isspace (c))
720
 
                        *scan = '\0';
721
 
                else
722
 
                        break;
723
 
                scan = g_utf8_find_prev_char (string, scan);
724
 
        }
725
 
        
726
 
        return string;
727
 
}
728
 
 
729
 
 
730
 
#define g_utf8_strstrip(string)    g_utf8_strchomp (g_utf8_strchug (string))
731
 
 
732
 
 
733
 
char **
734
 
search_util_get_patterns (const char *pattern_string)
735
 
{
736
 
        char **patterns;
737
 
        int    i;
738
 
        
739
 
        patterns = g_utf8_strsplit (pattern_string, ";", MAX_PATTERNS);
740
 
        for (i = 0; patterns[i] != NULL; i++) 
741
 
                patterns[i] = g_utf8_strstrip (patterns[i]);
742
 
        
743
 
        return patterns;
744
 
}
745
 
 
746
 
 
747
464
GnomeVFSFileSize
748
465
get_dest_free_space (const char  *path)
749
466
{
860
577
 
861
578
 
862
579
char *
863
 
_g_strdup_with_max_size (const char *s,
864
 
                         int         max_size)
865
 
{
866
 
        char *result;
867
 
        int   l = strlen (s);
868
 
 
869
 
        if (l > max_size) {
870
 
                char *first_half;
871
 
                char *second_half;
872
 
                int   offset;
873
 
                int   half_max_size = max_size / 2 + 1;
874
 
 
875
 
                first_half = g_strndup (s, half_max_size);
876
 
                offset = half_max_size + l - max_size;
877
 
                second_half = g_strndup (s + offset, half_max_size);
878
 
 
879
 
                result = g_strconcat (first_half, "...", second_half, NULL);
880
 
 
881
 
                g_free (first_half);
882
 
                g_free (second_half);
883
 
        } else
884
 
                result = g_strdup (s);
885
 
 
886
 
        return result;
887
 
}
888
 
 
889
 
 
890
 
const char *
891
 
eat_spaces (const char *line)
892
 
{
893
 
        if (line == NULL)
894
 
                return NULL;
895
 
        while ((*line == ' ') && (*line != 0))
896
 
                line++;
897
 
        return line;
898
 
}
899
 
 
900
 
 
901
 
char **
902
 
split_line (const char *line, 
903
 
            int         n_fields)
904
 
{
905
 
        char       **fields;
906
 
        const char  *scan, *field_end;
907
 
        int          i;
908
 
 
909
 
        fields = g_new0 (char *, n_fields + 1);
910
 
        fields[n_fields] = NULL;
911
 
 
912
 
        scan = eat_spaces (line);
913
 
        for (i = 0; i < n_fields; i++) {
914
 
                if (scan == NULL) {
915
 
                        fields[i] = NULL;
916
 
                        continue;
917
 
                }
918
 
                field_end = strchr (scan, ' ');
919
 
                if (field_end != NULL) {
920
 
                        fields[i] = g_strndup (scan, field_end - scan);
921
 
                        scan = eat_spaces (field_end);
922
 
                }
923
 
        }
924
 
 
925
 
        return fields;
926
 
}
927
 
 
928
 
 
929
 
const char *
930
 
get_last_field (const char *line,
931
 
                int         last_field)
932
 
{
933
 
        const char *field;
934
 
        int         i;
935
 
 
936
 
        if (line == NULL)
937
 
                return NULL;
938
 
 
939
 
        last_field--;
940
 
        field = eat_spaces (line);
941
 
        for (i = 0; i < last_field; i++) {
942
 
                if (field == NULL)
943
 
                        return NULL;
944
 
                field = strchr (field, ' ');
945
 
                field = eat_spaces (field);
946
 
        }
947
 
 
948
 
        return field;
949
 
}
950
 
 
951
 
 
952
 
char *
953
580
get_temp_work_dir (void)
954
581
{
955
582
        char temp_dir_template[] = "/tmp/fr-XXXXXX";
1074
701
 
1075
702
 
1076
703
char *
1077
 
str_substitute (const char *str,
1078
 
                const char *from_str,
1079
 
                const char *to_str)
1080
 
{
1081
 
        char    **tokens;
1082
 
        int       i;
1083
 
        GString  *gstr;
1084
 
 
1085
 
        if (str == NULL)
1086
 
                return NULL;
1087
 
 
1088
 
        if (from_str == NULL)
1089
 
                return g_strdup (str);
1090
 
 
1091
 
        if (strcmp (str, from_str) == 0)
1092
 
                return g_strdup (to_str);
1093
 
 
1094
 
        tokens = g_strsplit (str, from_str, -1);
1095
 
 
1096
 
        gstr = g_string_new (NULL);
1097
 
        for (i = 0; tokens[i] != NULL; i++) {
1098
 
                gstr = g_string_append (gstr, tokens[i]);
1099
 
                if ((to_str != NULL) && (tokens[i+1] != NULL))
1100
 
                        gstr = g_string_append (gstr, to_str);
1101
 
        }
1102
 
 
1103
 
        return g_string_free (gstr, FALSE);
1104
 
}
1105
 
 
1106
 
 
1107
 
char *
1108
704
escape_uri (const char *uri)
1109
705
{
1110
706
        const char *start = NULL;