home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / gui / textwin.lha / TextWin / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-06  |  7.7 KB  |  392 lines

  1.  
  2. #include <dos/dos.h>
  3. #include <dos/dostags.h>
  4.  
  5. /* Libraries */
  6. #include <libraries/mui.h>
  7. #include <libraries/reqtools.h>
  8.  
  9. /* protos */
  10. #include <clib/muimaster_protos.h>
  11. #include <clib/reqtools_protos.h>
  12. #include <clib/alib_protos.h>
  13. #include <clib/dos_protos.h>
  14. #include <clib/exec_protos.h>
  15.  
  16. /*  Pragmas  */
  17. #include <pragmas/muimaster_pragmas.h>
  18. #include <pragmas/reqtools.h>
  19. #include <pragmas/exec_lib.h>
  20.  
  21. /*  Ansi  */
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25.  
  26. /* MUIBuilder */
  27. #include "TextWin.h"
  28.  
  29. static char version_string[] = "$VER: TextWin 1.00 (05.12.94)";
  30.  
  31. char MyExtHelp[] =
  32. "\n"
  33. "Usage : TextWin\n"
  34. "\n"
  35. "  TITLE/A,BODYTEXT=BODY/A\n"
  36. "\n"
  37. "Arguments:\n"
  38. "\n"
  39. "  TITLE      <text> - Title text for requester window.\n"
  40. "  BODYTEXT   <text> -\n"
  41. "  >or BODY\n";
  42.  
  43. char *CMD_TEMPLATE = "TITLE/A,BODYTEXT=BODY/A";
  44.  
  45. #define OPT_TITLE 0
  46. #define OPT_BODYTEXT 1
  47. #define OPT_COUNT 2
  48.  
  49. /* defines */
  50. #define    ID_WINDOW_ACTIVATE 1
  51. #define    ID_MYBUTTON  2
  52.  
  53. BOOL show_bodytext = FALSE;
  54.  
  55. char window_title[80] = "";
  56. char *body_text = NULL;
  57.  
  58. struct ObjApp *App = NULL;    /* Application object */
  59.  
  60. struct Library *MUIMasterBase;
  61. struct Library *ReqToolsBase;
  62. struct Library *IntuitionBase;
  63.  
  64. /* Init function */
  65. static int
  66. init (int argc, char **argv)
  67. {
  68.   int retval = 0;
  69.  
  70.   if (!(MUIMasterBase = OpenLibrary (MUIMASTER_NAME, MUIMASTER_VMIN)))
  71.     {
  72.       printf ("Can't Open MUIMaster Library");
  73.       retval = 20;
  74.     }
  75.  
  76.   if (!(ReqToolsBase = OpenLibrary ("reqtools.library", 37)))
  77.     {
  78.       printf ("Can't Open Intuition Library");
  79.       retval = 20;
  80.     }
  81.  
  82.   if (!(IntuitionBase = OpenLibrary ("intuition.library", 37)))
  83.     {
  84.       printf ("Can't Open Intuition Library");
  85.       retval = 20;
  86.     }
  87.  
  88.  
  89.   if ((retval == 0) && (argc))
  90.     {
  91.       /* see if the big dummy is asking for help? */
  92.       if ((argc == 2) && (stricmp (argv[1], "HELP") == 0))
  93.     {
  94.       /* yeh, wouldn't you know it - You Dumb Shit! */
  95.       fprintf (stdout, "%s", MyExtHelp);
  96.       retval = 20;
  97.     }
  98.       else
  99.     /* this guy seems serious.... */
  100.     {
  101.       /* My custom RDArgs */
  102.       struct RDArgs *myrda;
  103.  
  104.       /* Need to ask DOS for a RDArgs structure */
  105.       if (myrda = (struct RDArgs *) AllocDosObject (DOS_RDARGS, NULL))
  106.         {
  107.           /*
  108.            * The array of LONGs where ReadArgs() will store the data from
  109.            * the command line arguments.  C guarantees that all the array
  110.            * entries will be set to zero.
  111.            */
  112.           LONG *result[OPT_COUNT] =
  113.           {
  114.         NULL
  115.           };
  116.  
  117.           /* lets see how serious he is - parse my command line */
  118.           if (ReadArgs (CMD_TEMPLATE, result, myrda))
  119.         {
  120.           if ((result[OPT_TITLE]) ? TRUE : FALSE)
  121.             {
  122.               strcpy (window_title, result[OPT_TITLE]);
  123.             }
  124.  
  125.           if (show_bodytext = (result[OPT_BODYTEXT]) ? TRUE : FALSE)
  126.             {
  127.               BPTR lock;
  128.  
  129.               if (lock = Lock (result[OPT_BODYTEXT], SHARED_LOCK))
  130.             {
  131.               struct FileInfoBlock *fib;
  132.  
  133.               if (fib = (struct FileInfoBlock *) AllocDosObject (DOS_FIB, NULL))
  134.                 {
  135.                   if (Examine (lock, fib))
  136.                 {
  137.                   ULONG line_cnt;
  138.  
  139.                   {
  140.                     FILE *infile;
  141.  
  142.                     if (infile = fopen (result[OPT_BODYTEXT], "r"))
  143.                       {
  144.                     char buf[512];
  145.  
  146.                     for (line_cnt = 0; fgets (buf, 512, infile); line_cnt++)
  147.                       {
  148.                         /* do nothing - just count lines */ ;
  149.                       }
  150.  
  151.                     fclose (infile);
  152.                       }
  153.                   }
  154.  
  155.                   if (body_text = (char *) malloc (fib->fib_Size + line_cnt + 1))
  156.                     {
  157.                       FILE *infile;
  158.  
  159.                       if (infile = fopen (result[OPT_BODYTEXT], "r"))
  160.                     {
  161.                       char buf[512];
  162.                       LONG i;
  163.  
  164.                       *body_text = '\000';
  165.  
  166.                       for (i = 0; fgets (buf, 512, infile); i++)
  167.                         {
  168.                           strcat (body_text, buf);
  169.                           strcat (body_text, " ");
  170.                         }
  171.  
  172.                       fclose (infile);
  173.                     }
  174.                       else
  175.                     {
  176.                       /* unable to open infile */
  177.                     }
  178.                     }
  179.                   else
  180.                     {
  181.                     }
  182.                 }
  183.  
  184.                   FreeDosObject (DOS_FIB, fib);
  185.                 }
  186.               else
  187.                 {
  188.                   /* unable to alloc dos object */
  189.                 }
  190.               UnLock (lock);
  191.             }
  192.               else
  193.             {
  194.               if (body_text = (char *) malloc (strlen (result[OPT_BODYTEXT]) + 1))
  195.                 {
  196.                   strcpy (body_text, result[OPT_BODYTEXT]);
  197.                 }
  198.               else
  199.                 {
  200.                   fprintf (stderr, "error: unable to alloc memory for bodytext\n");
  201.                 }
  202.             }
  203.  
  204.               {
  205.             char *s = body_text, *d = body_text, *t;
  206.  
  207.             while ((t = strchr (s, '\\')) && (strchr ("nrt", *(t + 1))))
  208.               {
  209.                 while (s < t)
  210.                   {
  211.                 if (isprint (*s))
  212.                   *d++ = *s++;
  213.                 else
  214.                   s++;
  215.                   }
  216.  
  217.                 switch (*(t + 1))
  218.                   {
  219.                   case 'n':
  220.                 *d++ = '\n';
  221.                 break;
  222.  
  223.                   case 't':
  224.                 *d++ = '\t';
  225.                 break;
  226.                   }
  227.  
  228.                 s++, s++;
  229.               }
  230.  
  231.             while (s[0])
  232.               *d++ = *s++;
  233.  
  234.             *d = '\0';
  235.               }
  236.             }
  237.  
  238.           FreeArgs (myrda);
  239.         }
  240.           else
  241.         {
  242.           retval = 20;    /* nah, he screwed it up - something went wrong in the parse */
  243.         }
  244.  
  245.           if (retval >= 20)
  246.         {
  247.           if (result[OPT_TITLE] == NULL)
  248.             {
  249.               fprintf (stderr, "TextWin: Required argument missing.\n");
  250.             }
  251.           else
  252.             {
  253.               /* something else is wrong */
  254.               fprintf (stderr, "TextWin: Command syntax error.\n");
  255.             }
  256.  
  257.           fprintf (stderr, "Try - TextWin help.\n");
  258.         }
  259.  
  260.           FreeDosObject (DOS_RDARGS, myrda);
  261.         }
  262.       else
  263.         {
  264.           /* Unable to alloc readargs structure */
  265.           retval = 20;
  266.         }
  267.     }
  268.     }
  269.   else
  270.     {
  271.       /* launched from workbench */
  272.       retval = 20;
  273.     }
  274.  
  275.   return (retval);
  276. }
  277.  
  278. int
  279. main (int argc, char **argv)
  280. {
  281.   int retval = 0;
  282.  
  283.   BOOL running = TRUE;
  284.   ULONG signal;
  285.  
  286.   /* Program initialisation ( you need to write it yourself) */
  287.   if ((retval = init (argc, argv)) != 0)
  288.     {
  289.       /* Ate shit and died! */
  290.       goto oops;
  291.     }
  292.  
  293.   /* Create Application : generated by MUIBuilder */
  294.   if (App = CreateApp ())
  295.     {
  296.       if (window_title)        /*setup window title */
  297.     set (App->WI_label_0, MUIA_Window_Title, window_title);
  298.       else
  299.     set (App->WI_label_0, MUIA_Window_Title, "Information");
  300.  
  301.       {
  302.     if (DoMethod (App->GR_ListView, OM_ADDMEMBER,
  303.               ListviewObject,
  304.               MUIA_Weight, 50,
  305.               MUIA_Listview_Input, FALSE,
  306.               MUIA_Listview_List, FloattextObject,
  307.               MUIA_Frame, MUIV_Frame_ReadList,
  308.               MUIA_Floattext_Text, body_text,
  309.               MUIA_Floattext_TabSize, 4,
  310.               MUIA_Floattext_Justify, FALSE,
  311.               End, End))
  312.       {
  313.  
  314.         APTR button;
  315.  
  316.         char *hotkey = NULL;
  317.         char *ptr = "Continue";
  318.  
  319.         DoMethod (App->GR_Buttons, OM_ADDMEMBER, HVSpace);
  320.  
  321.         if (DoMethod (App->GR_Buttons, OM_ADDMEMBER,
  322.               button = (APTR) KeyButton (ptr, tolower (hotkey))))
  323.           {
  324.         /* setup notification */
  325.         DoMethod (button, MUIM_Notify, MUIA_Pressed, FALSE,
  326.                App->App, 2, MUIM_Application_ReturnID, ID_MYBUTTON);
  327.           }
  328.  
  329.         DoMethod (App->GR_Buttons, OM_ADDMEMBER, HVSpace);
  330.  
  331.         DoMethod (App->WI_label_0, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
  332.               App->App, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
  333.  
  334.         set (App->WI_label_0, MUIA_Window_Open, TRUE);
  335.  
  336.         while (running)
  337.           {
  338.         LONG value;
  339.  
  340.         switch (value = DoMethod (App->App, MUIM_Application_Input, &signal))
  341.           {
  342.           case 0:    /* I'm not sure what this signal is */
  343.             break;
  344.  
  345.           case MUIV_Application_ReturnID_Quit:
  346.             retval = 0;
  347.             running = FALSE;
  348.             break;
  349.  
  350.           case ID_MYBUTTON:
  351.             running = FALSE;
  352.             break;
  353.  
  354.           default:
  355.             break;
  356.           }
  357.  
  358.         if (running && signal)
  359.           Wait (signal);
  360.           }
  361.  
  362.         /* Close Window - Shouldn't really have to be done but ....... */
  363.  
  364.         set (App->WI_label_0, MUIA_Window_Open, FALSE);
  365.  
  366.       }
  367.     else
  368.       {
  369.         /* unable to create bodytext object */
  370.       }
  371.  
  372.     DisposeApp (App);
  373.       }
  374.     }
  375.  
  376. oops:
  377.  
  378.   if (body_text)
  379.     free (body_text);
  380.  
  381.   if (IntuitionBase)
  382.     CloseLibrary (IntuitionBase);
  383.  
  384.   if (ReqToolsBase)
  385.     CloseLibrary (ReqToolsBase);
  386.  
  387.   if (MUIMasterBase)
  388.     CloseLibrary (MUIMasterBase);
  389.  
  390.   return (retval);
  391. }
  392.