home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / som / somk / c / tp / txtep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-24  |  11.3 KB  |  390 lines

  1.  
  2. /*
  3.  *  This file was generated by the SOM Compiler.
  4.  *  Generated using:
  5.  *     SOM incremental update: 6.34
  6.  */
  7.  
  8. #ifndef lint
  9. static char *sccsid = "%Z% %I% %W% %G% %U% [%H% %T%] (c)IBM Corp. 1992";
  10. #endif
  11.  
  12. /*
  13.  * This class is adapted from the book
  14.  *   Class Construction in C and C++, Object Oriented Fundamentals
  15.  *   by Roger Sessions, Copyright (c) 1992 Prentice Hall.
  16.  * Reprinted with permission.
  17.  */
  18.  
  19. #define txtEnvProcessor_Class_Source
  20. #if defined(_WIN32)
  21. #include "statcls.h"
  22. #endif
  23. #include "txtep.ih"
  24. #include "tpword.h"
  25. #include "textline.h"
  26. #include "page.h"
  27. #include "colblk.h"
  28.  
  29. #define PARAGRAPH_INDENTATION 0
  30. #define LINE_INDENTATION 0
  31.  
  32. /* ************************************************************ */
  33. /*
  34.  *  Default word processor: Add it to the current line, if
  35.  *  space is available, otherwise create a new line.
  36.  */
  37.  
  38. SOM_Scope void SOMLINK tpProcessWord(txtEnvProcessor * somSelf)
  39. {
  40.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  41.     textLine *thisLine = _tpGetLine(somSelf);
  42.     page *thisPage;
  43.     txtEnvProcessorMethodDebug("txtEnvProcessor", "tpProcessWord");
  44.  
  45.     thisPage = _epGetPage(somSelf);
  46.     _thisCB = _pgGetColumnBlock(thisPage);
  47.     if (!_cbLinesLeft(_thisCB))
  48.     _tpProcessFullPage(somSelf);
  49.     if (__get_length(_thisWord) > _tlCharsLeft(thisLine)) {
  50.     _tpProcessFullLine(somSelf);
  51.     _tpProcessNewLine(somSelf);
  52.     thisLine = _tpGetLine(somSelf);
  53.     }
  54.     _llAddTail(thisLine, (baseType *) _thisWord);
  55. }
  56.  
  57. /* ************************************************************ */
  58. /*
  59.  *  Default blank processor: Add to current line, if
  60.  *  space is available, otherwise discard (ie don't
  61.  *  use in a new line).
  62.  */
  63.  
  64. SOM_Scope void SOMLINK tpProcessBlanks(txtEnvProcessor * somSelf)
  65. {
  66.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  67.     textLine *thisLine = _tpGetLine(somSelf);
  68.     txtEnvProcessorMethodDebug("txtEnvProcessor", "tpProcessBlanks");
  69.  
  70.     if (!_tlCharsLeft(thisLine)) {
  71.     _somFree(_thisWord);
  72.     return;
  73.     }
  74.     _tpProcessWord(somSelf);
  75.  
  76. }
  77.  
  78. /* ************************************************************ */
  79. /*
  80.  *  Default line break processor: Treat line breaks as
  81.  *  blanks (ie an input line break does not cause a line
  82.  *  break in the output file).
  83.  */
  84.  
  85. SOM_Scope void SOMLINK tpProcessLineBreak(txtEnvProcessor * somSelf)
  86. {
  87.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  88.     textLine *thisLine = _tpGetLine(somSelf);
  89.     txtEnvProcessorMethodDebug("txtEnvProcessor", "tpProcessLineBreak");
  90.  
  91.     _somFree(_thisWord);
  92.     if (!_tlCharsInLine(thisLine))
  93.     return;
  94.     if (!_tlCharsLeft(thisLine))
  95.     return;
  96.     _thisWord = TPWordNew();
  97.     _wordInit1(_thisWord, " ");
  98.     _tpProcessWord(somSelf);
  99.  
  100. }
  101.  
  102. /* ************************************************************ */
  103. /*
  104.  *  Default full line processor: Add full input line to
  105.  *  columnBlock and start a new line.
  106.  */
  107.  
  108. SOM_Scope void SOMLINK tpProcessFullLine(txtEnvProcessor * somSelf)
  109. {
  110.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  111.     textLine *thisLine = _tpGetLine(somSelf);
  112.     page *thisPage;
  113.     txtEnvProcessorMethodDebug("txtEnvProcessor", "tpProcessFullLine");
  114.  
  115.     thisPage = _epGetPage(somSelf);
  116.     _thisCB = _pgGetColumnBlock(thisPage);
  117.     _llAddTail(_thisCB, (baseType *) thisLine);
  118.  
  119.     thisLine = textLineNew();
  120.     _tlInit(thisLine, _cbGetColumnWidth(_thisCB));
  121.     _tpSetLine(somSelf, thisLine);
  122. }
  123.  
  124. /* ************************************************************ */
  125. /*
  126.  *  Default full page processor: Print the page and start
  127.  *  another.
  128.  */
  129.  
  130. SOM_Scope void SOMLINK tpProcessFullPage(txtEnvProcessor * somSelf)
  131. {
  132.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  133.     page *thisPage;
  134.     txtEnvProcessorMethodDebug("txtEnvProcessor", "tpProcessFullPage");
  135.  
  136.     thisPage = _epGetPage(somSelf);
  137.     _pgPrint(thisPage, stdout);
  138.     _thisCB = _pgGetColumnBlock(thisPage);
  139. }
  140.  
  141. /* ************************************************************ */
  142. /*
  143.  *  Default new line processor: Handles a new output line.
  144.  *  Default is indent line 2 characters.
  145.  */
  146.  
  147. SOM_Scope void SOMLINK tpProcessNewLine(txtEnvProcessor * somSelf)
  148. {
  149.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  150.     int charsToIndent;
  151.     TPWord *blanks;
  152.     textLine *thisLine;
  153.     txtEnvProcessorMethodDebug("txtEnvProcessor", "tpProcessNewLine");
  154.  
  155.     charsToIndent = _epGetLineIndentation(somSelf);
  156.     if (charsToIndent) {
  157.     thisLine = _tpGetLine(somSelf);
  158.     blanks = TPWordNew();
  159.     _wordInit2(blanks, ' ', charsToIndent);
  160.     _llAddTail(thisLine, (baseType *) blanks);
  161.     }
  162. }
  163.  
  164. /* ************************************************************ */
  165. /*
  166.  *  Default new paragraph processor: Breaks the output line.
  167.  *  Starts a new output line.
  168.  */
  169.  
  170. SOM_Scope void SOMLINK tpProcessNewParagraph(txtEnvProcessor * somSelf)
  171. {
  172.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  173.     int charsToIndent;
  174.     TPWord *blanks;
  175.     textLine *thisLine;
  176.     txtEnvProcessorMethodDebug("txtEnvProcessor", "tpProcessNewParagraph");
  177.  
  178.     charsToIndent = _epGetParagraphIndentation(somSelf);
  179.     _tpProcessFullLine(somSelf);
  180.     _tpProcessNewLine(somSelf);
  181.     thisLine = _tpGetLine(somSelf);
  182.  
  183.     if (charsToIndent) {
  184.     blanks = TPWordNew();
  185.     _wordInit2(blanks, ' ', charsToIndent);
  186.     _llAddTail(thisLine, (baseType *) blanks);
  187.     }
  188.     _somFree(_thisWord);
  189. }
  190.  
  191. /* ************************************************************ */
  192. /*
  193.  *  Gets the current line.
  194.  */
  195.  
  196. SOM_Scope textLine *SOMLINK tpGetLine(txtEnvProcessor * somSelf)
  197. {
  198.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  199.     txtEnvProcessorMethodDebug("txtEnvProcessor", "tpGetLine");
  200.  
  201.     if (!_thisLine) {
  202.     _thisLine = textLineNew();
  203.     _tlInit(_thisLine, _cbGetColumnWidth(_thisCB));
  204.     }
  205.     return _thisLine;
  206. }
  207.  
  208. /* ************************************************************ */
  209. /*
  210.  *  Sets the current line.
  211.  */
  212.  
  213. SOM_Scope void SOMLINK tpSetLine(txtEnvProcessor * somSelf,
  214.                   textLine * newLine)
  215. {
  216.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  217.     txtEnvProcessorMethodDebug("txtEnvProcessor", "tpSetLine");
  218.     _thisLine = newLine;
  219. }
  220.  
  221. /* ************************************************************ */
  222. SOM_Scope void SOMLINK epProcessEnvironment(txtEnvProcessor * somSelf)
  223. {
  224.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  225.  
  226. /* Declare local variables.
  227.    ------------------------ */
  228.     page *thisPage;
  229.     fileMgr *thisFile;
  230.     int n = 0;
  231. /* char *myClass = getClassName(); */
  232.     thisPage = _epGetPage(somSelf);
  233.     thisFile = _epGetFileMgr(somSelf);
  234.     txtEnvProcessorMethodDebug("txtEnvProcessor", "epProcessEnvironment");
  235.  
  236. /* Prepare for looping.
  237.    -------------------- */
  238.     _epInitializeEnvironment(somSelf);
  239.  
  240. /* Loop until end of file.
  241.    ----------------------- */
  242.     while (_thisWord = readToken(thisFile)) {
  243. /*    _print(_thisWord, stdout); */
  244.     if (_tpwType(_thisWord) == TP_EOF) {
  245.         _somFree(_thisWord);
  246.         break;
  247.     }
  248.     if (_match(_thisWord, "[[end_environment]]")) {
  249.         _epShutdownEnvironment(somSelf);
  250.         break;
  251.     }
  252.     switch (_tpwType(_thisWord)) {
  253.         case TP_LINE_BREAK:
  254.         _tpProcessLineBreak(somSelf);
  255.         continue;
  256.         case TP_WORD:
  257.         _tpProcessWord(somSelf);
  258.         continue;
  259.         case TP_PARAGRAPH_BREAK:
  260.         _tpProcessNewParagraph(somSelf);
  261.         continue;
  262.         case TP_BLANK_SPACE:
  263.         _tpProcessBlanks(somSelf);
  264.         continue;
  265.         case TP_TOKEN:
  266.         _epStartUpNewEnvironment(somSelf, _thisWord);
  267.         _somFree(_thisWord);
  268.         continue;
  269.     }
  270.     }
  271.     _tpProcessFullLine(somSelf);
  272. }
  273.  
  274. /* ************************************************************ */
  275. SOM_Scope void SOMLINK epShutdownEnvironment(txtEnvProcessor * somSelf)
  276. {
  277.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  278.     envProcessor *lastEnv;
  279.     txtEnvProcessorMethodDebug("txtEnvProcessor", "epShutdownEnvironment");
  280.  
  281.     lastEnv = _epGetPreviousEnv(somSelf);
  282.     if (lastEnv)
  283.     _epInitializeEnvironment(lastEnv);
  284. }
  285.  
  286. /* ************************************************************ */
  287. SOM_Scope void SOMLINK epInitializeEnvironment(txtEnvProcessor * somSelf)
  288. {
  289.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  290.     int charsToIndent;
  291.     TPWord *blanks;
  292.     TPWord *lineFeed;
  293.     textLine *thisLine;
  294.     fileMgr *thisFile;
  295.  
  296.     txtEnvProcessorMethodDebug("txtEnvProcessor",
  297.                    "epInitializeEnvironment");
  298.  
  299.     thisFile = _epGetFileMgr(somSelf);
  300.     lineFeed = readToken(thisFile);
  301.     _somFree(lineFeed);
  302.     charsToIndent = _epGetParagraphIndentation(somSelf);
  303.     _tpProcessNewLine(somSelf);
  304.  
  305.     thisLine = _tpGetLine(somSelf);
  306.     if (charsToIndent) {
  307.     blanks = TPWordNew();
  308.     _wordInit2(blanks, ' ', charsToIndent);
  309.     _llAddTail(thisLine, (baseType *) blanks);
  310.     }
  311. }
  312.  
  313. /* ************************************************************ */
  314. SOM_Scope void SOMLINK epPrepareForNewEnvironment(txtEnvProcessor * somSelf)
  315. {
  316.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  317.     txtEnvProcessorMethodDebug("txtEnvProcessor",
  318.                    "epPrepareForNewEnvironment");
  319.     _tpProcessFullLine(somSelf);
  320. }
  321.  
  322. /* ************************************************************ */
  323. SOM_Scope long  SOMLINK epGetParagraphIndentation(txtEnvProcessor *somSelf)
  324. {
  325.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  326.     envProcessor *previousEnv;
  327.     txtEnvProcessorMethodDebug("txtEnvProcessor",
  328.                    "epGetParagraphIndentation");
  329.  
  330.     previousEnv = _epGetPreviousEnv(somSelf);
  331.     if (previousEnv)
  332.     return _epGetParagraphIndentation(previousEnv) +
  333.         PARAGRAPH_INDENTATION;
  334.     else
  335.     return 0;
  336. }
  337.  
  338. /* ************************************************************ */
  339. SOM_Scope long  SOMLINK epGetLineIndentation(txtEnvProcessor *somSelf)
  340. {
  341.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  342.     envProcessor *previousEnv;
  343.     txtEnvProcessorMethodDebug("txtEnvProcessor", "epGetLineIndentation");
  344.  
  345.     previousEnv = _epGetPreviousEnv(somSelf);
  346.     if (previousEnv)
  347.     return _epGetLineIndentation(previousEnv) +
  348.         LINE_INDENTATION;
  349.     else
  350.     return 0;
  351. }
  352.  
  353. /* ************************************************************ */
  354. SOM_Scope void SOMLINK somUninit(txtEnvProcessor * somSelf)
  355. {
  356.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  357.     txtEnvProcessorMethodDebug("txtEnvProcessor", "somUninit");
  358.     if (_thisWord)
  359.     _somFree(_thisWord);
  360.     if (_thisLine)
  361.     _somFree(_thisLine);
  362.     parent_somUninit(somSelf);
  363. }
  364.  
  365. /* ************************************************************ */
  366. SOM_Scope void SOMLINK somInit(txtEnvProcessor * somSelf)
  367. {
  368.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  369.     txtEnvProcessorMethodDebug("txtEnvProcessor", "somInit");
  370.     parent_somInit(somSelf);
  371.  
  372.     _thisWord = 0;
  373.     _thisLine = 0;
  374.     _thisCB = 0;
  375. }
  376.  
  377. /* ************************************************************ */
  378. SOM_Scope void SOMLINK epInit1(txtEnvProcessor * somSelf,
  379.                 envProcessor * lastEnv)
  380. {
  381.     txtEnvProcessorData *somThis = txtEnvProcessorGetData(somSelf);
  382.     page *thisPage;
  383.  
  384.     txtEnvProcessorMethodDebug("txtEnvProcessor", "epInit1");
  385.     parent_epInit1(somSelf, lastEnv);
  386.  
  387.     thisPage = _epGetPage(somSelf);
  388.     _thisCB = _pgGetColumnBlock(thisPage);
  389. }
  390.