home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 12 / CD_ASCQ_12_0294.iso / maj / 535 / cppsrc1.cpp < prev    next >
C/C++ Source or Header  |  1994-01-07  |  15KB  |  591 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. ofstream outf;
  14. char DlgName[50];   //holds the dialog varible name for easy access
  15. Boolean needcontrol1;  //set if need control1 variable name
  16.  
  17. char* quoted(const char* s)
  18. //if the first char of s is '@', assumes a variable wanted and strips the '@'
  19. //else returns a double quoted string or 0 for empty string
  20. {
  21.  static char q[300];
  22.  if (s[0] == '@')
  23.    {strcpy(q, &s[1]);
  24.     return q;
  25.    }
  26.  strcpy(q, "\"");   //the first quote in place
  27.  if (*s == '\0' || !s) return "0";
  28.  strcat(q, s);
  29.  short l = strlen(q);
  30.  q[l] = '\"';
  31.  q[l+1] = '\0';
  32.  return q;
  33. }
  34.  
  35. void aField(ViewObj *P, char* fieldtype)
  36. {
  37.  outf << "  " << fieldtype << " " << P->FieldName << ";   //" << P->Obj <<endl;
  38. }
  39.  
  40. void aVar(ViewObj *P)
  41. {
  42.  if (strcmp(P->VarName, "control") != 0)
  43.    outf << P->Obj << " *" << P->VarName << ";\n";
  44. }
  45.  
  46. //the following extend the ViewObj struct's to also write the code
  47. struct DialogWriteObj : DialogObj {
  48.      virtual void writeCode();
  49.      };
  50.  
  51. struct ButtonWriteObj : ButtonObj {
  52.      virtual void writeCode();
  53.      virtual void writeVars() {aVar(this);};
  54.      };
  55.  
  56. struct InputLongWriteObj : InputLongObj {
  57.      virtual void writeCode();
  58.      virtual void writeFields() {aField(this, "long");} ;
  59.      virtual void writeVars() {aVar(this);};
  60.      };
  61. struct LabelWriteObj : LabelObj {
  62.      virtual void writeCode();
  63.      };
  64. struct HistoryWriteObj : HistoryObj {
  65.      virtual void writeCode();
  66.      };
  67. struct InputLineWriteObj : InputLineObj {
  68.      virtual void writeCode();
  69.      virtual void writeFields();
  70.      virtual void writeVars() {aVar(this);};
  71.      };
  72. struct ClusterWriteObj : ClusterObj {
  73.      virtual void writeCode();
  74.      virtual void writeFields() {aField(this, "ushort");} ;
  75.      virtual void writeVars() {aVar(this);};
  76.      };
  77. struct ListBoxWriteObj : ListBoxObj {
  78.      virtual void writeCode();
  79.      virtual void writeFields() {aField(this, "TListBoxRec");} ;
  80.      virtual void writeVars() {aVar(this);};
  81.      };
  82. struct ScrollBarWriteObj : ScrollBarObj {
  83.      virtual void writeCode();
  84.      virtual void writeVars();
  85.      };
  86. struct MemoWriteObj : MemoObj {
  87.      virtual void writeCode();
  88.      virtual void writeFields();
  89.      virtual void writeVars() {aVar(this);};
  90.      };
  91. struct StaticTextWriteObj : StaticTextObj {
  92.      virtual void writeCode();
  93.      virtual void writeVars() {aVar(this);};
  94.      };
  95. struct ColoredTextWriteObj : ColoredTextObj {
  96.      virtual void writeCode();
  97.      virtual void writeVars() {aVar(this);};
  98.      };
  99.  
  100.  
  101. //now that we know the final extensions of ViewObj, we can write the
  102. //getKind function
  103. ViewObj *getKind(recType Kind)
  104. { ViewObj *P;
  105.   switch (Kind) {
  106.       case Dlg : P = new DialogWriteObj(); break;
  107.       case Button : P = new ButtonWriteObj(); break;
  108.       case InputL : P = new InputLineWriteObj(); break;
  109.       case Labl : P = new LabelWriteObj();  break;
  110.       case Histry : P = new HistoryWriteObj(); break;
  111.       case ILong : P = new InputLongWriteObj(); break;
  112.       case CheckB: P = new ClusterWriteObj(); break;
  113.       case RadioB: P = new ClusterWriteObj(); break;
  114.       case MultiCB: P = new MultiCheckBoxObj(); break;
  115.       case ListB: P = new ListBoxWriteObj(); break;
  116.       case ScrollB :  P = new ScrollBarWriteObj(); break;
  117.       case Memo: P = new MemoWriteObj(); break;
  118.       case SText: P = new StaticTextWriteObj(); break;
  119.       case CText : P = new ColoredTextWriteObj(); break;
  120.       default : P = 0; break;
  121.       }
  122. return P;
  123. }
  124.  
  125. char * getWinFlagWords(ushort w, char *s)
  126. //given the set bits return names in 'or' form
  127. {
  128.  static char *flagArray[4] =  {
  129.         "wfMove", "wfGrow", "wfClose", "wfZoom"};
  130.  s[0] = '\0';
  131.  for (int i = 0; i <= 3; i++) {
  132.    if ((w & 1) == 1 && flagArray[i][0] != '\0')
  133.      strcat(strcat(s, flagArray[i]), " | ");
  134.    w >>= 1;
  135.    }
  136.  int l = strlen(s);
  137.  if (l > 4)
  138.     s[l-3] = '\0';   //remove last " | "
  139.  return s;
  140. }
  141.  
  142. char * getEventWords(ushort w, char *s)
  143. //given the set bits return names in 'or' form
  144. {
  145.  static char *flagArray[16] =  {
  146.         "evMouseDown", "evMouseUp", "evMouseMove", "evMouseAuto",
  147.     "evKeyDown", "0x20", "0x40", "0x80", "evCommand", "evBroadcast",
  148.     "0x400", "0x800", "0x1000", "0x2000", "0x4000", "0x8000"};
  149.  s[0] = '\0';
  150.  for (int i = 0; i <= 15; i++) {
  151.    if ((w & 1) == 1 && flagArray[i][0] != '\0')
  152.      strcat(strcat(s, flagArray[i]), " | ");
  153.    w >>= 1;
  154.    }
  155.  int l = strlen(s);
  156.  if (l > 4)
  157.     s[l-3] = '\0';   //remove last " | "
  158.  return s;
  159. }
  160.  
  161. char * getOptionWords(ushort w, char *s)
  162. //given the set bits return names in 'or' form
  163. {
  164.  static char *flagArray[16] =  {
  165.         "ofSelectable", "ofTopSelect", "ofFirstClick", "ofFramed",
  166.         "ofPreProcess", "ofPostProcess", "ofBuffered", "ofTileable",
  167.         "ofCenterX", "ofCenterY", "ofValidate", "0x800", "0x1000",
  168.         "0x2000", "0x4000", "ofShoehorn"};
  169.  s[0] = '\0';
  170.  for (int i = 0; i <= 15; i++) {
  171.    if ((w & 1) == 1 && flagArray[i][0] != '\0')
  172.      strcat(strcat(s, flagArray[i]), " | ");
  173.    w >>= 1;
  174.    }
  175.  int l = strlen(s);
  176.  if (l > 4)
  177.     s[l-3] = '\0';   //remove last " | "
  178.  return s;
  179. }
  180.  
  181. short bitCount(ushort w) //numbers of set bits in the word
  182. {
  183.  short count = 0;
  184.  for (int i = 0; i <= 15; i++)  {
  185.     if ((w & 1) == 1)
  186.        count++;
  187.     w >>= 1;
  188.     }
  189. return count;
  190. }
  191.  
  192. void doBitOutput(const char* var, const char* pre, ushort actual,
  193.                        ushort defaul, char* (func)(ushort w, char* s))
  194. //output something like "foo->options |= ofSelectable or ofFramed;"
  195. //and/or "foo-> options &= ofTopSelect;"
  196. //actual is the bits set and defaul are the default settings
  197. //it's known that actual and defaul are not equal on entry
  198. {
  199.  char s[100];
  200.  ushort NOTs, ORs, diff ;
  201.  diff = actual ^ defaul;  //the bits that are different
  202.  if (bitCount(diff) > 5) { //this is too complex--output hex number
  203.    outf << var << pre << " = 0x" << hex << actual << dec <<";\n";
  204.    return;
  205.    }
  206.  NOTs = diff & defaul;  //the bits not in defaul
  207.  ORs = diff & actual;    //the extra bits in actual
  208.  s[0] = '\0';
  209.  if (NOTs != 0) {
  210.    outf << var <<pre;
  211.    if (bitCount(NOTs) == 1)
  212.      outf << " &= ~" << func(NOTs, s) << ";\n";
  213.    else
  214.      outf << " &= ~(" << func(NOTs, s) << ");\n";
  215.    }
  216.  s[0] = '\0';
  217.  if (ORs != 0)
  218.    outf << var << pre << " |= " << func(ORs, s) << ";\n";
  219. }
  220.  
  221. void doTRect(ViewObj *P)
  222. {
  223. outf << "(TRect(" << P->X1 << ", " << P->Y1 << ", "
  224.      << P->X2 << ", " << P->Y2 << ")";
  225. }
  226.  
  227. void insertControl(const char *Name)
  228. {
  229.  outf <<  DlgName << "->insert(" << Name << ");\n\n";
  230. }
  231.  
  232. void doOpEvent(ViewObj *P)
  233. {if (P->DefEvMsk != P->EvMsk)
  234.    doBitOutput(P->VarName, "->eventMask", P->EvMsk, P->DefEvMsk, getEventWords);
  235.  
  236. if (P->DefOptns != P->Optns)
  237.    doBitOutput(P->VarName, "->options", P->Optns, P->DefOptns, getOptionWords);
  238. }
  239.  
  240. void writeHelpCtx(char * VarName, char* H, ushort Ctx)
  241. {
  242. if (*H != '\0') {
  243.   if (strcmp(H, "hcNoContext")) {
  244.     outf << VarName << "->helpCtx = " << H << ";\n";
  245.     }
  246.   }
  247. else if (Ctx != 0) {
  248.     outf << VarName << "->helpCtx = " << Ctx << ";\n";
  249.     }
  250. }
  251.  
  252. void start(ViewObj *P)
  253. {
  254.   outf << P->VarName << " = new " << P->Obj;
  255.   doTRect(P);
  256. }
  257.  
  258. void finish(ViewObj *P)
  259. {
  260.   writeHelpCtx(P->VarName, P->HelpCtxSym, P->HCtx);
  261.  doOpEvent(P);
  262.  insertControl(P->VarName);
  263. }
  264.  
  265. void DoControls(void *p, void*)
  266. {ViewObj *P = (ViewObj*)p;
  267.  P->writeCode();
  268. }
  269.  
  270. void DialogWriteObj::writeCode()
  271. {
  272.   strncpy(DlgName, VarName, 49);
  273.  
  274.   outf << Obj << "* " << DlgFuncName << "(void)\n{\n";
  275.   outf << "TView *control";
  276.   if (needcontrol1) outf << ", *control1;\n";
  277.   else outf << ";\n";
  278.  
  279.   outf << Obj << "* " << VarName << " = new " << Obj;
  280.   doTRect(this);
  281.   outf << ", " << quoted(Title) << ");\n";
  282.   outf << "if (!" << VarName << ") return 0;\n\n";
  283.  
  284.   doOpEvent(Dialog);
  285.   if (WinFlags != 5)    //5 is the default
  286.      doBitOutput(VarName, "->flags", WinFlags, 5, getWinFlagWords);
  287.   writeHelpCtx(VarName, HelpCtxSym, HCtx);
  288.   outf << endl;
  289.  
  290.   ScriptColl->forEach(DoControls, 0);  //write code for each control
  291.  
  292.   outf << VarName << "->selectNext(False);\nreturn " << VarName << ";\n}\n";
  293. }
  294.  
  295. void flagsOut(ushort flags)    //used by TButton
  296. {
  297.  if (flags == 0) outf << "bfNormal";
  298.  else {
  299.     short plus = 0;
  300.     if ((flags & 1) != 0) {
  301.         plus = 1; outf << "bfDefault"; }
  302.     if ((flags & 2) != 0) {
  303.         if (plus) outf << "|";
  304.         plus = 1;
  305.         outf << "bfLeftJust";}
  306.     if ((flags & 4) != 0) {
  307.         if (plus) outf << "|";
  308.         plus = 1;
  309.         outf << "bfBroadcast"; }
  310.     if ((flags & 8) != 0)  {
  311.         if (plus) outf << "|";
  312.         outf << "bfGrabFocus"; }
  313.     }
  314. }
  315.  
  316. void ButtonWriteObj::writeCode()
  317. {
  318.  start(this);
  319.  if (strcmp("POptionButton", Obj) == 0)  //special POptionButton
  320.     outf << ", " << Param[0] << ", " << Param[1] << ");\n";
  321.  else {                                 // normal TButton
  322.     outf << ", " << quoted(ButtonText) << ", ";
  323.     if (*CommandName != '\0')
  324.        outf << CommandName;
  325.     else outf << CommandValue;
  326.     outf << ", ";
  327.     flagsOut(Flags);
  328.     outf << ");\n";
  329.     }
  330.  finish(this);
  331. }
  332.  
  333. void LabelWriteObj::writeCode()
  334. {
  335.  outf << "  " << DlgName << "->insert(new " << Obj;
  336.  doTRect(this);
  337.  outf << ", " << quoted(LabelText) << ", " << LinkName << "));\n\n";
  338. }
  339.  
  340. void HistoryWriteObj::writeCode()
  341. {
  342.  outf << "  " << DlgName << "->insert(new " << Obj;
  343.  doTRect(this);
  344.  outf << ", (TInputLine*)" << HistoryLink << ", " << HistoryID << "));\n\n";
  345. }
  346.  
  347. void InputLongWriteObj::writeCode()
  348. {
  349.   start(this);
  350.   outf << ", " << (LongStrLeng+1) << ", " << LLim << ", " << ULim <<  ", "
  351.        << ILOptions << ", " << quoted(LongLabelText) << ");\n";
  352.   finish(this);
  353. }
  354.  
  355. void InputLineWriteObj::writeCode()
  356. {
  357.   outf << VarName << " = new " << Obj;
  358.   doTRect(this);
  359.   outf << ", " << (StringLeng+1) << ");\n";    //Note: add 1
  360.   finish(this);
  361. }
  362. void InputLineWriteObj::writeFields()
  363. {
  364.   outf << "  char " << FieldName << "[" << (StringLeng+1)  //Note: add 1 !
  365.        << "];   //" << Obj << endl;
  366. }
  367.  
  368. void doAtText(char *s)  // an '@' reference string
  369. {
  370.  while (*s) {
  371.    char c = *s++;
  372.    switch (c) {
  373.      case 0x3 :
  374.      case '@': break;
  375.      case '\\' :
  376.        if (*s == 'n') s++;
  377.        else outf << c;
  378.        break;
  379.      default : outf << c;
  380.      }
  381.   }
  382. }
  383.  
  384. void doText(char *s)
  385. {    //split up a long string into several lines. String cannot exceed 254
  386.  if (!*s) {
  387.    outf << 0;
  388.    return;
  389.    }
  390.  if (strlen(s) > 254) s[254] = '\0';   //truncate if too big
  391.  outf << '\"';
  392.  short count = 47;
  393.  while (*s)  {
  394.     if (s[0] == '\\' && s[1] == 'n' && s[2]) {
  395.        outf << s[0] << s[1] << "\"\n     \"";
  396.        count = 5;
  397.        s++;    //extra increment to pass 'n'
  398.        }
  399.     else {
  400.        outf << *s;
  401.        count++;
  402.        }
  403.     s++;
  404.     if (count > 75 && *s) {
  405.        outf << "\"\n     \"";
  406.        count = 5;
  407.        }
  408.     }
  409.  outf << '\"';
  410. }
  411.  
  412. void StaticTextWriteObj::writeCode()
  413. {
  414.   start(this);
  415.   outf << ", ";
  416.   if(Text[0] == '@' || Text[0] == 3 && Text[1] == '@') 
  417.     doAtText(Text);
  418.   else
  419.     doText(Text);
  420.   outf << ");\n";
  421.   finish(this);
  422. }
  423.  
  424. void ColoredTextWriteObj::writeCode()
  425. {
  426.   start(this);
  427.   outf << ", ";
  428.   if(Text[0] == '@' || Text[0] == 3 && Text[1] == '@')
  429.     doAtText(Text);
  430.   else
  431.     doText(Text);
  432.   outf << ", 0x" << hex << Attrib << dec << ");\n";
  433.   finish(this);
  434. }
  435.  
  436. void ScrollBarWriteObj::writeCode()
  437. {
  438.   start(this);
  439.   outf << ");\n";
  440.   finish(this);
  441. }
  442.  
  443. void ScrollBarWriteObj::writeVars()
  444. {
  445.  if (strcmp(VarName, "control1") != 0) //control1 is used by TMemo
  446.    aVar(this);
  447.  else needcontrol1 = True;
  448. }
  449.  
  450. void ListBoxWriteObj::writeCode()
  451. {
  452.   start(this);
  453.   outf << ", " << Columns << ", ";
  454.   if (*ScrollBar != '\0')
  455.     outf << "(TScrollBar*)" << ScrollBar << ");\n";
  456.   else outf << "0);\n";
  457.   finish(this);
  458. }
  459.  
  460. void MemoWriteObj::writeCode()
  461. {
  462.   start(this);
  463.   outf << ", " ;
  464.   if (*HScroll != '\0')
  465.     outf << "(TScrollBar*)" << HScroll << ", ";
  466.   else outf << "0, ";
  467.   if (*VScroll != '\0')
  468.     outf << "(TScrollBar*)" << VScroll << ", 0, ";
  469.   else outf << "0, 0, ";
  470.   outf << BufSize << ");\n";
  471.   finish(this);
  472. }
  473. void MemoWriteObj::writeFields()
  474. {
  475.   outf << "  ushort " << FieldName << ";   //" << Obj << " text length\n";
  476.   outf << "  char " << TextFieldName << "[" << BufSize << "];   //"
  477.        << Obj << " text\n";
  478. }
  479.  
  480. void ClusterWriteObj::writeCode()
  481. {
  482.   start(this);
  483.   for (int i=0; i < Items; i++)
  484.     outf << ",\n  new TSItem(\"" << (char *)LabelColl->at(i) << "\"";
  485.   outf << ", 0";
  486.   for (i = 0; i <= Items; i++)
  487.     outf << ")";
  488.   outf << ";\n";
  489.   finish(this);
  490. }
  491.  
  492. void doVars(void *p, void*)
  493. {ViewObj *P = (ViewObj*)p;
  494.  P->writeVars();
  495. }
  496.  
  497. void chkFirst(int& first)  //called by doFields
  498. {
  499.  if ( first ) {
  500.     outf << "struct " << Dialog->FieldName << "  {\n";
  501.     first = False;
  502.     }
  503. }
  504.  
  505. void doFields(void *p, void* first)
  506. {     // fieldnames for the data transfer struct
  507.  ViewObj *P = (ViewObj*)p;
  508.  if ( *(P->FieldName) != '\0') {
  509.     chkFirst( *(int*)first );
  510.     P->writeFields();
  511.     }
  512.  else if (strcmp("POptionButton", P->Obj) == 0) { //special POptionButton
  513.     chkFirst( *(int*)first );
  514.     outf << "  OptionRec " << P->Param[2] <<  ";   //POptionButton\n";
  515.     }
  516. }
  517.  
  518. void writeSource()
  519. {//code to build the data transfer struct
  520.  int first = True;
  521.  
  522.  if (Dialog->FieldName[0])  {  //no fieldname, no datarecord
  523.    if (present[ListB]) outf <<
  524.     "#if !defined(__TListBoxRec)\n"
  525.     "#define __TListBoxRec\n"
  526.     "struct TListBoxRec\n"
  527.     "{\n"
  528.     "    TCollection* collection;\n"
  529.     "    short focused;\n"
  530.     "};\n"
  531.     "#endif\n\n";
  532.  
  533.    ScriptColl->forEach(doFields, &first);
  534.    if (!first) outf << "  };\n\n";  //if !first, there is at least one field
  535.    }
  536.  
  537.  //define variables whose names are not "control"
  538.  ScriptColl->forEach(doVars, 0);
  539.  outf << endl;
  540.  
  541.  //the code for the dialog and its controls
  542.  Dialog->writeCode();
  543. }
  544.  
  545. //-------------------------------------------------------------------
  546. // The following exit procedure is required in order to view error
  547. // messages before Turbo Vision wipes out the screen
  548. //-------------------------------------------------------------------
  549. int exit_flag = 1;
  550.  
  551. void exitfunc(void)
  552. {
  553.   if( exit_flag )
  554.   {
  555.      cout << "\nStrike Enter key to continue" << endl;
  556.      char c;
  557.      cin.get(c);
  558.   }
  559. }
  560.  
  561. #pragma exit exitfunc  100
  562.  
  563. int main(int argc, char** argv)
  564. // argv[1] is script file
  565. // argv[2] is .src name
  566. // argv[3] is error filename
  567.  
  568. {
  569.  if (argc < 3) {
  570.    cout << "Usage:  custom1 <script filename> <source filename> [error filename]";
  571.    exit(1);
  572.    }
  573.  if (argc >= 4) {
  574.    freopen(argv[3], "w", stdout);  //redirect output to error file
  575.    exit_flag = 0;   // won't need hold up on exit
  576.    }
  577.  
  578.  checkMemory();
  579.  
  580.  readScriptFile(argv[1]);  //argv(1) is temporary script file
  581.  
  582.  outf.open(argv[2], ios::out);
  583.  if (!outf.good())
  584.    cout << "Can't open source file" << endl;
  585.  
  586.  writeSource();
  587.  outf.close();
  588.  cout << "completed" << endl;
  589.  return 0;
  590. }
  591.