+ return res;
+ }
+}
+
+FILE *fileTable_createMacrosFile (fileTable ft, cstring fname)
+{
+ int fdesc = open (cstring_toCharsSafe (fname), O_RDWR | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR);
+
+ if (fdesc == -1)
+ {
+ osd_setTempError ();
+ llfatalerror (message ("Temporary file for "
+ "pre-processor output already exists. Trying to "
+ "open: %s.",
+ fname));
+
+ /*@notreached@*/ return NULL;
+ }
+ else
+ {
+ FILE *res = fdopen (fdesc, "w+");
+
+ if (res != NULL)
+ {
+ fileTable_addOpen (ft, res, cstring_copy (fname));
+ DPRINTF (("Opening file: %s / %p", fname, res));
+ }
+ else
+ {
+ DPRINTF (("Error opening: %s", fname));
+ }
+
+ return res;
+ }
+}
+
+FILE *fileTable_openReadFile (fileTable ft, cstring fname)
+{
+ FILE *res = fopen (cstring_toCharsSafe (fname), "r");
+
+ if (res != NULL)
+ {
+ fileTable_addOpen (ft, res, cstring_copy (fname));
+ DPRINTF (("Opening read file: %s / %p", fname, res));
+ }
+ else
+ {
+ DPRINTF (("Cannot open read file: %s", fname));
+ }
+
+ return res;
+}
+
+/*
+** Allows overwriting
+*/
+
+FILE *fileTable_openWriteFile (fileTable ft, cstring fname)
+{
+ FILE *res = fopen (cstring_toCharsSafe (fname), "w");
+
+ if (res != NULL) {
+ fileTable_addOpen (ft, res, cstring_copy (fname));
+ DPRINTF (("Opening file: %s / %p", fname, res));
+ }
+
+ return res;
+}
+
+FILE *fileTable_openWriteUpdateFile (fileTable ft, cstring fname)
+{
+ FILE *res = fopen (cstring_toCharsSafe (fname), "w+");
+
+ if (res != NULL) {
+ fileTable_addOpen (ft, res, cstring_copy (fname));
+ DPRINTF (("Opening file: %s / %p", fname, res));
+ }
+
+ return res;
+}
+
+bool fileTable_closeFile (fileTable ft, FILE *f)
+{
+ bool foundit = FALSE;
+ int i = 0;
+
+ llassert (fileTable_isDefined (ft));
+
+ DPRINTF (("Closing file: %p", f));
+
+ for (i = 0; i < ft->nopen; i++)
+ {
+ if (ft->openelements[i]->f == f)
+ {
+ DPRINTF (("Closing file: %p = %s", f, ft->openelements[i]->fname));
+
+ if (i == ft->nopen - 1)
+ {
+ foentry_free (ft->openelements[i]);
+ ft->openelements[i] = NULL;
+ }
+ else
+ {
+ foentry_free (ft->openelements[i]);
+ ft->openelements[i] = ft->openelements[ft->nopen - 1];
+ ft->openelements[ft->nopen - 1] = NULL;
+ }
+
+ ft->nopen--;
+ ft->nopenspace++;
+ foundit = TRUE;
+ break;
+ }
+ }
+
+ llassert (foundit);
+ return (fclose (f) == 0);
+}
+
+void fileTable_closeAll (fileTable ft)
+{
+ int i = 0;
+
+ llassert (fileTable_isDefined (ft));
+
+ for (i = 0; i < ft->nopen; i++)
+ {
+ /*
+ lldiagmsg (message ("Unclosed file at exit: %s", ft->openelements[i]->fname));
+ */
+
+ if (ft->openelements[i]->f != NULL)
+ {
+ (void) fclose (ft->openelements[i]->f); /* No check - cleaning up after errors */
+ }
+
+ ft->openelements[i]->f = NULL;
+ foentry_free (ft->openelements[i]);
+ ft->openelements[i] = NULL;
+ }
+
+ ft->nopenspace += ft->nopen;
+ ft->nopen = 0;
+}