home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / TRASLAT.CPP < prev    next >
C/C++ Source or Header  |  1997-05-27  |  8KB  |  318 lines

  1.  
  2. // ----------------------------------------------------------------------
  3. // Lora BBS Professional Edition - Version 3.00.35
  4. // Copyright (c) 1997 by Marco Maccaferri. All rights reserved.
  5. //
  6. // History:
  7. //    02/02/97 - Initial coding.
  8. // ----------------------------------------------------------------------
  9.  
  10. #include "_ldefs.h"
  11. #include "lora_api.h"
  12.  
  13. TTranslation::TTranslation (void)
  14. {
  15.    fdDat = -1;
  16.    strcpy (DataFile, "cost.dat");
  17. }
  18.  
  19. TTranslation::TTranslation (PSZ pszDataPath)
  20. {
  21.    fdDat = -1;
  22.    strcpy (DataFile, pszDataPath);
  23.    strcat (DataFile, "cost.dat");
  24.    AdjustPath (strlwr (DataFile));
  25. }
  26.  
  27. TTranslation::~TTranslation (void)
  28. {
  29.    if (fdDat != -1)
  30.       close (fdDat);
  31. }
  32.  
  33. VOID TTranslation::Class2Struct (VOID)
  34. {
  35.    int i;
  36.  
  37.    memset (&table, 0, sizeof (table));
  38.    table.Size = sizeof (table);
  39.    strcpy (table.Location, Location);
  40.    strcpy (table.Search, Search);
  41.    strcpy (table.Traslate, Traslate);
  42.  
  43.    for (i = 0; i < MAXCOST; i++) {
  44.       table.Cost[i].Days = Cost[i].Days;
  45.       table.Cost[i].Start = Cost[i].Start;
  46.       table.Cost[i].Stop = Cost[i].Stop;
  47.       table.Cost[i].CostFirst = Cost[i].CostFirst;
  48.       table.Cost[i].TimeFirst = Cost[i].TimeFirst;
  49.       table.Cost[i].Cost = Cost[i].Cost;
  50.       table.Cost[i].Time = Cost[i].Time;
  51.    }
  52. }
  53.  
  54. VOID TTranslation::Struct2Class (VOID)
  55. {
  56.    int i;
  57.  
  58.    strcpy (Location, table.Location);
  59.    strcpy (Search, table.Search);
  60.    strcpy (Traslate, table.Traslate);
  61.  
  62.    for (i = 0; i < MAXCOST; i++) {
  63.       Cost[i].Days = table.Cost[i].Days;
  64.       Cost[i].Start = table.Cost[i].Start;
  65.       Cost[i].Stop = table.Cost[i].Stop;
  66.       Cost[i].CostFirst = table.Cost[i].CostFirst;
  67.       Cost[i].TimeFirst = table.Cost[i].TimeFirst;
  68.       Cost[i].Cost = table.Cost[i].Cost;
  69.       Cost[i].Time = table.Cost[i].Time;
  70.    }
  71. }
  72.  
  73. VOID TTranslation::Add (VOID)
  74. {
  75.    USHORT DoClose = FALSE;
  76.  
  77.    if (fdDat == -1) {
  78.       fdDat = open (DataFile, O_RDWR|O_BINARY|O_CREAT, S_IREAD|S_IWRITE);
  79.       DoClose = TRUE;
  80.    }
  81.  
  82.    if (fdDat != -1) {
  83.       Class2Struct ();
  84.       lseek (fdDat, 0L, SEEK_END);
  85.       write (fdDat, &table, sizeof (table));
  86.    }
  87.  
  88.    if (DoClose == TRUE && fdDat != -1) {
  89.       close (fdDat);
  90.       fdDat = -1;
  91.    }
  92. }
  93.  
  94. VOID TTranslation::DeleteAll (VOID)
  95. {
  96.    if (fdDat != -1) {
  97.       close (fdDat);
  98.       fdDat = -1;
  99.    }
  100.  
  101.    unlink (DataFile);
  102. }
  103.  
  104. USHORT TTranslation::First (VOID)
  105. {
  106.    if (fdDat == -1)
  107.       fdDat = open (DataFile, O_RDWR|O_BINARY|O_CREAT, S_IREAD|S_IWRITE);
  108.  
  109.    if (fdDat != -1)
  110.       lseek (fdDat, 0L, SEEK_SET);
  111.  
  112.    return (Next ());
  113. }
  114.  
  115. VOID TTranslation::Export (PSZ pszFile)
  116. {
  117.    FILE *fp;
  118.    int i;
  119.    CHAR string[64];
  120.  
  121.    if ((fp = fopen (pszFile, "wt")) != NULL) {
  122.       if (First () == TRUE)
  123.          do {
  124.             if (Search[0] == '\0')
  125.                strcpy (Search, "/");
  126.             if (Traslate[0] == '\0')
  127.                strcpy (Traslate, "/");
  128.             fprintf (fp, "\nPrefix %s %s \"%s\"\n", Search, Traslate, Location);
  129.  
  130.             for (i = 0; i < MAXCOST; i++) {
  131.                if (Cost[i].Days == 0)
  132.                   continue;
  133.                strcpy (string, "-------");
  134.                if (Cost[i].Days & DAY_SUNDAY)
  135.                   string[0] = 'S';
  136.                if (Cost[i].Days & DAY_MONDAY)
  137.                   string[1] = 'M';
  138.                if (Cost[i].Days & DAY_TUESDAY)
  139.                   string[2] = 'T';
  140.                if (Cost[i].Days & DAY_WEDNESDAY)
  141.                   string[3] = 'W';
  142.                if (Cost[i].Days & DAY_THURSDAY)
  143.                   string[4] = 'T';
  144.                if (Cost[i].Days & DAY_FRIDAY)
  145.                   string[5] = 'F';
  146.                if (Cost[i].Days & DAY_SATURDAY)
  147.                   string[6] = 'S';
  148.                fprintf (fp, "    %s %2d:%02d-%2d:%02d %4d %3d.%d %4d %3d.%d\n",
  149.                            string,
  150.                            Cost[i].Start / 60, Cost[i].Start % 60,
  151.                            Cost[i].Stop / 60, Cost[i].Stop % 60,
  152.                            Cost[i].CostFirst, Cost[i].TimeFirst / 10, Cost[i].TimeFirst % 10,
  153.                            Cost[i].Cost, Cost[i].Time / 10, Cost[i].Time % 10);
  154.             }
  155.          } while (Next () == TRUE);
  156.       fclose (fp);
  157.    }
  158. }
  159.  
  160. USHORT TTranslation::Import (PSZ pszFile)
  161. {
  162.    FILE *fp;
  163.    int i, t1, t2;
  164.    USHORT RetVal = FALSE;
  165.    CHAR Temp[128], *p;
  166.  
  167.    if ((fp = fopen (pszFile, "rt")) != NULL) {
  168.       DeleteAll ();
  169.       RetVal = TRUE;
  170.       while (fgets (Temp, sizeof (Temp) - 1, fp) != NULL) {
  171.          Temp[strlen (Temp) - 1] = '\0';
  172.          if ((p = strtok (Temp, " ")) == NULL)
  173.             continue;
  174.          if (stricmp (p, "Prefix"))
  175.             continue;
  176.  
  177.          New ();
  178.          if ((p = strtok (NULL, " ")) != NULL) {
  179.             strcpy (Search, p);
  180.             if ((p = strtok (NULL, " ")) != NULL) {
  181.                if (strcmp (p, "/"))
  182.                   strcpy (Traslate, p);
  183.             }
  184.             if ((p = strtok (NULL, "\"")) != NULL)
  185.                strcpy (Location, p);
  186.  
  187.             for (i = 0; i < MAXCOST; i++) {
  188.                fgets (Temp, sizeof (Temp) - 1, fp);
  189.                Temp[strlen (Temp) - 1] = '\0';
  190.                if ((p = strtok (Temp, " ")) == NULL)
  191.                   break;
  192.                if (toupper (p[0]) == 'S')
  193.                   Cost[i].Days |= DAY_SUNDAY;
  194.                if (toupper (p[1]) == 'M')
  195.                   Cost[i].Days |= DAY_MONDAY;
  196.                if (toupper (p[2]) == 'T')
  197.                   Cost[i].Days |= DAY_TUESDAY;
  198.                if (toupper (p[3]) == 'W')
  199.                   Cost[i].Days |= DAY_WEDNESDAY;
  200.                if (toupper (p[4]) == 'T')
  201.                   Cost[i].Days |= DAY_THURSDAY;
  202.                if (toupper (p[5]) == 'F')
  203.                   Cost[i].Days |= DAY_FRIDAY;
  204.                if (toupper (p[6]) == 'S')
  205.                   Cost[i].Days |= DAY_SATURDAY;
  206.  
  207.                if ((p = strtok (NULL, " -")) == NULL)
  208.                   break;
  209.                sscanf (p, "%d:%d", &t1, &t2);
  210.                Cost[i].Start = (USHORT)(t1 * 60 + t2);
  211.  
  212.                if ((p = strtok (NULL, " ")) == NULL)
  213.                   break;
  214.                sscanf (p, "%d:%d", &t1, &t2);
  215.                Cost[i].Stop = (USHORT)(t1 * 60 + t2);
  216.  
  217.                if ((p = strtok (NULL, " ")) == NULL)
  218.                   break;
  219.                Cost[i].CostFirst = (USHORT)atoi (p);
  220.                if ((p = strtok (NULL, " ")) == NULL)
  221.                   break;
  222.                if (sscanf (p, "%d.%d", &t1, &t2) == 1)
  223.                   Cost[i].TimeFirst = (USHORT)(t1 * 10);
  224.                else
  225.                   Cost[i].TimeFirst = (USHORT)(t1 * 10 + t2);
  226.  
  227.                if ((p = strtok (NULL, " ")) == NULL)
  228.                   break;
  229.                Cost[i].Cost = (USHORT)atoi (p);
  230.                if ((p = strtok (NULL, " ")) == NULL)
  231.                   break;
  232.                if (sscanf (p, "%d.%d", &t1, &t2) == 1)
  233.                   Cost[i].Time = (USHORT)(t1 * 10);
  234.                else
  235.                   Cost[i].Time = (USHORT)(t1 * 10 + t2);
  236.  
  237.                Add ();
  238.             }
  239.          }
  240.       }
  241.       fclose (fp);
  242.    }
  243.  
  244.    return (RetVal);
  245. }
  246.  
  247. USHORT TTranslation::Next (VOID)
  248. {
  249.    USHORT RetVal = FALSE;
  250.  
  251.    if (fdDat != -1) {
  252.       if (read (fdDat, &table, sizeof (table)) == sizeof (table)) {
  253.          Struct2Class ();
  254.          RetVal = TRUE;
  255.       }
  256.    }
  257.  
  258.    return (RetVal);
  259. }
  260.  
  261. VOID TTranslation::New (VOID)
  262. {
  263.    int i;
  264.  
  265.    Location[0] = '\0';
  266.    Search[0] = '\0';
  267.    Traslate[0] = '\0';
  268.  
  269.    for (i = 0; i < MAXCOST; i++) {
  270.       Cost[i].Days = 0;
  271.       Cost[i].Start = 0;
  272.       Cost[i].Stop = 0;
  273.       Cost[i].CostFirst = 0;
  274.       Cost[i].TimeFirst = 0;
  275.       Cost[i].Cost = 0;
  276.       Cost[i].Time = 0;
  277.    }
  278. }
  279.  
  280. USHORT TTranslation::Read (PSZ pszName)
  281. {
  282.    USHORT RetVal = FALSE, DoClose = FALSE;
  283.  
  284.    if (fdDat == -1) {
  285.       fdDat = open (DataFile, O_RDWR|O_BINARY|O_CREAT, S_IREAD|S_IWRITE);
  286.       DoClose = TRUE;
  287.    }
  288.  
  289.    if (fdDat != -1) {
  290.       lseek (fdDat, 0L, SEEK_SET);
  291.       while (read (fdDat, &table, sizeof (table)) == sizeof (table)) {
  292.          if (!strncmp (table.Search, pszName, strlen (table.Search))) {
  293.             Struct2Class ();
  294.             RetVal = TRUE;
  295.             break;
  296.          }
  297.       }
  298.    }
  299.  
  300.    if (DoClose == TRUE && fdDat != -1) {
  301.       close (fdDat);
  302.       fdDat = -1;
  303.    }
  304.  
  305.    return (RetVal);
  306. }
  307.  
  308. VOID TTranslation::Update (VOID)
  309. {
  310.    if (fdDat != -1) {
  311.       lseek (fdDat, tell (fdDat) - sizeof (table), SEEK_SET);
  312.       Class2Struct ();
  313.       write (fdDat, &table, sizeof (table));
  314.    }
  315. }
  316.  
  317.  
  318.