home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / STATS.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  5KB  |  179 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. TStatistics::TStatistics (void)
  23. {
  24.    strcpy (DataFile, "stats.dat");
  25. }
  26.  
  27. TStatistics::TStatistics (PSZ pszDataPath)
  28. {
  29.    strcpy (DataFile, pszDataPath);
  30.    if (DataFile[0] != '\0') {
  31. #if defined(__LINUX__)
  32.       if (DataFile[strlen (DataFile) - 1] != '/')
  33.          strcat (DataFile, "/");
  34. #else
  35.       if (DataFile[strlen (DataFile) - 1] != '\\')
  36.          strcat (DataFile, "\\");
  37. #endif
  38.    }
  39.    strcat (DataFile, "stats.dat");
  40. }
  41.  
  42. TStatistics::~TStatistics (void)
  43. {
  44. }
  45.  
  46. USHORT TStatistics::First (VOID)
  47. {
  48.    LastTask = 0;
  49.    return (Next ());
  50. }
  51.  
  52. USHORT TStatistics::Next (VOID)
  53. {
  54.    int fd;
  55.    USHORT RetVal = FALSE;
  56.  
  57.    memset (&Sys, 0, sizeof (Sys));
  58.  
  59.    if ((fd = sopen (DataFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  60.       read (fd, &Sys, sizeof (Sys));
  61.       while (read (fd, &Line, sizeof (Line)) == sizeof (Line)) {
  62.          if (Line.Number > LastTask) {
  63.             RetVal = TRUE;
  64.             break;
  65.          }
  66.       }
  67.       close (fd);
  68.    }
  69.  
  70.    if (RetVal == TRUE) {
  71.       LineNumber = Line.Number;
  72.       Status = Line.Status;
  73.       strcpy (User, Line.User);
  74.       strcpy (From, Line.From);
  75.       strcpy (Action, Line.Action);
  76.       strcpy (LineLastCaller, Line.LastCaller);
  77.       if (LineLastCaller[0] == '\0')
  78.          strcpy (LineLastCaller, "None");
  79.       Calls = Line.Calls;
  80.       MailCalls = Line.MailCalls;
  81.       TodayCalls = Line.TodayCalls;
  82.  
  83.       LastTask = LineNumber;
  84.    }
  85.  
  86.    return (RetVal);
  87. }
  88.  
  89. VOID TStatistics::Read (USHORT usLine)
  90. {
  91.    int fd;
  92.    USHORT Found = FALSE;
  93.  
  94.    memset (&Sys, 0, sizeof (Sys));
  95.  
  96.    if ((fd = sopen (DataFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  97.       read (fd, &Sys, sizeof (Sys));
  98.       while (read (fd, &Line, sizeof (Line)) == sizeof (Line)) {
  99.          if (Line.Number == usLine) {
  100.             Found = TRUE;
  101.             break;
  102.          }
  103.       }
  104.       close (fd);
  105.    }
  106.  
  107.    strcpy (LastCaller, Sys.LastCaller);
  108.    if (LastCaller[0] == '\0')
  109.       strcpy (LastCaller, "None");
  110.    TotalCalls = Sys.Calls;
  111.    TotalMailCalls = Sys.MailCalls;
  112.    TotalTodayCalls = Sys.TodayCalls;
  113.  
  114.    if (Found == TRUE) {
  115.       LineNumber = Line.Number;
  116.       Status = Line.Status;
  117.       strcpy (User, Line.User);
  118.       strcpy (From, Line.From);
  119.       strcpy (Action, Line.Action);
  120.       strcpy (LineLastCaller, Line.LastCaller);
  121.       if (LineLastCaller[0] == '\0')
  122.          strcpy (LineLastCaller, "None");
  123.       Calls = Line.Calls;
  124.       MailCalls = Line.MailCalls;
  125.       TodayCalls = Line.TodayCalls;
  126.    }
  127.    else {
  128.       LineNumber = usLine;
  129.       Status = STAT_OFFLINE;
  130.       strcpy (User, "None");
  131.       Action[0] = From[0] = '\0';
  132.       strcpy (LineLastCaller, "None");
  133.       Calls = 0L;
  134.       MailCalls = 0L;
  135.       TodayCalls = 0L;
  136.    }
  137. }
  138.  
  139. VOID TStatistics::Update (VOID)
  140. {
  141.    int fd;
  142.    USHORT Found = FALSE;
  143.  
  144.    memset (&Sys, 0, sizeof (Sys));
  145.  
  146.    strcpy (Sys.LastCaller, LastCaller);
  147.    Sys.Calls = TotalCalls;
  148.    Sys.MailCalls = TotalMailCalls;
  149.    Sys.TodayCalls = TotalTodayCalls;
  150.  
  151.    if ((fd = sopen (DataFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  152.       write (fd, &Sys, sizeof (Sys));
  153.       while (read (fd, &Line, sizeof (Line)) == sizeof (Line)) {
  154.          if (Line.Number == LineNumber) {
  155.             Found = TRUE;
  156.             break;
  157.          }
  158.       }
  159.  
  160.       memset (&Line, 0, sizeof (Line));
  161.  
  162.       Line.Number = LineNumber;
  163.       strcpy (Line.LastCaller, LineLastCaller);
  164.       Line.Calls = Calls;
  165.       Line.MailCalls = MailCalls;
  166.       Line.TodayCalls = TodayCalls;
  167.       Line.Status = Status;
  168.       strcpy (Line.User, User);
  169.       strcpy (Line.From, From);
  170.       strcpy (Line.Action, Action);
  171.  
  172.       if (Found == TRUE)
  173.          lseek (fd, tell (fd) - sizeof (Line), SEEK_SET);
  174.       write (fd, &Line, sizeof (Line));
  175.  
  176.       close (fd);
  177.    }
  178. }
  179.