File: searchfolder.cc

package info (click to toggle)
simutrans 111.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 13,504 kB
  • ctags: 12,645
  • sloc: cpp: 101,849; ansic: 3,466; makefile: 694; sh: 44
file content (126 lines) | stat: -rw-r--r-- 3,101 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
#include <string>
#include <string.h>

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

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

/*
 *  Author:
 *      Volker Meyer
 *
 *  Description:
 *      If filepath ends with a slash, search for all files in the
 *      directory having the given extension.
 *      If filepath does not end with a slash and it also doesn't contain
 *      a dot after the last slash, then append extension to filepath and
 *      search for it.
 *	Otherwise searches directly for filepath.
 *
 *	No wildcards please!
 *
 *  Return type:
 *      int			number of matching files.
*/
int searchfolder_t::search(const std::string &filepath, const std::string &extension)
{
	std::string path(filepath);
	std::string name;
	std::string lookfor;
	std::string ext;

	FOR(vector_tpl<char*>, const i, files) {
		guarded_free(i);
	}
	files.clear();

	if(path[path.size() - 1] == '/') {
		// Look for a directory
		name = "*";
		ext = std::string(".") + extension;
	}
	else {
		int slash = path.rfind('/');
		int dot = path.rfind('.');

		if(dot == -1 || dot < slash) {
			// Look for a file with default extension
			name = path.substr(slash + 1, std::string::npos);
			path = path.substr(0, slash + 1);
			ext = std::string(".") + extension;
		}
		else {
			// Look for a file with own extension
			ext = path.substr(dot, std::string::npos);
			name = path.substr(slash + 1, dot - slash - 1);
			path = path.substr(0, slash + 1);
		}
	}
#ifdef _MSC_VER
	lookfor = path + name + ext;
	struct _finddata_t entry;
	intptr_t hfind = _findfirst(lookfor.c_str(), &entry);

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

	if (DIR* const dir = opendir(lookfor.c_str())) {
		lookfor = (name == "*") ? ext : name + ext;

		while (dirent const* const entry = readdir(dir)) {
			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.size(), lookfor.c_str()) == 0) {
					char* const c = MALLOCN(char, path.size() + entry_len + 1);
					sprintf(c,"%s%s", path.c_str(), entry->d_name);
					files.append(c);
				}
			}
		}
		closedir(dir);
	}
#endif
	return files.get_count();
}

std::string searchfolder_t::complete(const std::string &filepath, const std::string &extension)
{
	if(filepath[filepath.size() - 1] != '/') {
		int slash = filepath.rfind('/');
		int dot = filepath.rfind('.');

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


/*
 * since we explicitly alloc the char *'s we must free them here
 */
searchfolder_t::~searchfolder_t()
{
	FOR(vector_tpl<char*>, const i, files) {
		guarded_free(i);
	}
}