📄 gfile.cc
字号:
if (path->getChar(0) == '~') { if (path->getChar(1) == '/' ||#ifdef __EMX__ path->getChar(1) == '\\' ||#endif path->getLength() == 1) { path->del(0, 1); s = getHomeDir(); path->insert(0, s); delete s; } else { p1 = path->getCString() + 1;#ifdef __EMX__ for (p2 = p1; *p2 && *p2 != '/' && *p2 != '\\'; ++p2) ;#else for (p2 = p1; *p2 && *p2 != '/'; ++p2) ;#endif if ((n = p2 - p1) > PATH_MAX) n = PATH_MAX; strncpy(buf, p1, n); buf[n] = '\0'; if ((pw = getpwnam(buf))) { path->del(0, p2 - p1 + 1); path->insert(0, pw->pw_dir); } } } else if (!isAbsolutePath(path->getCString())) { if (getcwd(buf, sizeof(buf))) {#ifndef __EMX__ path->insert(0, '/');#endif path->insert(0, buf); } } return path;#endif}time_t getModTime(char *fileName) {#ifdef WIN32 //~ should implement this, but it's (currently) only used in xpdf return 0;#else struct stat statBuf; if (stat(fileName, &statBuf)) { return 0; } return statBuf.st_mtime;#endif}static char* getTempDir(){#ifdef WIN32 char*dir = getenv("TMP"); if(!dir) dir = getenv("TEMP"); if(!dir) dir = getenv("tmp"); if(!dir) dir = getenv("temp"); if(!dir) dir = "C:\\";#else char* dir = "/tmp/";#endif return dir;}char* mktmpname(char*ptr) { static char tmpbuf[128]; char*dir = getTempDir(); int l = strlen(dir); char*sep = ""; if(!ptr) ptr = tmpbuf; if(l && dir[l-1]!='/' && dir[l-1]!='\\') {#ifdef WIN32 sep = "\\";#else sep = "/";#endif } // used to be mktemp. This does remove the warnings, but // It's not exactly an improvement.#ifdef HAVE_LRAND48 sprintf(ptr, "%s%s%08x%08x",dir,sep,lrand48(),lrand48());#else# ifdef HAVE_RAND sprintf(ptr, "%s%s%08x%08x",dir,sep,rand(),rand());# else static int count = 1; sprintf(ptr, "%s%s%08x%04x%04x",dir,sep,time(0),(unsigned int)tmpbuf^((unsigned int)tmpbuf)>>16,count); count ++;# endif#endif return ptr;}GBool openTempFile(GString **name, FILE **f, char *mode, char *ext) {#if defined(WIN32) //---------- Win32 ---------- char *tempDir; GString *s, *s2; char buf[32]; FILE *f2; int t, i; // this has the standard race condition problem, but I haven't found // a better way to generate temp file names with extensions on // Windows if ((tempDir = getenv("TEMP"))) { s = new GString(tempDir); s->append('\\'); } else { s = new GString(); } s->append("x"); t = (int)time(NULL); for (i = 0; i < 1000; ++i) { sprintf(buf, "%d", t + i); s2 = s->copy()->append(buf); if (ext) { s2->append(ext); } if (!(f2 = fopen(s2->getCString(), "r"))) { if (!(f2 = fopen(s2->getCString(), mode))) { delete s2; delete s; return gFalse; } *name = s2; *f = f2; delete s; return gTrue; } fclose(f2); delete s2; } delete s; return gFalse;#elif defined(VMS) || defined(__EMX__) || defined(ACORN) || defined(MACOS) //---------- non-Unix ---------- char *s; // There is a security hole here: an attacker can create a symlink // with this file name after the tmpnam call and before the fopen // call. I will happily accept fixes to this function for non-Unix // OSs. if (!(s = mktmpname(NULL))) { return gFalse; } *name = new GString(s); if (ext) { (*name)->append(ext); } if (!(*f = fopen((*name)->getCString(), mode))) { delete (*name); return gFalse; } return gTrue;#else //---------- Unix ---------- char *s; int fd; if (ext) {#if HAVE_MKSTEMPS if ((s = getenv("TMPDIR"))) { *name = new GString(s); } else { *name = new GString("/tmp"); } (*name)->append("/XXXXXX")->append(ext); fd = mkstemps((*name)->getCString(), strlen(ext));#else if (!(s = mktmpname(NULL))) { return gFalse; } *name = new GString(s); (*name)->append(ext); fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600);#endif } else {#if HAVE_MKSTEMP if ((s = getenv("TMPDIR"))) { *name = new GString(s); } else { *name = new GString("/tmp"); } (*name)->append("/XXXXXX"); fd = mkstemp((*name)->getCString());#else // HAVE_MKSTEMP if (!(s = mktmpname(NULL))) { return gFalse; } *name = new GString(s); fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600);#endif // HAVE_MKSTEMP } if (fd < 0 || !(*f = fdopen(fd, mode))) { delete *name; return gFalse; } return gTrue;#endif}GBool executeCommand(char *cmd) {#ifdef VMS return system(cmd) ? gTrue : gFalse;#else return system(cmd) ? gFalse : gTrue;#endif}char *getLine(char *buf, int size, FILE *f) { int c, i; i = 0; while (i < size - 1) { if ((c = fgetc(f)) == EOF) { break; } buf[i++] = (char)c; if (c == '\x0a') { break; } if (c == '\x0d') { c = fgetc(f); if (c == '\x0a' && i < size - 1) { buf[i++] = (char)c; } else if (c != EOF) { ungetc(c, f); } break; } } buf[i] = '\0'; if (i == 0) { return NULL; } return buf;}//------------------------------------------------------------------------// GDir and GDirEntry//------------------------------------------------------------------------GDirEntry::GDirEntry(char *dirPath, char *nameA, GBool doStat) {#ifdef VMS char *p;#elif defined(WIN32) int fa; GString *s;#elif defined(ACORN)#else struct stat st; GString *s;#endif name = new GString(nameA); dir = gFalse; if (doStat) {#ifdef VMS if (!strcmp(nameA, "-") || ((p = strrchr(nameA, '.')) && !strncmp(p, ".DIR;", 5))) dir = gTrue;#elif defined(ACORN)#else s = new GString(dirPath); appendToPath(s, nameA);#ifdef WIN32 fa = GetFileAttributes(s->getCString()); dir = (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY));#else if (stat(s->getCString(), &st) == 0) dir = S_ISDIR(st.st_mode);#endif delete s;#endif }}GDirEntry::~GDirEntry() { delete name;}GDir::GDir(char *name, GBool doStatA) { path = new GString(name); doStat = doStatA;#if defined(WIN32) GString *tmp; tmp = path->copy(); tmp->append("/*.*"); hnd = FindFirstFile(tmp->getCString(), &ffd); delete tmp;#elif defined(ACORN)#elif defined(MACOS)#else dir = opendir(name);#ifdef VMS needParent = strchr(name, '[') != NULL;#endif#endif}GDir::~GDir() { delete path;#if defined(WIN32) if (hnd) { FindClose(hnd); hnd = NULL; }#elif defined(ACORN)#elif defined(MACOS)#else if (dir) closedir(dir);#endif}GDirEntry *GDir::getNextEntry() { GDirEntry *e;#if defined(WIN32) if (hnd) { e = new GDirEntry(path->getCString(), ffd.cFileName, doStat); if (hnd && !FindNextFile(hnd, &ffd)) { FindClose(hnd); hnd = NULL; } } else { e = NULL; }#elif defined(ACORN)#elif defined(MACOS)#elif defined(VMS) struct dirent *ent; e = NULL; if (dir) { if (needParent) { e = new GDirEntry(path->getCString(), "-", doStat); needParent = gFalse; return e; } ent = readdir(dir); if (ent) { e = new GDirEntry(path->getCString(), ent->d_name, doStat); } }#else struct dirent *ent; e = NULL; if (dir) { ent = (struct dirent *)readdir(dir); if (ent && !strcmp(ent->d_name, ".")) { ent = (struct dirent *)readdir(dir); } if (ent) { e = new GDirEntry(path->getCString(), ent->d_name, doStat); } }#endif return e;}void GDir::rewind() {#ifdef WIN32 GString *tmp; if (hnd) FindClose(hnd); tmp = path->copy(); tmp->append("/*.*"); hnd = FindFirstFile(tmp->getCString(), &ffd); delete tmp;#elif defined(ACORN)#elif defined(MACOS)#else if (dir) rewinddir(dir);#ifdef VMS needParent = strchr(path->getCString(), '[') != NULL;#endif#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -