]> cygwin.com Git - cygwin-apps/setup.git/blob - filemanip.cc
2002-05-19 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / filemanip.cc
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
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <ctype.h>
27
28 #include "filemanip.h"
29 #include <strings.h>
30 #include "io_stream.h"
31
32 /* legacy wrapper.
33 * Clients should use io_stream.get_size() */
34 size_t
35 get_file_size (String const &name)
36 {
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;
44 }
45
46 String
47 base (String const &aString)
48 {
49 if (!aString.size())
50 return 0;
51 const char *t = aString.cstr();
52 const char *s = t;
53 String rv = s;
54 while (*s)
55 {
56 if ((*s == '/' || *s == ':' || *s == '\\') && s[1])
57 rv = s + 1;
58 s++;
59 }
60 delete[] t;
61 return rv;
62 }
63
64 /* returns the number of characters of path that
65 * precede the extension
66 */
67 int
68 find_tar_ext (const char *path)
69 {
70 char *end = strchr (path, '\0');
71 /* check in longest first order */
72 const char *ext;
73 if ((ext = trail (path, ".tar.bz2")) && (end - ext) == 8)
74 return ext - path;
75 if ((ext = trail (path, ".tar.gz")) && (end - ext) == 7)
76 return ext - path;
77 if ((ext = trail (path, ".tar")) && (end - ext) == 4)
78 return ext - path;
79 return 0;
80 }
81
82 /* Parse a filename into package, version, and extension components. */
83 int
84 parse_filename (String const &in_fn, fileparse & f)
85 {
86 char *p, *ver;
87 char fn[in_fn.size() + 1];
88 strcpy (fn, in_fn.cstr_oneuse());
89
90 int n = find_tar_ext (fn);
91
92 if (!n)
93 return 0;
94
95 f.tail = fn + n;
96 fn[n] = '\0';
97 f.pkg = f.what = String();
98 p = base (fn).cstr();
99 char const *ext;
100 /* TODO: make const and non-const trail variant. */
101 if ((ext = trail (p, "-src")))
102 {
103 f.what = "-src";
104 *(char *)ext = '\0';
105 }
106 else if ((ext = trail (p, "-patch")))
107 {
108 f.what = "-patch";
109 *(char *)ext = '\0';
110 }
111 for (ver = p; *ver; ver++)
112 if (*ver == '-')
113 if (isdigit (ver[1]))
114 {
115 *ver++ = 0;
116 f.pkg = p;
117 break;
118 }
119 else if (strcasecmp (ver, "-src") == 0 ||
120 strcasecmp (ver, "-patch") == 0)
121 {
122 *ver++ = 0;
123 f.pkg = p;
124 f.what = strlwr (ver);
125 ver = strchr (ver, '\0');
126 break;
127 }
128
129 if (!f.pkg.size())
130 f.pkg = p;
131
132 if (!f.what.size())
133 {
134 int n;
135 char *p1 = strchr (ver, '\0');
136 if ((p1 -= 4) >= ver && strcasecmp (p1, "-src") == 0)
137 n = 4;
138 else if ((p1 -= 2) >= ver && strcasecmp (p1, "-patch") == 0)
139 n = 6;
140 else
141 n = 0;
142 if (n)
143 {
144 // get the 'src' or 'patch'.
145 f.what = p1 + 1;
146 }
147 }
148
149 f.ver = *ver ? ver : "0.0";
150 delete[] p;
151 return 1;
152 }
153
154 const char *
155 trail (const char *haystack, const char *needle)
156 {
157 /* See if the path ends with a specific suffix.
158 Just return if it doesn't. */
159 unsigned len = strlen (haystack);
160 int prefix_len = len - strlen (needle);
161 if (prefix_len < 0
162 || strcasecmp (haystack += prefix_len, needle) != 0)
163 return NULL;
164 return haystack;
165 }
166
167 String
168 backslash (String const & aString)
169 {
170 char * tempString = aString.cstr();
171 for (char *t = tempString; *t; t++)
172 if (*t == '/')
173 *t = '\\';
174 String theString(tempString);
175 delete[] tempString;
176 return theString;
177 }
This page took 0.043016 seconds and 5 git commands to generate.