home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / MENU.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  10KB  |  381 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 "menu.h"
  21.  
  22. TMenu::TMenu (void)
  23. {
  24.    strcpy (Path, "");
  25.    strcpy (AltPath, "");
  26.    Items.Clear ();
  27. }
  28.  
  29. TMenu::~TMenu (void)
  30. {
  31.    Items.Clear ();
  32. }
  33.  
  34. VOID TMenu::Add (VOID)
  35. {
  36.    memset (&Item, 0, sizeof (Item));
  37.  
  38.    strcpy (Item.Display, Display);
  39.    Item.Color = Color;
  40.    Item.Hilight = Hilight;
  41.    strcpy (Item.Key, Key);
  42.    Item.Command = Command;
  43.    strcpy (Item.Argument, Argument);
  44.    Item.Level = Level;
  45.    Item.AccessFlags = AccessFlags;
  46.    Item.DenyFlags = DenyFlags;
  47.    Item.Automatic = Automatic;
  48.    Item.FirstTime = FirstTime;
  49.  
  50.    Items.Add (&Item, sizeof (Item));
  51. }
  52.  
  53. USHORT TMenu::Check (PSZ pszKey)
  54. {
  55.    USHORT RetVal = FALSE;
  56.    ITEM *Current;
  57.  
  58.    if ((Current = (ITEM *)Items.First ()) != NULL)
  59.       do {
  60.          if (!stricmp (Current->Key, pszKey)) {
  61.             strcpy (Display, Current->Display);
  62.             Color = Current->Color;
  63.             Hilight = Current->Hilight;
  64.             strcpy (Key, Current->Key);
  65.             Command = Current->Command;
  66.             strcpy (Argument, Current->Argument);
  67.             Level = Current->Level;
  68.             AccessFlags = Current->AccessFlags;
  69.             DenyFlags = Current->DenyFlags;
  70.             Automatic = Current->Automatic;
  71.             FirstTime = Current->FirstTime;
  72.             RetVal = TRUE;
  73.          }
  74.       } while (RetVal == FALSE && (Current = (ITEM *)Items.Next ()) != NULL);
  75.  
  76.    return (RetVal);
  77. }
  78.  
  79. VOID TMenu::Delete (VOID)
  80. {
  81.    ITEM *Current;
  82.  
  83.    Items.Remove ();
  84.    if (Items.Value () == NULL)
  85.       New ();
  86.  
  87.    if ((Current = (ITEM *)Items.Value ()) != NULL) {
  88.       strcpy (Display, Current->Display);
  89.       Color = Current->Color;
  90.       Hilight = Current->Hilight;
  91.       strcpy (Key, Current->Key);
  92.       Command = Current->Command;
  93.       strcpy (Argument, Current->Argument);
  94.       Level = Current->Level;
  95.       AccessFlags = Current->AccessFlags;
  96.       DenyFlags = Current->DenyFlags;
  97.       Automatic = Current->Automatic;
  98.       FirstTime = Current->FirstTime;
  99.    }
  100. }
  101.  
  102. USHORT TMenu::First (VOID)
  103. {
  104.    USHORT RetVal = FALSE;
  105.    ITEM *Current;
  106.  
  107.    if ((Current = (ITEM *)Items.First ()) != NULL) {
  108.       strcpy (Display, Current->Display);
  109.       Color = Current->Color;
  110.       Hilight = Current->Hilight;
  111.       strcpy (Key, Current->Key);
  112.       Command = Current->Command;
  113.       strcpy (Argument, Current->Argument);
  114.       Level = Current->Level;
  115.       AccessFlags = Current->AccessFlags;
  116.       DenyFlags = Current->DenyFlags;
  117.       Automatic = Current->Automatic;
  118.       FirstTime = Current->FirstTime;
  119.       RetVal = TRUE;
  120.    }
  121.  
  122.    return (RetVal);
  123. }
  124.  
  125. VOID TMenu::Insert (VOID)
  126. {
  127.    memset (&Item, 0, sizeof (Item));
  128.  
  129.    strcpy (Item.Display, Display);
  130.    Item.Color = Color;
  131.    Item.Hilight = Hilight;
  132.    strcpy (Item.Key, Key);
  133.    Item.Command = Command;
  134.    strcpy (Item.Argument, Argument);
  135.    Item.Level = Level;
  136.    Item.AccessFlags = AccessFlags;
  137.    Item.DenyFlags = DenyFlags;
  138.    Item.Automatic = Automatic;
  139.    Item.FirstTime = FirstTime;
  140.  
  141. //   if (Items.Previous () != NULL)
  142.       Items.Insert (&Item, sizeof (Item));
  143. //   else {
  144. //      Current = (ITEM *)Items.Value ();
  145. //      Items.Insert (&Item, sizeof (Item));
  146. //      if (Current != NULL) {
  147. //         Items.Insert (Current, sizeof (Item));
  148. //         Items.First ();
  149. //         Items.Remove ();
  150. //      }
  151. //   }
  152. }
  153.  
  154. USHORT TMenu::Load (PSZ pszName, USHORT fAppend)
  155. {
  156.    int fd;
  157.    USHORT RetVal = FALSE;
  158.    CHAR Temp[128];
  159.    ITEM *Current = NULL;
  160.  
  161.    if (fAppend == TRUE)
  162.       Current = (ITEM *)Items.Value ();
  163.  
  164.    if (AltPath[0] != '\0') {
  165.       sprintf (Temp, "%s%s", AltPath, pszName);
  166.       if (strstr (strupr (Temp), ".MNU") == NULL)
  167.          strcat (Temp, ".MNU");
  168.  
  169.       AdjustPath (Temp);
  170.       if ((fd = sopen (strlwr (Temp), O_RDONLY|O_BINARY, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  171.          if (read (fd, &Hdr, sizeof (Hdr)) == sizeof (Hdr)) {
  172.             if (fAppend == FALSE) {
  173.                Items.Clear ();
  174.                New ();
  175.                strcpy (Name, Hdr.MenuName);
  176.                strcpy (Prompt, Hdr.Prompt);
  177.                PromptColor = Hdr.Color;
  178.                PromptHilight = Hdr.Hilight;
  179.             }
  180.             while (read (fd, &Item, sizeof (Item)) == sizeof (Item)) {
  181.                if (fAppend == TRUE)
  182.                   Items.Insert (&Item, sizeof (Item));
  183.                else
  184.                   Items.Add (&Item, sizeof (Item));
  185.             }
  186.             RetVal = TRUE;
  187.          }
  188.          close (fd);
  189.       }
  190.    }
  191.  
  192.    if (RetVal == FALSE) {
  193.       sprintf (Temp, "%s%s", Path, pszName);
  194.       if (strstr (strupr (Temp), ".MNU") == NULL)
  195.          strcat (Temp, ".MNU");
  196.  
  197.       AdjustPath (Temp);
  198.       if ((fd = sopen (strlwr (Temp), O_RDONLY|O_BINARY, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  199.          if (read (fd, &Hdr, sizeof (Hdr)) == sizeof (Hdr)) {
  200.             if (fAppend == FALSE) {
  201.                Items.Clear ();
  202.                New ();
  203.                strcpy (Name, Hdr.MenuName);
  204.                strcpy (Prompt, Hdr.Prompt);
  205.                PromptColor = Hdr.Color;
  206.                PromptHilight = Hdr.Hilight;
  207.             }
  208.             while (read (fd, &Item, sizeof (Item)) == sizeof (Item)) {
  209.                if (fAppend == TRUE)
  210.                   Items.Insert (&Item, sizeof (Item));
  211.                else
  212.                   Items.Add (&Item, sizeof (Item));
  213.             }
  214.             RetVal = TRUE;
  215.          }
  216.          close (fd);
  217.       }
  218.    }
  219.  
  220.    if (RetVal == TRUE) {
  221.       if (fAppend == TRUE) {
  222.          if (Items.First () != NULL)
  223.             do {
  224.                if (Items.Value () == Current)
  225.                   break;
  226.             } while (Items.Next () != NULL);
  227.       }
  228.       else
  229.          Current = (ITEM *)Items.First ();
  230.  
  231.       if (Current != NULL) {
  232.          strcpy (Display, Current->Display);
  233.          Color = Current->Color;
  234.          Hilight = Current->Hilight;
  235.          strcpy (Key, Current->Key);
  236.          Command = Current->Command;
  237.          strcpy (Argument, Current->Argument);
  238.          Level = Current->Level;
  239.          AccessFlags = Current->AccessFlags;
  240.          DenyFlags = Current->DenyFlags;
  241.          Automatic = Current->Automatic;
  242.          FirstTime = Current->FirstTime;
  243.       }
  244.    }
  245.  
  246.    return (RetVal);
  247. }
  248.  
  249. VOID TMenu::New (USHORT usNewPrompt)
  250. {
  251.    memset (Display, 0, sizeof (Display));
  252.    Color = 7;
  253.    Hilight = 14;
  254.    memset (Key, 0, sizeof (Key));
  255.    Command = 0;
  256.    memset (Argument, 0, sizeof (Argument));
  257.    Level = 0;
  258.    AccessFlags = 0L;
  259.    DenyFlags = 0L;
  260.    FirstTime = Automatic = FALSE;
  261.  
  262.    if (usNewPrompt == TRUE) {
  263.       memset (Name, 0, sizeof (Name));
  264.       memset (Prompt, 0, sizeof (Prompt));
  265.       PromptColor = 14;
  266.       PromptHilight = 14;
  267.    }
  268. }
  269.  
  270. USHORT TMenu::Next (VOID)
  271. {
  272.    USHORT RetVal = FALSE;
  273.    ITEM *Current;
  274.  
  275.    if ((Current = (ITEM *)Items.Next ()) != NULL) {
  276.       strcpy (Display, Current->Display);
  277.       Color = Current->Color;
  278.       Hilight = Current->Hilight;
  279.       strcpy (Key, Current->Key);
  280.       Command = Current->Command;
  281.       strcpy (Argument, Current->Argument);
  282.       Level = Current->Level;
  283.       AccessFlags = Current->AccessFlags;
  284.       DenyFlags = Current->DenyFlags;
  285.       Automatic = Current->Automatic;
  286.       FirstTime = Current->FirstTime;
  287.       RetVal = TRUE;
  288.    }
  289.  
  290.    return (RetVal);
  291. }
  292.  
  293. USHORT TMenu::Previous (VOID)
  294. {
  295.    USHORT RetVal = FALSE;
  296.    ITEM *Current;
  297.  
  298.    if ((Current = (ITEM *)Items.Previous ()) != NULL) {
  299.       strcpy (Display, Current->Display);
  300.       Color = Current->Color;
  301.       Hilight = Current->Hilight;
  302.       strcpy (Key, Current->Key);
  303.       Command = Current->Command;
  304.       strcpy (Argument, Current->Argument);
  305.       Level = Current->Level;
  306.       AccessFlags = Current->AccessFlags;
  307.       DenyFlags = Current->DenyFlags;
  308.       Automatic = Current->Automatic;
  309.       FirstTime = Current->FirstTime;
  310.       RetVal = TRUE;
  311.    }
  312.  
  313.    return (RetVal);
  314. }
  315.  
  316. USHORT TMenu::Save (PSZ pszName)
  317. {
  318.    int fd;
  319.    USHORT RetVal = FALSE;
  320.    CHAR Temp[128];
  321.    ITEM *Current, *Saved;
  322.  
  323.    if (AltPath[0] != '\0')
  324.       sprintf (Temp, "%s%s", AltPath, pszName);
  325.    else
  326.       sprintf (Temp, "%s%s", Path, pszName);
  327.    if (strstr (strupr (Temp), ".MNU") == NULL)
  328.       strcat (Temp, ".MNU");
  329.  
  330.    AdjustPath (Temp);
  331.    if ((fd = sopen (strlwr (Temp), O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  332.       memset (&Hdr, 0, sizeof (Hdr));
  333.       strcpy (Hdr.MenuName, Name);
  334.       strcpy (Hdr.Prompt, Prompt);
  335.       Hdr.Color = PromptColor;
  336.       Hdr.Hilight = PromptHilight;
  337.       write (fd, &Hdr, sizeof (Hdr));
  338.  
  339.       Saved = (ITEM *)Items.Value ();
  340.  
  341.       if ((Current = (ITEM *)Items.First ()) != NULL)
  342.          do {
  343.             write (fd, Current, sizeof (ITEM));
  344.          } while ((Current = (ITEM *)Items.Next ()) != NULL);
  345.  
  346.       close (fd);
  347.  
  348.       if (Items.First () != NULL)
  349.          do {
  350.             if ((ITEM *)Items.Value () == Saved)
  351.                break;
  352.          } while (Items.Next () != NULL);
  353.  
  354.       RetVal = TRUE;
  355.    }
  356.  
  357.    return (RetVal);
  358. }
  359.  
  360. VOID TMenu::Update (VOID)
  361. {
  362.    USHORT RetVal = FALSE;
  363.    ITEM *Current;
  364.  
  365.    if ((Current = (ITEM *)Items.Value ()) != NULL) {
  366.       strcpy (Current->Display, Display);
  367.       Current->Color = Color;
  368.       Current->Hilight = Hilight;
  369.       strcpy (Current->Key, Key);
  370.       Current->Command = Command;
  371.       strcpy (Current->Argument, Argument);
  372.       Current->Level = Level;
  373.       Current->AccessFlags = AccessFlags;
  374.       Current->DenyFlags = DenyFlags;
  375.       Current->Automatic = Automatic;
  376.       Current->FirstTime = FirstTime;
  377.    }
  378. }
  379.  
  380.  
  381.