From jmarshall@acm.org Wed Jul  4 20:10:07 2001
Date: Thu, 5 Jul 2001 04:18:33 +1200
From: John Marshall <jmarshall@acm.org>
To: prc-tools-devel@lists.sourceforge.net
Subject: Fix Pilrc PRC file writing [patch]

As mentioned in a recent palm-dev-forum thread, pilrc 2.8p5's PRC file
writer fwrite()s a host struct to try to output the headers of the PRC
file format.  For the usual non-portability of struct packing reasons,
this is broken non-portable code.

I've dusted off my old PRC-writing-via-PilRC's-CbEmitStruct patch, and
integrated it with the current 2.8p5.  It has the advantage that it
actually works.  :-)

I took a couple of liberties:

* I've rearranged the resource output code in util.c and unglobalised
  some of the global varables it uses.  I think this code is a bit more
  comprehensible now.

* I've made the PRC mode based off the same [outputpath] command line
  option as the *.bin directory, instead of off the -R option.  This is
  better for the code, and it's conceivable that some nut might still
  want to generate a .res file :-).  It also means that we can default
  the output filename:

	pilrc -ro foo.rcp foo.ro	-> writes foo.ro
	pilrc -ro foo.rcp		-> also writes foo.ro

* Also you see my patch used "-ro" instead of "-prc" :-).  Because the
  output filename is *.ro and "resource output" trips off the tongue,
  I think that makes a little more sense, but YMMV...

* I omitted two file deletions from the patch:

	rm prc.c prc.h

  The PRC writing functionality is now within util.c.

I had a bit of a nightmare applying this patch due to line terminator
issues in one or two files.  Good luck...

    John

====
diff -urNp orig-pilrc-2.8p5/Makefile.am pilrc-2.8p5/Makefile.am
--- orig-pilrc-2.8p5/Makefile.am	Sun May 27 01:51:43 2001
+++ pilrc-2.8p5/Makefile.am	Thu Jul  5 02:05:27 2001
@@ -9,11 +9,11 @@ endif
 
 bin_PROGRAMS=pilrc $(PILRCUI_PROG)
 
-COMMON_SOURCES=pilrc.c lex.c prc.c util.c restype.c bitmap.c font.c plex.c makeKbd.c
+COMMON_SOURCES=pilrc.c lex.c util.c restype.c bitmap.c font.c plex.c makeKbd.c
 
 pilrc_SOURCES=$(COMMON_SOURCES) main.c
 noinst_HEADERS=bitmap.h macres.h plex.h std.h \
-	font.h lex.h pilrc.h prc.h resource.h util.h makeKbd.h
+	font.h lex.h pilrc.h resource.h util.h makeKbd.h
 
 pilrcui_SOURCES=$(COMMON_SOURCES) xwin.c
 pilrcui_LDADD=$(GTK_LIBS)
diff -urNp orig-pilrc-2.8p5/Makefile.generic pilrc-2.8p5/Makefile.generic
--- orig-pilrc-2.8p5/Makefile.generic	Sun May 27 01:51:43 2001
+++ pilrc-2.8p5/Makefile.generic	Thu Jul  5 02:05:27 2001
@@ -33,7 +33,7 @@ PREFIX   = 
 CC       = gcc
 
 # source / outputs
-OBJS 	= pilrc.o lex.o util.o restype.o bitmap.o main.o font.o plex.o prc.o makeKbd.o
+OBJS 	= pilrc.o lex.o util.o restype.o bitmap.o main.o font.o plex.o makeKbd.o
 EXEC    = pilrc
 
 # compiler flags 
diff -urNp orig-pilrc-2.8p5/Makefile.gui pilrc-2.8p5/Makefile.gui
--- orig-pilrc-2.8p5/Makefile.gui	Sun May 27 02:24:35 2001
+++ pilrc-2.8p5/Makefile.gui	Thu Jul  5 02:05:27 2001
@@ -35,7 +35,7 @@ PREFIX   = 
 CC       = gcc
 
 # source / outputs
-OBJS 	= pilrc.o prc.o lex.o util.o restype.o bitmap.o font.o plex.o makeKbd.o xwin.o
+OBJS 	= pilrc.o lex.o util.o restype.o bitmap.o font.o plex.o makeKbd.o xwin.o
 EXEC    = pilrcui
 
 # compiler flags 
diff -urNp orig-pilrc-2.8p5/Makefile.in pilrc-2.8p5/Makefile.in
--- orig-pilrc-2.8p5/Makefile.in	Sun May 27 02:22:08 2001
+++ pilrc-2.8p5/Makefile.in	Thu Jul  5 02:05:27 2001
@@ -73,10 +73,10 @@ AUTOMAKE_OPTIONS = foreign
 
 bin_PROGRAMS = pilrc $(PILRCUI_PROG)
 
-COMMON_SOURCES = pilrc.c lex.c prc.c util.c restype.c bitmap.c font.c plex.c makeKbd.c
+COMMON_SOURCES = pilrc.c lex.c util.c restype.c bitmap.c font.c plex.c makeKbd.c
 
 pilrc_SOURCES = $(COMMON_SOURCES) main.c
-noinst_HEADERS = bitmap.h macres.h plex.h std.h 	font.h lex.h pilrc.h prc.h resource.h util.h makeKbd.h
+noinst_HEADERS = bitmap.h macres.h plex.h std.h 	font.h lex.h pilrc.h resource.h util.h makeKbd.h
 
 
 pilrcui_SOURCES = $(COMMON_SOURCES) xwin.c
@@ -106,13 +106,13 @@ DEFS = @DEFS@ -I. -I$(srcdir) 
 CPPFLAGS = @CPPFLAGS@
 LDFLAGS = @LDFLAGS@
 LIBS = @LIBS@
-pilrc_OBJECTS =  pilrc.o lex.o prc.o util.o restype.o bitmap.o font.o \
-plex.o makeKbd.o main.o
+pilrc_OBJECTS =  pilrc.o lex.o util.o restype.o bitmap.o font.o plex.o \
+makeKbd.o main.o
 pilrc_LDADD = $(LDADD)
 pilrc_DEPENDENCIES = 
 pilrc_LDFLAGS = 
-pilrcui_OBJECTS =  pilrc.o lex.o prc.o util.o restype.o bitmap.o font.o \
-plex.o makeKbd.o xwin.o
+pilrcui_OBJECTS =  pilrc.o lex.o util.o restype.o bitmap.o font.o plex.o \
+makeKbd.o xwin.o
 pilrcui_DEPENDENCIES = 
 pilrcui_LDFLAGS = 
 CFLAGS = @CFLAGS@
@@ -130,8 +130,8 @@ DISTFILES = $(DIST_COMMON) $(SOURCES) $(
 TAR = gtar
 GZIP_ENV = --best
 DEP_FILES =  .deps/bitmap.P .deps/font.P .deps/lex.P .deps/main.P \
-.deps/makeKbd.P .deps/pilrc.P .deps/plex.P .deps/prc.P .deps/restype.P \
-.deps/util.P .deps/xwin.P
+.deps/makeKbd.P .deps/pilrc.P .deps/plex.P .deps/restype.P .deps/util.P \
+.deps/xwin.P
 SOURCES = $(pilrc_SOURCES) $(pilrcui_SOURCES)
 OBJECTS = $(pilrc_OBJECTS) $(pilrcui_OBJECTS)
 
@@ -280,7 +280,7 @@ distdir: $(DISTFILES)
 	@for file in $(DISTFILES); do \
 	  d=$(srcdir); \
 	  if test -d $$d/$$file; then \
-	    cp -pr $$/$$file $(distdir)/$$file; \
+	    cp -pr $$d/$$file $(distdir)/$$file; \
 	  else \
 	    test -f $(distdir)/$$file \
 	    || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
diff -urNp orig-pilrc-2.8p5/doc/contrib.html pilrc-2.8p5/doc/contrib.html
--- orig-pilrc-2.8p5/doc/contrib.html	Tue May  8 02:43:18 2001
+++ pilrc-2.8p5/doc/contrib.html	Thu Jul  5 02:05:27 2001
@@ -39,7 +39,7 @@ Brandon Long     <a href="mailto:blong@f
 John Ludwig      <a href="mailto:jludwig@birch.northwoods.com">jludwig@birch.northwoods.com</a> 
 Karl Kraft       <a href="mailto:karl@nfox.com">karl@nfox.com</a> 
 Renaud Malaval   <a href="mailto:rmalaval@palm.com">rmalava@palm.com</a>
-John Marshall    <a href="mailto:john_w_marshall@palm.com">john_w_marshall@palm.com</a> 
+John Marshall    <a href="mailto:jmarshall@acm.org">jmarshall@acm.org</a> 
 Mike McCollister <a href="mailto:Mike@MikeMcCollister.com">Mike@MikeMcCollister.com</a> 
 Regis Nicolas    <a href="mailto:regis_nicolas@palm.com">regis_nicolas@palm.com</a> 
 Kurt Spaugh      <a href="mailto:spaugh@bellsouth.net">spaugh@bellsouth.net</a>
diff -urNp orig-pilrc-2.8p5/doc/history.html pilrc-2.8p5/doc/history.html
--- orig-pilrc-2.8p5/doc/history.html	Sun May 27 01:39:28 2001
+++ pilrc-2.8p5/doc/history.html	Thu Jul  5 02:05:27 2001
@@ -18,11 +18,10 @@ PilRC
 <pre>
 What's new in 2.x (coming)
 -----------------
-o added "-R output.ro -prc" option
+o added "-ro" option:
 - resources are bundled into a prc database (instead of .bin) !yay!
-- DEFAULT: cr8r: 'pRES' type: 'data' name: "PilRC resources"
+- DEFAULT: cr8r: 'pRES' type: 'RESO' name: same as filename
   o ability to custom specify the above using -creator -type and -name 
-  o added creationDate/modificationDate chunks
 o recursive includes (32-level deep) now supported
 o added src2unix.sh script, to strip ^M chars for UN*X users
   - fixed to be "older unix" friendly
@@ -37,7 +36,7 @@ o pilrcui compiles once again
   - code assumes BA16 resource types (m68k resources), someone wanna fix?
 o misc documentation fixes
 o slider defaults of (MIN=0, MAX=100, PAGESIZE=10, VALUE=50) implemented
-o redhat 7.1 (new gcc) pre-processor madness fixup
+o pre-processor madness fixup -- no more misuse of ## operator
 
 What's new in 2.8 (Tue, 1 May 2001)
 -----------------
diff -urNp orig-pilrc-2.8p5/doc/manual.html pilrc-2.8p5/doc/manual.html
--- orig-pilrc-2.8p5/doc/manual.html	Sun May 27 01:14:24 2001
+++ pilrc-2.8p5/doc/manual.html	Thu Jul  5 02:05:27 2001
@@ -107,9 +107,10 @@ Check the <a href="http://www.ardiri.com
 </font>
 <ul>
 <pre>
-pilrc   [-L LANGUAGE] [-I INCLUDE PATH] [-D MACRO(=VAL)] [-R RESFILE] 
-        [-q] [-V] [-Fh] [-Fj] [-F5] [-Fg] [-Fkm] [-Fkt] [-allowEditID] [-LE32]
-	file.rcp [output path]
+pilrc   [-L LANGUAGE] [-I INCLUDE PATH] [-D MACRO(=VAL)]
+        [-R RESFILE] [-H INCFILE] [-ro] [-q] [-V]
+        [-Fh] [-Fj] [-F5] [-Fg] [-Fkm] [-Fkt] [-allowEditID] [-LE32]
+        [-o OUTPUTPATH] file.rcp [output path]
 pilrcui [-L LANGUAGE] file.rcp</pre>
 <font face="Arial,Helvetica">
 
@@ -185,6 +186,19 @@ pilrcui [-L LANGUAGE] file.rcp</pre>
 <tr>
   <td valign=top>
   <font face="Arial,Helvetica">
+  -ro
+  </font>
+  </td>
+  <td valign=top>
+  <font face="Arial,Helvetica">
+  Output a single .ro file in PRC format containing all the generated
+  resources, instead of a collection of .bin files.
+  </font>
+  </td>
+</tr>
+<tr>
+  <td valign=top>
+  <font face="Arial,Helvetica">
   -q
   </font>
   </td>
@@ -310,10 +324,19 @@ pilrcui [-L LANGUAGE] file.rcp</pre>
   </td>
   <td valign=top>
   <font face="Arial,Helvetica">
-  Input file describing the resources to be emitted. Each resource is 
-  written as a separate file in the output path directory. The output 
-  filename is constructed by appending the hexcode resource ID to the 
-  four character resource type. <br>
+  Input file describing the resources to be emitted.
+  </font>
+  </td>
+</tr>
+<tr>
+  <td valign=top>
+  <font face="Arial,Helvetica">
+  -o OUTPUTPATH
+  </font>
+  </td>
+  <td valign=top>
+  <font face="Arial,Helvetica">
+  Alternative way of specifying an output path (below).
   </font>
   </td>
 </tr>
@@ -325,11 +348,28 @@ pilrcui [-L LANGUAGE] file.rcp</pre>
   </td>
   <td valign=top>
   <font face="Arial,Helvetica">
-  Directory where .bin files should be generated.
+  Directory where .bin files should be generated, or name of file to generate
+  containing all the generated resources.
   </font>
   </td>
 </tr>
 </table>
+<p>
+The resources generated are output as a collection of .bin files or as
+resources within a PRC file, depending on whether <code>-ro</code> is given:
+  <ul>
+  <li>
+  Without <code>-ro</code>, each resource is written as a separate file in
+  the output path directory.  Each output filename is constructed by appending
+  the hexcode resource ID and ".bin" to the four character resource type.
+  <li>
+  With <code>-ro<code>, the resources are written to a single file, with the
+  name given by the <code>output path</code>.  The extension ".ro" is added
+  if the name has no extension.  If no <code>output path</code> has been
+  given, a filename is constructed from the input filename by replacing ".rcp" 
+  with ".ro" (or appending ".ro" if the input filename does not end in ".rcp").
+  </ul>
+</p>
 </ul>
 
 <b><u>Example:</u></b>
@@ -338,7 +378,9 @@ pilrcui [-L LANGUAGE] file.rcp</pre>
 <pre>
 pilrc myprogram.rcp
 pilrc -I c:\resources -L FRENCH myprogram.rcp
-pilrc -I c:\resources -L BIG5 -F5 -R myprogram.res myprogram.rcp c:\output </pre>
+pilrc -I c:\resources -L BIG5 -F5 -R myprogram.res myprogram.rcp c:\output
+pilrc -ro -o myresoures.ro myprogram.rcp
+pilrc -ro myprogram.rcp</pre>
 </ul>
 <font face="Arial,Helvetica">
 
diff -urNp orig-pilrc-2.8p5/main.c pilrc-2.8p5/main.c
--- orig-pilrc-2.8p5/main.c	Sun May 27 02:20:44 2001
+++ pilrc-2.8p5/main.c	Thu Jul  5 02:05:27 2001
@@ -58,8 +58,9 @@ Usage(void)
      "        -I <path>    Search for bitmap and include files in <path>\n"
      "                     More than one -I <path> options may be given\n"
      "                     The current directory is always searched\n"
-     "        -R <resfile> Generate resource file (JUMP/PilA/prc)\n"
-     "        -prc         the file assigned with -R will be a prc file\n"
+     "        -R <resfile> Generate JUMP/PilA .res file\n"
+     "        -ro          Generate resource database file instead of .bins\n"
+     "        -o <filedir> Equivalent to [outfiledir]\n"
      "        -H <incfile> Autoassign IDs and write .h file with #defines\n"
      "        -D <macro>   Define a pre-processor macro symbol\n"
      "        -F5          Use Big5 Chinese font widths\n"
@@ -73,7 +74,7 @@ Usage(void)
      "        -V           Generate M$ (VS-type) error/warning output\n"
      "        -allowEditID Allow edit menu IDs (10000-10007)\n"
      "        -PalmRez     Generate res with PalmRez option\n"
-     "        -LE32        Generate Little Endian 32 bits compatible resources (ARM, NT)\n");
+     "        -LE32        Generate 32 bit little endian (ARM, NT) resources\n");
 
   exit(1);
 }
@@ -95,12 +96,12 @@ main(int cArg,
   char *szMacro;
   char *szValue;
   char *szIncFile;
-  int i, j;
+  int i;
   int fontType;
   int macroValue;
 
   // display the (c) string
-  printf("PilRC v2.8 patch release 5\n");
+  printf("PilRC v2.8 patch release 6\n");
   printf("  Copyright 1997-1999 Wes Cherry   (wesc@ricochet.net)\n");
   printf("  Copyright 2000-2001 Aaron Ardiri (aaron@ardiri.com)\n");
 
@@ -112,9 +113,11 @@ main(int cArg,
   szIncFile = NULL;
   fontType = fontDefault;
 
-  strcpy(vfPrcName, DEFAULT_PRCNAME);
-  vfPrcCreator = DEFAULT_PRCCR8R;
-  vfPrcType = DEFAULT_PRCTYPE;
+  szOutputPath = ".";
+
+  vfPrcName = NULL;
+  vfPrcCreator = NULL;
+  vfPrcType = NULL;
 
   // process as many command line arguments as possible
   for (i = 1; i < cArg; i++)
@@ -279,19 +282,29 @@ main(int cArg,
     }
 
     // Output a prc File
-    if (FSzEqI(rgszArg[i], "-prc"))
+    if (FSzEqI(rgszArg[i], "-ro"))
     {
       vfPrc = fTrue;
       continue;
     }
 
+    // Output filename
+    if (FSzEqI(rgszArg[i], "-o"))
+    {
+      if (i++ == cArg)
+	Usage();
+      
+      szOutputPath = rgszArg[i];
+      continue;
+    }
+
     // name definition for prc output?
     if (FSzEqI(rgszArg[i], "-name"))
     {
       if (i++ == cArg)
         Usage();
 
-      strcpy(vfPrcName, rgszArg[i]);
+      vfPrcName = rgszArg[i];
       continue;
     }
 
@@ -301,15 +314,7 @@ main(int cArg,
       if (i++ == cArg)
         Usage();
 
-      vfPrcCreator = 0;
-      if (strlen(rgszArg[i]) == 4)
-      {
-        for (j = 0; j < 4; j++)
-        {
-          vfPrcCreator = vfPrcCreator << 8;
-          vfPrcCreator |= (int)rgszArg[i][j];
-        }
-      }
+      vfPrcCreator = rgszArg[i];
       continue;
     }
 
@@ -319,15 +324,7 @@ main(int cArg,
       if (i++ == cArg)
         Usage();
 
-      vfPrcType = 0;
-      if (strlen(rgszArg[i]) == 4)
-      {
-        for (j = 0; j < 4; j++)
-        {
-          vfPrcType = vfPrcType << 8;
-          vfPrcType |= (int)rgszArg[i][j];
-        }
-      }
+      vfPrcType = rgszArg[i];
       continue;
     }
 
@@ -344,8 +341,6 @@ main(int cArg,
   // determine the ouput path
   if (cArg != i)
     szOutputPath = rgszArg[i++];
-  else
-    szOutputPath = ".";
 
   // last minute check? (extra stuff?)
   if (cArg != i)
diff -urNp orig-pilrc-2.8p5/makeKbd.c pilrc-2.8p5/makeKbd.c
--- orig-pilrc-2.8p5/makeKbd.c	Sun May 27 01:51:43 2001
+++ pilrc-2.8p5/makeKbd.c	Thu Jul  5 02:11:27 2001
@@ -35,10 +35,10 @@
 #define NON_INTERNATIONAL
 #include		"CharLatin.h"
 
+#include		"pilrc.h"
 #include		"makeKbd.h"
 #include		"util.h"
 #include		"restype.h"
-#include		"pilrc.h"
 
 char *keyTopLabels[numKeyTopLabels] = {
   "cap",
diff -urNp orig-pilrc-2.8p5/pilrc.c pilrc-2.8p5/pilrc.c
--- orig-pilrc-2.8p5/pilrc.c	Sun May 27 01:51:43 2001
+++ pilrc-2.8p5/pilrc.c	Thu Jul  5 02:05:27 2001
@@ -166,9 +166,9 @@ BOOL vfLE32 = fFalse;
  * LDu Output a Prc File
  */
 BOOL vfPrc = fFalse;
-char vfPrcName[32];
-int vfPrcCreator;
-int vfPrcType;
+const char *vfPrcName;
+const char *vfPrcCreator;
+const char *vfPrcType;
 
 /*
  * Menu globals 
@@ -5761,7 +5761,37 @@ ParseFile(char *szIn,
   prcpfile = calloc(1, sizeof(RCPFILE));
   InitRcpfile(prcpfile, fontType);
 
-  SetOutFileDir(szOutDir);
+  if (vfPrc)
+  {
+    char szFile[FILENAME_MAX];
+
+    if (strcmp(szOutDir, ".") == 0)
+    {
+      /* PRC file format was selected, but no output filename was given.
+         Deduce one from the input filename.  */
+      strcpy(szFile, szIn);
+      if (FSzEqI(szFile + strlen(szFile) - 4, ".rcp"))
+	szFile[strlen(szFile) - 4] = '\0';
+      strcat(szFile, ".ro");
+    }
+    else
+    {
+      /* A filename was given.  Append an extension if it doesn't have one.  */
+      char *dot;
+      strcpy(szFile, szOutDir);
+      dot = strrchr(szFile, '.');
+      if (dot && (strchr(dot, '/') != NULL || strchr(dot, '\\') != NULL))
+	dot = NULL;  /* The dot was in a directory name, not the basename.  */
+
+      if (dot == NULL)
+	strcat(szFile, ".ro");
+    }
+
+    OpenResDBFile(szFile);
+  }
+  else
+    SetOutFileDir(szOutDir);
+
   OpenResFile(szResFile);
   FInitLexer(NULL, fTrue);
 
@@ -5776,5 +5806,7 @@ ParseFile(char *szIn,
   FreeSymTable();
   FreeTranslations();
   CloseResFile();
+  if (vfPrc)
+    CloseResDBFile();
   return prcpfile;
 }
diff -urNp orig-pilrc-2.8p5/pilrc.dsp pilrc-2.8p5/pilrc.dsp
--- orig-pilrc-2.8p5/pilrc.dsp	Sun May 27 01:51:43 2001
+++ pilrc-2.8p5/pilrc.dsp	Thu Jul  5 02:05:27 2001
@@ -125,10 +125,6 @@ SOURCE=.\restype.c
 # End Source File
 # Begin Source File
 
-SOURCE=.\prc.c
-# End Source File
-# Begin Source File
-
 SOURCE=.\util.c
 # End Source File
 # End Group
@@ -154,10 +150,6 @@ SOURCE=.\font.h
 # Begin Source File
 
 SOURCE=.\lex.h
-# End Source File
-# Begin Source File
-
-SOURCE=.\prc.h
 # End Source File
 # Begin Source File
 
diff -urNp orig-pilrc-2.8p5/pilrc.h pilrc-2.8p5/pilrc.h
--- orig-pilrc-2.8p5/pilrc.h	Sun May 27 01:51:43 2001
+++ pilrc-2.8p5/pilrc.h	Thu Jul  5 02:11:41 2001
@@ -38,12 +38,6 @@
 #ifndef _pilrc_h
 #define _pilrc_h                                 // RMA : multiples include protection
 
-#include "std.h"
-#include "util.h"
-#include "lex.h"
-#include "plex.h"
-#include "font.h"
-
 #if (SIZEOF_INT == SIZEOF_CHAR_P)
 typedef int p_int;
 #elif (SIZEOF_LONG == SIZEOF_CHAR_P)
@@ -62,6 +56,12 @@ typedef int p_short;
 #define HOST_BIG_ENDIAN
 #endif
 
+#include "std.h"
+#include "util.h"
+#include "lex.h"
+#include "plex.h"
+#include "font.h"
+
 /*-----------------------------------------------------------------------------
 |	 PILOT STRUCTS
 |
@@ -2160,13 +2160,9 @@ extern BOOL vfLE32;
 
 //LDu : Output a Prc file
 extern BOOL vfPrc;
-extern char vfPrcName[32];
-extern int vfPrcCreator;
-extern int vfPrcType;
-
-#define DEFAULT_PRCNAME "PilRC resources"
-#define DEFAULT_PRCCR8R 0x70524553               // 'pRES'
-#define DEFAULT_PRCTYPE 0x64617461               // 'data'
+extern const char *vfPrcName;
+extern const char *vfPrcCreator;
+extern const char *vfPrcType;
 
 extern char *szLanguage;
 
@@ -2180,6 +2176,7 @@ int CbEmitStruct(void *pv,
                  char *szPic,
                  char **ppchText,
                  BOOL fEmit);
+int CbStruct(char *szPic);
 RCPFILE *ParseFile(char *szIn,
                    char *szOutDir,
                    char *szResFile,
diff -urNp orig-pilrc-2.8p5/pilrc.mak pilrc-2.8p5/pilrc.mak
--- orig-pilrc-2.8p5/pilrc.mak	Sun May 27 01:51:43 2001
+++ pilrc-2.8p5/pilrc.mak	Thu Jul  5 02:05:27 2001
@@ -349,10 +349,6 @@ SOURCE=.\restype.c
 
 "$(INTDIR)\restype.obj" : $(SOURCE) "$(INTDIR)"
 
-SOURCE=.\prc.c
-
-"$(INTDIR)\prc.obj" : $(SOURCE) "$(INTDIR)"
-
 SOURCE=.\win.c
 
 !IF  "$(CFG)" == "pilrc - Win32 Release"
diff -urNp orig-pilrc-2.8p5/util.c pilrc-2.8p5/util.c
--- orig-pilrc-2.8p5/util.c	Sun May 27 01:51:43 2001
+++ pilrc-2.8p5/util.c	Thu Jul  5 02:05:27 2001
@@ -42,24 +42,50 @@
 #include "pilrc.h"
 #include "util.h"
 #include "font.h"
-#include "prc.h"
+
+#ifdef WIN32
+#define DIRSEPARATOR  '\\'
+#else
+#define DIRSEPARATOR  '/'
+#endif
 
 /*
  * Globals 
  */
-FILE *vfhOut;
 FILE *vfhIn;
-FILE *vfhRes;
-int ibOut;                                       /* current output offset (from current .bin we are emitting)     */
 char szInFile[256];
-char szOutFile[256];
-char szOutFileDir[256];
-char szFullName[256];
 char rgbZero[16];
 BOOL vfErr;
 extern int iline;
-extern BOOL vfQuiet;
-extern BOOL vfVSErrors;
+
+/*
+ * Globals for output file handling
+ */
+
+static FILE *vfhOut;	/* file receiving binary resource data */
+static int ibOut;	/* current output offset (within .bin being emitted) */
+static int ibTotalOut;	/* total output offset (after .bins so far emitted) */
+
+static FILE *vfhRes = NULL;  /* file receiving resource file ("-R") output */
+
+static char szOutFileDir[FILENAME_MAX];  /* directory for *.bin files */
+
+static char szOutResDBFile[FILENAME_MAX];  /* filename for final .ro file */
+static char szTempFile[FILENAME_MAX];      /* temporary filename */
+
+static VOID WriteOutResourceDB();
+
+DEFPL(PLEXRESOURCEDIR);
+typedef struct
+{
+  int type[4];
+  int id;
+  int offset;
+}
+RESOURCEDIRENTRY;
+#define szRESOURCEDIRENTRY "b4,w,l"
+
+static PLEXRESOURCEDIR resdir;
 
 /*
  * Includes 
@@ -388,12 +414,70 @@ PadWordBoundary()
 /*-----------------------------------------------------------------------------
 |	SetOutFileDir
 |	
-|		Set output file path -- no trailing \
+|		Set output file path -- with trailing / or \
 -------------------------------------------------------------WESC------------*/
 VOID
 SetOutFileDir(char *sz)
 {
-  strcpy(szOutFileDir, sz);
+  if (sz == NULL || strcmp(sz, ".") == 0)
+    strcpy(szOutFileDir, "");
+  else
+    sprintf(szOutFileDir, "%s%c", sz, DIRSEPARATOR);
+}
+
+
+/*-----------------------------------------------------------------------------
+|	OpenResDBFile
+|	
+|		Set up to write a PRC formatted .ro file
+-------------------------------------------------------------JOHN------------*/
+
+static void
+RemoveTempFile()
+{
+  if (*szTempFile)
+  {
+    remove(szTempFile);
+    strcpy(szTempFile, "");
+  }
+}
+
+VOID
+OpenResDBFile(char *sz)
+{
+  static BOOL registered = fFalse;
+
+  strcpy(szOutResDBFile, sz);
+
+  strcpy(szTempFile, szOutResDBFile);
+  szTempFile[strlen(szTempFile) - 1] = '~';
+
+  remove(szTempFile);
+
+  if (!registered)
+  {
+    atexit(RemoveTempFile);
+    registered = fTrue;
+  }
+
+  PlexInit(&resdir, sizeof(RESOURCEDIRENTRY), 64, 64);
+}
+
+VOID
+CloseResDBFile()
+{
+  if (!vfQuiet)
+    printf("Collecting *.bin files into %s\n", szOutResDBFile);
+
+  Assert(vfhOut == NULL);
+  vfhOut = fopen(szOutResDBFile, "wb");
+  if (vfhOut == NULL)
+    Error3("Unable to open:", szOutResDBFile, strerror(errno));
+
+  WriteOutResourceDB();
+  fclose(vfhOut);
+  RemoveTempFile();
+  PlexFree(&resdir);
 }
 
 /*-----------------------------------------------------------------------------
@@ -411,46 +495,52 @@ OpenOutput(char *szBase,
 #ifdef CW_PLUGIN
   CWOpenOutput(szBase, id);
 #else
+  char szPrettyName[FILENAME_MAX];
+  char *szFileName;
+  char *szMode;
+
   /*
    * #ifdef BINOUT 
    */
   if (vfWinGUI)
     return;
 
-  // LDu : prc output mode
-  if (vfhRes && vfPrc)
-    vfhOut = PrcOpenResFile(szBase, id);
+  Assert(vfhOut == NULL);
+
+  if (vfPrc)
+  {
+    RESOURCEDIRENTRY entry;
+    intstrncpy(entry.type, szBase, 4);
+    entry.id = id;
+    entry.offset = ibTotalOut;
+    PlexAddElement(&resdir, &entry);
+
+    sprintf(szPrettyName, "temporary %s%04x.bin", szBase, id);
+    szFileName = szTempFile;
+    szMode = "ab";
+  }
   else
   {
-    // LDu : end modification
-
-    Assert(vfhOut == NULL);
-#if WIN32
-    sprintf(szOutFile, "%s\\%s%04x.bin", szOutFileDir, szBase, id);
-#else
-    sprintf(szOutFile, "%s/%s%04x.bin", szOutFileDir, szBase, id);
-#endif
-    vfhOut = fopen(szOutFile, "wb");
-    if (vfhOut == NULL)
-      Error3("Unable to open:", szOutFile, strerror(errno));
-    // LDu : prc output mode
+    sprintf(szPrettyName, "%s%s%04x.bin", szOutFileDir, szBase, id);
+    szFileName = szPrettyName;
+    szMode = "wb";
   }
-  // LDu : end modification
+
+  vfhOut = fopen(szFileName, szMode);
+  if (vfhOut == NULL)
+    Error3("Unable to open:", szFileName, strerror(errno));
 
   if (!vfQuiet)
-    printf("Writing %s\n", szOutFile);
+    printf("Writing %s\n", szPrettyName);
   ibOut = 0;
 
   /*
    * #endif 
    */
 
-  // LDu : prc output mode
-  // LDu : removed// if (vfhRes != NULL)
-  if (vfhRes && !vfPrc)
-    // LDu : end modification
+  if (vfhRes)
   {
-    fprintf(vfhRes, "\tres '%s', %d, \"%s\"\n", szBase, id, szOutFile);
+    fprintf(vfhRes, "\tres '%s', %d, \"%s\"\n", szBase, id, szPrettyName);
   }
 #endif
 }
@@ -476,15 +566,10 @@ CloseOutput()
 
   if (!vfQuiet)
     printf("%d bytes\n", ibOut);
+  ibTotalOut += ibOut;
   if (vfhOut != NULL)
   {
-    // LDu : prc output mode
-    if (vfhRes && vfPrc)
-      PrcCloseResFile(vfhOut, ibOut);
-    else
-      // LDu : end modification
-
-      fclose(vfhOut);
+    fclose(vfhOut);
     vfhOut = NULL;
   }
 
@@ -514,13 +599,7 @@ OpenResFile(char *sz)
   if (sz == NULL)
     return;
 
-  // LDu : prc output mode
-  if (vfPrc)
-    vfhRes = PrcOpenFile(sz);
-  else
-    // LDu : end modification
-    vfhRes = fopen(sz, "wt");
-
+  vfhRes = fopen(sz, "wt");
   if (vfhRes == NULL)
     Error3("Unable to open:", sz, strerror(errno));
   if (!vfQuiet)
@@ -535,13 +614,7 @@ CloseResFile()
 
   if (vfhRes != NULL)
   {
-    // LDu : prc output mode
-    if (vfPrc)
-      PrcCloseFile(vfhRes);
-    else
-      // LDu : end modification
-      fclose(vfhRes);
-
+    fclose(vfhRes);
     vfhRes = NULL;
   }
 }
@@ -558,16 +631,13 @@ FindAndOpenFile(char *szIn,
 
   if (file == NULL)
   {
+    static char szFullName[FILENAME_MAX];
 
     int i;
 
     for (i = 0; i < totalIncludePaths; i++)
     {
-#ifdef WIN32
-      sprintf(szFullName, "%s\\%s", includePaths[i], szIn);
-#else
-      sprintf(szFullName, "%s/%s", includePaths[i], szIn);
-#endif
+      sprintf(szFullName, "%s%c%s", includePaths[i], DIRSEPARATOR, szIn);
 
       file = fopen(szFullName, mode);
       if (file != NULL)
@@ -620,4 +690,123 @@ WMax(int w1,
      int w2)
 {
   return w1 > w2 ? w1 : w2;
+}
+
+
+/*-----------------------------------------------------------------------------
+|	intstrncpy
+-------------------------------------------------------------JOHN------------*/
+VOID
+intstrncpy(p_int *dst,
+	   const char *src,
+	   int n)
+{
+  while (n > 0)
+  {
+    n--;
+    if ((*dst++ = *src++) == 0)
+      break;
+  }
+  
+  while (n > 0)
+  {
+    n--;
+    *dst++ = 0;
+  }
+}
+
+
+/*-----------------------------------------------------------------------------
+|	WriteOutResourceDB
+-------------------------------------------------------------JOHN------------*/
+
+typedef struct
+{
+  p_int name[32];	/* b32 */
+  p_int attr;		/* w */
+  p_int version;	/* w */
+  p_int created;	/* l */
+  p_int modified;	/* l */
+  //p_int backup;	/* zl */
+  //p_int modnum;	/* zl */
+  //p_int appinfo;	/* zl */
+  //p_int sortinfo;	/* zl */
+  p_int type[4];	/* b4 */
+  p_int creator[4];	/* b4 */
+  //p_int uidseed;	/* zl */
+  //p_int nextlist;	/* zl */
+  p_int nrecords;	/* w */
+}
+DBHEADER;
+#define szDBHEADER "b32,w,w,l,l,zl4,b4,b4,zl2,w"
+
+static VOID
+WriteOutResourceDB()
+{
+  DBHEADER head;
+  char buf[4096];
+  int head_offset;
+  FILE *f;
+  int i;
+  size_t n;
+  BOOL saveLE32;
+
+  /* Even resources with LE32 contents go into a M68K-style PRC file.  */
+  saveLE32 = vfLE32;
+  vfLE32 = fFalse;
+
+  /* It was intended not to provide facilities to set the name, attributes,
+     type, creator, timestamps, etc because that's not really PilRC's job.
+     We're not generating fully flexible .prc files, we're really just using
+     the PRC format as an archive format.  But in the end we've provided
+     command line options for name, type, and creator.  Apparently it is
+     too difficult to use this:  :-)
+
+	pilrc -prc foo.rcp
+	build-prc -n NAME -t TYPE -c CRID foo.ro
+
+     The random number for the timestamps corresponds to 1996-05-16
+     11:14:40, which is the same fixed number emitted by build-prc from
+     prc-tools 0.5.0.  A little bit of history lives on.  :-)
+
+     We output a constant time because it doesn't seem to be worth getting
+     this right for a temporary file which won't be distributed, because
+     it's non-trivial to output the correct time in a portable way, and
+     especially because variable timestamps embedded in object files are
+     the spawn of the devil: they make it harder to determine when anything
+     has really changed -- cmp always detects differences after a rebuild.
+     This is an issue in certain debugging scenarios that you never want
+     to encounter.  */
+
+  intstrncpy(head.name, (vfPrcName)? vfPrcName : szOutResDBFile, 32);
+  head.attr = 1;  /* dmHdrAttrResDB */
+  head.version = 1;
+  head.created = head.modified = 0xadc0bea0;
+  intstrncpy(head.type, (vfPrcType)? vfPrcType : "RESO", 4);
+  intstrncpy(head.creator, (vfPrcCreator)? vfPrcCreator : "pRES", 4);
+  head.nrecords = PlexGetCount(&resdir);
+
+  head_offset = CbEmitStruct(&head, szDBHEADER, NULL, fTrue);
+  head_offset += head.nrecords * CbStruct(szRESOURCEDIRENTRY);
+  head_offset += 2;  /* Allow for that daft gap */
+
+  for (i = 0; i < head.nrecords; i++)
+  {
+    RESOURCEDIRENTRY *e = PlexGetElementAt(&resdir, i);
+    e->offset += head_offset;
+    CbEmitStruct(e, szRESOURCEDIRENTRY, NULL, fTrue);
+  }
+
+  CbEmitStruct(NULL, "zb2", NULL, fTrue);  /* The dreaded gap */
+
+  f = fopen(szTempFile, "rb");
+  if (f == NULL)
+    Error3("Unable to open:", szTempFile, strerror(errno));
+
+  while ((n = fread(buf, 1, sizeof buf, f)) > 0)
+    DumpBytes(buf, n);
+
+  fclose(f);
+
+  vfLE32 = saveLE32;
 }
diff -urNp orig-pilrc-2.8p5/util.h pilrc-2.8p5/util.h
--- orig-pilrc-2.8p5/util.h	Sun May 27 01:51:43 2001
+++ pilrc-2.8p5/util.h	Thu Jul  5 02:05:27 2001
@@ -62,6 +62,10 @@ void EmitB(unsigned char b);
 void EmitW(unsigned short w);
 void EmitL(unsigned long l);
 
+VOID intstrncpy(p_int *dst,
+		const char *src,
+		int n);
+
 /*
  * void SwapBytes(void *pv, int cb); 
  */
@@ -78,6 +82,11 @@ VOID OpenOutput(char *szBase,
 VOID CloseOutput();
 FILE *getOpenedOutputFile();
 
+VOID SetOutFileDir(char *sz);
+
+VOID OpenResDBFile(char *szFile);
+VOID CloseResDBFile();
+
 VOID OpenResFile(char *szFile);
 VOID CloseResFile();
 void DumpBytes(void *pv,
@@ -85,7 +94,6 @@ void DumpBytes(void *pv,
 void PadBoundary();
 void PadWordBoundary();
 int IbOut();
-VOID SetOutFileDir(char *sz);
 char *FindAndOpenFile(char *szIn,
                       char *mode,
                       FILE ** returnFile);
_______________________________________________
prc-tools-devel mailing list
prc-tools-devel@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/prc-tools-devel
