]> cygwin.com Git - cygwin-apps/setup.git/blob - filemanip.cc
2002-05-02 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 "win32.h"
25
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <ctype.h>
29
30 #include "filemanip.h"
31
32 unsigned int
33 get_file_size (String const &name)
34 {
35 HANDLE h;
36 WIN32_FIND_DATA buf;
37 DWORD ret = 0;
38
39 h = FindFirstFileA (name.cstr_oneuse(), &buf);
40 if (h != INVALID_HANDLE_VALUE)
41 {
42 if (buf.nFileSizeHigh == 0)
43 ret = buf.nFileSizeLow;
44 FindClose (h);
45 }
46 return ret;
47 }
48
49 String
50 base (String const &aString)
51 {
52 if (!aString.size())
53 return 0;
54 const char *t = aString.cstr();
55 const char *s = t;
56 String rv = s;
57 while (*s)
58 {
59 if ((*s == '/' || *s == ':' || *s == '\\') && s[1])
60 rv = s + 1;
61 s++;
62 }
63 delete[] t;
64 return rv;
65 }
66
67 /* returns the number of characters of path that
68 * precede the extension
69 */
70 int
71 find_tar_ext (const char *path)
72 {
73 char *end = strchr (path, '\0');
74 /* check in longest first order */
75 char *ext;
76 if ((ext = strstr (path, ".tar.bz2")) && (end - ext) == 8)
77 return ext - path;
78 if ((ext = strstr (path, ".tar.gz")) && (end - ext) == 7)
79 return ext - path;
80 if ((ext = strstr (path, ".tar")) && (end - ext) == 4)
81 return ext - path;
82 return 0;
83 }
84
85 /* Parse a filename into package, version, and extension components. */
86 int
87 parse_filename (String const &in_fn, fileparse & f)
88 {
89 char *p, *ver;
90 char fn[in_fn.size() + 1];
91 strcpy (fn, in_fn.cstr_oneuse());
92
93 int n = find_tar_ext (fn);
94
95 if (!n)
96 return 0;
97
98 f.tail = fn + n;
99 fn[n] = '\0';
100 f.pkg = f.what = String();
101 p = base (fn).cstr();
102 for (ver = p; *ver; ver++)
103 if (*ver == '-')
104 if (isdigit (ver[1]))
105 {
106 *ver++ = 0;
107 f.pkg = p;
108 break;
109 }
110 else if (strcasecmp (ver, "-src") == 0 ||
111 strcasecmp (ver, "-patch") == 0)
112 {
113 *ver++ = 0;
114 f.pkg = p;
115 f.what = strlwr (ver);
116 ver = strchr (ver, '\0');
117 break;
118 }
119
120 if (!f.pkg.size())
121 f.pkg = p;
122
123 if (!f.what.size())
124 {
125 int n;
126 char *p1 = strchr (ver, '\0');
127 if ((p1 -= 4) >= ver && strcasecmp (p1, "-src") == 0)
128 n = 4;
129 else if ((p1 -= 2) >= ver && strcasecmp (p1, "-patch") == 0)
130 n = 6;
131 else
132 n = 0;
133 if (n)
134 {
135 // get the 'src' or 'patch'.
136 f.what = p1 + 1;
137 }
138 }
139
140 f.ver = *ver ? ver : "0.0";
141 delete[] p;
142 return 1;
143 }
144
145 String
146 backslash (String const & aString)
147 {
148 char * tempString = aString.cstr();
149 for (char *t = tempString; *t; t++)
150 if (*t == '/')
151 *t = '\\';
152 String theString(tempString);
153 delete[] tempString;
154 return theString;
155 }
This page took 0.039187 seconds and 5 git commands to generate.