]>
Commit | Line | Data |
---|---|---|
341988b9 RC |
1 | /* |
2 | * Copyright (c) 2000, 2001, Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins <rbtcollins@redhat.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* The purpose of this file is to put all general purpose file manipulation | |
17 | code in one place. */ | |
18 | ||
19 | #if 0 | |
20 | static const char *cvsid = | |
21 | "\n%%% $Id$\n"; | |
22 | #endif | |
23 | ||
341988b9 RC |
24 | #include <stdio.h> |
25 | #include <unistd.h> | |
26 | #include <ctype.h> | |
27 | ||
28 | #include "filemanip.h" | |
e0a4db64 RC |
29 | #include <strings.h> |
30 | #include "io_stream.h" | |
341988b9 | 31 | |
e0a4db64 RC |
32 | /* legacy wrapper. |
33 | * Clients should use io_stream.get_size() */ | |
34 | size_t | |
3c054baf | 35 | get_file_size (String const &name) |
341988b9 | 36 | { |
e0a4db64 RC |
37 | io_stream *theFile = io_stream::open (name, "rb"); |
38 | if (!theFile) | |
39 | /* To consider: throw an exception ? */ | |
40 | return 0; | |
41 | ssize_t rv = theFile->get_size(); | |
42 | delete theFile; | |
43 | return rv; | |
341988b9 RC |
44 | } |
45 | ||
3c054baf RC |
46 | String |
47 | base (String const &aString) | |
341988b9 | 48 | { |
3c054baf | 49 | if (!aString.size()) |
341988b9 | 50 | return 0; |
3c054baf RC |
51 | const char *t = aString.cstr(); |
52 | const char *s = t; | |
53 | String rv = s; | |
341988b9 RC |
54 | while (*s) |
55 | { | |
56 | if ((*s == '/' || *s == ':' || *s == '\\') && s[1]) | |
57 | rv = s + 1; | |
58 | s++; | |
59 | } | |
3c054baf RC |
60 | delete[] t; |
61 | return rv; | |
341988b9 RC |
62 | } |
63 | ||
9835fb4a RC |
64 | /* returns the number of characters of path that |
65 | * precede the extension | |
66 | */ | |
341988b9 RC |
67 | int |
68 | find_tar_ext (const char *path) | |
69 | { | |
70 | char *end = strchr (path, '\0'); | |
71 | /* check in longest first order */ | |
0d4e0aad CF |
72 | const char *ext; |
73 | if ((ext = trail (path, ".tar.bz2")) && (end - ext) == 8) | |
9835fb4a | 74 | return ext - path; |
0d4e0aad | 75 | if ((ext = trail (path, ".tar.gz")) && (end - ext) == 7) |
9835fb4a | 76 | return ext - path; |
0d4e0aad | 77 | if ((ext = trail (path, ".tar")) && (end - ext) == 4) |
9835fb4a RC |
78 | return ext - path; |
79 | return 0; | |
341988b9 RC |
80 | } |
81 | ||
82 | /* Parse a filename into package, version, and extension components. */ | |
83 | int | |
3c054baf | 84 | parse_filename (String const &in_fn, fileparse & f) |
341988b9 RC |
85 | { |
86 | char *p, *ver; | |
528a8edb | 87 | char *fn = in_fn.cstr (); |
4e868a01 | 88 | int n; |
341988b9 | 89 | |
4e868a01 | 90 | if (fn == 0 || !(n = find_tar_ext (fn))) |
341988b9 RC |
91 | return 0; |
92 | ||
3c054baf | 93 | f.tail = fn + n; |
341988b9 | 94 | fn[n] = '\0'; |
3c054baf RC |
95 | f.pkg = f.what = String(); |
96 | p = base (fn).cstr(); | |
4e868a01 | 97 | delete[] fn; |
b401ef47 RC |
98 | char const *ext; |
99 | /* TODO: make const and non-const trail variant. */ | |
100 | if ((ext = trail (p, "-src"))) | |
101 | { | |
102 | f.what = "-src"; | |
103 | *(char *)ext = '\0'; | |
104 | } | |
105 | else if ((ext = trail (p, "-patch"))) | |
106 | { | |
107 | f.what = "-patch"; | |
108 | *(char *)ext = '\0'; | |
109 | } | |
341988b9 | 110 | for (ver = p; *ver; ver++) |
3c054baf | 111 | if (*ver == '-') |
341988b9 RC |
112 | if (isdigit (ver[1])) |
113 | { | |
114 | *ver++ = 0; | |
3c054baf | 115 | f.pkg = p; |
341988b9 RC |
116 | break; |
117 | } | |
118 | else if (strcasecmp (ver, "-src") == 0 || | |
119 | strcasecmp (ver, "-patch") == 0) | |
120 | { | |
121 | *ver++ = 0; | |
3c054baf RC |
122 | f.pkg = p; |
123 | f.what = strlwr (ver); | |
341988b9 RC |
124 | ver = strchr (ver, '\0'); |
125 | break; | |
126 | } | |
127 | ||
3c054baf RC |
128 | if (!f.pkg.size()) |
129 | f.pkg = p; | |
341988b9 | 130 | |
3c054baf | 131 | if (!f.what.size()) |
341988b9 RC |
132 | { |
133 | int n; | |
3c054baf | 134 | char *p1 = strchr (ver, '\0'); |
3c054baf | 135 | if ((p1 -= 4) >= ver && strcasecmp (p1, "-src") == 0) |
341988b9 | 136 | n = 4; |
3c054baf | 137 | else if ((p1 -= 2) >= ver && strcasecmp (p1, "-patch") == 0) |
341988b9 RC |
138 | n = 6; |
139 | else | |
140 | n = 0; | |
141 | if (n) | |
142 | { | |
7c6ef2c3 | 143 | // get the 'src' or 'patch'. |
3c054baf | 144 | f.what = p1 + 1; |
341988b9 RC |
145 | } |
146 | } | |
147 | ||
3c054baf RC |
148 | f.ver = *ver ? ver : "0.0"; |
149 | delete[] p; | |
341988b9 RC |
150 | return 1; |
151 | } | |
3c054baf | 152 | |
0d4e0aad CF |
153 | const char * |
154 | trail (const char *haystack, const char *needle) | |
155 | { | |
b401ef47 | 156 | /* See if the path ends with a specific suffix. |
0d4e0aad CF |
157 | Just return if it doesn't. */ |
158 | unsigned len = strlen (haystack); | |
159 | int prefix_len = len - strlen (needle); | |
160 | if (prefix_len < 0 | |
161 | || strcasecmp (haystack += prefix_len, needle) != 0) | |
162 | return NULL; | |
163 | return haystack; | |
164 | } | |
165 | ||
3c054baf RC |
166 | String |
167 | backslash (String const & aString) | |
168 | { | |
25e21380 | 169 | return aString.replace ('/', '\\'); |
3c054baf | 170 | } |