applied diff between xpdf-1.01-orig and xpdf-1.01-swftools.
[swftools.git] / pdf2swf / xpdf / gfile.cc
index 7bc9b22..5fa0762 100644 (file)
@@ -4,18 +4,13 @@
 //
 // Miscellaneous file and directory name manipulation.
 //
-// Copyright 1996 Derek B. Noonburg
+// Copyright 1996-2003 Glyph & Cog, LLC
 //
 //========================================================================
 
-#include "../../config.h"
-#ifdef WIN32
-   extern "C" {
-#  ifndef _MSC_VER
-#    include <kpathsea/win32lib.h>
-#  endif
-   }
-#else // !WIN32
+#include <aconf.h>
+
+#ifndef WIN32
 #  if defined(MACOS)
 #    include <sys/stat.h>
 #  elif !defined(ACORN)
@@ -441,15 +436,72 @@ time_t getModTime(char *fileName) {
   return statBuf.st_mtime;
 #endif
 }
-static char tmpbuf[128];
-static char* mktmpname(char*ptr) {
-//   used to be mktemp. This does remove the warnings, but
-//   It's not exactly an improvement.
-    sprintf(tmpbuf, "/tmp/%08x%08x",lrand48(),lrand48());
-    return tmpbuf;
+
+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(VMS) || defined(__EMX__) || defined(WIN32) || defined(ACORN) || defined(MACOS)
+#if defined(WIN32)
+  //---------- Win32 ----------
+  char *s;
+
+  if (!(s = _tempnam(getenv("TEMP"), NULL))) {
+    return gFalse;
+  }
+  *name = new GString(s);
+  free(s);
+  if (ext) {
+    (*name)->append(ext);
+  }
+  if (!(*f = fopen((*name)->getCString(), mode))) {
+    delete (*name);
+    return gFalse;
+  }
+  return gTrue;
+#elif defined(VMS) || defined(__EMX__) || defined(ACORN) || defined(MACOS)
   //---------- non-Unix ----------
   char *s;
 
@@ -471,20 +523,26 @@ GBool openTempFile(GString **name, FILE **f, char *mode, char *ext) {
   return gTrue;
 #else
   //---------- Unix ----------
-  char *s, *p;
+  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);
-    s = (*name)->getCString();
-    if ((p = strrchr(s, '.'))) {
-      (*name)->del(p - s, (*name)->getLength() - (p - s));
-    }
     (*name)->append(ext);
     fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600);
+#endif
   } else {
 #if HAVE_MKSTEMP
     if ((s = getenv("TMPDIR"))) {
@@ -510,11 +568,48 @@ GBool openTempFile(GString **name, FILE **f, char *mode, char *ext) {
 #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 *name1, GBool doStat) {
+GDirEntry::GDirEntry(char *dirPath, char *nameA, GBool doStat) {
 #ifdef VMS
   char *p;
 #elif defined(WIN32)
@@ -526,17 +621,17 @@ GDirEntry::GDirEntry(char *dirPath, char *name1, GBool doStat) {
   GString *s;
 #endif
 
-  name = new GString(name1);
+  name = new GString(nameA);
   dir = gFalse;
   if (doStat) {
 #ifdef VMS
-    if (!strcmp(name1, "-") ||
-       ((p = strrchr(name1, '.')) && !strncmp(p, ".DIR;", 5)))
+    if (!strcmp(nameA, "-") ||
+       ((p = strrchr(nameA, '.')) && !strncmp(p, ".DIR;", 5)))
       dir = gTrue;
 #elif defined(ACORN)
 #else
     s = new GString(dirPath);
-    appendToPath(s, name1);
+    appendToPath(s, nameA);
 #ifdef WIN32
     fa = GetFileAttributes(s->getCString());
     dir = (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY));
@@ -553,9 +648,9 @@ GDirEntry::~GDirEntry() {
   delete name;
 }
 
-GDir::GDir(char *name, GBool doStat1) {
+GDir::GDir(char *name, GBool doStatA) {
   path = new GString(name);
-  doStat = doStat1;
+  doStat = doStatA;
 #if defined(WIN32)
   GString *tmp;
 
@@ -589,36 +684,48 @@ GDir::~GDir() {
 }
 
 GDirEntry *GDir::getNextEntry() {
-  struct dirent *ent;
   GDirEntry *e;
 
-  e = NULL;
 #if defined(WIN32)
-  e = new GDirEntry(path->getCString(), ffd.cFileName, doStat);
-  if (hnd  && !FindNextFile(hnd, &ffd)) {
-    FindClose(hnd);
-    hnd = NULL;
+  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)
-#else
+#elif defined(VMS)
+  struct dirent *ent;
+  e = NULL;
   if (dir) {
-#ifdef VMS
     if (needParent) {
       e = new GDirEntry(path->getCString(), "-", doStat);
       needParent = gFalse;
       return e;
     }
-#endif
     ent = readdir(dir);
-#ifndef VMS
-    if (ent && !strcmp(ent->d_name, "."))
+    if (ent) {
+      e = new GDirEntry(path->getCString(), ent->d_name, doStat);
+    }
+  }
+#else
+  struct dirent *ent;
+  e = NULL;
+  if (dir) {
+    ent = readdir(dir);
+    if (ent && !strcmp(ent->d_name, ".")) {
       ent = readdir(dir);
-#endif
-    if (ent)
+    }
+    if (ent) {
       e = new GDirEntry(path->getCString(), ent->d_name, doStat);
+    }
   }
 #endif
+
   return e;
 }
 
@@ -631,6 +738,7 @@ void GDir::rewind() {
   tmp = path->copy();
   tmp->append("/*.*");
   hnd = FindFirstFile(tmp->getCString(), &ffd);
+  delete tmp;
 #elif defined(ACORN)
 #elif defined(MACOS)
 #else