first incarnation of indexing thread
This commit is contained in:
+2
-2
@@ -1,5 +1,5 @@
|
||||
|
||||
CXXFLAGS = -I.
|
||||
CXXFLAGS = -I. -g
|
||||
|
||||
BIGLIB = ../lib/librcl.a
|
||||
|
||||
@@ -15,7 +15,7 @@ trfstreewalk.o : fstreewalk.cpp fstreewalk.h
|
||||
|
||||
PATHUT_OBJS= trpathut.o pathut.o
|
||||
trpathut : $(PATHUT_OBJS)
|
||||
$(CXX) -o trpathut $(PATHUT_OBJS)
|
||||
$(CXX) $(CXXFLAGS) -o trpathut $(PATHUT_OBJS)
|
||||
trpathut.o : pathut.cpp pathut.h
|
||||
$(CXX) -o trpathut.o -c $(CXXFLAGS) \
|
||||
-DTEST_PATHUT pathut.cpp
|
||||
|
||||
+53
-13
@@ -1,15 +1,17 @@
|
||||
#ifndef lint
|
||||
static char rcsid[] = "@(#$Id: pathut.cpp,v 1.2 2004-12-14 17:54:16 dockes Exp $ (C) 2004 J.F.Dockes";
|
||||
static char rcsid[] = "@(#$Id: pathut.cpp,v 1.3 2005-01-31 14:31:10 dockes Exp $ (C) 2004 J.F.Dockes";
|
||||
#endif
|
||||
|
||||
#ifndef TEST_PATHUT
|
||||
|
||||
#include <pwd.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "pathut.h"
|
||||
using std::string;
|
||||
|
||||
std::string path_getfather(const std::string &s) {
|
||||
std::string father = s;
|
||||
string path_getfather(const string &s) {
|
||||
string father = s;
|
||||
|
||||
// ??
|
||||
if (father.empty())
|
||||
@@ -22,8 +24,8 @@ std::string path_getfather(const std::string &s) {
|
||||
father.erase(father.length()-1);
|
||||
}
|
||||
|
||||
std::string::size_type slp = father.rfind('/');
|
||||
if (slp == std::string::npos)
|
||||
string::size_type slp = father.rfind('/');
|
||||
if (slp == string::npos)
|
||||
return "./";
|
||||
|
||||
father.erase(slp);
|
||||
@@ -31,33 +33,57 @@ std::string path_getfather(const std::string &s) {
|
||||
return father;
|
||||
}
|
||||
|
||||
std::string path_getsimple(const std::string &s) {
|
||||
std::string simple = s;
|
||||
string path_getsimple(const string &s) {
|
||||
string simple = s;
|
||||
|
||||
if (simple.empty())
|
||||
return simple;
|
||||
|
||||
std::string::size_type slp = simple.rfind('/');
|
||||
if (slp == std::string::npos)
|
||||
string::size_type slp = simple.rfind('/');
|
||||
if (slp == string::npos)
|
||||
return simple;
|
||||
|
||||
simple.erase(0, slp+1);
|
||||
return simple;
|
||||
}
|
||||
|
||||
std::string path_home()
|
||||
string path_home()
|
||||
{
|
||||
uid_t uid = getuid();
|
||||
|
||||
struct passwd *entry = getpwuid(uid);
|
||||
if (entry == 0)
|
||||
if (entry == 0) {
|
||||
const char *cp = getenv("HOME");
|
||||
if (cp)
|
||||
return cp;
|
||||
else
|
||||
return "/";
|
||||
}
|
||||
|
||||
std::string homedir = entry->pw_dir;
|
||||
string homedir = entry->pw_dir;
|
||||
path_catslash(homedir);
|
||||
return homedir;
|
||||
}
|
||||
|
||||
extern string path_tildexpand(const string &s)
|
||||
{
|
||||
if (s.empty() || s[0] != '~')
|
||||
return s;
|
||||
string o = s;
|
||||
if (s.length() == 1) {
|
||||
o.replace(0, 1, path_home());
|
||||
} else if (s[1] == '/') {
|
||||
o.replace(0, 2, path_home());
|
||||
} else {
|
||||
string::size_type pos = s.find('/');
|
||||
int l = (pos == string::npos) ? s.length() - 1 : pos - 1;
|
||||
struct passwd *entry = getpwnam(s.substr(1, l).c_str());
|
||||
if (entry)
|
||||
o.replace(0, l+1, entry->pw_dir);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
#else // TEST_PATHUT
|
||||
|
||||
#include <iostream>
|
||||
@@ -71,15 +97,29 @@ const char *tstvec[] = {"", "/", "/dir", "/dir/", "/dir1/dir2",
|
||||
"/dir/.c",
|
||||
};
|
||||
|
||||
const string ttvec[] = {"/dir", "", "~", "~/sub", "~root", "~root/sub",
|
||||
"~nosuch", "~nosuch/sub"};
|
||||
int nttvec = sizeof(ttvec) / sizeof(string);
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
|
||||
#if 0
|
||||
for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
|
||||
cout << tstvec[i] << " FATHER " << path_getfather(tstvec[i]) << endl;
|
||||
}
|
||||
for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
|
||||
cout << tstvec[i] << " SIMPLE " << path_getsimple(tstvec[i]) << endl;
|
||||
}
|
||||
#endif
|
||||
string s;
|
||||
|
||||
for (int i = 0; i < nttvec; i++) {
|
||||
cout << "tildexp: '" << ttvec[i] << "' -> '" <<
|
||||
path_tildexpand(ttvec[i]) << "'" << endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,6 +1,6 @@
|
||||
#ifndef _PATHUT_H_INCLUDED_
|
||||
#define _PATHUT_H_INCLUDED_
|
||||
/* @(#$Id: pathut.h,v 1.2 2004-12-14 17:54:16 dockes Exp $ (C) 2004 J.F.Dockes */
|
||||
/* @(#$Id: pathut.h,v 1.3 2005-01-31 14:31:10 dockes Exp $ (C) 2004 J.F.Dockes */
|
||||
|
||||
#include <string>
|
||||
|
||||
@@ -16,5 +16,6 @@ inline void path_cat(std::string &s1, const std::string &s2) {
|
||||
extern std::string path_getsimple(const std::string &s);
|
||||
extern std::string path_getfather(const std::string &s);
|
||||
extern std::string path_home();
|
||||
extern std::string path_tildexpand(const std::string &s);
|
||||
|
||||
#endif /* _PATHUT_H_INCLUDED_ */
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
#ifndef lint
|
||||
static char rcsid[] = "@(#$Id: smallut.cpp,v 1.1 2005-01-31 14:31:10 dockes Exp $ (C) 2004 J.F.Dockes";
|
||||
#endif
|
||||
|
||||
#ifndef TEST_SMALLUT
|
||||
#include <string>
|
||||
#include "smallut.h"
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
#define MIN(A,B) ((A)<(B)?(A):(B))
|
||||
|
||||
int stringicmp(const string & s1, const string& s2)
|
||||
{
|
||||
string::const_iterator it1 = s1.begin();
|
||||
string::const_iterator it2 = s2.begin();
|
||||
int size1 = s1.length(), size2 = s2.length();
|
||||
char c1, c2;
|
||||
|
||||
if (size1 > size2) {
|
||||
while (it1 != s1.end()) {
|
||||
c1 = ::toupper(*it1);
|
||||
c2 = ::toupper(*it2);
|
||||
if (c1 != c2) {
|
||||
return c1 > c2 ? 1 : -1;
|
||||
}
|
||||
++it1; ++it2;
|
||||
}
|
||||
return size1 == size2 ? 0 : 1;
|
||||
} else {
|
||||
while (it2 != s2.end()) {
|
||||
c1 = ::toupper(*it1);
|
||||
c2 = ::toupper(*it2);
|
||||
if (c1 != c2) {
|
||||
return c1 > c2 ? 1 : -1;
|
||||
}
|
||||
++it1; ++it2;
|
||||
}
|
||||
return size1 == size2 ? 0 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
// s1 is already lowercase
|
||||
int stringlowercmp(const string & s1, const string& s2)
|
||||
{
|
||||
string::const_iterator it1 = s1.begin();
|
||||
string::const_iterator it2 = s2.begin();
|
||||
int size1 = s1.length(), size2 = s2.length();
|
||||
char c2;
|
||||
|
||||
if (size1 > size2) {
|
||||
while (it1 != s1.end()) {
|
||||
c2 = ::tolower(*it2);
|
||||
if (*it1 != c2) {
|
||||
return *it1 > c2 ? 1 : -1;
|
||||
}
|
||||
++it1; ++it2;
|
||||
}
|
||||
return size1 == size2 ? 0 : 1;
|
||||
} else {
|
||||
while (it2 != s2.end()) {
|
||||
c2 = ::tolower(*it2);
|
||||
if (*it1 != c2) {
|
||||
return *it1 > c2 ? 1 : -1;
|
||||
}
|
||||
++it1; ++it2;
|
||||
}
|
||||
return size1 == size2 ? 0 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
// s1 is already uppercase
|
||||
int stringuppercmp(const string & s1, const string& s2)
|
||||
{
|
||||
string::const_iterator it1 = s1.begin();
|
||||
string::const_iterator it2 = s2.begin();
|
||||
int size1 = s1.length(), size2 = s2.length();
|
||||
char c2;
|
||||
|
||||
if (size1 > size2) {
|
||||
while (it1 != s1.end()) {
|
||||
c2 = ::toupper(*it2);
|
||||
if (*it1 != c2) {
|
||||
return *it1 > c2 ? 1 : -1;
|
||||
}
|
||||
++it1; ++it2;
|
||||
}
|
||||
return size1 == size2 ? 0 : 1;
|
||||
} else {
|
||||
while (it2 != s2.end()) {
|
||||
c2 = ::toupper(*it2);
|
||||
if (*it1 != c2) {
|
||||
return *it1 > c2 ? 1 : -1;
|
||||
}
|
||||
++it1; ++it2;
|
||||
}
|
||||
return size1 == size2 ? 0 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <string>
|
||||
#include "smallut.h"
|
||||
|
||||
struct spair {
|
||||
const char *s1;
|
||||
const char *s2;
|
||||
};
|
||||
struct spair pairs[] = {
|
||||
{"", ""},
|
||||
{"", "a"},
|
||||
{"a", ""},
|
||||
{"a", "a"},
|
||||
{"A", "a"},
|
||||
{"a", "A"},
|
||||
{"A", "A"},
|
||||
{"12", "12"},
|
||||
{"a", "ab"},
|
||||
{"ab", "a"},
|
||||
{"A", "Ab"},
|
||||
{"a", "Ab"},
|
||||
};
|
||||
int npairs = sizeof(pairs) / sizeof(struct spair);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
for (int i = 0; i < npairs; i++) {
|
||||
{
|
||||
int c = stringicmp(pairs[i].s1, pairs[i].s2);
|
||||
printf("'%s' %s '%s' ", pairs[i].s1,
|
||||
c == 0 ? "==" : c < 0 ? "<" : ">", pairs[i].s2);
|
||||
}
|
||||
{
|
||||
int cl = stringlowercmp(pairs[i].s1, pairs[i].s2);
|
||||
printf("L '%s' %s '%s' ", pairs[i].s1,
|
||||
cl == 0 ? "==" : cl < 0 ? "<" : ">", pairs[i].s2);
|
||||
}
|
||||
{
|
||||
int cu = stringuppercmp(pairs[i].s1, pairs[i].s2);
|
||||
printf("U '%s' %s '%s' ", pairs[i].s1,
|
||||
cu == 0 ? "==" : cu < 0 ? "<" : ">", pairs[i].s2);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef _SMALLUT_H_INCLUDED_
|
||||
#define _SMALLUT_H_INCLUDED_
|
||||
/* @(#$Id: smallut.h,v 1.1 2005-01-31 14:31:10 dockes Exp $ (C) 2004 J.F.Dockes */
|
||||
#include <string>
|
||||
|
||||
using std::string;
|
||||
|
||||
extern int stringicmp(const string& s1, const string& s2);
|
||||
extern int stringlowercmp(const string& alreadylower, const string& s2);
|
||||
extern int stringuppercmp(const string& alreadyupper, const string& s2);
|
||||
|
||||
|
||||
#endif /* _SMALLUT_H_INCLUDED_ */
|
||||
Reference in New Issue
Block a user