1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * fileutil.cpp --
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | * $Id: fileutil.cpp 534 1999-09-07 04:57:43Z sjboddie $
|
---|
25 | *
|
---|
26 | *********************************************************************/
|
---|
27 |
|
---|
28 | /*
|
---|
29 | $Log$
|
---|
30 | Revision 1.8 1999/09/07 04:57:42 sjboddie
|
---|
31 | added gpl notice
|
---|
32 |
|
---|
33 | Revision 1.7 1999/08/31 08:00:43 rjmcnab
|
---|
34 | Fixed a windows specific error in the file_exists function.
|
---|
35 |
|
---|
36 | Revision 1.6 1999/04/06 22:16:49 rjmcnab
|
---|
37 | Added more options for filename_cat.
|
---|
38 |
|
---|
39 | Revision 1.5 1999/01/19 01:38:14 rjmcnab
|
---|
40 |
|
---|
41 | Made the source more portable.
|
---|
42 |
|
---|
43 | Revision 1.4 1999/01/12 01:50:59 rjmcnab
|
---|
44 |
|
---|
45 | Standard header.
|
---|
46 |
|
---|
47 | Revision 1.3 1999/01/08 02:33:14 rjmcnab
|
---|
48 |
|
---|
49 | Added standard header to source files.
|
---|
50 |
|
---|
51 | */
|
---|
52 |
|
---|
53 |
|
---|
54 | #include "fileutil.h"
|
---|
55 |
|
---|
56 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
57 | # include <ospace\std\iostream>
|
---|
58 | # include <ospace\std\fstream>
|
---|
59 | #elif defined(GSDL_USE_IOS_H)
|
---|
60 | # include <iostream.h>
|
---|
61 | # include <fstream.h>
|
---|
62 | #else
|
---|
63 | # include <iostream>
|
---|
64 | # include <fstream>
|
---|
65 | #endif
|
---|
66 |
|
---|
67 |
|
---|
68 | // returns the proper concatenation of the two paths
|
---|
69 | text_t filename_cat (text_t path1, text_t path2) {
|
---|
70 | text_t::iterator here;
|
---|
71 | text_t::iterator begin;
|
---|
72 | text_t::iterator end;
|
---|
73 |
|
---|
74 | // make sure there is just one slash, of the correct type,
|
---|
75 | // at the end of path1 (unless path1 is an empty string).
|
---|
76 | if (!path1.empty()) {
|
---|
77 | // remove all trailing slashes
|
---|
78 | here = path1.end();
|
---|
79 | here--;
|
---|
80 | begin = path1.begin();
|
---|
81 | while (here != begin && (*here == '/' || *here == '\\')) {
|
---|
82 | here--;
|
---|
83 | }
|
---|
84 | here++;
|
---|
85 | path1.erase(here,path1.end());
|
---|
86 |
|
---|
87 | // add one final slash
|
---|
88 | #ifdef __WIN32__
|
---|
89 | path1 += "\\";
|
---|
90 | #else
|
---|
91 | path1 += "/";
|
---|
92 | #endif
|
---|
93 | }
|
---|
94 |
|
---|
95 | // remove all slashes from the start of path2
|
---|
96 | here = path2.begin();
|
---|
97 | end = path2.end();
|
---|
98 | while (here != end && (*here == '/' || *here == '\\')) {
|
---|
99 | here++;
|
---|
100 | }
|
---|
101 | path2.erase (path2.begin(), here);
|
---|
102 |
|
---|
103 | // return the concatenation of the two strings
|
---|
104 | return path1 + path2;
|
---|
105 | }
|
---|
106 |
|
---|
107 | text_t filename_cat (text_t path1, text_t path2, text_t path3) {
|
---|
108 | return filename_cat(filename_cat(path1,path2),path3);
|
---|
109 | }
|
---|
110 |
|
---|
111 | text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4) {
|
---|
112 | return filename_cat(filename_cat(filename_cat(path1,path2),path3),path4);
|
---|
113 | }
|
---|
114 |
|
---|
115 | text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4,
|
---|
116 | text_t path5) {
|
---|
117 | return filename_cat(filename_cat(filename_cat(filename_cat(path1,path2),path3),
|
---|
118 | path4),path5);
|
---|
119 | }
|
---|
120 |
|
---|
121 | // returns true if filename can be opened
|
---|
122 | bool file_exists (const text_t &filename) {
|
---|
123 | char *cstr = filename.getcstr();
|
---|
124 | ifstream filestream (cstr, ios::in | ios::nocreate);
|
---|
125 | delete cstr;
|
---|
126 |
|
---|
127 | if (filestream) {
|
---|
128 | // file exists
|
---|
129 | filestream.close ();
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 |
|
---|
133 | // file does not exist
|
---|
134 | return false;
|
---|
135 | }
|
---|