]>
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
=
25 #include "filemanip.h"
26 #include "io_stream.h"
32 * Clients should use io_stream.get_size() */
34 get_file_size (const std::string
& name
)
36 io_stream
*theFile
= io_stream::open (name
, "rb");
38 /* To consider: throw an exception ? */
40 ssize_t rv
= theFile
->get_size();
46 base (const std::string
& aString
)
50 const char *s
= aString
.c_str();
54 if ((*s
== '/' || *s
== ':' || *s
== '\\') && s
[1])
61 /* returns the number of characters of path that
62 * precede the extension
65 find_tar_ext (const char *path
)
67 char *end
= strchr (path
, '\0');
68 /* check in longest first order */
70 if ((ext
= trail (path
, ".tar.bz2")) && (end
- ext
) == 8)
72 if ((ext
= trail (path
, ".tar.gz")) && (end
- ext
) == 7)
74 if ((ext
= trail (path
, ".tar")) && (end
- ext
) == 4)
79 /* Parse a filename into package, version, and extension components. */
81 parse_filename (const string
&fn
, fileparse
& f
)
86 if (!(n
= find_tar_ext (fn
.c_str ())))
92 f
.tail
= fn
.substr (n
, string::npos
);
94 p
= new_cstr_char_array (base (fn
.substr (0, n
)));
96 /* TODO: make const and non-const trail variant. */
97 if ((ext
= trail (p
, "-src")))
102 else if ((ext
= trail (p
, "-patch")))
107 for (ver
= p
; *ver
; ver
++)
110 if (isdigit (ver
[1]))
116 else if (strcasecmp (ver
, "-src") == 0 ||
117 strcasecmp (ver
, "-patch") == 0)
121 f
.what
= strlwr (ver
);
122 ver
= strchr (ver
, '\0');
133 char *p1
= strchr (ver
, '\0');
134 if ((p1
-= 4) >= ver
&& strcasecmp (p1
, "-src") == 0)
136 else if ((p1
-= 2) >= ver
&& strcasecmp (p1
, "-patch") == 0)
142 // get the 'src' or 'patch'.
147 f
.ver
= *ver
? ver
: "0.0";
153 trail (const char *haystack
, const char *needle
)
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
);
160 || strcasecmp (haystack
+= prefix_len
, needle
) != 0)
166 backslash(const std::string
& s
)
170 for (std::string::iterator it
= rv
.begin(); it
!= rv
.end(); ++it
)
This page took 0.043227 seconds and 5 git commands to generate.