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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
// # Header includes directory change tool
// Used to automatically change #include "[somepath/]header" to #include "correctheaderpath"
// Give the base project directory as argument:
// Example for spring:
// dch <trunk-path>/rts -ISystem
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <list>
#include <map>
#ifdef WIN32
#include <io.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif
// define a common indicator for 32bit or 64bit-ness
#if defined _WIN64 || defined __LP64__ || defined __ppc64__ || defined __ILP64__ || defined __SILP64__ || defined __LLP64__ || defined(__sparcv9)
#define __arch64__
#else
#define __arch32__
#endif
// define a cross-platform/-compiler compatible "%z" format replacement for
// printf() style functions.
// "%z" being the propper way for size_t typed values,
// but support for it has not yet spread wide enough.
#if defined __arch64__
#define __SIZE_T_PRINTF_FORMAT__ "%lu"
#else
#define __SIZE_T_PRINTF_FORMAT__ "%u"
#endif
// a shorter form
#define _STPF_ __SIZE_T_PRINTF_FORMAT__
using std::vector;
using std::list;
using std::map;
using std::set;
using std::string;
struct file_t {
string path;
string name;
string ext;
string inclpath; // path used to include this file
};
list<file_t> filelist;
void RemoveExtension(char *file)
{
int a = strlen(file)-1;
while(a>0)
{
if(file[a] == '.') {
file[a] = 0;
break;
}
a--;
}
}
const char *GetExtension(const char *file)
{
int a = strlen(file)-1;
while(a>0)
{
if(file[a] == '.')
return &file[a+1];
a--;
}
return 0;
}
const char *GetFilename (const char *file)
{
int a = strlen (file) - 1;
while (a>0)
{
if(file[a] == '/' || file[a] == '\\')
return &file[a+1];
a--;
}
return file;
}
#ifdef WIN32
void Scan(const string& curPath, const string& storePath)
{
struct _finddata_t files;
intptr_t handle;
int moreFiles;
string curPattern = curPath;
curPattern += "/*.*";
handle = _findfirst(curPattern.c_str(), &files);
for (moreFiles = (int)handle; moreFiles != -1; moreFiles = _findnext(handle, &files)) {
string filepath=curPath + "/" + files.name;
//printf("looking at %s\n", filepath.c_str());
// Is it a directory?
if ((files.attrib & 16) != 0) {
// Avoid the special directories
if (files.name[0] != '.') {
string newPath = curPath + "/" + files.name;
string newStorePath;
if (storePath.empty()) newStorePath=files.name;
else newStorePath = storePath + "/" + files.name;
Scan(newPath, newStorePath);
}
} else {
file_t f;
f.name = files.name;
f.path = filepath;
if(storePath.empty())
f.inclpath = files.name;
else
f.inclpath = storePath + "/" + files.name;
const char *ext=GetExtension (files.name);
if (ext)
f.ext = ext;
filelist.push_back (f);
}
}
_findclose(handle);
}
#else
void Scan(const string& curPath, const string& storePath)
{
DIR *dp;
struct dirent *ep;
dp = opendir(curPath.c_str());
if (dp == NULL) {
perror((string("opendir: ") + curPath).c_str());
return;
}
while( (ep = readdir(dp)) != NULL ) {
string filepath = curPath + "/" + ep->d_name;
struct stat info;
if (stat(filepath.c_str(), &info) != 0) {
perror((string("stat: ") + filepath).c_str());
continue;
}
// Is it a directory?
if (S_ISDIR(info.st_mode)) {
// Avoid the special directories
if (ep->d_name[0] != '.') {
string newPath = filepath;
string newStorePath;
if (storePath.empty()) newStorePath=ep->d_name;
else newStorePath = storePath + "/" + ep->d_name;
Scan(newPath, newStorePath);
}
} else {
file_t f;
f.name = ep->d_name;
f.path = filepath;
if(storePath.empty())
f.inclpath = ep->d_name;
else
f.inclpath = storePath + "/" + ep->d_name;
const char *ext=GetExtension (ep->d_name);
if (ext)
f.ext = ext;
filelist.push_back (f);
}
}
closedir(dp);
}
#endif
void SimplifyFilePaths(const set<string>& inclPaths)
{
for (list<file_t>::iterator fi = filelist.begin(); fi != filelist.end(); ++fi) {
int p=0;
string path, endInclPath;
while (p < fi->inclpath.length ()) {
while (fi->inclpath[p] != '/' && p < fi->inclpath.length()) {
p ++;
}
if (p == fi->inclpath.length()) // go to next file?
break;
p++;
path=fi->inclpath.substr (0, p);
if (inclPaths.find (path) != inclPaths.end())
endInclPath = fi->inclpath.substr (p,fi->inclpath.length());
}
if (!endInclPath.empty())
fi->inclpath = endInclPath;
}
}
string GetHeaderPath (const string& headerPath, const string& srcFilePath)
{
// only set the part of the paths that is not shared
int i=0;
int lastSharedPos=0;
while (i < headerPath.length() && i < srcFilePath.length()) {
if (headerPath[i] != srcFilePath[i])
break;
if (headerPath[i]=='/')
lastSharedPos=i+1;
i++;
}
int srcFilename=srcFilePath.rfind ('/');
if (srcFilename!=string::npos) {
srcFilename++;
if (srcFilename!=lastSharedPos)
lastSharedPos=0;
}
return headerPath.substr (lastSharedPos, headerPath.length());
}
int main (int argc, char *argv[])
{
const char *searchPath=0;
set<string> inclPaths; // Include paths defined with -I
map<string, file_t*> hmap; // all headers
for (int a=1;a<argc;a++) {
const char *arg=argv[a];
if (arg[0]=='-' && arg[1]=='I') {
string path=&arg[2];
if(!path.empty()) {
if(path[path.length ()-1] != '/')
path+='/';
inclPaths.insert (path);
}
} else
searchPath=arg;
}
if (!searchPath) {
printf ("Usage example: dch c:\\projects\\someproj -\n");
return -1;
}
Scan(searchPath,string());
if (filelist.empty())
return 0;
// strip away unnecessary parts based on the added include paths
SimplifyFilePaths (inclPaths);
printf ("Creating header list...");
for (list<file_t>::iterator i=filelist.begin();i!=filelist.end();++i)
{
if (i->ext == "h") {
printf("Header: %s\n", i->inclpath.c_str());
hmap [i->name]=&*i;
}
}
printf (_STPF_ " headers.\n", hmap.size());
for (list<file_t>::iterator i=filelist.begin();i!=filelist.end();++i)
{
// read all cpp and h files for #include <headerfile>
// and replace the headerfile with header path
if (i->ext != "cpp" && i->ext != "h")
continue;
FILE *f=fopen(i->path.c_str(), "rb");
if (!f) continue;
fseek (f,0,SEEK_END);
int len=ftell(f);
fseek (f,0,SEEK_SET);
vector <char> buffer;
buffer.resize(len);
fread (&buffer.front(), len, 1, f);
fclose (f);
string result;
int x=0;
const char *incl="#include \"";
int a;
while (x < buffer.size()) {
char *src = &buffer[x];
for (a=0;incl[a] && src[a]==incl[a];a++)
;
if (!incl[a]) {
// find the header name
char *headernamesrc = &src[a];
string headerpath;
int j=0;
while (headernamesrc[j]!='"' && headernamesrc+j<&buffer.back())
headerpath += headernamesrc[j++];
// strip the directory part of it
string headername = GetFilename (headerpath.c_str());
// match headername with a registered header
map<string,file_t*>::iterator fh=hmap.find (headername);
if (fh == hmap.end()) {
result += incl;
result += headername;
x+=j+a;
continue;
}
result += incl;
result += GetHeaderPath (fh->second->inclpath, i->inclpath);
// printf ("In file %s: Replaced %s with %s\n", i->path.c_str(), headername.c_str(), fh->second->localpath.c_str());
x+=j+a;
continue;
}
result += buffer[x];
x ++;
}
// write the result
string outfn=i->path;
FILE *outf = fopen (outfn.c_str(), "wb");
if (outf) {
fwrite (result.c_str(),result.size(),1,outf);
fclose (outf);
}
printf ("Output: %s\n", outfn.c_str());
}
return 0;
}
|