home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 12 / CD_ASCQ_12_0294.iso / maj / 535 / cpprsrc.cpp < prev    next >
C/C++ Source or Header  |  1993-08-06  |  12KB  |  456 lines

  1.  
  2. #define Uses_TEventQueue
  3. #define Uses_TEvent
  4. #define Uses_TProgram
  5. #define Uses_TApplication
  6. #define Uses_TKeys
  7. #define Uses_TRect
  8. #define Uses_TMenuBar
  9. #define Uses_TSubMenu
  10. #define Uses_TMenuItem
  11. #define Uses_TStatusLine
  12. #define Uses_TStatusItem
  13. #define Uses_TStatusDef
  14. #define Uses_TDeskTop
  15. #define Uses_TView
  16. #define Uses_TWindow
  17. #define Uses_TFrame
  18. #define Uses_TScroller
  19. #define Uses_TScrollBar
  20. #define Uses_TDialog
  21. #define Uses_TButton
  22. #define Uses_TSItem
  23. #define Uses_TCheckBoxes
  24. #define Uses_TRadioButtons
  25. #define Uses_TLabel
  26. #define Uses_TInputLine
  27. #define Uses_TCollection
  28. #define Uses_THistory
  29. #define Uses_THistoryWindow
  30. #define Uses_TListBox
  31. #define Uses_TStringCollection
  32. #define Uses_TMemo
  33. #define Uses_TColoredText
  34. #define Uses_TInputLong
  35. #define Uses_fpstream
  36. #define Uses_MsgBox
  37. #define Uses_TResourceCollection
  38. #define Uses_TResourceFile
  39. #include <tv.h>
  40. #include <stdlib.h>
  41. #include <dos.h>
  42. #include <dir.h>
  43. #include <fcntl.h>
  44. #include <string.h>
  45. #include <stdio.h>
  46. #include <new.h>
  47. #include "tinplong.h"
  48. #include "tcolortx.h"
  49. #include "readscpt.h"
  50.  
  51. __link(RView);
  52. __link(RDialog);
  53. __link(RResourceCollection);
  54. __link(RButton);
  55. __link(RCluster);
  56. __link(RCheckBoxes);
  57. __link(RRadioButtons);
  58. __link(RLabel);
  59. __link(RStaticText);
  60. __link(RScrollBar);
  61. __link(RInputLine);
  62. __link(RHistory);
  63. __link(RListBox);
  64. __link(RMemo);
  65. __link(RInputLong);
  66. __link(RColoredText);
  67.  
  68. TDialog *dlg;   //hold the dialog as it is built up
  69. TView *control;
  70. TScrollBar *hScrollBar;
  71.  
  72. //the following extend the ViewObj struct's to also write the code
  73. struct DialogResourceObj : DialogObj {
  74.      virtual void writeCode();
  75.      };
  76. struct ButtonResourceObj : ButtonObj {
  77.      virtual void writeCode();
  78.      };
  79. struct InputLongResourceObj : InputLongObj {
  80.      virtual void writeCode();
  81.      };
  82. struct LabelResourceObj : LabelObj {
  83.      virtual void writeCode();
  84.      };
  85. struct HistoryResourceObj : HistoryObj {
  86.      virtual void writeCode();
  87.      };
  88. struct InputLineResourceObj : InputLineObj {
  89.      virtual void writeCode();
  90.      };
  91. struct ClusterResourceObj : ClusterObj {
  92.      virtual void writeCode();
  93.      };
  94. struct ListBoxResourceObj : ListBoxObj {
  95.      virtual void writeCode();
  96.      };
  97. struct ScrollBarResourceObj : ScrollBarObj {
  98.      virtual void writeCode();
  99.      };
  100. struct MemoResourceObj : MemoObj {
  101.      virtual void writeCode();
  102.      };
  103. struct StaticTextResourceObj : StaticTextObj {
  104.      virtual void writeCode();
  105.      };
  106. struct ColoredTextResourceObj : ColoredTextObj {
  107.      virtual void writeCode();
  108.      };
  109.  
  110. char* convert(char *s)
  111. // converts the strings (which are meant to be quoted ascii strings) to
  112. // string suitable for input without quotes.  \n (two chars) is converted
  113. // to newline, \" (two chars) is converted to single ", etc.
  114. {
  115.  static char rslt[300];
  116.  char *d = rslt, c = *s++;
  117.  while( c )  {
  118.    if (c == '\\') {
  119.        if (*s == '\\') {*d++ = '\\'; s++;}
  120.        else if (*s == 'n') {*d++ = '\n'; s++;}
  121.        else if (*s == '"') {*d++ = '"'; s++;}
  122.       }
  123.    else *d++ = c;
  124.    c = *s++;
  125.    }
  126.  *d = '\0';
  127.  rslt[254] = '\0';  //this is max string len for TStaticText!
  128.  return rslt;
  129. }
  130.  
  131. void reportError(const char* s)
  132. {
  133.  cout << s << endl;
  134.  exit(1);
  135. }
  136.  
  137. void doOptionsEtc(TView *P, ViewObj *V)
  138. {
  139.  P->options = V->Optns;
  140.  P->eventMask = V->EvMsk;
  141.  P->helpCtx = V->HCtx;
  142.  P->growMode = V->Grow;
  143. }
  144.  
  145. void DialogResourceObj::writeCode()
  146. {
  147.  dlg = new TDialog(TRect(X1, Y1, X2, Y2), convert(Title));
  148.  if (dlg) {
  149.    doOptionsEtc(dlg, this);
  150.    dlg->palette = Palette;
  151.    dlg->flags = WinFlags;
  152.    }
  153.  else reportError("Cannot construct TDialog");
  154. }
  155.  
  156. void ButtonResourceObj::writeCode()
  157. {
  158.  control = new TButton(TRect(X1, Y1, X2, Y2), convert(ButtonText),
  159.                CommandValue, Flags);
  160.  if (control) {
  161.    doOptionsEtc(control, this);
  162.    dlg->insert(control);
  163.    }
  164.  else reportError("Cannot construct TButton");
  165. }
  166.  
  167. void InputLongResourceObj::writeCode()
  168. {
  169.  control = new TInputLong(TRect(X1, Y1, X2, Y2), LongStrLeng, LLim, ULim,
  170.                                     ILOptions, convert(LongLabelText));
  171.  if (control) {
  172.    doOptionsEtc(control, this);
  173.    dlg->insert(control);
  174.    }
  175.  else reportError("Cannot construct TInputLong");
  176. }
  177.  
  178. void InputLineResourceObj::writeCode()
  179. {
  180.  control = new TInputLine(TRect(X1, Y1, X2, Y2), StringLeng);
  181.  if (control) {
  182.    doOptionsEtc(control, this);
  183.    dlg->insert(control);
  184.    }
  185.  else reportError("Cannot construct TInputLine");
  186. }
  187.  
  188. void StaticTextResourceObj::writeCode()
  189. {
  190.  control = new TStaticText(TRect(X1, Y1, X2, Y2), convert(Text));
  191.  if (control) {
  192.    doOptionsEtc(control, this);
  193.    dlg->insert(control);
  194.    }
  195.  else reportError("Cannot construct TStaticText");
  196. }
  197.  
  198. void ColoredTextResourceObj::writeCode()
  199. {
  200.  control = new TColoredText(TRect(X1, Y1, X2, Y2), convert(Text), Attrib);
  201.  if (control) {
  202.    doOptionsEtc(control, this);
  203.    dlg->insert(control);
  204.    }
  205.  else reportError("Cannot construct TColoredText");
  206. }
  207.  
  208. void ScrollBarResourceObj::writeCode()
  209. {
  210.  TScrollBar *tmp = new TScrollBar(TRect(X1, Y1, X2, Y2));
  211.  if (tmp) {
  212.    doOptionsEtc(tmp, this);
  213.    dlg->insert(tmp);
  214.    if (stricmp(VarName, "hscroll") == 0)
  215.      hScrollBar = tmp;     // probably a horizontal scroll bar for TMemo
  216.    else control = tmp;
  217.    }
  218.  else reportError("Cannot construct TScrollBar");
  219. }
  220.  
  221. void ListBoxResourceObj::writeCode()
  222. {
  223.  TScrollBar *scroll = 0;
  224.  if (ScrollBar != 0)
  225.    scroll = (TScrollBar*)control;
  226.  
  227.  control = new TListBox(TRect(X1, Y1, X2, Y2), Columns, scroll);
  228.  if (control) {
  229.    doOptionsEtc(control, this);
  230.    dlg->insert(control);
  231.    }
  232.  else reportError("Cannot construct TListBox");
  233. }
  234.  
  235. void MemoResourceObj::writeCode()
  236. {
  237.  TScrollBar *vbar = 0, *hbar = 0;
  238.  if (VScroll != 0)
  239.    vbar = (TScrollBar*)control;
  240.  if (HScroll != 0)
  241.    hbar = hScrollBar;
  242.  
  243.  control = new TMemo(TRect(X1, Y1, X2, Y2), hbar, vbar, 0, BufSize);
  244.  if (control) {
  245.    doOptionsEtc(control, this);
  246.    dlg->insert(control);
  247.    }
  248.  else reportError("Cannot construct TMemo");
  249. }
  250.  
  251. void formTSItem(void *p, void* l)
  252. {char *s = (char*)p;
  253.  TSItem *last = new TSItem(newStr(convert(s)), *(TSItem**)l);
  254.  *(TSItem**)l = last;
  255. }
  256.  
  257. void ClusterResourceObj::writeCode()
  258. {
  259.  TSItem *lastItem = 0;
  260. // LabelColl->forEach(&formTSItem, &lastItem);
  261.  
  262.   for (int i=Items-1; i >= 0; i--) {     //must do this one backwards
  263.     char *s = (char *)LabelColl->at(i);
  264.     lastItem = new TSItem(convert(s), lastItem);
  265.     }
  266.  
  267. if (strcmp(Obj, "TCheckBoxes") == 0)
  268.      control = new TCheckBoxes(TRect(X1, Y1, X2, Y2), lastItem);
  269.  else if (strcmp(Obj, "TRadioButtons") == 0)
  270.      control = new TRadioButtons(TRect(X1, Y1, X2, Y2), lastItem);
  271.  else control = 0;
  272.  
  273.  if (control) {
  274.    doOptionsEtc(control, this);
  275.    dlg->insert(control);
  276.    }
  277.  else {
  278.    cout << "Cannot construct " << Obj << endl;
  279.    exit(1);
  280.    }
  281. }
  282.  
  283. void LabelResourceObj::writeCode()
  284. {
  285.  TLabel *labl = new TLabel(TRect(X1, Y1, X2, Y2), convert(LabelText), control);
  286.  if (labl) {
  287.    doOptionsEtc(labl, this);
  288.    dlg->insert(labl);
  289.    }
  290.  else reportError("Cannot construct TLabel");
  291. }
  292.  
  293. void HistoryResourceObj::writeCode()
  294. {
  295.  THistory *history = new THistory(TRect(X1, Y1, X2, Y2), (TInputLine*)control,
  296.                       HistoryID);
  297.  if (history) {
  298.    doOptionsEtc(history, this);
  299.    dlg->insert(history);
  300.    }
  301.  else reportError("Cannot construct THistory");
  302. }
  303.  
  304. //now that we know the final extensions of ViewObj, we can write the
  305. //getKind function
  306. ViewObj *getKind(recType Kind)
  307. { ViewObj *P;
  308.   switch (Kind) {
  309.       case Dlg : P = new DialogResourceObj(); break;
  310.       case Button : P = new ButtonResourceObj(); break;
  311.       case InputL : P = new InputLineResourceObj(); break;
  312.       case Labl : P = new LabelResourceObj();  break;
  313.       case Histry : P = new HistoryResourceObj(); break;
  314.       case ILong : P = new InputLongResourceObj(); break;
  315.       case CheckB: P = new ClusterResourceObj(); break;
  316.       case RadioB: P = new ClusterResourceObj(); break;
  317.       case MultiCB: P = new MultiCheckBoxObj(); break;
  318.       case ListB: P = new ListBoxResourceObj(); break;
  319.       case ScrollB :  P = new ScrollBarResourceObj(); break;
  320.       case Memo: P = new MemoResourceObj(); break;
  321.       case SText: P = new StaticTextResourceObj(); break;
  322.       case CText : P = new ColoredTextResourceObj(); break;
  323.       default : P = 0; break;
  324.       }
  325. return P;
  326. }
  327.  
  328. void DoControls(void *p, void*)
  329. {ViewObj *P = (ViewObj*)p;
  330.  checkMemory();
  331.  P->writeCode();
  332. }
  333.  
  334. void makeDialog()
  335. {
  336.  Dialog->writeCode();
  337.  ScriptColl->forEach(&DoControls, 0);
  338.  dlg->selectNext(False);
  339. }
  340.  
  341. int Copy(char* from, char* to)  //copy one file to another
  342. {int h1, h2;
  343.  unsigned numRead, numWritten;
  344.  char buf[1000];
  345.  if (!(_dos_open(from, O_RDONLY, &h1) == 0)) return 0;
  346.  if (!(_dos_creat(to, _A_NORMAL, &h2) == 0)) return 0;
  347.  do  {
  348.    _dos_read(h1, buf, 1000, &numRead);
  349.    _dos_write(h2, buf, numRead, &numWritten);
  350.    }
  351.  while (numRead > 0 && numWritten == numRead);
  352.  _dos_close(h1);
  353.  _dos_close(h2);
  354.  return numWritten == numRead;
  355. }
  356.  
  357. #define TNAME "TMP$$.$$$"
  358.  
  359. void writeResource(char* fname, char* id)
  360. {
  361.  makeDialog();   //dialog is not pointed to by 'dlg'
  362.  
  363.  struct ffblk ffblk;
  364.  char backName[MAXPATH];
  365.  char drive[MAXDRIVE];
  366.  char dir[MAXDIR];
  367.  char name[MAXFILE];
  368.  
  369.  if (findfirst(fname, &ffblk, 0) == 0) {   //the file exists already
  370.    if (!Copy(fname, TNAME)) {      //copy so we can backup the original
  371.      cout << "Copying original file failed\n";
  372.      exit(1);
  373.      }
  374.    fnsplit(fname, drive, dir, name, 0);
  375.    fnmerge(backName, drive, dir, name, ".BAK");
  376.    remove(backName);              //in case a .BAK file already exists
  377.    if (rename(fname, backName) != 0) {
  378.      cout << "Renaming original file failed\n";
  379.      exit(1);
  380.      }
  381.    if (rename(TNAME, fname) != 0) {
  382.      cout << "Renaming temporary file failed\n";
  383.      exit(1);
  384.      }
  385.    }
  386.  
  387.  fpstream *ifps;
  388.  ifps = new fpstream(fname, ios::out|ios::binary|ios::ate|ios::in);
  389.  ifps->seekg(0);
  390.  if (!ifps->good()) {
  391.    cout << "Error opening " << fname << endl;
  392.    exit(1);
  393.    }
  394.  
  395.  TResourceFile *myRez;
  396.  myRez = new TResourceFile( ifps);
  397.  if (!myRez) {
  398.    cout << "Resource file constructor failed\n";
  399.    exit(1);
  400.    }
  401.  
  402.  myRez->put(dlg, id);
  403.  if (ifps->fail())  {
  404.    cout << "Dialog put failed\n";
  405.    exit(1);
  406.    }
  407.  
  408.  TObject::destroy(dlg);
  409.  TObject::destroy(myRez);
  410. }
  411.  
  412. //-------------------------------------------------------------------
  413. // The following exit procedure is required in order to view error
  414. // messages before Turbo Vision wipes out the screen
  415. //-------------------------------------------------------------------
  416. int exit_flag = 1;
  417.  
  418. void exitfunc(void)
  419. {
  420.   if( exit_flag )
  421.   {
  422.      cout << "\nStrike Enter key to continue" << endl;
  423.      char c;
  424.      cin.get(c);
  425.   }
  426. }
  427.  
  428. #pragma exit exitfunc  100
  429.  
  430. int main(int argc, char** argv)
  431. // argv[1] is script filename
  432. // argv[2] is resource filename
  433. // argv[3] is resource ID
  434. // argv[4] is error filename
  435.  
  436. {
  437.  if (argc < 3) {
  438.    cout << "Usage: cpprsrc <script filename> <RezFilename> <RezID> [error filename]";
  439.    exit(1);
  440.    }
  441.  
  442.  if (argc > 4) {   // redirect stdout to the error file
  443.    freopen(argv[4], "w", stdout);
  444.    exit_flag = 0;   // won't need hold up on exit
  445.    }
  446.  
  447.  checkMemory();
  448.  
  449.  readScriptFile(argv[1]);  //argv(1) is temporary script file
  450.  
  451.  writeResource(argv[2], argv[3]);
  452.  
  453.  cout << "completed" << endl;
  454.  return 0;
  455. }
  456.