home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / UTILS / DLGDS4 / READSCPT.CPP < prev    next >
C/C++ Source or Header  |  1993-08-06  |  8KB  |  345 lines

  1. #define Uses_TStringCollection
  2. #include <tv.h>
  3. #include <stdlib.h>
  4. #include <dos.h>
  5. #include <dir.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <iostream.h>
  9. #include <fstream.h>
  10. #include <stdio.h>
  11. #include "readscpt.h"
  12.  
  13. #pragma warn -sig
  14.  
  15. #define MAXBUFF 350
  16. char *ch, buff[MAXBUFF];
  17. int lineNo;
  18. ifstream *inf;
  19. TNSCollection *ScriptColl;   //holds all the controls read
  20. ViewObj *Dialog;             //hols the dialog itself
  21. Boolean present[ScrollB + 1];  //tells which Kinds are present
  22. TStringCollection *classCollection;  //a collection of all the class names
  23.  
  24. void getch();
  25. long getNumber();
  26. char* getString();       //never NULL
  27. void skipWhiteSpace();
  28. void error(const char *S);
  29.  
  30. ViewObj::ViewObj()
  31. //the base struct constructor.  Read all the field common to all controls and dialog.
  32. {
  33.  BaseObj = getString();
  34.  Obj = getString();
  35.  X1 = getNumber();
  36.  Y1 = getNumber();
  37.  X2 = getNumber();
  38.  Y2 = getNumber();
  39.  DefOptns = getNumber() & ~0x1000; //make sure version 2 bit isn't present
  40.  Optns = getNumber() & ~0x1000;
  41.  DefEvMsk = getNumber();
  42.  EvMsk = getNumber();
  43.  HCtx = getNumber();
  44.  Grow = getNumber();
  45.  for (int i = 0; i < MAXPARAM; i++)
  46.    Param[i] = getString();
  47.  HelpCtxSym = getString();
  48.  FieldName = getString();
  49.  VarName = getString();
  50. }
  51.  
  52. DialogObj::DialogObj() : ViewObj()
  53. //dialog constructor. read fields special to the dialog.
  54. {
  55.  Palette  = getNumber();
  56.  WinFlags  = getNumber();
  57.  DlgFuncName  = getString();
  58.  KeyString = getString();
  59.  Title  = getString();
  60. }
  61.  
  62. ButtonObj::ButtonObj() : ViewObj()
  63. {
  64.  CommandName = getString();
  65.  ButtonText = getString();
  66.  CommandValue = getNumber();
  67.  Flags = getNumber();
  68. }
  69.  
  70. LabelObj::LabelObj() : ViewObj()
  71. {
  72.   LabelText = getString();
  73.   LinkName = getString();
  74. }
  75.  
  76. StaticTextObj::StaticTextObj() : ViewObj()
  77. {
  78.   Attrib = getNumber();
  79.   Text = getString();
  80. }
  81.  
  82. HistoryObj::HistoryObj() : ViewObj()
  83. {
  84.   HistoryID = getNumber();
  85.   HistoryLink = getString();
  86. }
  87.  
  88. InputLongObj::InputLongObj() : ViewObj()
  89. {
  90.   LongLabelText = getString();
  91.   LongStrLeng = getNumber();
  92.   LLim = getNumber();
  93.   ULim = getNumber();
  94.   ILOptions = getNumber();
  95. }
  96.  
  97. ListBoxObj::ListBoxObj() : ViewObj()
  98. {
  99.   Columns = getNumber();
  100.   ScrollBar = getString();
  101. }
  102.  
  103. MemoObj::MemoObj() : ViewObj()
  104. {
  105.   TextFieldName = getString();
  106.   BufSize = getNumber();
  107.   VScroll = getString();
  108.   HScroll = getString();
  109. }
  110.  
  111. ClusterObj::ClusterObj() : ViewObj()
  112. { int i;
  113.   Items = getNumber();
  114.   Mask = getNumber();
  115.   if (Items > 0) {
  116.     LabelColl = new TStringCollection(10,10);
  117.     for (i = 0; i < Items; i++)
  118.       LabelColl->atInsert(i, getString());   //entered in received order.
  119.                                              //prevent sorting
  120.     }
  121. }
  122. MultiCheckBoxObj::MultiCheckBoxObj() : ClusterObj()
  123. {
  124.   MCBFlags = getNumber();
  125.   SelRange = getNumber();
  126.   States = getString();
  127. }
  128.  
  129.  
  130.      PictureValidatorObj::PictureValidatorObj() : ValidatorObj()
  131.      {
  132.        AutoFill = getNumber();
  133.        PictureString = getString();
  134.      }
  135.  
  136.      RangeValidatorObj::RangeValidatorObj() : ValidatorObj()
  137.      {
  138.        LowLim = getNumber();
  139.        UpLim = getNumber();
  140.        Transfer = getNumber();
  141.      }
  142.  
  143.      FilterValidatorObj::FilterValidatorObj() : ValidatorObj()
  144.      {
  145.        CharSet = getString();
  146.        for (int i = 0; i <= 7; i++)
  147.          ActualCharSet[i] = getNumber();
  148.      }
  149.  
  150.      StringLookupValidatorObj::StringLookupValidatorObj() : ValidatorObj()
  151.      {
  152.       List = getString();
  153.      }
  154.  
  155. InputLineObj::InputLineObj() : ViewObj()
  156. {
  157.   StringLeng = getNumber();
  158.   valType ValKind = (valType)getNumber();
  159.   ValPtrName = getString();
  160.   switch (ValKind)  {
  161.     case Picture: val = new PictureValidatorObj(); break;
  162.     case Range:   val = new RangeValidatorObj();   break;
  163.     case Filter:  val = new FilterValidatorObj();  break;
  164.     case StringLookup: val = new StringLookupValidatorObj(); break;
  165.     }
  166. }
  167.  
  168. char* myNewStr(const char* S)
  169. //like newStr but never returns a NULL pointer
  170. {
  171.  char *P;
  172.  if (S[0] == '\0') {
  173.     P = new char[ 1 ];     //kind of silly, but saves a lot of testing here
  174.     *P = '\0';
  175.    }
  176.  else P = newStr(S);
  177.  return P;
  178. }
  179.  
  180. void getch()    //read next character in script file
  181. {
  182.  if (*ch == '\0') {     //need to read a line
  183.     if (!inf->eof()) {
  184.       inf->getline(buff, 255);
  185.       lineNo++;
  186.       ch = buff;
  187.       }
  188.     else error("Unexpected end of file");
  189.    }
  190.  else ch++;
  191. }
  192.  
  193. void spaces(int n)
  194. {for (int i = 1; i <= n; i++)
  195.   cout << ' ';
  196. }
  197.  
  198. void error(const char *S)  //handles error reporting
  199. {
  200.  short X;
  201.  char newS[80], tmp[20];
  202.  
  203.  cout << buff << endl;
  204.  X = ch-buff-1;
  205.  if (X < 1) X = 1;
  206.  strcpy(newS, "Line ");
  207.  itoa(lineNo, tmp, 10);
  208.  strcat(newS, tmp);
  209.  strcat(newS, " Error, ");
  210.  strncat(newS, S, 79-strlen(newS));
  211.  if (X > strlen(newS)) {
  212.    spaces(X-strlen(newS)-1);
  213.    cout << newS << '^' <<endl;
  214.    }
  215.  else {
  216.    spaces(X-1);
  217.    cout << '^' << newS << endl;
  218.    }
  219.  inf->close();
  220.  exit(1);
  221. }
  222.  
  223. void checkMemory()
  224. {
  225.  if (lowMemory()) {
  226.    cout << "Out of memory\n";
  227.    exit(1);
  228.    }
  229. }
  230.  
  231. void skipWhiteSpace()
  232. {
  233.  char c = *ch;
  234.  while (c == ' ' || c == '\t' || c == '\0')  {
  235.    getch();
  236.    c = *ch;
  237.    }
  238. }
  239.  
  240. char* getString()
  241. //reads a string in double quotes "like this \"one\"".  Never returns NULL
  242. {
  243.  char S[MAXBUFF] = "";
  244.  int i = 0;
  245.  skipWhiteSpace();
  246.  if (*ch != '\"')
  247.    error("Quoted string expected");
  248.  getch();
  249.  while ((*ch != '\"' || ch[1] == '+') && i < MAXBUFF-3) {
  250.    if (ch[0] == '\\' && ch[1] =='\"') {
  251.      S[i++] = '\\';
  252.      S[i++] = '\"';
  253.      getch();        //use up the extra character
  254.      }
  255.    else if (ch[0] == '\"' && ch[1] == '+') {  // a string continuation
  256.      getch();  //skip '"'
  257.      getch();  //skip '+'
  258.      skipWhiteSpace();
  259.      if (*ch != '\"')
  260.         error("Quoted string continuation expected");
  261.      }
  262.    else S[i++] = *ch;  //Normal case
  263.    getch();
  264.    }
  265.  getch();    //use up last "
  266.  S[i] = '\0';
  267.  return myNewStr(S);  //getString is never NULL
  268. }
  269.  
  270. long getNumber()  //reads a decimal number
  271. {
  272.  char S[20] = "";
  273.  int i = 0;
  274.  skipWhiteSpace();
  275.  if (*ch == '-') {
  276.    S[i++] = '-';
  277.    getch();
  278.    }
  279.  if (*ch < '0' || *ch > '9')
  280.    error("Number expected");
  281.  while (*ch >= '0' && *ch <= '9' && i < 20) {
  282.    S[i++] = *ch;
  283.    getch();
  284.    }
  285.  return atol(S);
  286. }
  287.  
  288. void readScriptFile(char* scriptName)
  289. {
  290.  inf = new ifstream(scriptName);
  291.  if (!inf->good()) {
  292.    cout << "Can't open script file, " << scriptName << endl;
  293.    inf->close();
  294.    exit(1);
  295.    }
  296.  
  297.  char tmp[10];
  298.  inf->getline(tmp, 10);
  299.  if (strcmp(VersionID, tmp) != 0) {
  300.    cout << scriptName << " is not a valid script file\n";
  301.    inf->close();
  302.    exit(1);
  303.    }
  304.  
  305.  lineNo = 1;
  306.  buff[0] = '\0';  //start the reading
  307.  ch = buff;
  308.  getch();
  309.  getString();    // reserved
  310.  getNumber();    //Field number--of no use here
  311.  
  312.  ScriptColl = new TNSCollection(10,10); //start collection for the controls
  313.  classCollection = new TStringCollection(10,10);
  314.  
  315.  recType Kind;
  316.  
  317.  Kind = (recType)getNumber();
  318.  if (Kind != Dlg)
  319.    error("First item is not TDialog type");
  320.  Dialog = getKind(Kind);
  321.  classCollection->insert(newStr(Dialog->Obj));
  322.  present[Kind] = True;
  323.  skipWhiteSpace;
  324.  
  325.  ViewObj *P;
  326.  Kind = (recType)getNumber();
  327.  while (Kind != Done)  {
  328.    checkMemory();
  329.    //getKind must be defined elsewhere.  It returns the final ViewObj extension
  330.    //appropriate for Kind.
  331.    P = getKind(Kind);
  332.    if (P == 0)
  333.      error("Unrecognized control type");
  334.    ScriptColl->insert(P);
  335.    ccIndex i;
  336.    if (!classCollection->search(P->Obj, i))
  337.        classCollection->insert(newStr(P->Obj));
  338.    present[Kind] = True;   //keep track of which types are present
  339.    skipWhiteSpace();
  340.    Kind = (recType)getNumber();
  341.    }
  342. }
  343.  
  344.  
  345.