home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / OKFILE.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  4KB  |  167 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. TOkFile::TOkFile (void)
  23. {
  24.    fdDat = -1;
  25.    strcpy (DataFile, "okfile.dat");
  26. }
  27.  
  28. TOkFile::TOkFile (PSZ pszDataPath)
  29. {
  30.    fdDat = -1;
  31.    strcpy (DataFile, pszDataPath);
  32.    strcat (DataFile, "okfile.dat");
  33.    AdjustPath (strlwr (DataFile));
  34. }
  35.  
  36. TOkFile::~TOkFile (void)
  37. {
  38.    if (fdDat != -1)
  39.       close (fdDat);
  40. }
  41.  
  42. VOID TOkFile::Add (VOID)
  43. {
  44.    USHORT DoClose = FALSE;
  45.    OKFILE ok;
  46.  
  47.    if (fdDat == -1) {
  48.       fdDat = open (DataFile, O_RDWR|O_BINARY|O_CREAT, S_IREAD|S_IWRITE);
  49.       DoClose = TRUE;
  50.    }
  51.  
  52.    if (fdDat != -1) {
  53.       memset (&ok, 0, sizeof (ok));
  54.       ok.Size = sizeof (ok);
  55.       strcpy (ok.Name, Name);
  56.       strcpy (ok.Path, Path);
  57.       strcpy (ok.Pwd, Pwd);
  58.       ok.Normal = Normal;
  59.       ok.Known = Known;
  60.       ok.Protected = Protected;
  61.  
  62.       lseek (fdDat, 0L, SEEK_END);
  63.       write (fdDat, &ok, sizeof (ok));
  64.    }
  65.  
  66.    if (DoClose == TRUE && fdDat != -1) {
  67.       close (fdDat);
  68.       fdDat = -1;
  69.    }
  70. }
  71.  
  72. VOID TOkFile::DeleteAll (VOID)
  73. {
  74.    if (fdDat != -1) {
  75.       close (fdDat);
  76.       fdDat = -1;
  77.    }
  78.  
  79.    unlink (DataFile);
  80. }
  81.  
  82. USHORT TOkFile::First (VOID)
  83. {
  84.    if (fdDat == -1)
  85.       fdDat = open (DataFile, O_RDWR|O_BINARY|O_CREAT, S_IREAD|S_IWRITE);
  86.  
  87.    if (fdDat != -1)
  88.       lseek (fdDat, 0L, SEEK_SET);
  89.  
  90.    return (Next ());
  91. }
  92.  
  93. USHORT TOkFile::Next (VOID)
  94. {
  95.    USHORT RetVal = FALSE;
  96.    OKFILE ok;
  97.  
  98.    if (fdDat != -1) {
  99.       if (read (fdDat, &ok, sizeof (ok)) == sizeof (ok)) {
  100.          strcpy (Name, ok.Name);
  101.          strcpy (Path, ok.Path);
  102.          strcpy (Pwd, ok.Pwd);
  103.          Normal = ok.Normal;
  104.          Known = ok.Known;
  105.          Protected = ok.Protected;
  106.  
  107.          RetVal = TRUE;
  108.       }
  109.    }
  110.  
  111.    return (RetVal);
  112. }
  113.  
  114. USHORT TOkFile::Read (PSZ pszName)
  115. {
  116.    USHORT RetVal = FALSE, DoClose = FALSE;
  117.    OKFILE ok;
  118.  
  119.    if (fdDat == -1) {
  120.       fdDat = open (DataFile, O_RDWR|O_BINARY|O_CREAT, S_IREAD|S_IWRITE);
  121.       DoClose = TRUE;
  122.    }
  123.  
  124.    if (fdDat != -1) {
  125.       lseek (fdDat, 0L, SEEK_SET);
  126.       while (read (fdDat, &ok, sizeof (ok)) == sizeof (ok)) {
  127.          if (!stricmp (ok.Name, pszName)) {
  128.             strcpy (Name, ok.Name);
  129.             strcpy (Path, ok.Path);
  130.             strcpy (Pwd, ok.Pwd);
  131.             Normal = ok.Normal;
  132.             Known = ok.Known;
  133.             Protected = ok.Protected;
  134.  
  135.             RetVal = TRUE;
  136.             break;
  137.          }
  138.       }
  139.    }
  140.  
  141.    if (DoClose == TRUE && fdDat != -1) {
  142.       close (fdDat);
  143.       fdDat = -1;
  144.    }
  145.  
  146.    return (RetVal);
  147. }
  148.  
  149. VOID TOkFile::Update (VOID)
  150. {
  151.    OKFILE ok;
  152.  
  153.    if (fdDat != -1) {
  154.       lseek (fdDat, tell (fdDat) - sizeof (ok), SEEK_SET);
  155.       ok.Size = sizeof (ok);
  156.       strcpy (ok.Name, Name);
  157.       strcpy (ok.Path, Path);
  158.       strcpy (ok.Pwd, Pwd);
  159.       ok.Normal = Normal;
  160.       ok.Known = Known;
  161.       ok.Protected = Protected;
  162.       write (fdDat, &ok, sizeof (ok));
  163.    }
  164. }
  165.  
  166.  
  167.