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