home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / PROTOCOL.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  6KB  |  250 lines

  1.  
  2. // LoraBBS Version 2.99 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include "_ldefs.h"
  20. #include "lora_api.h"
  21.  
  22. TProtocol::TProtocol (void)
  23. {
  24.    fdDat = -1;
  25.    strcpy (DataFile, "protocol.dat");
  26. }
  27.  
  28. TProtocol::TProtocol (PSZ path)
  29. {
  30.    fdDat = -1;
  31.    strcpy (DataFile, path);
  32.    strcat (DataFile, "protocol.dat");
  33.    AdjustPath (strlwr (DataFile));
  34. }
  35.  
  36. TProtocol::~TProtocol (void)
  37. {
  38.    if (fdDat != -1)
  39.       close (fdDat);
  40. }
  41.  
  42. VOID TProtocol::Struct2Class (PROTOCOL *proto)
  43. {
  44.    strcpy (Key, proto->Key);
  45.    strcpy (Description, proto->Description);
  46.    Active = proto->Active;
  47.    Batch = proto->Batch;
  48.    DisablePort = proto->DisablePort;
  49.    ChangeToUploadPath = proto->ChangeToUploadPath;
  50.    strcpy (DownloadCmd, proto->DownloadCmd);
  51.    strcpy (UploadCmd, proto->UploadCmd);
  52.    strcpy (LogFileName, proto->LogFileName);
  53.    strcpy (CtlFileName, proto->CtlFileName);
  54.    strcpy (DownloadCtlString, proto->DownloadCtlString);
  55.    strcpy (UploadCtlString, proto->UploadCtlString);
  56.    strcpy (DownloadKeyword, proto->DownloadKeyword);
  57.    strcpy (UploadKeyword, proto->UploadKeyword);
  58.    FileNamePos = proto->FileNamePos;
  59.    SizePos = proto->SizePos;
  60.    CpsPos = proto->CpsPos;
  61. }
  62.  
  63. VOID TProtocol::Class2Struct (PROTOCOL *proto)
  64. {
  65.    memset (proto, 0, sizeof (PROTOCOL));
  66.  
  67.    proto->Size = sizeof (PROTOCOL);
  68.    strcpy (proto->Key, Key);
  69.    strcpy (proto->Description, Description);
  70.    proto->Active = Active;
  71.    proto->Batch = Batch;
  72.    proto->DisablePort = DisablePort;
  73.    proto->ChangeToUploadPath = ChangeToUploadPath;
  74.    strcpy (proto->DownloadCmd, DownloadCmd);
  75.    strcpy (proto->UploadCmd, UploadCmd);
  76.    strcpy (proto->LogFileName, LogFileName);
  77.    strcpy (proto->CtlFileName, CtlFileName);
  78.    strcpy (proto->DownloadCtlString, DownloadCtlString);
  79.    strcpy (proto->UploadCtlString, UploadCtlString);
  80.    strcpy (proto->DownloadKeyword, DownloadKeyword);
  81.    strcpy (proto->UploadKeyword, UploadKeyword);
  82.    proto->FileNamePos = FileNamePos;
  83.    proto->SizePos = SizePos;
  84.    proto->CpsPos = CpsPos;
  85. }
  86.  
  87. VOID TProtocol::Add (VOID)
  88. {
  89.    USHORT DoClose = FALSE;
  90.  
  91.    if (fdDat == -1) {
  92.       fdDat = sopen (DataFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE);
  93.       DoClose = TRUE;
  94.    }
  95.  
  96.    if (fdDat != -1) {
  97.       Class2Struct (&prot);
  98.       lseek (fdDat, 0L, SEEK_END);
  99.       write (fdDat, &prot, sizeof (PROTOCOL));
  100.    }
  101.  
  102.    if (fdDat != -1 && DoClose == TRUE) {
  103.       close (fdDat);
  104.       fdDat = -1;
  105.    }
  106. }
  107.  
  108. VOID TProtocol::Delete (VOID)
  109. {
  110.    int fdNew;
  111.    USHORT DoClose = FALSE;
  112.    ULONG Position;
  113.  
  114.    if (fdDat == -1) {
  115.       fdDat = sopen (DataFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE);
  116.       DoClose = TRUE;
  117.    }
  118.  
  119.    fdNew = sopen ("temp.dat", O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE);
  120.  
  121.    if (fdDat != -1 && fdNew != -1) {
  122.       if ((Position = tell (fdDat)) > 0L)
  123.          Position -= sizeof (PROTOCOL);
  124.  
  125.       lseek (fdDat, 0L, SEEK_SET);
  126.  
  127.       while (read (fdDat, &prot, sizeof (PROTOCOL)) == sizeof (PROTOCOL)) {
  128.          if (strcmp (Key, prot.Key))
  129.             write (fdNew, &prot, sizeof (PROTOCOL));
  130.       }
  131.  
  132.       lseek (fdDat, 0L, SEEK_SET);
  133.       lseek (fdNew, 0L, SEEK_SET);
  134.  
  135.       while (read (fdNew, &prot, sizeof (PROTOCOL)) == sizeof (PROTOCOL))
  136.             write (fdDat, &prot, sizeof (PROTOCOL));
  137.  
  138.       chsize (fdDat, tell (fdDat));
  139.  
  140.       lseek (fdDat, Position, SEEK_SET);
  141.       if (Next () == FALSE) {
  142.          if (Previous () == FALSE)
  143.             New ();
  144.       }
  145.    }
  146.  
  147.    if (fdNew != -1) {
  148.       close (fdNew);
  149.       unlink ("temp.dat");
  150.    }
  151.    if (fdDat != -1 && DoClose == TRUE) {
  152.       close (fdDat);
  153.       fdDat = -1;
  154.    }
  155. }
  156.  
  157. USHORT TProtocol::First (VOID)
  158. {
  159.    USHORT retVal = FALSE;
  160.  
  161.    if (fdDat == -1)
  162.       fdDat = sopen (DataFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE);
  163.  
  164.    if (fdDat != -1) {
  165.       lseek (fdDat, 0L, SEEK_SET);
  166.       retVal = Next ();
  167.    }
  168.  
  169.    return (retVal);
  170. }
  171.  
  172. VOID TProtocol::New (VOID)
  173. {
  174.    memset (&prot, 0, sizeof (PROTOCOL));
  175.    Struct2Class (&prot);
  176. }
  177.  
  178. USHORT TProtocol::Next (VOID)
  179. {
  180.    USHORT retVal = FALSE;
  181.  
  182.    if (fdDat != -1) {
  183.       if (read (fdDat, &prot, sizeof (PROTOCOL)) == sizeof (PROTOCOL)) {
  184.          Struct2Class (&prot);
  185.          retVal = TRUE;
  186.       }
  187.    }
  188.  
  189.    return (retVal);
  190. }
  191.  
  192. USHORT TProtocol::Previous (VOID)
  193. {
  194.    USHORT retVal = FALSE;
  195.  
  196.    if (fdDat != -1) {
  197.       if (tell (fdDat) > sizeof (PROTOCOL)) {
  198.          lseek (fdDat, tell (fdDat) - sizeof (PROTOCOL) * 2, SEEK_SET);
  199.          read (fdDat, &prot, sizeof (PROTOCOL));
  200.          Struct2Class (&prot);
  201.          retVal = TRUE;
  202.       }
  203.    }
  204.  
  205.    return (retVal);
  206. }
  207.  
  208. USHORT TProtocol::Read (PSZ key)
  209. {
  210.    USHORT RetVal = FALSE, DoClose = FALSE;
  211.  
  212.    if (fdDat == -1) {
  213.       fdDat = sopen (DataFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE);
  214.       DoClose = TRUE;
  215.    }
  216.  
  217.    if (fdDat != -1) {
  218.       lseek (fdDat, 0L, SEEK_SET);
  219.       while (read (fdDat, &prot, sizeof (PROTOCOL)) == sizeof (PROTOCOL)) {
  220.          if (!stricmp (key, prot.Key)) {
  221.             Struct2Class (&prot);
  222.             RetVal = TRUE;
  223.             break;
  224.          }
  225.       }
  226.    }
  227.  
  228.    if (fdDat != -1 && DoClose == TRUE) {
  229.       close (fdDat);
  230.       fdDat = -1;
  231.    }
  232.  
  233.    return (RetVal);
  234. }
  235.  
  236. VOID TProtocol::Update (VOID)
  237. {
  238.    USHORT retVal = FALSE;
  239.  
  240.    if (fdDat != -1) {
  241.       if (tell (fdDat) >= sizeof (PROTOCOL)) {
  242.          lseek (fdDat, tell (fdDat) - sizeof (PROTOCOL), SEEK_SET);
  243.          Class2Struct (&prot);
  244.          write (fdDat, &prot, sizeof (PROTOCOL));
  245.       }
  246.    }
  247. }
  248.  
  249.  
  250.