home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / compress / boo / !Boo / c / BOO
Encoding:
Text File  |  1990-11-13  |  9.6 KB  |  389 lines

  1. /* >c.Boo
  2.  *
  3.  * Desktop Application to de-boo ibmpc files.
  4.  *
  5.  * (c) John Gordon (3 November 1990)
  6.  * 
  7.  * Notice: This software is public domain. It may be copied and distributed
  8.  *         freely provided it is kept as a complete package. No profit is to
  9.  *         be made from the distribution of this software. No guarantees are
  10.  *         given with this software, and no responsibility is taken
  11.  *         for any damage caused by the author or any other person - keep
  12.  *         backups of your boo format files!
  13.  *
  14.  *         It may be possible to contact me at ukc as jg2@uk.ac.ukc (or
  15.  *         more reliably J.Gordon@uk.ac.ukc since the login changes every
  16.  *         so often...). If not post to the net and if I see it I'll reply!
  17.  */
  18.  
  19. /***********************   INCLUDES   **********************/
  20.  
  21. /* Standard C libraries */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26.  
  27. /* RISC_OSlib functions */
  28.  
  29. #include "wimp.h"
  30. #include "wimpt.h"
  31. #include "win.h"
  32. #include "baricon.h"
  33. #include "event.h"
  34. #include "werr.h"
  35. #include "res.h"
  36. #include "resspr.h"
  37. #include "template.h"
  38. #include "dbox.h"
  39. #include "dboxquery.h"
  40. #include "menu.h"
  41. #include "xferrecv.h"
  42. #include "visdelay.h"
  43.  
  44. /*********************** DEFINITIONS **********************/
  45.  
  46. /* Fields for the main dialogue box */
  47.  
  48. #define FInputFile  0
  49. #define FOutputFile 1
  50. #define FOk         2
  51. #define FCancel     3
  52.  
  53. /* Menu Options */
  54.  
  55. #define MInfo 1
  56. #define MQuit 2
  57.  
  58. /* Fields for the proginfo dialogue box */
  59.  
  60. #define FVersionString 4
  61.  
  62. /* Misc defines */
  63.  
  64. #define NAMELENGTH 100
  65. #define fixchr(x) ((x)-'0')
  66. #define NULLCHR fixchr('~')
  67.  
  68. /********************* GLOBALS *************************/
  69.  
  70. BOOL boo_active = FALSE;
  71. char infile[NAMELENGTH], outfile[NAMELENGTH];
  72. FILE *ifp, *ofp;
  73.  
  74. static char *boo_Version_String = "0.1 (3 Nov 1990)";
  75.  
  76. /*********************  CODE  *************************/
  77.  
  78. /* Very simple filename conversion function */
  79.  
  80. static void boo_convertname(char *fname)
  81. {
  82.  
  83. /* Following rules apply to the conversion:
  84.  
  85.                    Up to 6 chars. from the main part of the name
  86.                    Underscore character ("_")
  87.                    Up to 3 characters from the extension
  88.  
  89.    Note: it is assumed that only a filename, and not a pathname, is supplied.
  90. */
  91.  
  92.    int i,j,k;
  93.    char tempname[11];
  94.  
  95.    for (i=0;i<=6;i++)
  96.    {
  97.       if (fname[i] != '.')        /* Copy up to 6 characters */
  98.          tempname[i] = fname[i];
  99.       else
  100.          break;                   /* if name less than 6 then get out */
  101.    }
  102.    j=i;
  103.    tempname[j++]='_';             /* Put in the underscore character */
  104.    while ((i<strlen(fname)) && (fname[i]!='.')) i++;  /* find separator */
  105.    i++;
  106.    k=j+3;
  107.    for (;j<=k;j++)
  108.    {
  109.       tempname[j]=fname[i];
  110.       if (fname[i++]=='\0')
  111.          break;
  112.    }
  113.    tempname[10]='\0';             /* Add terminator in case */
  114.    strcpy(fname, tempname);
  115. }
  116.  
  117. /*=======================================================================*/
  118.  
  119. static void boo_decode(char *inputfile, char *outputfile, dbox db)
  120. {
  121.    dboxquery_REPLY dbr;
  122.    int index, rptcnt, i;
  123.    int a, b, c, d;
  124.    char temp[NAMELENGTH];
  125.  
  126. /* Check input file */
  127.  
  128.    if ((ifp=fopen(inputfile, "r"))==NULL)
  129.    {
  130.       werr(FALSE, "Can't open input file");
  131.       return;
  132.    }
  133.  
  134. /* Read Output filename from input file */
  135.  
  136.    fgets(temp, NAMELENGTH, ifp);
  137.    if ((temp[strlen(temp)-2]=='\r'))
  138.       temp[strlen(temp)-2]='\0';
  139.    else
  140.       temp[strlen(temp)-1]='\0';
  141.    if (strcmp(outputfile, "")==0)
  142.    {
  143.       strcpy(outputfile, temp);
  144.       boo_convertname(outputfile);       /* convert to adfs filename */
  145.  
  146.    /* Update the dialogue box */
  147.  
  148.       dbox_setfield(db, FOutputFile, outputfile);
  149.       dbox_show(db);
  150.    }
  151.  
  152. /* Check for existence of output file */
  153.  
  154.    if ((ofp=fopen(outputfile, "r"))!=NULL)
  155.    {  /* File Exists */
  156.       fclose (ofp);
  157.       dbr = dboxquery("File Exists: Overwrite it?");
  158.       if (dbr != dboxquery_YES)
  159.       {
  160.          fclose (ifp);                    /* Close the input file */
  161.          return;                          /* and get out of here */
  162.       }
  163.    }
  164.    if ((ofp=fopen(outputfile, "wb"))==NULL)
  165.    {
  166.       werr(FALSE, "Unable to open output file");
  167.       return;
  168.    }
  169.    else
  170.       visdelay_begin();                   /* This could take some time! */
  171.  
  172. /* Finally do the decoding! */
  173.  
  174.    while (fgets(temp, NAMELENGTH, ifp)!=NULL)
  175.    {
  176.       index=0;
  177.       while(index<strlen(temp) && temp[index]!='\r' && temp[index]!='\n')
  178.       {
  179.          if (fixchr(temp[index])==NULLCHR)
  180.          {                        /* This bit copes with null compression */
  181.             index++;
  182.             rptcnt=fixchr(temp[index]);   /* get the repeat count */
  183.             for (i=0;i<rptcnt;i++)
  184.                putc('\0', ofp);
  185.             index++;
  186.          }
  187.          else
  188.          {                          /* Must be a quad for decoding */
  189.             a=fixchr(temp[index++]);
  190.             b=fixchr(temp[index++]);
  191.             c=fixchr(temp[index++]);
  192.             d=fixchr(temp[index++]);
  193.  
  194.          /* output the bytes */
  195.  
  196.             putc(((a*4)+(b/16)) & 255, ofp);
  197.             putc(((b*16)+(c/4)) & 255, ofp);
  198.             putc(((c*64)+d) & 255, ofp);
  199.          }
  200.       }
  201.    }
  202.    fclose(ofp);       /* Close the files up when finished! */
  203.    fclose(ifp);
  204.    visdelay_end();    /* Delay is over now so return to pointer */
  205. }
  206.  
  207. /*====================================================================*/
  208.  
  209. static void boo_new()
  210. {
  211.    dbox d;
  212.    dbox_field reply;
  213.  
  214.    if ((d=dbox_new("Main"))==FALSE)
  215.       werr(TRUE, "No Space for !Boo Utility");
  216.    boo_active=TRUE;
  217.    dbox_setfield(d, FInputFile, "");
  218.    dbox_setfield(d, FOutputFile, "");
  219.    dbox_show(d);
  220.    while ((reply=dbox_fillin(d))!=FOk && reply!=FCancel && reply!=dbox_CLOSE);
  221.    if (reply!=FCancel && reply!=dbox_CLOSE)
  222.    {
  223.       dbox_getfield(d, FInputFile, infile, NAMELENGTH);
  224.       dbox_getfield(d, FOutputFile, outfile, NAMELENGTH);
  225.       boo_decode(infile, outfile, d);
  226.    }
  227.    dbox_dispose(&d);
  228.    boo_active=FALSE;
  229. }
  230.  
  231. /*====================================================================*/
  232.  
  233. static void boo_iconclick(wimp_i icon)
  234. {
  235.    icon=icon;       /* Shuts the compiler up :-) */
  236.    if (boo_active==FALSE)
  237.       boo_new();
  238.    else
  239.       werr(FALSE, "!Boo already active");
  240. }
  241.  
  242. /*====================================================================*/
  243.  
  244. static void boo_infoaboutprog()
  245. {
  246.    dbox d;
  247.  
  248.    if ((d=dbox_new("ProgInfo"))==FALSE)
  249.       werr(TRUE, "No space for !Boo Utility");
  250.    dbox_setfield(d, FVersionString, boo_Version_String);
  251.    dbox_show(d);
  252.    dbox_fillin(d);
  253.    dbox_dispose(&d);
  254. }
  255.  
  256. /*========================================================================*/
  257.  
  258. static void boo_menuproc(void *handle, char *hit)
  259. {
  260.    handle=handle;   /* Guess what this is for ! */
  261.    switch (*hit)
  262.    {
  263.       case MInfo:
  264.                   boo_infoaboutprog();
  265.                   break;
  266.       case MQuit:
  267.                   exit(0);
  268.    }
  269. }
  270.  
  271. /*======================================================================*/
  272.  
  273. static void boo_loaddata()
  274. {
  275.    dbox d;
  276.    dbox_field reply;
  277.    char *tempfile;
  278.    int filetype;
  279.    os_filestr file;
  280.  
  281.    filetype=xferrecv_checkinsert(&tempfile);
  282.  
  283. /* Check length of filename & make copy for us */
  284.  
  285.    if (strlen(tempfile) > NAMELENGTH)
  286.    {
  287.       werr(FALSE, "File name is too long");
  288.       return;
  289.    }
  290.    strcpy(infile, tempfile);
  291.  
  292. /* Check the file type */
  293.  
  294.    file.action=5;
  295.    file.name=infile;
  296.    if (wimpt_complain(os_file(&file))!=0)
  297.       return;
  298.    switch (file.action)
  299.    {
  300.       case 0: werr (FALSE, "File Not Found");
  301.               return;
  302.       case 2: werr (FALSE, "You can't de-boo a directory");
  303.               return;
  304.    }
  305.  
  306. /* Create the de-boo dialogue box */
  307.  
  308.    if ((d=dbox_new("Main"))==FALSE)
  309.       werr(TRUE, "No space for !Boo Utility");
  310.    boo_active=TRUE;
  311.    dbox_setfield(d, FInputFile, infile);
  312.    dbox_setfield(d, FOutputFile, "");
  313.    dbox_show(d);
  314.    while ((reply=dbox_fillin(d))!=FOk && reply!=FCancel && reply!=dbox_CLOSE);
  315.    if (reply!=FCancel && reply!=dbox_CLOSE)
  316.    {
  317.       dbox_getfield(d, FInputFile, infile, NAMELENGTH);
  318.       dbox_getfield(d, FOutputFile, outfile, NAMELENGTH);
  319.       boo_decode(infile, outfile, d);
  320.       xferrecv_insertfileok();
  321.    }
  322.    dbox_dispose(&d);
  323.    boo_active=FALSE;
  324. }
  325.  
  326. /*====================================================================*/
  327.  
  328. static void boo_load(wimp_eventstr *e, void *handle)
  329. {
  330.    handle=handle;           /* seen this before ? */
  331.    switch (e->data.msg.hdr.action)
  332.    {
  333.       case wimp_MDATASAVE:        /* import data not supported (yet?) */
  334.                            werr(FALSE, "Data Import Not Supported");
  335.                            return;
  336.       case wimp_MDATALOAD:        /* insert data */
  337.       case wimp_MDATAOPEN:
  338.                            boo_loaddata();
  339.                            break;
  340.    }
  341. }
  342.  
  343. /*====================================================================*/
  344.  
  345. static BOOL boo_initialise()
  346. {
  347.    menu boo_menu;
  348.  
  349. /* initialise RISC_OSlib modules */
  350.  
  351.    wimpt_init("DeBoo Program");
  352.    res_init("Boo");
  353.    resspr_init();
  354.    template_init();
  355.    dbox_init();
  356.    visdelay_init();
  357.  
  358. /* Menu Info next */
  359.  
  360.    if ((boo_menu=menu_new("Boo",">Info,Quit"))==NULL)
  361.       return FALSE; /* Failed to create menu */
  362.  
  363. /* Now for the icon bar */
  364.  
  365.    baricon("!Boo", (int)resspr_area(), boo_iconclick);
  366.    if (!event_attachmenu(win_ICONBAR, boo_menu, boo_menuproc, 0))
  367.       return FALSE;  /* Failed to attach menu */
  368.  
  369. /* Register an event handler for load ops via icon bar */
  370.  
  371.    win_register_event_handler(win_ICONBARLOAD, boo_load, 0);
  372.  
  373. /* That's it - ready to go now! */
  374.  
  375.    return TRUE;
  376. }
  377.  
  378. /*=====================================================================*/
  379.  
  380. int main()
  381. {
  382.    if (boo_initialise())
  383.    {
  384.       while (TRUE)
  385.          event_process();
  386.    }
  387.    return 0;
  388. }
  389.