home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / biz / misc / banker / src / common.c < prev    next >
C/C++ Source or Header  |  1993-12-17  |  15KB  |  556 lines

  1. /*
  2.  * common.c
  3.  */
  4.  
  5. /* $Id: common.c,v 1.5 1993/09/03 00:17:28 beust Exp beust $ */
  6.  
  7. #define STRINGARRAY
  8.  
  9. #include "common.h"
  10.  
  11. BOOL FileModified;              /* True if file was modified */
  12. char FileName[128];            /* last accessed filename  */
  13. char ExportFileName[128];     /* exported file name  */
  14. char PrintFileName[128];     /* print filename */
  15. char Pattern[128];          /* pattern for file requester  */
  16. char ExportPattern[128];   /* pattern for export requester  */
  17. char PrintPattern[128];   /* pattern for print requester  */
  18.  
  19.  
  20. /* The console window used for output */
  21. FILE *BankerWindow = stderr;
  22.  
  23. struct IconBase *IconBase = NULL;
  24. struct IFFParseBase *IFFParseBase = NULL;
  25. struct GadToolsBase *GadToolsBase = NULL;
  26. struct UtilityBase *UtilityBase = NULL;
  27. struct IntuitionBase* IntuitionBase = NULL;
  28. struct GfxBase* GfxBase = NULL;
  29. struct LocaleBase *LocaleBase = NULL;
  30. struct DOSBase *DOSBase= NULL;
  31.  
  32. struct Locale *MyLocale = NULL;
  33. struct Catalog *MyCatalog = NULL;
  34.  
  35. Entries MainList;
  36.  
  37. /* Used to parse date with respect to locale */
  38. #define LOCALEDATEBUFFERSIZE 100
  39. char LocaleDateBuffer[LOCALEDATEBUFFERSIZE];
  40. int LocaleDateBufferPos = 0;
  41.  
  42. /* Command line */
  43. char *CL_Usage = "Language/K,File/K,OptionsFile/K";
  44. struct CommandLineStruct CL_Struct;
  45.  
  46. /* Options file */
  47. struct OptionsFileStruct OF_Struct;
  48.  
  49. /* Format variables */
  50. struct StringAspect PrintFormatStructure, ExportFormatStructure,
  51.                     DisplayFormatStructure;
  52.  
  53. struct ListEntry DisplayEntryFormat[MAX_FIELDS];
  54.  
  55. /* Automatic backup */
  56. struct timerequest *AutosaveTR = NULL;    /* autosave time request */
  57. LONG AutosaveSignal;
  58. struct MsgPort *AutosavePort = NULL;
  59.  
  60. /* Asl */
  61. struct AslBase *AslBase = NULL;
  62.  
  63. /* Gadtools */
  64. struct Screen *MyScreen;
  65. struct Window *GlobalWindow, *BDWindow,   /* backdrop window */
  66.               *GetentryWindow, *AutomaticWindow, *ListWindow;
  67. char ScreenTitle[128];
  68.  
  69. int UniqueID = 0;
  70.  
  71. /***********
  72.  * Private
  73.  ***********/
  74.  
  75.  
  76. void
  77. freeEntry(Entry entry)
  78. {
  79. }
  80.  
  81. void
  82. freeAutomatic(Automatic aut)
  83. {
  84. }
  85.  
  86.  
  87. char
  88. getSignificantCharacter(int n)
  89. {
  90.    char result, *s = GetCatalogStr(MyCatalog, n, AppStrings[n].as_Str), *p;
  91.  
  92.    result = s[0];
  93.    p = s;
  94.    while (*p && *p != '_') p++;
  95.    if (*p && *(p+1)) result = *(p+1);
  96.    result = ToLower(result);
  97.  
  98.    return result;
  99. }
  100.  
  101. char *
  102. getString(int n)
  103. {
  104.    char *result = (char *) GetCatalogStr(MyCatalog, n, AppStrings[n].as_Str), *p;
  105.    result = strdup(result);
  106.    p = result;
  107.    while (*p && *p != '_') p++;
  108.    if (*p == '_') {
  109.      while (*p != '\0') { *p = *(p+1); p++; }
  110.    }
  111.    return result;
  112. /*
  113.    char *result;
  114.  
  115.    STRPTR s;
  116.  
  117.    s = AppStrings[n].as_Str;
  118.    if (MyCatalog)
  119.       result = GetCatalogStr(MyCatalog, n, s);
  120.    else
  121.       result = s;
  122.  
  123.    return result;
  124. */
  125. }
  126.  
  127. void
  128. openLocale(char *locale)
  129. {
  130.    if ((LocaleBase = (struct LocaleBase *)
  131.                     OpenLibrary("locale.library", 38L)) == NULL) {
  132.       myMsg1("couldn't open locale.library, using default language");
  133.    }
  134.    else {
  135.       if (LocaleBase == NULL ||
  136.           (LocaleBase && LocaleBase -> lb_LibNode.lib_Revision < 27)) {
  137.          myMsg1(getString(MSG_BETA_VERSION));
  138.       }
  139.  
  140.       MyCatalog = OpenCatalog(NULL, "banker.catalog",
  141.                   OC_BuiltInLanguage, "english",
  142. /*
  143.   OC_Language, locale,
  144. */
  145.                   TAG_DONE);
  146.       printf("string = '%s'\n",
  147.          GetCatalogStr(MyCatalog, MSG_MENU_FILE, "engllish"));
  148.       if (MyCatalog == NULL)
  149.     myMsg3("OpenCatalog NULL", "IoErr():", IoErr());
  150.    } /* else */
  151. }
  152.  
  153. /*********
  154.  * General
  155.  *********/
  156.  
  157. void
  158. myMsg1(char *s1)
  159. {
  160.    fprintf(BankerWindow, "%s\n", s1);
  161. }
  162.  
  163. void
  164. myMsg2(char *s1, char *s2)
  165. {
  166.    fprintf(BankerWindow, "%s : %s\n", s1, s2);
  167. }
  168.  
  169. void
  170. myMsg3(char *s1, char *s2, int n)
  171. {
  172.    fprintf(BankerWindow, "%s : %s (%d)\n", s1, s2, n);
  173. }
  174.  
  175. void
  176. closeAll()
  177. {
  178.  
  179. /* Delete autosave stuff */
  180.    if (AutosaveTR) {
  181.       if (OF_Struct.autoSave > 0) AbortIO(AutosaveTR);
  182.       CloseDevice((struct IORequest *) AutosaveTR);
  183.       DeleteExtIO((struct IORequest *) AutosaveTR);
  184.    }
  185.    DeletePort(AutosavePort);
  186.  
  187. /* Delete the crash file */
  188.    DeleteFile(crashFileName(FileName));
  189.  
  190. /* Close the screen */
  191.    if (MyScreen) CloseScreen(MyScreen); MyScreen = NULL;
  192.  
  193. /* And the possible output window */
  194.    if (BankerWindow != stderr)
  195.       fclose(BankerWindow);
  196.  
  197. /* And all the libraries */
  198.    if (AslBase) CloseLibrary(AslBase); AslBase = NULL;
  199.    if (GfxBase) CloseLibrary(GfxBase); GfxBase = NULL;
  200.    if (IFFParseBase) CloseLibrary(IFFParseBase); IFFParseBase = NULL;
  201.    if (GadToolsBase) CloseLibrary(GadToolsBase); GadToolsBase = NULL;
  202.    if (UtilityBase) CloseLibrary(UtilityBase); UtilityBase = NULL;
  203.    if (IconBase) CloseLibrary(IconBase); IconBase = NULL;
  204.  
  205. }
  206.  
  207. void
  208. openLibraries(Entries *entries)
  209. {
  210.    ULONG error;
  211.  
  212.    DOSBase = (struct DOSBase *) OpenLibrary("dos.library", 37L);
  213.    if (! DOSBase) {
  214.       myMsg1("couldn't open dos.library");
  215.       closeAll();
  216.    }
  217.  
  218.    IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37L);
  219.    if (! IntuitionBase) {
  220.       myMsg1("couldn't open intuition.library");
  221.       closeAll();
  222.    }
  223.  
  224.    LocaleBase = (struct LocaleBase *) OpenLibrary("locale.library", 38L);
  225.    if (! LocaleBase) {
  226.       myMsg1("couldn't open locale.library, using default language");
  227.    }
  228.    else {
  229.       if (LocaleBase -> lb_LibNode.lib_Revision < 27) {
  230.      myMsg1(getString(MSG_BETA_VERSION));
  231.       }
  232.       MyCatalog = OpenCatalog(NULL, "banker.catalog",
  233.                   OC_BuiltInLanguage, "english",
  234.                   TAG_DONE);
  235.       if (MyCatalog == NULL)
  236.     myMsg3("OpenCatalog NULL", "IoErr():", IoErr());
  237.    } /* else */
  238.  
  239.    AslBase = (struct AslBase *) OpenLibrary("asl.library", 37L);
  240.    if (! AslBase) {
  241.       myMsg1("couldn't open asl.library");
  242.       closeAll();
  243.    }
  244.  
  245.    GfxBase= (struct GfxBase*) OpenLibrary("graphics.library", 37L);
  246.    if (! GfxBase) {
  247.       myMsg1("couldn't open graphics.library");
  248.       closeAll();
  249.    }
  250.  
  251.    IFFParseBase = (struct IFFParseBase *) OpenLibrary("iffparse.library", 0L);
  252.    if (! IFFParseBase) {
  253.       myMsg1("couldn't open iffparse.library");
  254.       closeAll();
  255.    }
  256.  
  257.    GadToolsBase = (struct GadToolsBase*) OpenLibrary("gadtools.library", 37L);
  258.    if (! GadToolsBase) {
  259.       myMsg1("couldn't open gadtools.library");
  260.       closeAll();
  261.    }
  262.  
  263.    UtilityBase= (struct UtilityBase*) OpenLibrary("utility.library", 37L);
  264.    if (! UtilityBase) {
  265.       myMsg1("couldn't open utility.library");
  266.       closeAll();
  267.    }
  268.  
  269.    IconBase= (struct IconBase*) OpenLibrary("icon.library", 37L);
  270.    if (! IconBase) {
  271.       myMsg1("couldn't open icon.library");
  272.       closeAll();
  273.    }
  274.  
  275.    NEW(*entries, struct _Entries);
  276.  
  277.    (*entries) -> list = (Entry *)
  278.      DB_NewDataBase(sizeof(struct _Entry), freeEntry);
  279.    (*entries) -> automatic =(Automatic*)
  280.      DB_NewDataBase(sizeof(struct _Automatic), freeAutomatic);
  281.  
  282. /* Global variables changing through the execution */
  283.    strcpy(FileName, getString(MSG_NO_FILE_LOADED));
  284.    ExportFileName[0] = '\0';
  285.    strcpy(PrintFileName, "PRT:");
  286.    strcpy(Pattern, "#?.bank");
  287.    strcpy(PrintPattern, "");
  288.    strcpy(ExportPattern, "");
  289.  
  290. /* Allocate structures for autosave */
  291.    AutosavePort = (struct MsgPort *) CreatePort(0, 0);
  292.    if (! AutosavePort) {
  293.       myMsg1("*** couldn't allocate autosave port");
  294.       closeAll();
  295.    }
  296.  
  297.    AutosaveSignal = 1 << AutosavePort -> mp_SigBit;
  298.  
  299.    AutosaveTR = (struct timerequest *)
  300.                      CreateExtIO(AutosavePort, sizeof(struct timerequest));
  301.  
  302.    if (! AutosaveTR) {
  303.       myMsg1("*** couldn't allocate time request");
  304.       closeAll();
  305.    }
  306.  
  307.    error = OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest*) AutosaveTR, 0L);
  308.    if (error) {
  309.       myMsg3("*** couldn't open device", TIMERNAME, error);
  310.       closeAll();
  311.    }
  312.  
  313. /* Initialize windows */
  314.    GetentryWindow = NULL;
  315.    AutomaticWindow = NULL;
  316.    ListWindow = NULL;
  317.  
  318. /* The file hasn't been modified yet */
  319.    FileModified = 0;
  320.  
  321. /* Defaults values for options */
  322.    OF_Struct.mainWindow.isOpen = TRUE;
  323.    OF_Struct.list.isOpen = FALSE;
  324.    OF_Struct.automatic.isOpen = FALSE;
  325.    OF_Struct.getEntry.isOpen = FALSE;
  326.  
  327.    OF_Struct.dateFormat = "%D";      /* default extended date %m/%d/%y */
  328.    OF_Struct.defaultDate = "CDN";   /* default poor-man's-date mm-dd-yy */
  329.    OF_Struct.autoSave = 1;       /* 1 minute */
  330.  
  331.    OF_Struct.displayFormat = "date amount transaction \"F\"";   /* default %m/%d/%y */
  332.    OF_Struct.exportFormat = "date amount transaction \"F\"";   /* default %m/%d/%y */
  333.    OF_Struct.printFormat = "date amount transaction \"F\"";   /* default %m/%d/%y */
  334.    readOptionsFile();
  335. }
  336.  
  337. char *
  338. crashFileName(char *filename)
  339. {
  340.    char *result = (char *) malloc(strlen(filename) + 10);
  341.    strcpy(result, filename);
  342.    strcat(result, ".crash");
  343.    return result;
  344. }
  345.  
  346. void
  347. readEnvironment(int argc, char **argv)
  348. {
  349.    struct CSource cs;
  350.    struct RDArgs rda, *rda2;
  351.    struct CommandLineStruct result;
  352.    char language[64];
  353.  
  354.    result.filename = NULL;
  355.    result.language = NULL;
  356.    result.optionsFile= NULL;
  357.  
  358.    if (argc) {    /* called from CLI */
  359.       BankerWindow = stderr;
  360.       rda2 = ReadArgs(CL_Usage, & result, NULL);
  361.  
  362.    /* We have to copy any returned value, its reference is not safe */
  363.       if (result.filename) {
  364.          CL_Struct.filename = (char *) malloc(strlen(result.filename) + 2);
  365.          strcpy(CL_Struct.filename, result.filename);
  366.       }
  367.  
  368.       if (result.language) {
  369.          CL_Struct.language = (char *) malloc(strlen(result.language) + 2);
  370.          strcpy(CL_Struct.language, result.language);
  371.       }
  372.  
  373.       if (result.optionsFile) {
  374.          CL_Struct.optionsFile = (char *) malloc(strlen(result.optionsFile) + 2);
  375.          strcpy(CL_Struct.optionsFile, result.optionsFile);
  376.       }
  377.  
  378.       FreeArgs(rda2);
  379.    }
  380.  
  381.    else {    /* called from Workbench */
  382.       struct WBStartup *wbs;
  383.       struct WBArg *wba;
  384.       struct DiskObject *dobj;
  385.       char **array, *s;
  386.       int i;
  387.  
  388.       BankerWindow = fopen(BANKEROUTPUTWINDOW, "w");
  389.  
  390.       wbs = (struct WBStartup *) argv;       /* cast argc into WBStartup */
  391.       wba = & wbs -> sm_ArgList[0];          /* retrieve argv[0] (ourselves) */
  392.       myMsg2("getting disk object", wba -> wa_Name);
  393.       dobj = GetDiskObject(wba -> wa_Name);  /* and find the disk object */
  394.  
  395.    /* Retrieve all the tooltypes for myself */
  396.       array = (char **) dobj -> do_ToolTypes;
  397.       if (array) {               /* there are tooltypes */
  398.          if ((s = FindToolType(array, "FILE"))) {
  399.             CL_Struct.filename = strdup(s);
  400.          }
  401.          if ((s = FindToolType(array, "OPTIONSFILE"))) {
  402.             CL_Struct.optionsFile = strdup(s);
  403.          }
  404.          if ((s = FindToolType(array, "LANGUAGE"))) {
  405.             CL_Struct.language = strdup(s);
  406.          }
  407.       }
  408.  
  409.    /* Parse all the files that were possibly multi-selected with me */
  410.       for (i = 1; i < wbs -> sm_NumArgs; i++) {    /* read all the icons */
  411.          wba = & wbs -> sm_ArgList[i];
  412.          if (wba -> wa_Lock != NULL && *wba -> wa_Name)
  413.             CurrentDir(wba -> wa_Lock);    /* change to this directory */
  414.       }
  415.    }
  416.  
  417.    MyLocale = OpenLocale(CL_Struct.language);
  418.  
  419. }
  420.  
  421. void __saveds __asm putCharFunc(register __a0 struct Hook *h,
  422.                                 register __a2 void *object,
  423.                                 register __a1 char c)
  424. {
  425.     if (LocaleDateBufferPos < LOCALEDATEBUFFERSIZE)
  426.       ((char *)h->h_Data)[LocaleDateBufferPos++] = c;
  427. }
  428.  
  429. struct Hook putCharHook = {
  430.     {NULL, NULL},
  431.     (unsigned long (*) ()) putCharFunc,
  432.     NULL,
  433.     LocaleDateBuffer
  434. };
  435.  
  436. unsigned long __saveds __asm getCharFunc(register __a0 struct Hook *h,
  437.                                 register __a2 void *object,
  438.                                 register __a1 void *locale)
  439. {
  440.    unsigned long result;
  441.  
  442.     if (LocaleDateBufferPos < LOCALEDATEBUFFERSIZE)
  443.       result = (((char *)h->h_Data)[LocaleDateBufferPos++]) & 0xff;
  444.  
  445.    return result;
  446. }
  447.  
  448. struct Hook getCharHook = {
  449.     {NULL, NULL},
  450.     (unsigned long (*) ()) getCharFunc,
  451.     NULL,
  452.     LocaleDateBuffer
  453. };
  454.  
  455.  
  456. char *
  457. myDateToStr(struct DateStamp *ds)
  458. {
  459.    int fullFunctionalities = 1;
  460.    STRPTR format;
  461.    char *result, time[64];
  462.    struct DateTime dt;
  463.    BOOL bool;
  464.  
  465. /*
  466.       format = MyLocale -> loc_ShortDateFormat;  /* short date format *
  467. */
  468.    if (LocaleBase == NULL) fullFunctionalities = 0;
  469.    if (LocaleBase && LocaleBase -> lb_LibNode.lib_Revision < 27)
  470.       fullFunctionalities = 0;
  471.  
  472.    if (fullFunctionalities) {
  473.       format = OF_Struct.dateFormat;
  474.       LocaleDateBufferPos = 0;
  475.       FormatDate(MyLocale, format, ds, &putCharHook);
  476.       sprintf(time,"%12s", LocaleDateBuffer);   /* Reformatting */
  477.       strcpy(LocaleDateBuffer, time);
  478.       result = LocaleDateBuffer;
  479.    }
  480.    else {
  481.       if (ds -> ds_Days == 0) return "";
  482.       result = (char *) malloc(64);
  483.       dt.dat_Stamp = *ds;
  484.       dt.dat_StrDate = result;
  485.       dt.dat_StrTime = NULL;
  486.       dt.dat_StrDay = NULL;
  487.       dt.dat_Format = FORMAT_CDN;
  488.       if (STREQUAL(OF_Struct.defaultDate, "DOS")) dt.dat_Format = FORMAT_DOS;
  489.       else if (STREQUAL(OF_Struct.defaultDate, "CDN")) dt.dat_Format = FORMAT_CDN;
  490.       else if (STREQUAL(OF_Struct.defaultDate, "INT")) dt.dat_Format = FORMAT_INT;
  491.       else if (STREQUAL(OF_Struct.defaultDate, "USA")) dt.dat_Format = FORMAT_USA;
  492.       else {
  493.          myMsg2("Unknown defaultDate keyword (using CDN) : ", OF_Struct.defaultDate);
  494.          dt.dat_Format = FORMAT_CDN;
  495.       }
  496.       dt.dat_Flags = NULL; /* could be DTF_SUBST to have Monday when possible */
  497.       bool = DateToStr(& dt);
  498.       if (! bool) result = "ErrDateStr";
  499.    }
  500.    while (*result == ' ') result++;
  501.    return result;
  502. }
  503.  
  504.  
  505. struct DateStamp *
  506. myStrToDate(char *dateString)
  507. {
  508.  
  509.    int fullFunctionalities = 1;
  510.    struct DateTime *dt;
  511.    struct DateStamp *result;
  512.    char *format;
  513.    int bool;
  514.  
  515.    dt = (struct DateTime *) malloc(sizeof *dt);
  516.    result = & dt -> dat_Stamp;
  517.  
  518.    result -> ds_Days = 0;
  519.    result -> ds_Minute = 0;
  520.    result -> ds_Tick = 0;
  521.  
  522.    if (dateString == NULL || (dateString && *dateString == '\0'))
  523.       return result;
  524.  
  525.    if (LocaleBase == NULL) fullFunctionalities = 0;
  526.    if (LocaleBase && LocaleBase -> lb_LibNode.lib_Revision < 27)
  527.       fullFunctionalities = 0;
  528.  
  529.    if (! fullFunctionalities) {
  530.       if (STREQUAL(OF_Struct.defaultDate, "CDN")) dt -> dat_Format = FORMAT_CDN;
  531.       else if (STREQUAL(OF_Struct.defaultDate, "INT")) dt->dat_Format= FORMAT_INT;
  532.       else if (STREQUAL(OF_Struct.defaultDate, "DOS")) dt->dat_Format= FORMAT_DOS;
  533.       else if (STREQUAL(OF_Struct.defaultDate, "USA")) dt->dat_Format= FORMAT_USA;
  534.       else {
  535.          myMsg2("unknown defaultdate token (using CDN)", OF_Struct.defaultDate);
  536.          dt -> dat_Format = FORMAT_CDN;
  537.       }
  538.       dt -> dat_StrDay = NULL;
  539.       dt -> dat_StrDate = dateString;
  540.       dt -> dat_StrTime = NULL;
  541.       dt -> dat_Flags = 0;
  542.       StrToDate(dt);
  543.       result = & dt -> dat_Stamp;
  544.    }
  545.    else {
  546.       format = OF_Struct.dateFormat;
  547.       LocaleDateBufferPos = 0;
  548.       strcpy(LocaleDateBuffer, dateString);
  549.       bool = ParseDate(MyLocale, result, format, & getCharHook);
  550.    }
  551.  
  552.    return result;
  553.  
  554. }
  555.  
  556.