Switch to unified view

a/src/utils/pathut.cpp b/src/utils/pathut.cpp
1
#ifndef lint
1
#ifndef lint
2
static char rcsid[] = "@(#$Id: pathut.cpp,v 1.6 2005-12-13 12:42:59 dockes Exp $ (C) 2004 J.F.Dockes";
2
static char rcsid[] = "@(#$Id: pathut.cpp,v 1.7 2006-01-09 16:53:31 dockes Exp $ (C) 2004 J.F.Dockes";
3
#endif
3
#endif
4
4
5
#ifndef TEST_PATHUT
5
#ifndef TEST_PATHUT
6
#include <unistd.h>
6
#include <unistd.h>
7
#include <sys/param.h>
7
#include <pwd.h>
8
#include <pwd.h>
9
8
#include <iostream>
10
#include <iostream>
11
#include <list>
12
#include <stack>
9
13
10
#include "pathut.h"
14
#include "pathut.h"
11
#ifndef NO_NAMESPACES
15
#ifndef NO_NAMESPACES
12
using std::string;
16
using std::string;
17
using std::list;
18
using std::stack;
13
#endif /* NO_NAMESPACES */
19
#endif /* NO_NAMESPACES */
14
20
15
void path_catslash(std::string &s) {
21
void path_catslash(std::string &s) {
16
    if (s.empty() || s[s.length() - 1] != '/')
22
    if (s.empty() || s[s.length() - 1] != '/')
17
    s += '/';
23
    s += '/';
...
...
56
    string::size_type slp = simple.rfind('/');
62
    string::size_type slp = simple.rfind('/');
57
    if (slp == string::npos)
63
    if (slp == string::npos)
58
    return simple;
64
    return simple;
59
65
60
    simple.erase(0, slp+1);
66
    simple.erase(0, slp+1);
67
    return simple;
68
}
69
70
string path_basename(const string &s, const string &suff)
71
{
72
    string simple = path_getsimple(s);
73
    string::size_type pos = string::npos;
74
    if (suff.length() && simple.length() > suff.length()) {
75
  pos = simple.rfind(suff);
76
  if (pos != string::npos && pos + suff.length() == simple.length())
77
      return simple.substr(0, pos);
78
    } 
61
    return simple;
79
    return simple;
62
}
80
}
63
81
64
string path_home()
82
string path_home()
65
{
83
{
...
...
96
        o.replace(0, l+1, entry->pw_dir);
114
        o.replace(0, l+1, entry->pw_dir);
97
    }
115
    }
98
    return o;
116
    return o;
99
}
117
}
100
118
119
#include <smallut.h>
120
extern std::string path_canon(const std::string &is)
121
{
122
    if (is.length() == 0)
123
  return is;
124
    string s = is;
125
    if (s[0] != '/') {
126
  char buf[MAXPATHLEN];
127
  if (!getcwd(buf, MAXPATHLEN)) {
128
      return "";
129
  }
130
  s = path_cat(string(buf), s); 
131
    }
132
    list<string>elems;
133
    stringToTokens(s, elems, "/");
134
    list<string> cleaned;
135
    for (list<string>::const_iterator it = elems.begin(); 
136
   it != elems.end(); it++){
137
  if (*it == "..") {
138
      if (!cleaned.empty())
139
      cleaned.pop_back();
140
  } else if (it->empty() || *it == ".") {
141
  } else {
142
      cleaned.push_back(*it);
143
  }
144
    }
145
    string ret;
146
    if (!cleaned.empty()) {
147
  for (list<string>::const_iterator it = cleaned.begin(); 
148
       it != cleaned.end(); it++) {
149
      ret += "/";
150
      ret += *it;
151
  }
152
    } else {
153
  ret = "/";
154
    }
155
    return ret;
156
}
157
158
#include <glob.h>
159
#include <sys/stat.h>
160
list<std::string> path_dirglob(const std::string &dir, 
161
                  const std::string pattern)
162
{
163
    list<string> res;
164
    glob_t mglob;
165
    string mypat=path_cat(dir, pattern);
166
    if (glob(mypat.c_str(), 0, 0, &mglob)) {
167
  return res;
168
    }
169
    for (int i = 0; i < mglob.gl_pathc; i++) {
170
  res.push_back(mglob.gl_pathv[i]);
171
    }
172
    globfree(&mglob);
173
    return res;
174
}
175
176
101
#else // TEST_PATHUT
177
#else // TEST_PATHUT
102
178
103
#include <iostream>
179
#include <iostream>
104
using namespace std;
180
using namespace std;
105
181
106
#include "pathut.h"
182
#include "pathut.h"
107
183
108
const char *tstvec[] = {"", "/", "/dir", "/dir/", "/dir1/dir2",
184
const char *tstvec[] = {"", "/", "/dir", "/dir/", "/dir1/dir2",
109
             "/dir1/dir2",
185
             "/dir1/dir2",
110
            "./dir", "./dir1/", "dir", "../dir", "/dir/toto.c",
186
            "./dir", "./dir1/", "dir", "../dir", "/dir/toto.c",
111
          "/dir/.c",
187
          "/dir/.c", "/dir/toto.txt", "toto.txt1"
112
};
188
};
113
189
114
const string ttvec[] = {"/dir", "", "~", "~/sub", "~root", "~root/sub",
190
const string ttvec[] = {"/dir", "", "~", "~/sub", "~root", "~root/sub",
115
         "~nosuch", "~nosuch/sub"};
191
         "~nosuch", "~nosuch/sub"};
116
int nttvec = sizeof(ttvec) / sizeof(string);
192
int nttvec = sizeof(ttvec) / sizeof(string);
117
193
118
int main(int argc, const char **argv)
194
int main(int argc, const char **argv)
119
{
195
{
196
    string s;
197
    list<string>::const_iterator it;
120
#if 0
198
#if 0
121
    for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
199
    for (unsigned int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
122
    cout << tstvec[i] << " FATHER " << path_getfather(tstvec[i]) << endl;
200
    cout << tstvec[i] << " Father " << path_getfather(tstvec[i]) << endl;
123
    }
201
    }
124
    for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
202
    for (unsigned int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
125
    cout << tstvec[i] << " SIMPLE " << path_getsimple(tstvec[i]) << endl;
203
    cout << tstvec[i] << " Simple " << path_getsimple(tstvec[i]) << endl;
126
    }
204
    }
205
    for (unsigned int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
206
  cout << tstvec[i] << " Basename " << 
207
      path_basename(tstvec[i], ".txt") << endl;
208
    }
127
#endif
209
#endif
128
    string s;
129
210
211
#if 0
130
    for (int i = 0; i < nttvec; i++) {
212
    for (int i = 0; i < nttvec; i++) {
131
    cout << "tildexp: '" << ttvec[i] << "' -> '" << 
213
    cout << "tildexp: '" << ttvec[i] << "' -> '" << 
132
        path_tildexpand(ttvec[i]) << "'" << endl;
214
        path_tildexpand(ttvec[i]) << "'" << endl;
133
    }
215
    }
216
#endif
217
218
#if 0
219
    const string canontst[] = {"/dir1/../../..", "/////", "", 
220
                 "/dir1/../../.././/////dir2///////",
221
                 "../../", 
222
                 "../../../../../../../../../../"
223
    };
224
    unsigned int nttvec = sizeof(canontst) / sizeof(string);
225
    for (unsigned int i = 0; i < nttvec; i++) {
226
  cout << "canon: '" << canontst[i] << "' -> '" << 
227
      path_canon(canontst[i]) << "'" << endl;
134
    
228
    }
135
229
#endif    
230
#if 1
231
    if (argc != 3) {
232
  fprintf(stderr, "Usage: trpathut <dir> <pattern>\n");
233
  exit(1);
234
    }
235
    string dir=argv[1], pattern=argv[2];
236
    list<string> matched = path_dirglob(dir, pattern);
237
    for (it = matched.begin(); it != matched.end();it++) {
238
  cout << *it << endl;
239
    }
240
#endif
136
241
137
    return 0;
242
    return 0;
138
}
243
}
139
244
140
#endif // TEST_PATHUT
245
#endif // TEST_PATHUT