|
a/sc2src/pathut.cpp |
|
b/sc2src/pathut.cpp |
|
... |
|
... |
13 |
* along with this program; if not, write to the
|
13 |
* along with this program; if not, write to the
|
14 |
* Free Software Foundation, Inc.,
|
14 |
* Free Software Foundation, Inc.,
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
16 |
*/
|
16 |
*/
|
17 |
|
17 |
|
18 |
#ifndef TEST_PATHUT
|
|
|
19 |
#ifdef BUILDING_RECOLL
|
18 |
#ifdef BUILDING_RECOLL
|
20 |
#include "autoconfig.h"
|
19 |
#include "autoconfig.h"
|
21 |
#else
|
20 |
#else
|
22 |
#include "config.h"
|
21 |
#include "config.h"
|
23 |
#endif
|
22 |
#endif
|
|
... |
|
... |
64 |
void path_slashize(string& s)
|
63 |
void path_slashize(string& s)
|
65 |
{
|
64 |
{
|
66 |
for (string::size_type i = 0; i < s.size(); i++) {
|
65 |
for (string::size_type i = 0; i < s.size(); i++) {
|
67 |
if (s[i] == '\\') {
|
66 |
if (s[i] == '\\') {
|
68 |
s[i] = '/';
|
67 |
s[i] = '/';
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
void path_backslashize(string& s)
|
|
|
72 |
{
|
|
|
73 |
for (string::size_type i = 0; i < s.size(); i++) {
|
|
|
74 |
if (s[i] == '/') {
|
|
|
75 |
s[i] = '\\';
|
69 |
}
|
76 |
}
|
70 |
}
|
77 |
}
|
71 |
}
|
78 |
}
|
72 |
static bool path_strlookslikedrive(const string& s)
|
79 |
static bool path_strlookslikedrive(const string& s)
|
73 |
{
|
80 |
{
|
|
... |
|
... |
434 |
ret += *it;
|
441 |
ret += *it;
|
435 |
}
|
442 |
}
|
436 |
} else {
|
443 |
} else {
|
437 |
ret = "/";
|
444 |
ret = "/";
|
438 |
}
|
445 |
}
|
|
|
446 |
|
|
|
447 |
#ifdef _WIN32
|
|
|
448 |
// Raw drive needs a final /
|
|
|
449 |
if (path_strlookslikedrive(ret)) {
|
|
|
450 |
path_catslash(ret);
|
|
|
451 |
}
|
|
|
452 |
#endif
|
|
|
453 |
|
439 |
return ret;
|
454 |
return ret;
|
440 |
}
|
455 |
}
|
441 |
|
456 |
|
442 |
bool path_makepath(const string& ipath, int mode)
|
457 |
bool path_makepath(const string& ipath, int mode)
|
443 |
{
|
458 |
{
|
|
... |
|
... |
594 |
}
|
609 |
}
|
595 |
}
|
610 |
}
|
596 |
return out;
|
611 |
return out;
|
597 |
}
|
612 |
}
|
598 |
|
613 |
|
|
|
614 |
static inline int h2d(int c) {
|
|
|
615 |
if ('0' <= c && c <= '9')
|
|
|
616 |
return c - '0';
|
|
|
617 |
else if ('A' <= c && c <= 'F')
|
|
|
618 |
return 10 + c - 'A';
|
|
|
619 |
else
|
|
|
620 |
return -1;
|
|
|
621 |
}
|
|
|
622 |
|
|
|
623 |
string url_decode(const string &in)
|
|
|
624 |
{
|
|
|
625 |
if (in.size() <= 2)
|
|
|
626 |
return in;
|
|
|
627 |
string out;
|
|
|
628 |
out.reserve(in.size());
|
|
|
629 |
const char *cp = in.c_str();
|
|
|
630 |
string::size_type i = 0;
|
|
|
631 |
for (; i < in.size() - 2; i++) {
|
|
|
632 |
if (cp[i] == '%') {
|
|
|
633 |
int d1 = h2d(cp[i+1]);
|
|
|
634 |
int d2 = h2d(cp[i+2]);
|
|
|
635 |
if (d1 != -1 && d2 != -1) {
|
|
|
636 |
out += (d1 << 4) + d2;
|
|
|
637 |
} else {
|
|
|
638 |
out += '%';
|
|
|
639 |
out += cp[i+1];
|
|
|
640 |
out += cp[i+2];
|
|
|
641 |
}
|
|
|
642 |
i += 2;
|
|
|
643 |
} else {
|
|
|
644 |
out += cp[i];
|
|
|
645 |
}
|
|
|
646 |
}
|
|
|
647 |
while (i < in.size()) {
|
|
|
648 |
out += cp[i++];
|
|
|
649 |
}
|
|
|
650 |
return out;
|
|
|
651 |
}
|
|
|
652 |
|
599 |
string url_gpath(const string& url)
|
653 |
string url_gpath(const string& url)
|
600 |
{
|
654 |
{
|
601 |
// Remove the access schema part (or whatever it's called)
|
655 |
// Remove the access schema part (or whatever it's called)
|
602 |
string::size_type colon = url.find_first_of(":");
|
656 |
string::size_type colon = url.find_first_of(":");
|
603 |
if (colon == string::npos || colon == url.size() - 1) {
|
657 |
if (colon == string::npos || colon == url.size() - 1) {
|
|
... |
|
... |
849 |
// Call funcs that need static init (not initially reentrant)
|
903 |
// Call funcs that need static init (not initially reentrant)
|
850 |
void pathut_init_mt()
|
904 |
void pathut_init_mt()
|
851 |
{
|
905 |
{
|
852 |
path_home();
|
906 |
path_home();
|
853 |
}
|
907 |
}
|
854 |
|
|
|
855 |
|
|
|
856 |
#else // TEST_PATHUT
|
|
|
857 |
#include <stdlib.h>
|
|
|
858 |
#include <iostream>
|
|
|
859 |
using namespace std;
|
|
|
860 |
|
|
|
861 |
#include "pathut.h"
|
|
|
862 |
|
|
|
863 |
void path_to_thumb(const string& _input)
|
|
|
864 |
{
|
|
|
865 |
string input(_input);
|
|
|
866 |
// Make absolute path if needed
|
|
|
867 |
if (input[0] != '/') {
|
|
|
868 |
input = path_absolute(input);
|
|
|
869 |
}
|
|
|
870 |
|
|
|
871 |
input = string("file://") + path_canon(input);
|
|
|
872 |
|
|
|
873 |
string path;
|
|
|
874 |
//path = url_encode(input, 7);
|
|
|
875 |
thumbPathForUrl(input, 7, path);
|
|
|
876 |
cout << path << endl;
|
|
|
877 |
}
|
|
|
878 |
|
|
|
879 |
const char *tstvec[] = {"", "/", "/dir", "/dir/", "/dir1/dir2",
|
|
|
880 |
"/dir1/dir2",
|
|
|
881 |
"./dir", "./dir1/", "dir", "../dir", "/dir/toto.c",
|
|
|
882 |
"/dir/.c", "/dir/toto.txt", "toto.txt1"
|
|
|
883 |
};
|
|
|
884 |
|
|
|
885 |
const string ttvec[] = {"/dir", "", "~", "~/sub", "~root", "~root/sub",
|
|
|
886 |
"~nosuch", "~nosuch/sub"
|
|
|
887 |
};
|
|
|
888 |
int nttvec = sizeof(ttvec) / sizeof(string);
|
|
|
889 |
|
|
|
890 |
const char *thisprog;
|
|
|
891 |
|
|
|
892 |
int main(int argc, const char **argv)
|
|
|
893 |
{
|
|
|
894 |
thisprog = *argv++;
|
|
|
895 |
argc--;
|
|
|
896 |
|
|
|
897 |
string s;
|
|
|
898 |
vector<string>::const_iterator it;
|
|
|
899 |
#if 0
|
|
|
900 |
for (unsigned int i = 0; i < sizeof(tstvec) / sizeof(char *); i++) {
|
|
|
901 |
cout << tstvec[i] << " Father " << path_getfather(tstvec[i]) << endl;
|
|
|
902 |
}
|
|
|
903 |
for (unsigned int i = 0; i < sizeof(tstvec) / sizeof(char *); i++) {
|
|
|
904 |
cout << tstvec[i] << " Simple " << path_getsimple(tstvec[i]) << endl;
|
|
|
905 |
}
|
|
|
906 |
for (unsigned int i = 0; i < sizeof(tstvec) / sizeof(char *); i++) {
|
|
|
907 |
cout << tstvec[i] << " Basename " <<
|
|
|
908 |
path_basename(tstvec[i], ".txt") << endl;
|
|
|
909 |
}
|
|
|
910 |
#endif
|
|
|
911 |
|
|
|
912 |
#if 0
|
|
|
913 |
for (int i = 0; i < nttvec; i++) {
|
|
|
914 |
cout << "tildexp: '" << ttvec[i] << "' -> '" <<
|
|
|
915 |
path_tildexpand(ttvec[i]) << "'" << endl;
|
|
|
916 |
}
|
|
|
917 |
#endif
|
|
|
918 |
|
|
|
919 |
#if 0
|
|
|
920 |
const string canontst[] = {"/dir1/../../..", "/////", "",
|
|
|
921 |
"/dir1/../../.././/////dir2///////",
|
|
|
922 |
"../../",
|
|
|
923 |
"../../../../../../../../../../"
|
|
|
924 |
};
|
|
|
925 |
unsigned int nttvec = sizeof(canontst) / sizeof(string);
|
|
|
926 |
for (unsigned int i = 0; i < nttvec; i++) {
|
|
|
927 |
cout << "canon: '" << canontst[i] << "' -> '" <<
|
|
|
928 |
path_canon(canontst[i]) << "'" << endl;
|
|
|
929 |
}
|
|
|
930 |
#endif
|
|
|
931 |
#if 0
|
|
|
932 |
if (argc != 2) {
|
|
|
933 |
cerr << "Usage: trpathut <dir> <pattern>" << endl;
|
|
|
934 |
exit(1);
|
|
|
935 |
}
|
|
|
936 |
string dir = *argv++;
|
|
|
937 |
argc--;
|
|
|
938 |
string pattern = *argv++;
|
|
|
939 |
argc--;
|
|
|
940 |
vector<string> matched = path_dirglob(dir, pattern);
|
|
|
941 |
for (it = matched.begin(); it != matched.end(); it++) {
|
|
|
942 |
cout << *it << endl;
|
|
|
943 |
}
|
|
|
944 |
#endif
|
|
|
945 |
|
|
|
946 |
#if 0
|
|
|
947 |
if (argc != 1) {
|
|
|
948 |
fprintf(stderr, "Usage: fsocc: trpathut <path>\n");
|
|
|
949 |
exit(1);
|
|
|
950 |
}
|
|
|
951 |
string path = *argv++;
|
|
|
952 |
argc--;
|
|
|
953 |
|
|
|
954 |
int pc;
|
|
|
955 |
long long blocks;
|
|
|
956 |
if (!fsocc(path, &pc, &blocks)) {
|
|
|
957 |
fprintf(stderr, "fsocc failed\n");
|
|
|
958 |
return 1;
|
|
|
959 |
}
|
|
|
960 |
printf("pc %d, megabytes %ld\n", pc, blocks);
|
|
|
961 |
#endif
|
|
|
962 |
|
|
|
963 |
#if 0
|
|
|
964 |
Pidfile pidfile("/tmp/pathutpidfile");
|
|
|
965 |
pid_t pid;
|
|
|
966 |
if ((pid = pidfile.open()) != 0) {
|
|
|
967 |
cerr << "open failed. reason: " << pidfile.getreason() <<
|
|
|
968 |
" return " << pid << endl;
|
|
|
969 |
exit(1);
|
|
|
970 |
}
|
|
|
971 |
pidfile.write_pid();
|
|
|
972 |
sleep(10);
|
|
|
973 |
pidfile.close();
|
|
|
974 |
pidfile.remove();
|
|
|
975 |
#endif
|
|
|
976 |
|
|
|
977 |
#if 0
|
|
|
978 |
if (argc > 1) {
|
|
|
979 |
cerr << "Usage: thumbpath <filepath>" << endl;
|
|
|
980 |
exit(1);
|
|
|
981 |
}
|
|
|
982 |
string input;
|
|
|
983 |
if (argc == 1) {
|
|
|
984 |
input = *argv++;
|
|
|
985 |
if (input.empty()) {
|
|
|
986 |
cerr << "Usage: thumbpath <filepath>" << endl;
|
|
|
987 |
exit(1);
|
|
|
988 |
}
|
|
|
989 |
path_to_thumb(input);
|
|
|
990 |
} else {
|
|
|
991 |
while (getline(cin, input)) {
|
|
|
992 |
path_to_thumb(input);
|
|
|
993 |
}
|
|
|
994 |
}
|
|
|
995 |
|
|
|
996 |
|
|
|
997 |
exit(0);
|
|
|
998 |
#endif
|
|
|
999 |
|
|
|
1000 |
#if 0
|
|
|
1001 |
if (argc != 1) {
|
|
|
1002 |
cerr << "Usage: trpathut <filename>" << endl;
|
|
|
1003 |
exit(1);
|
|
|
1004 |
}
|
|
|
1005 |
string fn = *argv++;
|
|
|
1006 |
argc--;
|
|
|
1007 |
string ext = path_suffix(fn);
|
|
|
1008 |
cout << "Suffix: [" << ext << "]" << endl;
|
|
|
1009 |
return 0;
|
|
|
1010 |
#endif
|
|
|
1011 |
|
|
|
1012 |
#if 0
|
|
|
1013 |
if (argc != 1) {
|
|
|
1014 |
cerr << "Usage: trpathut url" << endl;
|
|
|
1015 |
exit(1);
|
|
|
1016 |
}
|
|
|
1017 |
string url = *argv++;
|
|
|
1018 |
argc--;
|
|
|
1019 |
|
|
|
1020 |
cout << "File: [" << fileurltolocalpath(url) << "]\n";
|
|
|
1021 |
return 0;
|
|
|
1022 |
#endif
|
|
|
1023 |
|
|
|
1024 |
#if 1
|
|
|
1025 |
if (argc != 1) {
|
|
|
1026 |
cerr << "Usage: trpathut path" << endl;
|
|
|
1027 |
exit(1);
|
|
|
1028 |
}
|
|
|
1029 |
string path = *argv++;
|
|
|
1030 |
argc--;
|
|
|
1031 |
|
|
|
1032 |
int pc;
|
|
|
1033 |
long long avmbs;
|
|
|
1034 |
if (fsocc(path, &pc, &avmbs)) {
|
|
|
1035 |
cout << "Percent occup " << pc << " avmbs " << avmbs << endl;
|
|
|
1036 |
return 0;
|
|
|
1037 |
} else {
|
|
|
1038 |
cerr << "fsocc failed\n";
|
|
|
1039 |
return 1;
|
|
|
1040 |
}
|
|
|
1041 |
#endif
|
|
|
1042 |
|
|
|
1043 |
|
|
|
1044 |
|
|
|
1045 |
}
|
|
|
1046 |
|
|
|
1047 |
#endif // TEST_PATHUT
|
|
|
1048 |
|
|
|