]>
cygwin.com Git - cygwin-apps/setup.git/blob - filemanip.cc
2 * Copyright (c) 2000, 2001, Red Hat, Inc.
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.
9 * A copy of the GNU General Public License can be found at
12 * Written by Robert Collins <rbtcollins@redhat.com>
16 /* The purpose of this file is to put all general purpose file manipulation
20 static const char *cvsid
=
28 #include "filemanip.h"
30 #include "io_stream.h"
33 * Clients should use io_stream.get_size() */
35 get_file_size (String
const &name
)
37 io_stream
*theFile
= io_stream::open (name
, "rb");
39 /* To consider: throw an exception ? */
41 ssize_t rv
= theFile
->get_size();
47 base (String
const &aString
)
51 const char *t
= aString
.cstr();
56 if ((*s
== '/' || *s
== ':' || *s
== '\\') && s
[1])
64 /* returns the number of characters of path that
65 * precede the extension
68 find_tar_ext (const char *path
)
70 char *end
= strchr (path
, '\0');
71 /* check in longest first order */
73 if ((ext
= trail (path
, ".tar.bz2")) && (end
- ext
) == 8)
75 if ((ext
= trail (path
, ".tar.gz")) && (end
- ext
) == 7)
77 if ((ext
= trail (path
, ".tar")) && (end
- ext
) == 4)
82 /* Parse a filename into package, version, and extension components. */
84 parse_filename (String
const &in_fn
, fileparse
& f
)
87 char fn
[in_fn
.size() + 1];
88 strcpy (fn
, in_fn
.cstr_oneuse());
90 int n
= find_tar_ext (fn
);
97 f
.pkg
= f
.what
= String();
100 /* TODO: make const and non-const trail variant. */
101 if ((ext
= trail (p
, "-src")))
106 else if ((ext
= trail (p
, "-patch")))
111 for (ver
= p
; *ver
; ver
++)
113 if (isdigit (ver
[1]))
119 else if (strcasecmp (ver
, "-src") == 0 ||
120 strcasecmp (ver
, "-patch") == 0)
124 f
.what
= strlwr (ver
);
125 ver
= strchr (ver
, '\0');
135 char *p1
= strchr (ver
, '\0');
136 if ((p1
-= 4) >= ver
&& strcasecmp (p1
, "-src") == 0)
138 else if ((p1
-= 2) >= ver
&& strcasecmp (p1
, "-patch") == 0)
144 // get the 'src' or 'patch'.
149 f
.ver
= *ver
? ver
: "0.0";
155 trail (const char *haystack
, const char *needle
)
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
);
162 || strcasecmp (haystack
+= prefix_len
, needle
) != 0)
168 backslash (String
const & aString
)
170 char * tempString
= aString
.cstr();
171 for (char *t
= tempString
; *t
; t
++)
174 String
theString(tempString
);
This page took 0.043016 seconds and 5 git commands to generate.