home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff374.lzh / PopArt / sorcery / file.c < prev    next >
C/C++ Source or Header  |  1990-10-08  |  17KB  |  628 lines

  1.  
  2.  
  3.               /*  file  ---  pray to the AmigaDOS god ...  */
  4.  
  5.  
  6. #include <libraries/dosextens.h>
  7. #include <exec/memory.h>
  8. #include <ctype.h>
  9. #include <stdio.h>
  10. #include "stuff.h"
  11.  
  12. #define BUFFERSIZE  256L
  13.  
  14.  
  15.                extern  struct    Window      * win        ;
  16.                extern  struct    Screen      * scr        ;
  17.                extern   char               *getFileName ();
  18.                extern  ULONG                   msgclass   ;
  19.                extern  USHORT                  msgcode    ;
  20.                extern    int                   gadcode    ;
  21.                extern  struct   RastPort     * reep       ;
  22.                extern    int                   no_help    ;
  23.                extern  UBYTE                   flubb []   ;
  24.                extern  struct   Gadget         namegad    ,
  25.                                                TGads []   ;
  26.                        char       def_name [50] = "NuIm.h";
  27.                        char       def_dir  [50] = "Ram:"  ;
  28.                        void       messWith ();
  29.  
  30.     SS FileHandle *out;
  31.     static UBYTE *flubber;
  32.  
  33.     static struct IntuiText
  34.         sweat = { 2, 1,JAM2, 7, 5, NL, (UB) "OverWrite Existing File:", NL },
  35.         filen = { 2, 1,JAM2, 7,15, NL,  NL, &sweat },
  36.         killr = {22,28,JAM2, 6, 3, NL, (UB) "DO IT!", NL },
  37.         pansy = {28,22,JAM2, 6, 3, NL, (UB) "be nice", NL };
  38.  
  39.  
  40.        void
  41. cloze ()                /*  note this works like a microcosm of init ()...  */
  42. {
  43.  
  44.     if (out)  Close (out);
  45.     if (flubber)  FreeMem (flubber, BUFFERSIZE);
  46.     onInput ();
  47.  
  48. }
  49.  
  50.  
  51.                 /*===================SAVE===================*/
  52.        void
  53. save ()
  54. {
  55.     USHORT get_spot (), nz, glob;
  56.     unsigned int over, down, in;
  57.     UBYTE marioBros [128], destroy [99];
  58.     int Zpop ();
  59.  
  60. #define ZPOP  {ifn(Zpop())return;}
  61. #define TPOP(x) {strcpy(flubber, x "\n");ZPOP;}
  62.  
  63.  
  64.     out = NL;
  65.     flubber = NL;
  66.     Mess ("FileName:  <Return> == \"Okay\"");
  67.     offInput ();
  68.  
  69.     ifn (getFileName (win, scr, "Enter FileName to Get It", def_name, def_dir))
  70.         {  onInput ();  RET;  }
  71.  
  72.     messWith (marioBros);
  73.     out = Open (marioBros, MODE_OLDFILE);
  74.  
  75.     if (out)  {
  76.         strcpy (destroy, marioBros);
  77.         strcat (destroy, "?");
  78.         filen.IText = destroy;
  79.         Mess ("L-Amiga-V == DOIT!; -B == be nice");
  80.  
  81.         ifn (AutoRequest (win, &filen, &killr, &pansy, 0L, 0L, 280L, 60L))  {
  82.             cloze ();  Mess ("Save Cancelled - Wrong FileName");  RET;  }
  83.  
  84.         Mess ("Bet you didn't know that!");
  85.  
  86.     }                                     /*  kill hurt destroy stoned ...  */
  87.  
  88.     cloze ();
  89.     glob = gadzon (TGads [8])? 1: 2;
  90.     out = Open (marioBros, MODE_NEWFILE);
  91.     IFN (out)  { ALERT ("Can't Open File!!!"); cloze ();  RET; }
  92.     Mess ("Saving your Image");
  93.     flubber = AllocMem (BUFFERSIZE, MEMF_CHIP | MEMF_PUBLIC);
  94.     IFN (flubber)  {  cloze ();  ALERT ("NO CHIP MEMORY!!!");  RET;  }
  95.  
  96.     if gadzon (TGads [18])  {
  97. TPOP ("\n\n/*  You can edit an \"ni_\" declaration, put it in a bigger file,");
  98. TPOP (    "    change its size, and still load it into PopArt for further");
  99. TPOP (   "    blandishment.  The PopArt loader is simple but effective.");
  100. TPOP (  "    It scans thru a file, looking for \"HITE\", \"WIDT\", and");
  101. TPOP ( "    \" ni_dataName[]=\".  Every time it finds \"HITE\" or \"WIDT\",");
  102. TPOP ("    it scans to '(' and extracts the following number.  It knows");
  103. TPOP ("    the difference between a decimal and a hex, and they both must");
  104. TPOP ("    end with ','.  It ignores any line containing \"plane\", and it");
  105. TPOP ("    stops reading \" ni_dataName[]=\" when it encounters \"end of\".");
  106. TPOP ("    It can't deal with data edited to use PlanePick and PlaneOnOff  */");  }
  107.  
  108.     strcpy (flubber, "\n\n#define ");
  109.     strcat (flubber, flubb);
  110.     strcat (flubber, "SIZE ((long)sizeof(");
  111.     strcat (flubber, flubb);
  112.     strcat (flubber, "))\n");
  113.     ZPOP;
  114.     strcpy (flubber, "#define ");
  115.     strcat (flubber, flubb);
  116.     strcat (flubber, "WIDT (");
  117.     ZPOP;
  118.     decimal (32 * glob, flubber);
  119.     strcat (flubber, ")\n");
  120.     ZPOP;
  121.  
  122.     strcpy (flubber, "#define ");
  123.     strcat (flubber, flubb);
  124.     strcat (flubber, "HITE (");
  125.     ZPOP;
  126.     decimal (20 * glob, flubber);
  127.     strcat (flubber, ")\n");
  128.     ZPOP;
  129.     strcpy (flubber, "#define DO");
  130.     strcat (flubber, flubb);
  131.     strcat (flubber, "(x,y) popChip(");
  132.     strcat (flubber, flubb);
  133.     strcat (flubber, ",");
  134.     strcat (flubber, flubb);
  135.     strcat (flubber, "SIZE,");
  136.     strcat (flubber, flubb);
  137.     strcat (flubber, "WIDT,");
  138.     strcat (flubber, flubb);
  139.     strcat (flubber, "HITE,x,y)\n");
  140.     ZPOP;
  141.  
  142.     strcpy (flubber, "\nUSHORT *");
  143.     strcat (flubber, flubb);
  144.     strcat (flubber, "Chip;\n");
  145.     ZPOP;
  146.     strcpy (flubber, "\nUSHORT ");
  147.     strcat (flubber, flubb);
  148.     strcat (flubber, "[]={");
  149.     ZPOP;
  150.  
  151.     LOOP (in = 0;  in < 5 ;  ++in)  {
  152.         strcpy (flubber, "/* plane ");
  153.         ZPOP;
  154.         strcat (decimal (in + 1, flubber), ", ");
  155.         strcat (flubber, flubb);
  156.         strcat (flubber, " */\n\t");
  157.         ZPOP;
  158.  
  159.         loop (nz=8, down=PixDown;  down<PixDown+MaxDN*glob/2; ++down)  {
  160.             loop (over=PixOver; over<=PixOver+ImageWid*glob/2-1; over+=16)  {
  161.                 char number [16];
  162.  
  163.                 if gadzon (TGads[23])  
  164.                     decimal (get_spot(reep,over,down,in),flubber); 
  165.                 else  {
  166.                     strcpy(flubber, "0x0000");
  167.                     hex(get_spot(reep,over,down,in),number);
  168.                     strcpy (&flubber [6 - strlen (number)], number);  }
  169.                     
  170.                 strcat (flubber, ",");
  171.                 ZPOP;
  172.                 ifn(--nz) { strcpy(flubber,"\n\t"); nz=8;             ZPOP; }
  173.             }
  174.         }/*handsupeveryonewhothinksthisispreferabletohavingyourfingersdropofffromtypingtomuch*/
  175.  
  176.     }                                           /*  looping thru the spots  */
  177.  
  178.     strcpy (flubber, "}; /* end of ");
  179.     strcat (flubber, flubb);
  180.     strcat (flubber, " image data */\n");
  181.     ZPOP;
  182.     cloze ();
  183.     Mess ("Image Saved");
  184.  
  185. }                                                          /*  end of save  */
  186.  
  187.  
  188. Zpop ()
  189. {
  190.     REG long Qscreen;
  191.  
  192.     Qscreen = Write (out, flubber, (long) strlen (flubber));
  193.  
  194.     if (Qscreen != (long) strlen (flubber))  {
  195.         cloze ();  ALERT ("Error Writing File!!!");  return 0;  }
  196.  
  197.     return 1;
  198.  
  199. }
  200.  
  201.  
  202.                                 USHORT
  203. get_spot (reep, xx, yy, plane)  ST RastPort *reep;
  204. {
  205.     REG unsigned int it;
  206.     REG USHORT thing = 0;
  207.     REG UBYTE pixC;
  208.  
  209.  
  210.     loop (it = 0; it < 16; ++it)  {
  211.         pixC = (UBYTE) ReadPixel (reep, (long) xx + it, (long) yy);
  212.         if ( (pixC >> plane) & 0x01 )  thing |= 0x8000 >> it;
  213.     }                      /*  end of scan-thru-one-USHORT's-worth-of-dots  */
  214.  
  215.     return thing;
  216.  
  217. }                 /*  is it safe to look this up thru the rastport bitmap?  */
  218.  
  219.  
  220.                 /*===================LOAD===================*/
  221.  
  222. #define ALLOCSIZE  ((MaxUP * MaxDN * 5L + 10L) * sizeof (USHORT))
  223. #define ARRAYSIZE  ((MaxUP * MaxDN * 5L))
  224.  
  225.     FILE *in;
  226.     USHORT *imagine;
  227.  
  228.        void
  229. load ()
  230. {
  231.     char line [128], *liner, example [75], realname [56];
  232.     UBYTE marioBros [128];
  233.     int hite = 40, widt = 4*16, foundit = 0;
  234.     USHORT *stuff = NL;
  235.  
  236.  
  237.     in = NL;
  238.     imagine = NL;
  239.     Mess ("Change Data Name to Name to Read!");
  240.     offInput ();
  241.  
  242.     ifn (getFileName (win, scr, "Enter FileName to Grab", def_name, def_dir))
  243.         {  Rcloze ();  RET;  }
  244.  
  245.     ifn (stuff = imagine = (USHORT*) AllocMem (ALLOCSIZE, MEMF_CLEAR))  {
  246.         ALERT ("Low Memory!  Help!");  Rcloze ();  RET;  }
  247.  
  248.     messWith (marioBros);
  249.  
  250.     ifn (in = fopen (marioBros, "r"))  {
  251.         ALERT ("Error Opening File!!");  Rcloze ();  RET;  }
  252.  
  253.     strcpy (example, "Loading ");
  254.     strcat (example, marioBros);
  255.     strcat (example, "->");
  256.     strcat (example, flubb);
  257.     Mess (example);
  258.     strcpy (example, "DO");
  259.     strcat (example, flubb);
  260.     strcpy (realname, " ");
  261.     strcat (realname, flubb);
  262.     strcat (realname, "[]=");
  263.  
  264.     while (fgets (line, 127, in))  {
  265.         if (findstr (line, "HITE") && !findstr (line, example))
  266.             hite = findnum (line, hite);
  267.  
  268.         if (findstr (line, "WIDT") && !findstr (line, example))  {
  269.             widt = findnum (line, widt);
  270.             if (findstr (line, "2*16"))  widt = 2*16;
  271.             if (findstr (line, "4*16"))  widt = 4*16;  }
  272.  
  273.         if (findstr (line, realname))  {
  274.             foundit = 1;
  275.  
  276.             while (fgets (line, 127, in))  {
  277.                 if (findstr (line, "plane"))  continue;
  278.                 if (findstr (line, "end of"))  break;
  279.                 liner = line;
  280. nxnum:          while (!isdigit (*liner) && *liner)  ++liner;
  281.                 ifn (*liner)  continue;
  282.  
  283.                 if (liner [0] == '0' && tolower (liner [1]) == 'x')
  284.                       *stuff =  hexToUshort (&liner [2]);
  285.                 else  *stuff = (USHORT) atoi (liner);
  286.  
  287.                 while (*liner != ',' && *liner)  ++liner;
  288.                 if (++stuff >= &imagine [ARRAYSIZE])  break;
  289.                 ifn (*liner)  continue;
  290.                 goto nxnum;  }
  291.                 
  292.             break;  }
  293.  
  294.     }                     /*  the less said about such nonsense the better  */
  295.  
  296.     ifn (foundit)  {  Mess ("Data Item Not Found!!!");  Rcloze ();  RET;  }
  297.     SetAPen (reep, 0L);
  298.  
  299.     RectFill (reep, (long) PixOver,            (long) PixDown,
  300.                     (long) PixOver + ImageWid, (long) PixDown + MaxDN);
  301.  
  302.     popChip (imagine, ALLOCSIZE, widt, hite, PixOver, PixDown);
  303.     suck ();
  304.     redrawWorkArea (0);
  305.     Rcloze ();
  306.  
  307. }                                                          /*  end of load  */
  308.  
  309.  
  310.               void
  311. saveColors ()
  312. {
  313.     USHORT nz = 12;
  314.     unsigned int in;
  315.     UBYTE marioBros [128], destroy [99];
  316.     int Zpop ();
  317.  
  318. #define ZPOP  {ifn(Zpop())return;}
  319.  
  320.  
  321.     out = NL;
  322.     flubber = NL;
  323.     Mess ("FileName:  <Return> == \"Okay\"");
  324.     offInput ();
  325.  
  326.     ifn (getFileName (win, scr, "Enter FileName to Get Colors", def_name, def_dir))
  327.         {  onInput ();  RET;  }
  328.  
  329.     messWith (marioBros);
  330.     out = Open (marioBros, MODE_OLDFILE);
  331.  
  332.     if (out)  {
  333.         strcpy (destroy, marioBros);
  334.         strcat (destroy, "?");
  335.         filen.IText = destroy;
  336.         Mess ("L-Amiga-V == DOIT!; -B == be nice");
  337.  
  338.         ifn (AutoRequest (win, &filen, &killr, &pansy, 0L, 0L, 280L, 60L))  {
  339.             cloze ();  Mess ("Save Cancelled - Wrong FileName");  RET;  }
  340.  
  341.         Mess ("Bet you didn't know that!");
  342.  
  343.     }
  344.  
  345.     cloze ();
  346.     out = Open (marioBros, MODE_NEWFILE);
  347.     IFN (out)  { ALERT ("Can't Open File!!!"); cloze ();  RET; }
  348.     Mess ("Saving your Colors");
  349.     flubber = AllocMem (BUFFERSIZE, MEMF_CHIP | MEMF_PUBLIC);
  350.     IFN (flubber)  {  cloze ();  ALERT ("NO CHIP MEMORY!!!");  RET;  }
  351.  
  352.     strcpy (flubber, "\n#define ");
  353.     strcat (flubber, flubb);
  354.     strcat (flubber, "COLORS(vp) LoadRGB4((vp),");
  355.     strcat (flubber, flubb);
  356.     strcat (flubber, "CMAP,32L)\n");
  357.     ZPOP;
  358.     strcpy (flubber, "\nUWORD ");
  359.     strcat (flubber, flubb);
  360.     strcat (flubber, "CMAP[]={\n\t");
  361.     ZPOP;
  362.  
  363.     LOOP (in = 0;  in < 32 ;  ++in)  {  char number [16];
  364.         strcpy (flubber, "0x000");
  365.         hex ((int) GetRGB4 (scr->ViewPort.ColorMap, (long) in), number);
  366.         strcpy (&flubber [5 - strlen (number)], number);
  367.         strcat (flubber, ",");
  368.         ZPOP;
  369.         ifn (--nz)  {  strcpy (flubber, "\n\t");  nz = 12;            ZPOP; }
  370.  
  371.     }
  372.  
  373.     strcpy (flubber, "\n}; /* end of ");
  374.     strcat (flubber, flubb);
  375.     strcat (flubber, "CMAP */\n");
  376.     ZPOP;
  377.     cloze ();
  378.     Mess ("Colors Saved");
  379.  
  380. }                                                    /*  end of saveColors  */
  381.  
  382.  
  383.               void
  384. loadColors ()
  385. {
  386.     char line [128], *liner, example [75], realname [56];
  387.     UBYTE marioBros [128];
  388.     int stuff, hite = 40, widt = 4*16, foundit = 0;
  389.     UWORD cmap [32];
  390.     long pragma = 0;
  391.  
  392.  
  393.     in = NL;
  394.     imagine = NL;
  395.     Mess ("Change Data Name to Name to Read!");
  396.     offInput ();
  397.     stuff = 0;
  398.  
  399.     ifn (getFileName (win, scr, "Enter FileName to Load Colors", def_name, def_dir))
  400.         {  Rcloze ();  RET;  }
  401.  
  402.     messWith (marioBros);
  403.  
  404.     ifn (in = fopen (marioBros, "r"))
  405.         {  ALERT ("Error Opening File!!");  Rcloze ();  RET;  }
  406.  
  407.     strcpy (example, "Loading ");
  408.     strcat (example, marioBros);
  409.     strcat (example, "->");
  410.     strcat (example, flubb);
  411.     strcat (example, "CMAP");
  412.     Mess (example);
  413.     strcpy (example, "DO");
  414.     strcat (example, flubb);
  415.     strcpy (realname, " ");
  416.     strcat (realname, flubb);
  417.     strcat (realname, "CMAP[]=");
  418.  
  419.     while (fgets (line, 127, in))  {
  420.         if (findstr (line, realname))  {
  421.             foundit = 1;
  422.  
  423.             while (fgets (line, 127, in))  {
  424.                 liner = line;
  425. nxnum:          while (!isdigit (*liner) && *liner)  ++liner;
  426.                 ifn (*liner)  continue;
  427.  
  428.                 if (liner [0] == '0' && tolower (liner [1]) == 'x')
  429.                       cmap [stuff] =  hexToUshort (&liner [2]);
  430.                 else  cmap [stuff] = (USHORT) atoi (liner);
  431.  
  432.                 while (*liner != ',' && *liner)  ++liner;
  433.                 if (++stuff >= 32)  break;
  434.                 ifn (*liner)  continue;
  435.                 goto nxnum;  }
  436.  
  437.             break;  }
  438.             
  439.     }                                       /*  end of while-therez- lines  */
  440.  
  441.     ifn (foundit)  {  Mess ("Data Item Not Found!!!");  Rcloze ();  RET;  }
  442.     SetAPen (reep, 0L);
  443.     Rcloze ();
  444.     if (++stuff > 32)  stuff = 32;
  445.     LoadRGB4 (&(scr->ViewPort), cmap, (long) stuff);
  446.     Mess ("New Colors Loaded");
  447.  
  448. }                                                    /*  end of loadColors  */
  449.  
  450.  
  451.  
  452.          void
  453. Rcloze ()
  454. {
  455.  
  456.     if (in)  fclose (in);
  457.     if (imagine)  FreeMem (imagine, ALLOCSIZE);
  458.     onInput ();
  459.     in = 0;
  460.     imagine = 0;
  461.  
  462. }
  463.  
  464.  
  465.             void
  466. increment ()
  467. {
  468.  
  469.     inky (flubb, 9);
  470.     RefreshGadgets (&namegad, win, (long) NL);
  471.     inky (def_name, 31);
  472.  
  473. }
  474.  
  475.  
  476. inky (dunk, size)       char *dunk;
  477. {
  478.     int numat, numiz;
  479.     char cat [128], *boid, dink [128];
  480.  
  481.  
  482.     strcpy (dink, dunk);
  483.     if ((numat = hazNum (dink)) == -1)  numat = izIn (dink, '.');
  484.     numiz = atoi (&dink [numat]);
  485.     strcpy (cat, &dink [numat]);
  486.     boid = cat;
  487.     while (*boid && boid < &cat [127] && isdigit (*boid))  ++boid;
  488.     dink [numat] = 0;
  489.     decimal (++numiz, &dink [numat]);
  490.     strcat (dink, boid);
  491.     strncpy (dunk, dink, size);
  492.  
  493. }                    /*  parsers have got to be the ugliest code around...  */
  494.  
  495.  
  496.                 char *
  497. decimal (it, s)  unsigned int it;  char *s;
  498. {
  499.     REG int v;
  500.     REG char *p;
  501.     long pwr10 ();
  502.  
  503.  
  504.     loop (v = 7;  v > -1;  --v)  s [7 - v] = '0' + ( it/pwr10(v) ) % 10;
  505.     s [8] = 0;
  506.     p = s;
  507.     while (p [0] == '0' && p [1] != 0)  ++p;
  508.     if (p != s)  strcpy (s, p);
  509.     return s;
  510.  
  511. }
  512.  
  513.  
  514.           long
  515. pwr10 (x)
  516. {
  517.     REG long r = 10;
  518.  
  519.  
  520.     if (x == 0)  return 1;
  521.     while (--x)  r *= 10;
  522.     return r;
  523.  
  524. }
  525.  
  526.  
  527.     static char table [] = "0123456789abcdef";
  528.  
  529.              char *
  530. hex (it, s)  char *s;
  531. {
  532.     REG int v;
  533.     REG char *p;
  534.  
  535.  
  536.     loop (v = 3;  v > -1;  --v)  s [3 - v] = table [(it >> (v * 4)) & 0xf];
  537.     s [4] = 0;
  538.     p = s;
  539.     while (p [0] == '0' && p [1] != 0)  ++p;
  540.     if (p != s)  strcpy (s, p);
  541.     return s;
  542.  
  543. }
  544.  
  545.           
  546.                 USHORT
  547. hexToUshort (s)  char *s;
  548. {
  549.     REG USHORT g = 0;
  550.     REG int v = 0, k, q;
  551.  
  552.  
  553.     while (!isxdigit (*s) && *s)  ++s;
  554.     ifn (*s)  return 0;
  555.     while ( isxdigit (s [  v]))  ++v;
  556.     if    (!isxdigit (s [--v]))  return 0;
  557.  
  558.     loop (q = v;  v >= 0;  --v)  {  
  559.         k = 0;
  560.         while (table [k] != tolower (s [v]) && k < 16)  ++k;
  561.         g |= k << ((q - v) * 4);  }
  562.  
  563.     return g;
  564.  
  565. }
  566.  
  567.  
  568.                     void
  569. messWith (marioBros)  char *marioBros;
  570. {
  571.  
  572.     marioBros [0] = 0;                             /*  no '\000' bullshit!  */
  573.  
  574.     if (izIn (def_name, ':') >= strlen (def_name))  {
  575.         strcpy (marioBros, def_dir);
  576.  
  577.         if (def_dir [strlen (def_dir)-1] != ':' &&
  578.             def_dir [strlen (def_dir)-1] != '/')  strcat (marioBros, "/");  }
  579.  
  580.     strcat (marioBros, def_name);
  581.  
  582. }      /*  allow part of path and filename in dir and name, or all in name  */
  583.  
  584.  
  585. hazNum (s)  char *s;
  586. {
  587.     REG int reggae = 0;
  588.  
  589.  
  590.     while (*s)  {  if (isdigit (*s))  return reggae; ++reggae,  ++s; }
  591.     return -1;
  592.  
  593. }                                                /*  I love C ... I need C  */
  594.  
  595.  
  596. izIn (s, d)  char *s, d;
  597. {
  598.     REG int reggae = 0;
  599.  
  600.  
  601.     while (*s)  { if (*s == d)  break;   ++s, ++reggae;  }
  602.     return reggae;
  603.  
  604. }                                 /*  return location of d, or length of s  */
  605.  
  606.  
  607. findstr (a, b)  char *a, *b;
  608. {
  609.  
  610.     while (*a && *a != *b)  ++a;
  611.     ifn (*a)  return 0;
  612.     while (*a == *b && *a && *b)  ++a, ++b;
  613.     return !*b;
  614.  
  615. }                                    /*  return a boolean for if b is in a  */
  616.  
  617.  
  618. findnum (a, defalt)  char *a;
  619. {
  620.  
  621.     while (*a  !=  '('   && *a)  ++a;
  622.     while (!isdigit (*a) && *a)  ++a;
  623.     ifn (*a)  return defalt;
  624.     return atoi (a);
  625.  
  626. }
  627.  
  628.