Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_Pathname_win32.cc
1  /************************************************************************/
2  /* */
3  /* Centre for Speech Technology Research */
4  /* University of Edinburgh, UK */
5  /* Copyright (c) 1996,1997 */
6  /* All Rights Reserved. */
7  /* */
8  /* Permission is hereby granted, free of charge, to use and distribute */
9  /* this software and its documentation without restriction, including */
10  /* without limitation the rights to use, copy, modify, merge, publish, */
11  /* distribute, sublicense, and/or sell copies of this work, and to */
12  /* permit persons to whom this work is furnished to do so, subject to */
13  /* the following conditions: */
14  /* 1. The code must retain the above copyright notice, this list of */
15  /* conditions and the following disclaimer. */
16  /* 2. Any modifications must be clearly marked as such. */
17  /* 3. Original authors' names are not deleted. */
18  /* 4. The authors' names are not used to endorse or promote products */
19  /* derived from this software without specific prior written */
20  /* permission. */
21  /* */
22  /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23  /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24  /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25  /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26  /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27  /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28  /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29  /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30  /* THIS SOFTWARE. */
31  /* */
32  /************************************************************************/
33  /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
34  /* Date: Tue Mar 18 1997 */
35  /************************************************************************/
36  /* */
37  /* Implementation of a class for manipulating filenames and so on. */
38  /* */
39  /* This is all hard coded to be unix filenames. I think the best */
40  /* strategy is to have a separate version of this for any other */
41  /* pathname format rather than trying to parameterise this. Most of */
42  /* it is fairly simple. */
43  /* */
44  /************************************************************************/
45 
46 #include <cstdio>
47 #include "EST_System.h"
48 #include "EST_Pathname.h"
49 
50 void EST_Pathname::setup(void)
51 {
52  this->gsub("/", "\\");
53 }
54 
55 int EST_Pathname::is_absolute(void) const
56 {
57  return length()>0 && (*this)[0] == '\\';
58 }
59 
60 int EST_Pathname::is_dirname(void) const
61 {
62  return length()>0 && (*this)[length()-1] == '\\';
63 }
64 
65 EST_Pathname EST_Pathname::directory(void) const {
66 
67  if (is_dirname())
68  return *this;
69 
70  int pos;
71  if ((pos=index("\\", -1)) >=0)
72  return before(pos+1);
73  else
74  return ".\\";
75  }
76 
77 EST_Pathname EST_Pathname::as_file(void) const
78 {
79  if (is_filename())
80  return *this;
81 
82  if (length() > 0)
83  return before(-1);
84 
85  return ".";
86 }
87 
88 EST_Pathname EST_Pathname::as_directory(void) const
89 {
90  if (is_dirname())
91  return *this;
92 
93  if (length() > 0)
94  return ((EST_String)(*this) + (EST_String)"\\");
95 
96  return ".\\";
97 }
98 
99 EST_Pathname EST_Pathname::construct(EST_Pathname dir,
100  EST_String filename)
101 {
102  EST_Pathname result(dir.as_directory());
103 
104  result += filename;
105  return result;
106 }
107 
108 EST_Pathname EST_Pathname::construct(EST_Pathname dir,
109  EST_String basename,
110  EST_String extension)
111 {
112  EST_Pathname filename(basename + "." + extension);
113  return EST_Pathname::construct(dir, filename);
114 }
115 
116 EST_TList<EST_String> EST_Pathname::entries(int check_for_directories) const
117 {
118  WIN32_FIND_DATA find_data;
119  HANDLE handle;
121  EST_Pathname pattern(this->as_directory() + EST_Pathname("*"));
122 
123  handle = FindFirstFile(pattern, &find_data);
124  if (handle != INVALID_HANDLE_VALUE)
125  while (1==1)
126  {
127  EST_Pathname name(find_data.cFileName);
128 
129  if (check_for_directories
130  && (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
131  list.append(name.as_directory());
132  else
133  list.append(name);
134  if (!FindNextFile(handle, &find_data))
135  break;
136  }
137  FindClose(handle);
138  return list;
139 }
140 
141 EST_Pathname EST_Pathname::append(EST_Pathname directory, EST_Pathname addition)
142 {
143  if (addition.is_absolute())
144  return addition;
145 
146  EST_String add(addition);
147 
148  EST_Pathname result(directory.as_directory());
149 
150  result.EST_String::operator += (add);
151 
152  return result;
153 }
154 
155 
156 EST_String EST_Pathname::extension(void) const
157 {
158  EST_String result("");
159 
160  if (length() <= 0)
161  return result;
162 
163  if (contains("."))
164  result = after(index(".",-1));
165 
166  return result;
167 
168 }
169 
170 EST_Pathname EST_Pathname::filename(void) const
171 {
172  EST_String result(this->as_file());
173 
174  if (contains("\\"))
175  result = result.after(index("\\",-1));
176  return result;
177 }
178 
179 EST_String EST_Pathname::basename(int remove_all) const
180 {
181  EST_String result(this->as_file().filename());
182 
183  if (remove_all)
184  {
185  if (result.contains("."))
186  result = result.before(".");
187  }
188  return result;
189 }
190 
191 EST_Pathname operator + (const EST_Pathname p, const EST_Pathname addition)
192 {return EST_Pathname::append(p, addition); }
193 
194 EST_Pathname operator + (const char *p, const EST_Pathname addition)
195 {return EST_Pathname::append(p, addition); }
196 
197 EST_Pathname &operator += (EST_Pathname p, const EST_Pathname addition)
198 { p = EST_Pathname::append(p, addition); return p; }
199 EST_Pathname &operator += (EST_Pathname p, const EST_String addition)
200 { p = EST_Pathname::append(p, addition); return p; }
201