File: searchfolder.cc

package info (click to toggle)
simutrans 100.0%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,776 kB
  • ctags: 9,485
  • sloc: cpp: 72,459; ansic: 5,646; makefile: 450
file content (137 lines) | stat: -rw-r--r-- 3,221 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <string.h>
#include <sys/stat.h>

#ifndef _MSC_VER
#include <unistd.h>
#include <dirent.h>
#else
#include <io.h>
#endif

#include "../simdebug.h"
#include "../simmem.h"
#include "searchfolder.h"

#ifdef _MSC_VER
#define STRICMP stricmp
#else
#define STRICMP strcasecmp
#endif

 /*
 *  Autor:
 *      Volker Meyer
 *
 *  Beschreibung:
 *      Endet filepath mit einem slash so werden alle Files im diesem
 *	Verzeichnis mit der angegebene Extension gesucht.
 *      Endet filepath nicht mit einem slash und enthlt keinen Punkt
 *	nach dem letzten Slash. So wird er um die Extension erweitert
 *	und gesucht.
 *	Ansonsten wird direkt nach filepath gesucht.
 *
 *	Keine Wildcards, bitte!
 *
 *  Return type:
 *      int	    Anzahl gefundener Dateien.
 */
int searchfolder_t::search(const char *filepath, const char *extension)
{
    cstring_t path(filepath);
    cstring_t name;
    cstring_t lookfor;
    cstring_t ext;

	for (vector_tpl<char*>::const_iterator i = files.begin(), end = files.end(); i != end; ++i) {
		guarded_free(*i);
	}
	files.clear();

	if(path.right(1) == "/") {
		// Look for a directory
		name = "*";
		ext = cstring_t(".") + extension;
	}
	else {
		int slash = path.find_back('/');
		int dot = path.find_back('.');

		if(dot == -1 || dot < slash) {
			// Look for a file with default extension
			name = path.mid(slash + 1);
			path = path.left(slash + 1);
			ext = cstring_t(".") + extension;
		}
		else {
			// Look for a file with own extension
			ext = path.mid(dot);
			name = path.mid(slash + 1, dot - slash - 1);
			path = path.left(slash + 1);
		}
	}
#ifdef _MSC_VER
	lookfor = path + name + ext;
	struct _finddata_t entry;
	long hfind = _findfirst((const char*)lookfor, &entry);

	if(hfind != -1) {
		lookfor = ext;
		do {
			int entry_len = strlen(entry.name);
			if(stricmp(entry.name + entry_len - lookfor.len(), (const char*)lookfor) == 0) {
				char* const c = MALLOCN(char, path.len() + entry_len + 1);
				sprintf(c,"%s%s",(const char*)path,entry.name);
				files.push_back(c);
			}
		} while(_findnext(hfind, &entry) == 0 );
	}
#else
	lookfor = path + ".";

	DIR* dir = opendir(lookfor);
	struct  dirent  *entry;

	if(dir != NULL) {
		lookfor = (name == "*") ? ext : name + ext;

		while((entry = readdir(dir)) != NULL) {
			if(entry->d_name[0]!='.' || (entry->d_name[1]!='.' && entry->d_name[1]!=0)) {
				int entry_len = strlen(entry->d_name);
				if (strcasecmp(entry->d_name + entry_len - lookfor.len(), lookfor) == 0) {
					char* const c = MALLOCN(char, path.len() + entry_len + 1);
					sprintf(c,"%s%s", (const char*)path, entry->d_name);
					files.push_back(c);
				}
			}
		}
		closedir(dir);
	}
#endif
	return files.get_count();
}

cstring_t searchfolder_t::complete(const char *filepath, const char *extension)
{
	cstring_t path = filepath;

	if(path.right(1) != "/") {
		int slash = path.find_back('/');
		int dot = path.find_back('.');

		if(dot == -1 || dot < slash) {
			return path + "." + extension;
		}
	}
	return path;
}


/*
 * since we explicitly alloc the char *'s we must free them here
 */
searchfolder_t::~searchfolder_t()
{
	for (vector_tpl<char*>::const_iterator i = files.begin(), end = files.end(); i != end; ++i) {
		guarded_free(*i);
	}
}