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