| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// gfile.cc |
| 4 |
// |
| 5 |
// Miscellaneous file and directory name manipulation. |
| 6 |
// |
| 7 |
// Copyright 1996-2003 Glyph & Cog, LLC |
| 8 |
// |
| 9 |
//======================================================================== |
| 10 |
|
| 11 |
#include <aconf.h> |
| 12 |
|
| 13 |
#ifdef _WIN32 |
| 14 |
# undef WIN32_LEAN_AND_MEAN |
| 15 |
# include <windows.h> |
| 16 |
# include <time.h> |
| 17 |
# include <direct.h> |
| 18 |
# include <shobjidl.h> |
| 19 |
# include <shlguid.h> |
| 20 |
#else |
| 21 |
# if !defined(ACORN) |
| 22 |
# include <sys/types.h> |
| 23 |
# include <sys/stat.h> |
| 24 |
# include <fcntl.h> |
| 25 |
# endif |
| 26 |
# include <time.h> |
| 27 |
# include <limits.h> |
| 28 |
# include <string.h> |
| 29 |
# if !defined(VMS) && !defined(ACORN) |
| 30 |
# include <pwd.h> |
| 31 |
# endif |
| 32 |
# if defined(VMS) && (__DECCXX_VER < 50200000) |
| 33 |
# include <unixlib.h> |
| 34 |
# endif |
| 35 |
#endif // _WIN32 |
| 36 |
#include "gmem.h" |
| 37 |
#include "gmempp.h" |
| 38 |
#include "GString.h" |
| 39 |
#include "gfile.h" |
| 40 |
|
| 41 |
// Some systems don't define this, so just make it something reasonably |
| 42 |
// large. |
| 43 |
#ifndef PATH_MAX |
| 44 |
#define PATH_MAX 1024 |
| 45 |
#endif |
| 46 |
|
| 47 |
//------------------------------------------------------------------------ |
| 48 |
|
| 49 |
GString *getHomeDir() { |
| 50 |
#ifdef VMS |
| 51 |
//---------- VMS ---------- |
| 52 |
return new GString("SYS$LOGIN:"); |
| 53 |
|
| 54 |
#elif defined(_WIN32) |
| 55 |
//---------- Win32 ---------- |
| 56 |
char *s; |
| 57 |
GString *ret; |
| 58 |
|
| 59 |
if ((s = getenv("USERPROFILE"))) |
| 60 |
ret = new GString(s); |
| 61 |
else |
| 62 |
ret = new GString("."); |
| 63 |
return ret; |
| 64 |
|
| 65 |
#elif defined(__EMX__) |
| 66 |
//---------- OS/2+EMX ---------- |
| 67 |
char *s; |
| 68 |
GString *ret; |
| 69 |
|
| 70 |
if ((s = getenv("HOME"))) |
| 71 |
ret = new GString(s); |
| 72 |
else |
| 73 |
ret = new GString("."); |
| 74 |
return ret; |
| 75 |
|
| 76 |
#elif defined(ACORN) |
| 77 |
//---------- RISCOS ---------- |
| 78 |
return new GString("@"); |
| 79 |
|
| 80 |
#else |
| 81 |
//---------- Unix ---------- |
| 82 |
char *s; |
| 83 |
struct passwd *pw; |
| 84 |
GString *ret; |
| 85 |
|
| 86 |
if ((s = getenv("HOME"))) { |
| 87 |
ret = new GString(s); |
| 88 |
} else { |
| 89 |
if ((s = getenv("USER"))) |
| 90 |
pw = getpwnam(s); |
| 91 |
else |
| 92 |
pw = getpwuid(getuid()); |
| 93 |
if (pw) |
| 94 |
ret = new GString(pw->pw_dir); |
| 95 |
else |
| 96 |
ret = new GString("."); |
| 97 |
} |
| 98 |
return ret; |
| 99 |
#endif |
| 100 |
} |
| 101 |
|
| 102 |
GString *getCurrentDir() { |
| 103 |
char buf[PATH_MAX+1]; |
| 104 |
|
| 105 |
#if defined(__EMX__) |
| 106 |
if (_getcwd2(buf, sizeof(buf))) |
| 107 |
#elif defined(_WIN32) |
| 108 |
if (GetCurrentDirectoryA(sizeof(buf), buf)) |
| 109 |
#elif defined(ACORN) |
| 110 |
if (strcpy(buf, "@")) |
| 111 |
#else |
| 112 |
if (getcwd(buf, sizeof(buf))) |
| 113 |
#endif |
| 114 |
return new GString(buf); |
| 115 |
return new GString(); |
| 116 |
} |
| 117 |
|
| 118 |
GString *appendToPath(GString *path, const char *fileName) { |
| 119 |
#if defined(VMS) |
| 120 |
//---------- VMS ---------- |
| 121 |
//~ this should handle everything necessary for file |
| 122 |
//~ requesters, but it's certainly not complete |
| 123 |
char *p0, *p1, *p2; |
| 124 |
char *q1; |
| 125 |
|
| 126 |
p0 = path->getCString(); |
| 127 |
p1 = p0 + path->getLength() - 1; |
| 128 |
if (!strcmp(fileName, "-")) { |
| 129 |
if (*p1 == ']') { |
| 130 |
for (p2 = p1; p2 > p0 && *p2 != '.' && *p2 != '['; --p2) ; |
| 131 |
if (*p2 == '[') |
| 132 |
++p2; |
| 133 |
path->del(p2 - p0, p1 - p2); |
| 134 |
} else if (*p1 == ':') { |
| 135 |
path->append("[-]"); |
| 136 |
} else { |
| 137 |
path->clear(); |
| 138 |
path->append("[-]"); |
| 139 |
} |
| 140 |
} else if ((q1 = strrchr(fileName, '.')) && !strncmp(q1, ".DIR;", 5)) { |
| 141 |
if (*p1 == ']') { |
| 142 |
path->insert(p1 - p0, '.'); |
| 143 |
path->insert(p1 - p0 + 1, fileName, q1 - fileName); |
| 144 |
} else if (*p1 == ':') { |
| 145 |
path->append('['); |
| 146 |
path->append(']'); |
| 147 |
path->append(fileName, q1 - fileName); |
| 148 |
} else { |
| 149 |
path->clear(); |
| 150 |
path->append(fileName, q1 - fileName); |
| 151 |
} |
| 152 |
} else { |
| 153 |
if (*p1 != ']' && *p1 != ':') |
| 154 |
path->clear(); |
| 155 |
path->append(fileName); |
| 156 |
} |
| 157 |
return path; |
| 158 |
|
| 159 |
#elif defined(_WIN32) |
| 160 |
//---------- Win32 ---------- |
| 161 |
GString *tmp; |
| 162 |
char buf[256]; |
| 163 |
char *fp; |
| 164 |
|
| 165 |
tmp = new GString(path); |
| 166 |
tmp->append('/'); |
| 167 |
tmp->append(fileName); |
| 168 |
GetFullPathNameA(tmp->getCString(), sizeof(buf), buf, &fp); |
| 169 |
delete tmp; |
| 170 |
path->clear(); |
| 171 |
path->append(buf); |
| 172 |
return path; |
| 173 |
|
| 174 |
#elif defined(ACORN) |
| 175 |
//---------- RISCOS ---------- |
| 176 |
char *p; |
| 177 |
int i; |
| 178 |
|
| 179 |
path->append("."); |
| 180 |
i = path->getLength(); |
| 181 |
path->append(fileName); |
| 182 |
for (p = path->getCString() + i; *p; ++p) { |
| 183 |
if (*p == '/') { |
| 184 |
*p = '.'; |
| 185 |
} else if (*p == '.') { |
| 186 |
*p = '/'; |
| 187 |
} |
| 188 |
} |
| 189 |
return path; |
| 190 |
|
| 191 |
#elif defined(__EMX__) |
| 192 |
//---------- OS/2+EMX ---------- |
| 193 |
int i; |
| 194 |
|
| 195 |
// appending "." does nothing |
| 196 |
if (!strcmp(fileName, ".")) |
| 197 |
return path; |
| 198 |
|
| 199 |
// appending ".." goes up one directory |
| 200 |
if (!strcmp(fileName, "..")) { |
| 201 |
for (i = path->getLength() - 2; i >= 0; --i) { |
| 202 |
if (path->getChar(i) == '/' || path->getChar(i) == '\\' || |
| 203 |
path->getChar(i) == ':') |
| 204 |
break; |
| 205 |
} |
| 206 |
if (i <= 0) { |
| 207 |
if (path->getChar(0) == '/' || path->getChar(0) == '\\') { |
| 208 |
path->del(1, path->getLength() - 1); |
| 209 |
} else if (path->getLength() >= 2 && path->getChar(1) == ':') { |
| 210 |
path->del(2, path->getLength() - 2); |
| 211 |
} else { |
| 212 |
path->clear(); |
| 213 |
path->append(".."); |
| 214 |
} |
| 215 |
} else { |
| 216 |
if (path->getChar(i-1) == ':') |
| 217 |
++i; |
| 218 |
path->del(i, path->getLength() - i); |
| 219 |
} |
| 220 |
return path; |
| 221 |
} |
| 222 |
|
| 223 |
// otherwise, append "/" and new path component |
| 224 |
if (path->getLength() > 0 && |
| 225 |
path->getChar(path->getLength() - 1) != '/' && |
| 226 |
path->getChar(path->getLength() - 1) != '\\') |
| 227 |
path->append('/'); |
| 228 |
path->append(fileName); |
| 229 |
return path; |
| 230 |
|
| 231 |
#else |
| 232 |
//---------- Unix ---------- |
| 233 |
int i; |
| 234 |
|
| 235 |
// appending "." does nothing |
| 236 |
if (!strcmp(fileName, ".")) |
| 237 |
return path; |
| 238 |
|
| 239 |
// appending ".." goes up one directory |
| 240 |
if (!strcmp(fileName, "..")) { |
| 241 |
for (i = path->getLength() - 2; i >= 0; --i) { |
| 242 |
if (path->getChar(i) == '/') |
| 243 |
break; |
| 244 |
} |
| 245 |
if (i <= 0) { |
| 246 |
if (path->getChar(0) == '/') { |
| 247 |
path->del(1, path->getLength() - 1); |
| 248 |
} else { |
| 249 |
path->clear(); |
| 250 |
path->append(".."); |
| 251 |
} |
| 252 |
} else { |
| 253 |
path->del(i, path->getLength() - i); |
| 254 |
} |
| 255 |
return path; |
| 256 |
} |
| 257 |
|
| 258 |
// otherwise, append "/" and new path component |
| 259 |
if (path->getLength() > 0 && |
| 260 |
path->getChar(path->getLength() - 1) != '/') |
| 261 |
path->append('/'); |
| 262 |
path->append(fileName); |
| 263 |
return path; |
| 264 |
#endif |
| 265 |
} |
| 266 |
|
| 267 |
GString *grabPath(char *fileName) { |
| 268 |
#ifdef VMS |
| 269 |
//---------- VMS ---------- |
| 270 |
char *p; |
| 271 |
|
| 272 |
if ((p = strrchr(fileName, ']'))) |
| 273 |
return new GString(fileName, p + 1 - fileName); |
| 274 |
if ((p = strrchr(fileName, ':'))) |
| 275 |
return new GString(fileName, p + 1 - fileName); |
| 276 |
return new GString(); |
| 277 |
|
| 278 |
#elif defined(__EMX__) || defined(_WIN32) |
| 279 |
//---------- OS/2+EMX and Win32 ---------- |
| 280 |
char *p; |
| 281 |
|
| 282 |
if ((p = strrchr(fileName, '/'))) |
| 283 |
return new GString(fileName, (int)(p - fileName)); |
| 284 |
if ((p = strrchr(fileName, '\\'))) |
| 285 |
return new GString(fileName, (int)(p - fileName)); |
| 286 |
if ((p = strrchr(fileName, ':'))) |
| 287 |
return new GString(fileName, (int)(p + 1 - fileName)); |
| 288 |
return new GString(); |
| 289 |
|
| 290 |
#elif defined(ACORN) |
| 291 |
//---------- RISCOS ---------- |
| 292 |
char *p; |
| 293 |
|
| 294 |
if ((p = strrchr(fileName, '.'))) |
| 295 |
return new GString(fileName, p - fileName); |
| 296 |
return new GString(); |
| 297 |
|
| 298 |
#else |
| 299 |
//---------- Unix ---------- |
| 300 |
char *p; |
| 301 |
|
| 302 |
if ((p = strrchr(fileName, '/'))) |
| 303 |
return new GString(fileName, (int)(p - fileName)); |
| 304 |
return new GString(); |
| 305 |
#endif |
| 306 |
} |
| 307 |
|
| 308 |
GBool isAbsolutePath(char *path) { |
| 309 |
#ifdef VMS |
| 310 |
//---------- VMS ---------- |
| 311 |
return strchr(path, ':') || |
| 312 |
(path[0] == '[' && path[1] != '.' && path[1] != '-'); |
| 313 |
|
| 314 |
#elif defined(__EMX__) || defined(_WIN32) |
| 315 |
//---------- OS/2+EMX and Win32 ---------- |
| 316 |
return path[0] == '/' || path[0] == '\\' || path[1] == ':'; |
| 317 |
|
| 318 |
#elif defined(ACORN) |
| 319 |
//---------- RISCOS ---------- |
| 320 |
return path[0] == '$'; |
| 321 |
|
| 322 |
#else |
| 323 |
//---------- Unix ---------- |
| 324 |
return path[0] == '/'; |
| 325 |
#endif |
| 326 |
} |
| 327 |
|
| 328 |
GString *makePathAbsolute(GString *path) { |
| 329 |
#ifdef VMS |
| 330 |
//---------- VMS ---------- |
| 331 |
char buf[PATH_MAX+1]; |
| 332 |
|
| 333 |
if (!isAbsolutePath(path->getCString())) { |
| 334 |
if (getcwd(buf, sizeof(buf))) { |
| 335 |
path->insert(0, buf); |
| 336 |
} |
| 337 |
} |
| 338 |
return path; |
| 339 |
|
| 340 |
#elif defined(_WIN32) |
| 341 |
//---------- Win32 ---------- |
| 342 |
char buf[MAX_PATH]; |
| 343 |
char *fp; |
| 344 |
|
| 345 |
buf[0] = '\0'; |
| 346 |
if (!GetFullPathNameA(path->getCString(), MAX_PATH, buf, &fp)) { |
| 347 |
path->clear(); |
| 348 |
return path; |
| 349 |
} |
| 350 |
path->clear(); |
| 351 |
path->append(buf); |
| 352 |
return path; |
| 353 |
|
| 354 |
#elif defined(ACORN) |
| 355 |
//---------- RISCOS ---------- |
| 356 |
path->insert(0, '@'); |
| 357 |
return path; |
| 358 |
|
| 359 |
#else |
| 360 |
//---------- Unix and OS/2+EMX ---------- |
| 361 |
struct passwd *pw; |
| 362 |
char buf[PATH_MAX+1]; |
| 363 |
GString *s; |
| 364 |
char *p1, *p2; |
| 365 |
int n; |
| 366 |
|
| 367 |
if (path->getChar(0) == '~') { |
| 368 |
if (path->getChar(1) == '/' || |
| 369 |
#ifdef __EMX__ |
| 370 |
path->getChar(1) == '\\' || |
| 371 |
#endif |
| 372 |
path->getLength() == 1) { |
| 373 |
path->del(0, 1); |
| 374 |
s = getHomeDir(); |
| 375 |
path->insert(0, s); |
| 376 |
delete s; |
| 377 |
} else { |
| 378 |
p1 = path->getCString() + 1; |
| 379 |
#ifdef __EMX__ |
| 380 |
for (p2 = p1; *p2 && *p2 != '/' && *p2 != '\\'; ++p2) ; |
| 381 |
#else |
| 382 |
for (p2 = p1; *p2 && *p2 != '/'; ++p2) ; |
| 383 |
#endif |
| 384 |
if ((n = (int)(p2 - p1)) > PATH_MAX) |
| 385 |
n = PATH_MAX; |
| 386 |
strncpy(buf, p1, n); |
| 387 |
buf[n] = '\0'; |
| 388 |
if ((pw = getpwnam(buf))) { |
| 389 |
path->del(0, (int)(p2 - p1 + 1)); |
| 390 |
path->insert(0, pw->pw_dir); |
| 391 |
} |
| 392 |
} |
| 393 |
} else if (!isAbsolutePath(path->getCString())) { |
| 394 |
if (getcwd(buf, sizeof(buf))) { |
| 395 |
#ifndef __EMX__ |
| 396 |
path->insert(0, '/'); |
| 397 |
#endif |
| 398 |
path->insert(0, buf); |
| 399 |
} |
| 400 |
} |
| 401 |
return path; |
| 402 |
#endif |
| 403 |
} |
| 404 |
|
| 405 |
GBool pathIsFile(const char *path) { |
| 406 |
#ifdef _WIN32 |
| 407 |
wchar_t wPath[winMaxLongPath + 1]; |
| 408 |
fileNameToUCS2(path, wPath, winMaxLongPath + 1); |
| 409 |
DWORD attr = GetFileAttributesW(wPath); |
| 410 |
return attr != INVALID_FILE_ATTRIBUTES && |
| 411 |
!(attr & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)); |
| 412 |
#else |
| 413 |
struct stat statBuf; |
| 414 |
return stat(path, &statBuf) == 0 && S_ISREG(statBuf.st_mode); |
| 415 |
#endif |
| 416 |
} |
| 417 |
|
| 418 |
GBool pathIsDir(const char *path) { |
| 419 |
#ifdef _WIN32 |
| 420 |
wchar_t wPath[winMaxLongPath + 1]; |
| 421 |
fileNameToUCS2(path, wPath, winMaxLongPath + 1); |
| 422 |
DWORD attr = GetFileAttributesW(wPath); |
| 423 |
return attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY); |
| 424 |
#else |
| 425 |
struct stat statBuf; |
| 426 |
return stat(path, &statBuf) == 0 && S_ISDIR(statBuf.st_mode); |
| 427 |
#endif |
| 428 |
} |
| 429 |
|
| 430 |
time_t getModTime(char *fileName) { |
| 431 |
#ifdef _WIN32 |
| 432 |
//~ should implement this, but it's (currently) only used in xpdf |
| 433 |
return 0; |
| 434 |
#else |
| 435 |
struct stat statBuf; |
| 436 |
|
| 437 |
if (stat(fileName, &statBuf)) { |
| 438 |
return 0; |
| 439 |
} |
| 440 |
return statBuf.st_mtime; |
| 441 |
#endif |
| 442 |
} |
| 443 |
|
| 444 |
#ifndef PDF_PARSER_ONLY |
| 445 |
GBool openTempFile(GString **name, FILE **f, |
| 446 |
const char *mode, const char *ext) { |
| 447 |
#if defined(_WIN32) |
| 448 |
//---------- Win32 ---------- |
| 449 |
char tempPath[MAX_PATH + 1]; |
| 450 |
GString *s, *s2; |
| 451 |
FILE *f2; |
| 452 |
DWORD n; |
| 453 |
int t, i; |
| 454 |
|
| 455 |
// this has the standard race condition problem, but I haven't found |
| 456 |
// a better way to generate temp file names with extensions on |
| 457 |
// Windows |
| 458 |
n = GetTempPathA(sizeof(tempPath), tempPath); |
| 459 |
if (n > 0 && n <= sizeof(tempPath)) { |
| 460 |
s = new GString(tempPath); |
| 461 |
if (tempPath[n-1] != '\\') { |
| 462 |
s->append('\\'); |
| 463 |
} |
| 464 |
} else { |
| 465 |
s = new GString(".\\"); |
| 466 |
} |
| 467 |
s->appendf("xpdf_{0:d}_{1:d}_", |
| 468 |
(int)GetCurrentProcessId(), (int)GetCurrentThreadId()); |
| 469 |
t = (int)time(NULL); |
| 470 |
for (i = 0; i < 1000; ++i) { |
| 471 |
s2 = GString::format("{0:t}{1:d}", s, t + i); |
| 472 |
if (ext) { |
| 473 |
s2->append(ext); |
| 474 |
} |
| 475 |
if (!(f2 = fopen(s2->getCString(), "r"))) { |
| 476 |
if (!(f2 = fopen(s2->getCString(), mode))) { |
| 477 |
delete s2; |
| 478 |
delete s; |
| 479 |
return gFalse; |
| 480 |
} |
| 481 |
*name = s2; |
| 482 |
*f = f2; |
| 483 |
delete s; |
| 484 |
return gTrue; |
| 485 |
} |
| 486 |
fclose(f2); |
| 487 |
delete s2; |
| 488 |
} |
| 489 |
delete s; |
| 490 |
return gFalse; |
| 491 |
#elif defined(VMS) || defined(__EMX__) || defined(ACORN) |
| 492 |
//---------- non-Unix ---------- |
| 493 |
char *s; |
| 494 |
|
| 495 |
// There is a security hole here: an attacker can create a symlink |
| 496 |
// with this file name after the tmpnam call and before the fopen |
| 497 |
// call. I will happily accept fixes to this function for non-Unix |
| 498 |
// OSs. |
| 499 |
if (!(s = tmpnam(NULL))) { |
| 500 |
return gFalse; |
| 501 |
} |
| 502 |
*name = new GString(s); |
| 503 |
if (ext) { |
| 504 |
(*name)->append(ext); |
| 505 |
} |
| 506 |
if (!(*f = fopen((*name)->getCString(), mode))) { |
| 507 |
delete (*name); |
| 508 |
*name = NULL; |
| 509 |
return gFalse; |
| 510 |
} |
| 511 |
return gTrue; |
| 512 |
#else |
| 513 |
//---------- Unix ---------- |
| 514 |
char *s; |
| 515 |
int fd; |
| 516 |
|
| 517 |
if (ext) { |
| 518 |
#if HAVE_MKSTEMPS |
| 519 |
if ((s = getenv("TMPDIR"))) { |
| 520 |
*name = new GString(s); |
| 521 |
} else { |
| 522 |
*name = new GString("/tmp"); |
| 523 |
} |
| 524 |
(*name)->append("/XXXXXX")->append(ext); |
| 525 |
fd = mkstemps((*name)->getCString(), (int)strlen(ext)); |
| 526 |
#else |
| 527 |
if (!(s = tmpnam(NULL))) { |
| 528 |
return gFalse; |
| 529 |
} |
| 530 |
*name = new GString(s); |
| 531 |
(*name)->append(ext); |
| 532 |
fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600); |
| 533 |
#endif |
| 534 |
} else { |
| 535 |
#if HAVE_MKSTEMP |
| 536 |
if ((s = getenv("TMPDIR"))) { |
| 537 |
*name = new GString(s); |
| 538 |
} else { |
| 539 |
*name = new GString("/tmp"); |
| 540 |
} |
| 541 |
(*name)->append("/XXXXXX"); |
| 542 |
fd = mkstemp((*name)->getCString()); |
| 543 |
#else // HAVE_MKSTEMP |
| 544 |
if (!(s = tmpnam(NULL))) { |
| 545 |
return gFalse; |
| 546 |
} |
| 547 |
*name = new GString(s); |
| 548 |
fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600); |
| 549 |
#endif // HAVE_MKSTEMP |
| 550 |
} |
| 551 |
if (fd < 0 || !(*f = fdopen(fd, mode))) { |
| 552 |
delete *name; |
| 553 |
*name = NULL; |
| 554 |
return gFalse; |
| 555 |
} |
| 556 |
return gTrue; |
| 557 |
#endif |
| 558 |
} |
| 559 |
#endif /* !PDF_PARSER_ONLY */ |
| 560 |
|
| 561 |
GBool createDir(char *path, int mode) { |
| 562 |
#ifdef _WIN32 |
| 563 |
return !_mkdir(path); |
| 564 |
#else |
| 565 |
return !mkdir(path, mode); |
| 566 |
#endif |
| 567 |
} |
| 568 |
|
| 569 |
GBool executeCommand(char *cmd) { |
| 570 |
#ifdef VMS |
| 571 |
return system(cmd) ? gTrue : gFalse; |
| 572 |
#else |
| 573 |
return system(cmd) ? gFalse : gTrue; |
| 574 |
#endif |
| 575 |
} |
| 576 |
|
| 577 |
#ifdef _WIN32 |
| 578 |
GString *fileNameToUTF8(char *path) { |
| 579 |
GString *s; |
| 580 |
char *p; |
| 581 |
|
| 582 |
s = new GString(); |
| 583 |
for (p = path; *p; ++p) { |
| 584 |
if (*p & 0x80) { |
| 585 |
s->append((char)(0xc0 | ((*p >> 6) & 0x03))); |
| 586 |
s->append((char)(0x80 | (*p & 0x3f))); |
| 587 |
} else { |
| 588 |
s->append(*p); |
| 589 |
} |
| 590 |
} |
| 591 |
return s; |
| 592 |
} |
| 593 |
|
| 594 |
GString *fileNameToUTF8(wchar_t *path) { |
| 595 |
GString *s; |
| 596 |
wchar_t *p; |
| 597 |
|
| 598 |
s = new GString(); |
| 599 |
for (p = path; *p; ++p) { |
| 600 |
if (*p < 0x80) { |
| 601 |
s->append((char)*p); |
| 602 |
} else if (*p < 0x800) { |
| 603 |
s->append((char)(0xc0 | ((*p >> 6) & 0x1f))); |
| 604 |
s->append((char)(0x80 | (*p & 0x3f))); |
| 605 |
} else { |
| 606 |
s->append((char)(0xe0 | ((*p >> 12) & 0x0f))); |
| 607 |
s->append((char)(0x80 | ((*p >> 6) & 0x3f))); |
| 608 |
s->append((char)(0x80 | (*p & 0x3f))); |
| 609 |
} |
| 610 |
} |
| 611 |
return s; |
| 612 |
} |
| 613 |
|
| 614 |
GString *fileNameMultiByteToUTF8(char *path) { |
| 615 |
wchar_t fileNameW[winMaxLongPath + 1]; |
| 616 |
if (MultiByteToWideChar(CP_ACP, 0, path, -1, |
| 617 |
fileNameW, sizeof(fileNameW) / sizeof(wchar_t))) { |
| 618 |
return fileNameToUTF8(fileNameW); |
| 619 |
} else { |
| 620 |
// shouldn't happen, but just in case... |
| 621 |
return new GString(path); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
wchar_t *fileNameToUCS2(const char *path, wchar_t *out, size_t outSize) { |
| 626 |
const char *p; |
| 627 |
size_t i; |
| 628 |
|
| 629 |
for (p = path, i = 0; *p && i < outSize - 1; ++i) { |
| 630 |
if ((p[0] & 0xe0) == 0xc0 && |
| 631 |
p[1] && (p[1] & 0xc0) == 0x80) { |
| 632 |
out[i] = (wchar_t)(((p[0] & 0x1f) << 6) | |
| 633 |
(p[1] & 0x3f)); |
| 634 |
p += 2; |
| 635 |
} else if ((p[0] & 0xf0) == 0xe0 && |
| 636 |
(p[1] & 0xc0) == 0x80 && |
| 637 |
(p[2] & 0xc0) == 0x80) { |
| 638 |
out[i] = (wchar_t)(((p[0] & 0x0f) << 12) | |
| 639 |
((p[1] & 0x3f) << 6) | |
| 640 |
(p[2] & 0x3f)); |
| 641 |
p += 3; |
| 642 |
} else { |
| 643 |
out[i] = (wchar_t)(p[0] & 0xff); |
| 644 |
p += 1; |
| 645 |
} |
| 646 |
} |
| 647 |
out[i] = (wchar_t)0; |
| 648 |
return out; |
| 649 |
} |
| 650 |
#endif |
| 651 |
|
| 652 |
FILE *openFile(const char *path, const char *mode) { |
| 653 |
#if 0 /* we use fsyscp_fopen for fopen on win32 */ |
| 654 |
wchar_t wPath[winMaxLongPath + 1]; |
| 655 |
wchar_t wMode[8]; |
| 656 |
int i; |
| 657 |
|
| 658 |
fileNameToUCS2(path, wPath, winMaxLongPath + 1); |
| 659 |
for (i = 0; mode[i] && i < sizeof(wMode)/sizeof(wchar_t) - 1; ++i) { |
| 660 |
wMode[i] = (wchar_t)(mode[i] & 0xff); |
| 661 |
} |
| 662 |
wMode[i] = (wchar_t)0; |
| 663 |
readWindowsShortcut(wPath, winMaxLongPath + 1); |
| 664 |
return _wfopen(wPath, wMode); |
| 665 |
#elif defined(VMS) |
| 666 |
return fopen(path, mode, "ctx=stm"); |
| 667 |
#else |
| 668 |
return fopen(path, mode); |
| 669 |
#endif |
| 670 |
} |
| 671 |
|
| 672 |
#if 0 |
| 673 |
void readWindowsShortcut(wchar_t *wPath, size_t wPathSize) { |
| 674 |
size_t n = wcslen(wPath); |
| 675 |
if (n < 4 || wcscmp(wPath + n - 4, L".lnk")) { |
| 676 |
return; |
| 677 |
} |
| 678 |
IShellLinkW *shellLink; |
| 679 |
HRESULT hres; |
| 680 |
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, |
| 681 |
IID_IShellLinkW, (LPVOID *)&shellLink); |
| 682 |
bool needCoUninit = false; |
| 683 |
if (hres == CO_E_NOTINITIALIZED) { |
| 684 |
CoInitialize(NULL); |
| 685 |
needCoUninit = true; |
| 686 |
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, |
| 687 |
IID_IShellLinkW, (LPVOID *)&shellLink); |
| 688 |
} |
| 689 |
if (FAILED(hres)) { |
| 690 |
return; |
| 691 |
} |
| 692 |
IPersistFile *persistFile; |
| 693 |
hres = shellLink->QueryInterface(IID_IPersistFile, (LPVOID *)&persistFile); |
| 694 |
if (FAILED(hres)) { |
| 695 |
return; |
| 696 |
} |
| 697 |
hres = persistFile->Load(wPath, STGM_READ); |
| 698 |
if (FAILED(hres)) { |
| 699 |
fprintf(stderr, "IPersistFile.Load failed: 0x%08lx\n", hres); |
| 700 |
exit(1); |
| 701 |
} |
| 702 |
wchar_t target[winMaxLongPath + 1]; |
| 703 |
hres = shellLink->GetPath(target, winMaxLongPath + 1, NULL, 0); |
| 704 |
if (FAILED(hres)) { |
| 705 |
return; |
| 706 |
} |
| 707 |
shellLink->Release(); |
| 708 |
if (needCoUninit) { |
| 709 |
CoUninitialize(); |
| 710 |
} |
| 711 |
if (wcslen(target) > wPathSize - 1) { |
| 712 |
return; |
| 713 |
} |
| 714 |
wcscpy(wPath, target); |
| 715 |
} |
| 716 |
#endif |
| 717 |
|
| 718 |
int makeDir(const char *path, int mode) { |
| 719 |
#ifdef _WIN32 |
| 720 |
/* |
| 721 |
wchar_t wPath[winMaxLongPath + 1]; |
| 722 |
return _wmkdir(fileNameToUCS2(path, wPath, winMaxLongPath + 1)); |
| 723 |
*/ |
| 724 |
return _mkdir(path); |
| 725 |
#else |
| 726 |
return mkdir(path, (mode_t)mode); |
| 727 |
#endif |
| 728 |
} |
| 729 |
|
| 730 |
char *getLine(char *buf, int size, FILE *f) { |
| 731 |
int c, i; |
| 732 |
|
| 733 |
i = 0; |
| 734 |
while (i < size - 1) { |
| 735 |
if ((c = fgetc(f)) == EOF) { |
| 736 |
break; |
| 737 |
} |
| 738 |
buf[i++] = (char)c; |
| 739 |
if (c == '\x0a') { |
| 740 |
break; |
| 741 |
} |
| 742 |
if (c == '\x0d') { |
| 743 |
c = fgetc(f); |
| 744 |
if (c == '\x0a' && i < size - 1) { |
| 745 |
buf[i++] = (char)c; |
| 746 |
} else if (c != EOF) { |
| 747 |
ungetc(c, f); |
| 748 |
} |
| 749 |
break; |
| 750 |
} |
| 751 |
} |
| 752 |
buf[i] = '\0'; |
| 753 |
if (i == 0) { |
| 754 |
return NULL; |
| 755 |
} |
| 756 |
return buf; |
| 757 |
} |
| 758 |
|
| 759 |
int gfseek(FILE *f, GFileOffset offset, int whence) { |
| 760 |
#if HAVE_FSEEKO |
| 761 |
return fseeko(f, offset, whence); |
| 762 |
#elif HAVE_FSEEK64 |
| 763 |
return fseek64(f, offset, whence); |
| 764 |
#elif HAVE_FSEEKI64 |
| 765 |
return _fseeki64(f, offset, whence); |
| 766 |
#else |
| 767 |
return fseek(f, offset, whence); |
| 768 |
#endif |
| 769 |
} |
| 770 |
|
| 771 |
GFileOffset gftell(FILE *f) { |
| 772 |
#if HAVE_FSEEKO |
| 773 |
return ftello(f); |
| 774 |
#elif HAVE_FSEEK64 |
| 775 |
return ftell64(f); |
| 776 |
#elif HAVE_FSEEKI64 |
| 777 |
return _ftelli64(f); |
| 778 |
#else |
| 779 |
return ftell(f); |
| 780 |
#endif |
| 781 |
} |
| 782 |
|
| 783 |
#ifndef PDF_PARSER_ONLY |
| 784 |
void fixCommandLine(int *argc, char **argv[]) { |
| 785 |
#ifdef _WIN32 |
| 786 |
int argcw; |
| 787 |
wchar_t **argvw; |
| 788 |
GString *arg; |
| 789 |
int i; |
| 790 |
|
| 791 |
argvw = CommandLineToArgvW(GetCommandLineW(), &argcw); |
| 792 |
if (!argvw || argcw < 0) { |
| 793 |
return; |
| 794 |
} |
| 795 |
|
| 796 |
*argc = argcw; |
| 797 |
|
| 798 |
*argv = (char **)gmallocn(argcw + 1, sizeof(char *)); |
| 799 |
for (i = 0; i < argcw; ++i) { |
| 800 |
arg = fileNameToUTF8(argvw[i]); |
| 801 |
(*argv)[i] = copyString(arg->getCString()); |
| 802 |
delete arg; |
| 803 |
} |
| 804 |
(*argv)[argcw] = NULL; |
| 805 |
|
| 806 |
LocalFree(argvw); |
| 807 |
#endif |
| 808 |
} |
| 809 |
#endif /* !PDF_PARSER_ONLY */ |