home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / books / csc.db < prev    next >
Encoding:
Text File  |  1991-03-01  |  7.7 MB  |  214,554 lines

Text Truncated. Only the first 1MB is shown below. Download the file for the complete contents.
  1. %@1@%%@AH@%Microsoft C Optimizing Compiler - v6.0%@EH@%%@AE@%
  2. %@NL@%
  3. %@NL@%
  4. %@2@%%@AH@%CHRTDEMO.C%@AE@%%@EH@%%@NL@%
  5. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\MSC60\CHRTDEMO.C%@AE@%%@NL@%
  6. %@NL@%
  7. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  8. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  9. %@AI@%#include %@AE@%<conio.h> %@NL@%
  10. %@AI@%#include %@AE@%<string.h> %@NL@%
  11. %@AI@%#include %@AE@%<graph.h> %@NL@%
  12. %@AI@%#include %@AE@%<pgchart.h> %@NL@%
  13. %@AI@%#include %@AE@%"chrtdemo.h" %@NL@%
  14. %@NL@%
  15. %@AB@%/* Structures for system configuration and chart environment. */%@AE@%%@NL@%
  16. struct videoconfig vc;%@NL@%
  17. chartenv ce;%@NL@%
  18. %@NL@%
  19. %@AB@%/* Category variables.  */%@AE@%%@NL@%
  20. short cCat;%@NL@%
  21. char _far *aCat[MAXVALUES];%@NL@%
  22. %@NL@%
  23. %@AB@%/* Series variables.  */%@AE@%%@NL@%
  24. short cSeries;%@NL@%
  25. short _far acSeries[MAXSERIES];%@NL@%
  26. char _far *aSeriesName[MAXSERIES];%@NL@%
  27. %@NL@%
  28. %@AB@%/* Temporary holding array for all data. Data values for multi-series%@NL@%
  29. %@AB@% * bar, column, and line charts remain here. Data for other kinds of%@NL@%
  30. %@AB@% * charts are transferred to the arrays below.%@NL@%
  31. %@AB@% */%@AE@%%@NL@%
  32. float _far aValue[MAXSERIES][MAXVALUES];%@NL@%
  33. %@NL@%
  34. %@AB@%/* Data values for single-series charts. First array is used for%@NL@%
  35. %@AB@% * bar, column, line, and pie. First and second are both used for%@NL@%
  36. %@AB@% * single-series scatter.%@NL@%
  37. %@AB@% */%@AE@%%@NL@%
  38. float _far axValue[MAXVALUES];%@NL@%
  39. float _far ayValue[MAXVALUES];%@NL@%
  40. %@NL@%
  41. %@AB@%/* Data values for multi-series scatter charts.  */%@AE@%%@NL@%
  42. float _far axValueMS[MAXVALUES][MAXVALUES];%@NL@%
  43. float _far ayValueMS[MAXVALUES][MAXVALUES];%@NL@%
  44. %@NL@%
  45. %@AB@%/* Exploded flags for pie chart.  */%@AE@%%@NL@%
  46. short _far aExplode[MAXVALUES];%@NL@%
  47. %@NL@%
  48. %@AB@%/* Variable used to track control and screen position.  */%@AE@%%@NL@%
  49. struct SCREENINFO si;%@NL@%
  50. %@NL@%
  51. %@AB@%/* Colors of menus and prompts. */%@AE@%%@NL@%
  52. struct tagColor co;%@NL@%
  53. %@NL@%
  54. %@AB@%/* Flags to indicate whether to use imported or default data.  */%@AE@%%@NL@%
  55. BOOL fDefault = TRUE;%@NL@%
  56. %@NL@%
  57. %@AB@%/* Arrays of strings used by the Menu function. The first string is the%@NL@%
  58. %@AB@% * menu title. The next non-null strings are the menu selections. A null%@NL@%
  59. %@AB@% * string indicates the end of the list.%@NL@%
  60. %@AB@% */%@AE@%%@NL@%
  61. char *pszChartOpt[] =%@NL@%
  62.     { "Options", "Screen Mode", "Windows", "Titles",%@NL@%
  63.       "Axis (X and Y)", "Legend", "Fonts", "Reset", "" };%@NL@%
  64. %@NL@%
  65. char *pszChartType[] =%@NL@%
  66.     { "Type", "Bar", "Column", "Line", "Scatter", "Pie", "" };%@NL@%
  67. %@NL@%
  68. char *pszMainMenu[] =%@NL@%
  69.     { "Main Menu", "Demo", "View Chart", "Chart Type", "Options",%@NL@%
  70.       "Show Chart Data", "Quit", "" };%@NL@%
  71. %@NL@%
  72. %@AB@%/* Sample data.  */%@AE@%%@NL@%
  73. %@AI@%#define %@AE@%O_JUICE 0 %@NL@%
  74. %@AI@%#define %@AE@%I_TEA   1 %@NL@%
  75. %@AI@%#define %@AE@%H_CHOC  2 %@NL@%
  76. %@AI@%#define %@AE@%TEMPERATURE  3 %@NL@%
  77. %@NL@%
  78. char _far *aQuarters[] =%@NL@%
  79.     { "First", "Second", "Third", "Fourth" };%@NL@%
  80. %@NL@%
  81. char _far *aMonths[] =%@NL@%
  82.     { "Jan", "Feb", "Mar", "Apr", "May", "Jun",%@NL@%
  83.       "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };%@NL@%
  84. %@NL@%
  85. float _far aSales[3][12] =%@NL@%
  86. {%@NL@%
  87.     {  3.6F,  3.2F,  3.3F,  3.4F,  3.1F,  2.9F,%@NL@%
  88.        3.0F,  3.6F,  3.2F,  3.3F,  3.5F,  3.9F },%@NL@%
  89.     {  1.0F,  1.3F,  1.4F,  1.7F,  2.2F,  2.9F,%@NL@%
  90.        2.9F,  3.1F,  2.6F,  1.8F,  1.1F,  1.2F },%@NL@%
  91.     {  2.4F,  2.3F,  2.0F,  1.6F,  1.3F,  1.0F,%@NL@%
  92.        0.9F,  0.8F,  1.1F,  1.4F,  1.9F,  2.5F }%@NL@%
  93. };%@NL@%
  94.   %@NL@%
  95. float _far aTemperature[12] =%@NL@%
  96.     {  2.9F,  3.2F,  3.9F,  4.8F,  6.0F,  6.5F,%@NL@%
  97.        7.0F,  7.2F,  6.0F,  4.7F,  4.1F,  3.0F };%@NL@%
  98. %@NL@%
  99. char _far *aSalesTitles[] =%@NL@%
  100.     { "Orange Juice Sales", "Iced Tea Sales", "Hot Chocolate Sales" };%@NL@%
  101. %@NL@%
  102. char *TempTitle = "Average Temperature";%@NL@%
  103. %@NL@%
  104. int main()%@NL@%
  105. {%@NL@%
  106.     Initialize();%@NL@%
  107.     MainMenu();%@NL@%
  108. %@NL@%
  109.     %@AB@%/* Reset the video mode and screen colors prior to leaving. */%@AE@%%@NL@%
  110.     _setvideomode( _DEFAULTMODE );%@NL@%
  111.     _settextcolor( co.InfoColor );%@NL@%
  112.     _clearscreen( _GCLEARSCREEN );%@NL@%
  113. %@NL@%
  114.     return 0;%@NL@%
  115. }%@NL@%
  116. %@NL@%
  117. %@AB@%/*  ChartOptions - Gets chart options.%@NL@%
  118. %@AB@% *%@NL@%
  119. %@AB@% *  Params: None%@NL@%
  120. %@AB@% */%@AE@%%@NL@%
  121. void ChartOptions()%@NL@%
  122. {%@NL@%
  123.     int iChoice;%@NL@%
  124. %@NL@%
  125.     PushTitle( pszChartOpt[0] );%@NL@%
  126.     while( (iChoice = Menu( pszChartOpt )) != ESCAPE )%@NL@%
  127.     {%@NL@%
  128. %@NL@%
  129.         %@AB@%/* Get chart options.  */%@AE@%%@NL@%
  130.         switch( iChoice )%@NL@%
  131.         {%@NL@%
  132. %@NL@%
  133.             %@AB@%/* Branch to the appropriate menu.  */%@AE@%%@NL@%
  134.             case 1:%@NL@%
  135.                 ScreenMode();%@NL@%
  136.                 break;%@NL@%
  137. %@NL@%
  138.             case 2:%@NL@%
  139.                 Windows();%@NL@%
  140.                 break;%@NL@%
  141. %@NL@%
  142.             case 3:%@NL@%
  143.                 Titles();%@NL@%
  144.                 break;%@NL@%
  145. %@NL@%
  146.             case 4:%@NL@%
  147.                 Axes();%@NL@%
  148.                 break;%@NL@%
  149. %@NL@%
  150.             case 5:%@NL@%
  151.                 Legend();%@NL@%
  152.                 break;%@NL@%
  153. %@NL@%
  154.             case 6:%@NL@%
  155.                 FontOptions();%@NL@%
  156.                 break;%@NL@%
  157. %@NL@%
  158.             case 7:%@NL@%
  159.                 ResetOptions();%@NL@%
  160.                 break;%@NL@%
  161. %@NL@%
  162.         }%@NL@%
  163.     }%@NL@%
  164.     PopTitle();%@NL@%
  165. }%@NL@%
  166. %@NL@%
  167. %@AB@%/*  ChartType - Gets chart type.%@NL@%
  168. %@AB@% *%@NL@%
  169. %@AB@% *  Params: None%@NL@%
  170. %@AB@% */%@AE@%%@NL@%
  171. void ChartType()%@NL@%
  172. {%@NL@%
  173.     int iChoice;%@NL@%
  174. %@NL@%
  175.     %@AB@%/* Get chart type.  */%@AE@%%@NL@%
  176.     PushTitle( pszChartType[0] );%@NL@%
  177.     iChoice = Menu( pszChartType );%@NL@%
  178. %@NL@%
  179.     if( iChoice != ESCAPE )%@NL@%
  180.     {%@NL@%
  181.         %@AB@%/* Set the chart type, and get the chart style.  */%@AE@%%@NL@%
  182.         ce.charttype = iChoice;%@NL@%
  183.         switch( iChoice )%@NL@%
  184.         {%@NL@%
  185. %@NL@%
  186.             case 1:%@NL@%
  187.             case 2:%@NL@%
  188.                 iChoice = BlankMenu( "Style", "Plain Bars", "Stacked Bars" );%@NL@%
  189.                 break;%@NL@%
  190. %@NL@%
  191.             case 3:%@NL@%
  192.             case 4:%@NL@%
  193.                 iChoice = BlankMenu( "Style", "Lines-Points", "Points Only" );%@NL@%
  194.                 break;%@NL@%
  195. %@NL@%
  196.             case 5:%@NL@%
  197.                 iChoice = BlankMenu( "Style", "Percent", "No Percent" );%@NL@%
  198.         }%@NL@%
  199. %@NL@%
  200.         if( iChoice != ESCAPE)%@NL@%
  201.             ce.chartstyle = iChoice;%@NL@%
  202. %@NL@%
  203.         %@AB@%/* Set default data without changing environment defaults.  */%@AE@%%@NL@%
  204.         DefaultData( ce.charttype, ce.chartstyle, FALSE );%@NL@%
  205.         PopTitle();%@NL@%
  206.     }%@NL@%
  207.     PopTitle();%@NL@%
  208. }%@NL@%
  209. %@NL@%
  210. %@AB@%/*  ClearData - Clears category and value data.%@NL@%
  211. %@AB@% *%@NL@%
  212. %@AB@% *  Params: fConfirm - flag specifying whether to query for confirmation%@NL@%
  213. %@AB@% */%@AE@%%@NL@%
  214. void ClearData( BOOL fConfirm )%@NL@%
  215. {%@NL@%
  216.     char chResponse = 'Y';%@NL@%
  217.     int iCat;%@NL@%
  218. %@NL@%
  219.     WrtForm( 18 );%@NL@%
  220. %@NL@%
  221.     %@AB@%/* Query for confirmation.  */%@AE@%%@NL@%
  222.     if( fConfirm )%@NL@%
  223.         chResponse = InputCh( "Are you sure? ", "YN\x1b" );%@NL@%
  224. %@NL@%
  225.     if( chResponse == 'Y' )%@NL@%
  226.     {%@NL@%
  227. %@NL@%
  228.         %@AB@%/* Clear all relevant data.  */%@AE@%%@NL@%
  229.         for( iCat = 0; iCat < cCat; iCat++ )%@NL@%
  230.             aCat[iCat] = NULL;%@NL@%
  231.         cCat = 0;%@NL@%
  232.         cSeries = 0;%@NL@%
  233.     }%@NL@%
  234. }%@NL@%
  235. %@NL@%
  236. %@AB@%/*  DefaultData - Initializes default data for each kind of chart.%@NL@%
  237. %@AB@% *%@NL@%
  238. %@AB@% *  Params: iType - Chart type to be initialized%@NL@%
  239. %@AB@% *          iStyle - Chart style%@NL@%
  240. %@AB@% *          fClear - Signal to clear all defaults%@NL@%
  241. %@AB@% */%@AE@%%@NL@%
  242. void DefaultData( short iType, short iStyle, BOOL fClear )%@NL@%
  243. {%@NL@%
  244.     int iCat, iValue, iSubValue, iSeries;%@NL@%
  245. %@NL@%
  246.     %@AB@%/* Call default chart to clear old values.  */%@AE@%%@NL@%
  247.     if( fClear )%@NL@%
  248.         _pg_defaultchart( &ce, iType, iStyle );%@NL@%
  249. %@NL@%
  250.     %@AB@%/* Initialize category titles.  */%@AE@%%@NL@%
  251.     cCat = 12;%@NL@%
  252.     for( iCat = 0; iCat < cCat; iCat++ )%@NL@%
  253.         aCat[iCat] = aMonths[iCat];%@NL@%
  254. %@NL@%
  255.     switch( ce.charttype )%@NL@%
  256.     {%@NL@%
  257. %@NL@%
  258.         %@AB@%/* Initialize data for each chart type.  */%@AE@%%@NL@%
  259.         case _PG_BARCHART:%@NL@%
  260.         %@NL@%
  261.             strcpy( ce.maintitle.title, "Orange Juice and Iced Tea Sales" );%@NL@%
  262.             cSeries = 2;%@NL@%
  263.             for( iSeries = 0; iSeries < cSeries; iSeries++ )%@NL@%
  264.             {%@NL@%
  265.                 aSeriesName[iSeries] = aSalesTitles[iSeries];%@NL@%
  266.                 acSeries[iSeries] = cCat;%@NL@%
  267.                 for( iValue = 0; iValue < cCat; iValue++ )%@NL@%
  268.                     aValue[iSeries][iValue]  = (float)aSales[iSeries][iValue];%@NL@%
  269.             }%@NL@%
  270.             break;%@NL@%
  271. %@NL@%
  272.         case _PG_COLUMNCHART:%@NL@%
  273.        %@NL@%
  274.             strcpy( ce.maintitle.title, "Orange Juice Sales" );%@NL@%
  275.             cSeries = 1;%@NL@%
  276.             for( iSeries = 0; iSeries < cSeries; iSeries++ )%@NL@%
  277.             {%@NL@%
  278.                 aSeriesName[iSeries] = aSalesTitles[iSeries];%@NL@%
  279.                 acSeries[iSeries] = cCat;%@NL@%
  280.                 for( iValue = 0; iValue < cCat; iValue++ )%@NL@%
  281.                     aValue[iSeries][iValue]  = (float)aSales[iSeries][iValue];%@NL@%
  282.             }%@NL@%
  283.             break;%@NL@%
  284. %@NL@%
  285.         case _PG_LINECHART:%@NL@%
  286. %@NL@%
  287.             strcpy( ce.maintitle.title, "Beverage Sales" );%@NL@%
  288.             cSeries = 3;%@NL@%
  289.             for( iSeries = 0; iSeries < cSeries; iSeries++ )%@NL@%
  290.             {%@NL@%
  291.                 aSeriesName[iSeries] = aSalesTitles[iSeries];%@NL@%
  292.                 acSeries[iSeries] = cCat;%@NL@%
  293.                 for( iValue = 0; iValue < cCat; iValue++ )%@NL@%
  294.                     aValue[iSeries][iValue] = (float)aSales[iSeries][iValue];%@NL@%
  295.             }%@NL@%
  296.             break;%@NL@%
  297. %@NL@%
  298.         case _PG_SCATTERCHART:%@NL@%
  299. %@NL@%
  300.             strcpy( ce.maintitle.title,%@NL@%
  301.                     "Average Temperature Compared to Beverage Sales" );%@NL@%
  302.             %@AB@%/* ce.chartstyle = _PG_POINTONLY; */%@AE@%%@NL@%
  303.             cSeries = 4;%@NL@%
  304. %@NL@%
  305.             aSeriesName[0] = aSalesTitles[I_TEA];%@NL@%
  306.             aSeriesName[2] = aSalesTitles[H_CHOC];%@NL@%
  307.             acSeries[0] = acSeries[1] = acSeries[2] = acSeries[3] = 12;%@NL@%
  308.             for( iValue = 0; iValue < 12; iValue++ )%@NL@%
  309.             {%@NL@%
  310.                 aValue[0][iValue]  = (float)aSales[I_TEA][iValue] ;%@NL@%
  311.                 aValue[1][iValue]  = (float)aSales[TEMPERATURE][iValue] ;%@NL@%
  312.                 aValue[2][iValue]  = (float)aSales[H_CHOC][iValue] ;%@NL@%
  313.                 aValue[3][iValue]  = (float)aSales[TEMPERATURE][iValue] ;%@NL@%
  314.             }%@NL@%
  315.             break;%@NL@%
  316.            %@NL@%
  317.         case _PG_PIECHART:%@NL@%
  318.         default:%@NL@%
  319. %@NL@%
  320.             strcpy( ce.maintitle.title, "Iced Tea Sales" );%@NL@%
  321.             cCat = 4;%@NL@%
  322.             for( iCat = 0; iCat < cCat; iCat++ )%@NL@%
  323.                 aCat[iCat] = aQuarters[iCat];%@NL@%
  324. %@NL@%
  325.             cSeries = 1;%@NL@%
  326.             aSeriesName[0] = aSalesTitles[I_TEA];%@NL@%
  327.             acSeries[0] = cCat;%@NL@%
  328. %@NL@%
  329.             for( iValue = 0; iValue < cCat; iValue++ )%@NL@%
  330.             {%@NL@%
  331.                 aValue[0][iValue] = 0.0;%@NL@%
  332.                 for( iSubValue = 0; iSubValue < 3; iSubValue++ )%@NL@%
  333.                     aValue[0][iValue] += (float)aSales[I_TEA][iSubValue * iValue];%@NL@%
  334.             }%@NL@%
  335.             aExplode[3] = 1;%@NL@%
  336.             break;%@NL@%
  337.     }%@NL@%
  338. }%@NL@%
  339. %@NL@%
  340. %@AB@%/*  Demo - Displays a series of sample charts.%@NL@%
  341. %@AB@% *%@NL@%
  342. %@AB@% *  Params: None%@NL@%
  343. %@AB@% */%@AE@%%@NL@%
  344. void Demo()%@NL@%
  345. {%@NL@%
  346.     int  cValue;%@NL@%
  347.     palettetype palette_struct;%@NL@%
  348. %@NL@%
  349.     %@AB@%/* Display the sample data in spreadsheet form. */%@AE@%%@NL@%
  350.     ShowSampleData();%@NL@%
  351. %@NL@%
  352.     DefaultData( _PG_PIECHART, _PG_NOPERCENT, TRUE );%@NL@%
  353. %@NL@%
  354.     %@AB@%/* Set video mode and draw charts. For each chart, set default%@NL@%
  355. %@AB@%     * data and modify any desired environment fields. If error,%@NL@%
  356. %@AB@%     * terminate demo.%@NL@%
  357. %@AB@%     */%@AE@%%@NL@%
  358.     _setvideomode( si.mode );%@NL@%
  359. %@NL@%
  360.     cValue = 4;%@NL@%
  361.     strcpy( ce.subtitle.title, "Default Pie Chart" );%@NL@%
  362.     if( ViewChart() )%@NL@%
  363.         return;%@NL@%
  364.     else%@NL@%
  365.         _clearscreen( _GCLEARSCREEN );%@NL@%
  366. %@NL@%
  367.     strcpy( ce.subtitle.title, "Customized Pie Chart" );%@NL@%
  368.     ce.chartstyle = _PG_PERCENT;%@NL@%
  369.     ce.legend.place = _PG_BOTTOM;%@NL@%
  370.     if (si.fColor)%@NL@%
  371.     {%@NL@%
  372.         ce.maintitle.titlecolor = ce.subtitle.titlecolor = 0;%@NL@%
  373.         ce.chartwindow.background = 1;%@NL@%
  374.         ce.datawindow.background = ce.legend.legendwindow.background = 6;%@NL@%
  375.         ce.legend.textcolor = 1;%@NL@%
  376.     }%@NL@%
  377.     if( ViewChart() )%@NL@%
  378.         return;%@NL@%
  379.     else%@NL@%
  380.         _clearscreen( _GCLEARSCREEN );%@NL@%
  381. %@NL@%
  382.     cValue = 12;%@NL@%
  383.     DefaultData( _PG_BARCHART, _PG_PLAINBARS, TRUE );%@NL@%
  384.     strcpy( ce.subtitle.title, "Default Bar Chart" );%@NL@%
  385.     if( ViewChart() )%@NL@%
  386.         return;%@NL@%
  387.     else%@NL@%
  388.         _clearscreen( _GCLEARSCREEN );%@NL@%
  389. %@NL@%
  390.     strcpy( ce.subtitle.title, "Customized Stacked Bar Chart" );%@NL@%
  391.     strcpy( ce.xaxis.axistitle.title, "Sales in Thousands" );%@NL@%
  392.     strcpy( ce.yaxis.axistitle.title, "Month" );%@NL@%
  393.     ce.chartstyle = _PG_STACKEDBARS;%@NL@%
  394.     ce.legend.place = _PG_RIGHT;%@NL@%
  395.     ce.xaxis.ticdecimals = 2;%@NL@%
  396.     if (si.fColor)%@NL@%
  397.     {%@NL@%
  398.         ce.maintitle.titlecolor = ce.subtitle.titlecolor = 12;%@NL@%
  399.         ce.chartwindow.background = 7;%@NL@%
  400.         ce.datawindow.background = 8;%@NL@%
  401.         ce.legend.textcolor = 0;%@NL@%
  402.         ce.legend.legendwindow.background = 8;%@NL@%
  403.         ce.legend.autosize = FALSE;%@NL@%
  404.         ce.legend.legendwindow.y1 = vc.numypixels - 85;%@NL@%
  405.         ce.legend.legendwindow.y2 = vc.numypixels - 45;%@NL@%
  406.     }%@NL@%
  407.     if( ViewChart() )%@NL@%
  408.         return;%@NL@%
  409.     else%@NL@%
  410.         _clearscreen( _GCLEARSCREEN );%@NL@%
  411. %@NL@%
  412.     DefaultData( _PG_COLUMNCHART, _PG_PLAINBARS, TRUE );%@NL@%
  413.     strcpy( ce.subtitle.title, "Default Column Chart" );%@NL@%
  414.     if( ViewChart() )%@NL@%
  415.         return;%@NL@%
  416.     else%@NL@%
  417.         _clearscreen( _GCLEARSCREEN );%@NL@%
  418. %@NL@%
  419.     strcpy( ce.subtitle.title, "Customized Column Chart" );%@NL@%
  420.     strcpy( ce.xaxis.axistitle.title, "Month" );%@NL@%
  421.     strcpy( ce.yaxis.axistitle.title, "Sales in Thousands" );%@NL@%
  422.     ce.legend.place = _PG_BOTTOM;%@NL@%
  423.     if (si.fColor)%@NL@%
  424.     {%@NL@%
  425.         ce.maintitle.titlecolor     = 0;%@NL@%
  426.         ce.subtitle.titlecolor     = 0;%@NL@%
  427.         ce.chartwindow.background = 8;%@NL@%
  428.         ce.datawindow.background = 2;%@NL@%
  429.         ce.legend.legendwindow.background = 10;%@NL@%
  430.     }%@NL@%
  431.     if( ViewChart() )%@NL@%
  432.         return;%@NL@%
  433.     else%@NL@%
  434.         _clearscreen( _GCLEARSCREEN );%@NL@%
  435. %@NL@%
  436.     DefaultData( _PG_LINECHART, _PG_POINTANDLINE, TRUE );%@NL@%
  437.     strcpy( ce.subtitle.title, "Default Line Chart" );%@NL@%
  438.     if( ViewChart() )%@NL@%
  439.         return;%@NL@%
  440.     else%@NL@%
  441.         _clearscreen( _GCLEARSCREEN );%@NL@%
  442. %@NL@%
  443.     strcpy( ce.subtitle.title, "Customized Line Chart" );%@NL@%
  444.     strcpy( ce.xaxis.axistitle.title, "Month" );%@NL@%
  445.     strcpy( ce.yaxis.axistitle.title, "Sales in Thousands" );%@NL@%
  446.     ce.legend.place = _PG_RIGHT;%@NL@%
  447.     if (si.fColor)%@NL@%
  448.     {%@NL@%
  449.         ce.maintitle.titlecolor = 1;%@NL@%
  450.         ce.subtitle.titlecolor = 1;%@NL@%
  451.         ce.chartwindow.background = 3;%@NL@%
  452.         ce.datawindow.background = 7;%@NL@%
  453.         ce.legend.legendwindow.background = 7;%@NL@%
  454.         ce.legend.autosize = FALSE;%@NL@%
  455.         ce.legend.legendwindow.y1 = vc.numypixels - 85;%@NL@%
  456.         ce.legend.legendwindow.y2 = vc.numypixels - 45;%@NL@%
  457.     }%@NL@%
  458.     if( ViewChart() )%@NL@%
  459.         return;%@NL@%
  460.     else%@NL@%
  461.         _clearscreen( _GCLEARSCREEN );%@NL@%
  462. %@NL@%
  463.     DefaultData( _PG_SCATTERCHART, _PG_POINTONLY, TRUE );%@NL@%
  464.     strcpy( ce.subtitle.title, "Default Scatter Chart" );%@NL@%
  465.     if( ViewChart() )%@NL@%
  466.         return;%@NL@%
  467.     else%@NL@%
  468.         _clearscreen( _GCLEARSCREEN );%@NL@%
  469. %@NL@%
  470.     cSeries = 4;%@NL@%
  471.     strcpy( ce.subtitle.title, "Customized Scatter Chart" );%@NL@%
  472.     strcpy( ce.xaxis.axistitle.title, "Sales in Thousands" );%@NL@%
  473.     strcpy( ce.yaxis.axistitle.title, "Average Temperature" );%@NL@%
  474.     ce.legend.place = _PG_RIGHT;%@NL@%
  475.     if (si.fColor)%@NL@%
  476.     {%@NL@%
  477.         ce.maintitle.titlecolor = 0;%@NL@%
  478.         ce.subtitle.titlecolor = 0;%@NL@%
  479.         ce.chartwindow.background = 4;%@NL@%
  480.         ce.datawindow.background = 8;%@NL@%
  481.         ce.legend.legendwindow.background = 8;%@NL@%
  482.         ce.legend.autosize = FALSE;%@NL@%
  483.         ce.legend.legendwindow.y1 = vc.numypixels - 85;%@NL@%
  484.         ce.legend.legendwindow.y2 = vc.numypixels - 45;%@NL@%
  485.     }%@NL@%
  486.     if( ViewChart() )%@NL@%
  487.         return;%@NL@%
  488.     else%@NL@%
  489.         _clearscreen( _GCLEARSCREEN );%@NL@%
  490. %@NL@%
  491.     DefaultData( _PG_BARCHART, _PG_PERCENT, TRUE );%@NL@%
  492. }%@NL@%
  493. %@NL@%
  494. %@AB@%/*  FindVideoMode - Finds the "best" video mode for the adaptor in use.%@NL@%
  495. %@AB@% *%@NL@%
  496. %@AB@% *  Params: vc - structure of type struct videoconfig%@NL@%
  497. %@AB@% *%@NL@%
  498. %@AB@% *  Returns: Best mode%@NL@%
  499. %@AB@% */%@AE@%%@NL@%
  500. int FindVideoMode( struct videoconfig vc )%@NL@%
  501. {%@NL@%
  502.     switch( vc.adapter )%@NL@%
  503.     {%@NL@%
  504.         case _CGA:%@NL@%
  505.         case _OCGA:%@NL@%
  506.             return _HRESBW;%@NL@%
  507.         case _EGA:%@NL@%
  508.                 case _OEGA:%@NL@%
  509.                         return( vc.monitor == _MONO ) ? _ERESNOCOLOR : _ERESCOLOR;%@NL@%
  510.         case _VGA:%@NL@%
  511.         case _OVGA:%@NL@%
  512.         case _MCGA:%@NL@%
  513.             return _VRES16COLOR;%@NL@%
  514.         case _HGC:%@NL@%
  515.             return _HERCMONO;%@NL@%
  516.         default:%@NL@%
  517.             return _DEFAULTMODE;%@NL@%
  518.     }%@NL@%
  519. }%@NL@%
  520. %@NL@%
  521. %@AB@%/*  Initialize - Does various initialization tasks.%@NL@%
  522. %@AB@% *%@NL@%
  523. %@AB@% *  Params: None%@NL@%
  524. %@AB@% */%@AE@%%@NL@%
  525. void Initialize( void )%@NL@%
  526. {%@NL@%
  527.     int iSeries, iValue;%@NL@%
  528. %@NL@%
  529.     %@AB@%/* Initialize all value arrays to missing.  */%@AE@%%@NL@%
  530.     for( iSeries = 0; iSeries < MAXSERIES; iSeries++ )%@NL@%
  531.     {%@NL@%
  532. %@NL@%
  533.         axValue[iSeries] = _PG_MISSINGVALUE;%@NL@%
  534.         ayValue[iSeries] = _PG_MISSINGVALUE;%@NL@%
  535. %@NL@%
  536.         for( iValue = 0; iValue < MAXVALUES; iValue++ )%@NL@%
  537.             aValue[iSeries][iValue] = _PG_MISSINGVALUE;%@NL@%
  538. %@NL@%
  539.         for( iValue = 0; iValue < MAXVALUES; iValue++ )%@NL@%
  540.         {%@NL@%
  541.             axValueMS[iSeries][iValue] = _PG_MISSINGVALUE;%@NL@%
  542.             ayValueMS[iSeries][iValue] = _PG_MISSINGVALUE;%@NL@%
  543.         }%@NL@%
  544.     }%@NL@%
  545. %@NL@%
  546.     %@AB@%/* Initialize zero sets. */%@AE@%%@NL@%
  547.     cSeries = 0;%@NL@%
  548. %@NL@%
  549.     %@AB@%/* Initialize default chart environment, screen mode, and data.  */%@AE@%%@NL@%
  550.     _pg_initchart();%@NL@%
  551.     _getvideoconfig( &vc );%@NL@%
  552. %@NL@%
  553.     %@AB@%/* Find the best available mode for display.%@NL@%
  554. %@AB@%     * Don't set 256 color, medium resolution (_MRES256COLOR).%@NL@%
  555. %@AB@%     */%@AE@%%@NL@%
  556.     si.mode = FindVideoMode( vc );%@NL@%
  557. %@NL@%
  558.     if( si.mode == _TEXTMONO )%@NL@%
  559.     {%@NL@%
  560.         _clearscreen( _GCLEARSCREEN );%@NL@%
  561.         _outtext( "No graphics available.  Can't run chart demo." );%@NL@%
  562.         exit( 1 );%@NL@%
  563.     }%@NL@%
  564. %@NL@%
  565.     SetDisplayColors();%@NL@%
  566. %@NL@%
  567.     SetGraphMode( si.mode );%@NL@%
  568.     DefaultData( _PG_BARCHART, _PG_PLAINBARS, TRUE );%@NL@%
  569. %@NL@%
  570.     _setvideomode( _DEFAULTMODE );%@NL@%
  571. %@NL@%
  572. %@NL@%
  573. }%@NL@%
  574. %@NL@%
  575. %@AB@%/*  MainMenu - Manages the main menu.%@NL@%
  576. %@AB@% *%@NL@%
  577. %@AB@% *  Params: None%@NL@%
  578. %@AB@% */%@AE@%%@NL@%
  579. void MainMenu( void )%@NL@%
  580. {%@NL@%
  581.     int iChoice;%@NL@%
  582.     char chResponse = 'Y';%@NL@%
  583.     char chVerify;%@NL@%
  584. %@NL@%
  585.     PushTitle( pszMainMenu[0] );%@NL@%
  586.     do%@NL@%
  587.     {%@NL@%
  588.         %@AB@%/* If the user selects Quit, iChoice will contain 6.  If the%@NL@%
  589. %@AB@%         *   user presses ESCAPE, iChoice will be ESCAPE, which is%@NL@%
  590. %@AB@%         *   equal to 27.  In any case, we can test both conditions%@NL@%
  591. %@AB@%         *   by checking to see whether iChoice is less than 6.%@NL@%
  592. %@AB@%         */%@AE@%%@NL@%
  593.         while( (iChoice = Menu( pszMainMenu )) < 6 )%@NL@%
  594.         {%@NL@%
  595.             %@AB@%/* Get main menu selection.  */%@AE@%%@NL@%
  596.             switch( iChoice )%@NL@%
  597.             {%@NL@%
  598. %@NL@%
  599.                 case 1:%@NL@%
  600.                     %@AB@%/* Display demo charts.  */%@AE@%%@NL@%
  601.                     Demo();%@NL@%
  602.                     _setvideomode( _DEFAULTMODE );%@NL@%
  603.                     break;%@NL@%
  604. %@NL@%
  605.                 case 2:%@NL@%
  606.                     %@AB@%/* Set graphics video mode, display current chart,%@NL@%
  607. %@AB@%                     * and restore text video mode.%@NL@%
  608. %@AB@%                     */%@AE@%%@NL@%
  609.                     _setvideomode( si.mode );%@NL@%
  610.                     ViewChart();%@NL@%
  611.                     _setvideomode( _DEFAULTMODE );%@NL@%
  612.                     break;%@NL@%
  613. %@NL@%
  614.                 case 3:%@NL@%
  615.                     %@AB@%/* Get chart type and style.  */%@AE@%%@NL@%
  616.                     ChartType();%@NL@%
  617.                     break;%@NL@%
  618. %@NL@%
  619.                 case 4:%@NL@%
  620.                     %@AB@%/* Get chart options.  */%@AE@%%@NL@%
  621.                     ChartOptions();%@NL@%
  622.                     break;%@NL@%
  623.                 case 5:%@NL@%
  624.                     %@AB@%/* Show chart data. */%@AE@%%@NL@%
  625.                     ShowChartData();%@NL@%
  626.                     break;%@NL@%
  627. %@NL@%
  628.             }%@NL@%
  629. %@NL@%
  630.         }%@NL@%
  631. %@NL@%
  632.         %@AB@%/* If the user is trying to leave the program using the ESCAPE%@NL@%
  633. %@AB@%         *   key, we must verify the choice.  This is done to prevent%@NL@%
  634. %@AB@%         *   an eager typist from pressing ESCAPE one time too often%@NL@%
  635. %@AB@%         *   and exiting at an unanticipated point.%@NL@%
  636. %@AB@%         */%@AE@%%@NL@%
  637.         if( iChoice == ESCAPE )%@NL@%
  638.         {%@NL@%
  639.             Help( "Press \"Q\" to Actually Quit", co.InputColor );%@NL@%
  640. %@NL@%
  641.             putchar( BEEP );%@NL@%
  642.             _settextposition( si.help - 1, 32 );%@NL@%
  643.             chVerify = getch();%@NL@%
  644.             if( tolower( chVerify ) != 'q' )%@NL@%
  645.                 iChoice = 0;%@NL@%
  646.             else%@NL@%
  647.                 iChoice = 6;%@NL@%
  648.         }%@NL@%
  649. %@NL@%
  650.     } while( iChoice != 6 );%@NL@%
  651.     PopTitle();%@NL@%
  652. }%@NL@%
  653. %@NL@%
  654. %@AB@%/*  ResetOptions - After confirmation, resets chart options to default.%@NL@%
  655. %@AB@% *%@NL@%
  656. %@AB@% *  Params: None%@NL@%
  657. %@AB@% */%@AE@%%@NL@%
  658. void ResetOptions()%@NL@%
  659. {%@NL@%
  660.     char chResponse;%@NL@%
  661. %@NL@%
  662.     %@AB@%/* Prompt for confirmation before setting default environment.  */%@AE@%%@NL@%
  663.     ClrForm();%@NL@%
  664.     Help( "Type  'Y' to reset all options, 'N' to keep them.", co.InputColor );%@NL@%
  665.     chResponse = InputCh( "Are you sure? ", "YN\x1b" );%@NL@%
  666.     if( chResponse == 'Y' )%@NL@%
  667.         _pg_defaultchart( &ce, 1, 1 );%@NL@%
  668. %@NL@%
  669. }%@NL@%
  670. %@NL@%
  671. %@AB@%/*  SetGraphMode - Tests the specified graphics mode and sets the xMax%@NL@%
  672. %@AB@% *  and yMax values in the si (Screen Information) structure.%@NL@%
  673. %@AB@% *%@NL@%
  674. %@AB@% *  Params: mode number%@NL@%
  675. %@AB@% *%@NL@%
  676. %@AB@% *  Return: FALSE if mode invalid, TRUE if valid%@NL@%
  677. %@AB@% */%@AE@%%@NL@%
  678. BOOL SetGraphMode(int mode)%@NL@%
  679. {%@NL@%
  680.     if (!_setvideomode( mode ) )%@NL@%
  681.         return FALSE;%@NL@%
  682.     else%@NL@%
  683.     {%@NL@%
  684.         _getvideoconfig ( &vc );%@NL@%
  685.         if( !vc.numxpixels )%@NL@%
  686.             return FALSE;%@NL@%
  687.         si.xMax = vc.numxpixels;%@NL@%
  688.         si.yMax = vc.numypixels;%@NL@%
  689.         si.mode = mode;%@NL@%
  690. %@NL@%
  691.         %@AB@%/* Set flag to indicate whether multiple colors are available.  */%@AE@%%@NL@%
  692.         si.fColor = iscolor( mode );%@NL@%
  693. %@NL@%
  694.         return TRUE;%@NL@%
  695.     }%@NL@%
  696. }%@NL@%
  697. %@NL@%
  698. %@AB@%/*  ShowChartData - Displays the data in the chart environment.%@NL@%
  699. %@AB@% *%@NL@%
  700. %@AB@% *  Params: None%@NL@%
  701. %@AB@% */%@AE@%%@NL@%
  702. void ShowChartData()%@NL@%
  703. {%@NL@%
  704.     int iRow = 2;%@NL@%
  705.     struct _fontinfo fd;%@NL@%
  706.     static char *szContinue =%@NL@%
  707.         "Press any key to continue, ESC to return to the menu.";%@NL@%
  708. %@NL@%
  709.     _clearscreen( _GCLEARSCREEN );%@NL@%
  710.     SprintAt( iRow++, 1, "short      charttype =  %d", ce.charttype );%@NL@%
  711.     SprintAt( iRow++, 1, "short      chartstyle = %d", ce.chartstyle );%@NL@%
  712.     SprintAt( iRow++, 1, "windowtype chartwindow =" );%@NL@%
  713.     iRow = ShowWindowType( iRow, 1, ce.chartwindow );%@NL@%
  714.     SprintAt( iRow++, 1, "windowtype datawindow =" );%@NL@%
  715.     iRow = ShowWindowType( iRow, 1, ce.datawindow );%@NL@%
  716.     SprintAt( ++iRow, 1, szContinue );%@NL@%
  717.     if( getch() == ESCAPE )%@NL@%
  718.         return;%@NL@%
  719. %@NL@%
  720.     iRow = 2;%@NL@%
  721.     _clearscreen( _GCLEARSCREEN );%@NL@%
  722.     SprintAt( iRow++, 1, "titletype  maintitle =" );%@NL@%
  723.     iRow = ShowTitleType( iRow, 1, ce.maintitle );%@NL@%
  724.     SprintAt( iRow++, 1, "titletype   subtitle =" );%@NL@%
  725.     iRow = ShowTitleType( iRow, 1, ce.subtitle );%@NL@%
  726.     SprintAt( ++iRow, 1, szContinue );%@NL@%
  727.     if( getch() == ESCAPE )%@NL@%
  728.         return;%@NL@%
  729. %@NL@%
  730.     iRow = 2;%@NL@%
  731.     _clearscreen( _GCLEARSCREEN );%@NL@%
  732.     SprintAt( iRow++, 1, "axistype       xaxis =" );%@NL@%
  733.     iRow = ShowAxisType( iRow, 1, ce.xaxis );%@NL@%
  734.     SprintAt( ++iRow, 1, szContinue );%@NL@%
  735.     if( getch() == ESCAPE )%@NL@%
  736.         return;%@NL@%
  737. %@NL@%
  738.     iRow = 2;%@NL@%
  739.     _clearscreen( _GCLEARSCREEN );%@NL@%
  740.     SprintAt( iRow++, 1, "axistype       yaxis =" );%@NL@%
  741.     iRow = ShowAxisType( iRow, 1, ce.yaxis );%@NL@%
  742.     SprintAt( ++iRow, 1, szContinue );%@NL@%
  743.     if( getch() == ESCAPE )%@NL@%
  744.         return;%@NL@%
  745. %@NL@%
  746.     iRow = 2;%@NL@%
  747.     _clearscreen( _GCLEARSCREEN );%@NL@%
  748.     SprintAt( iRow++, 1, "legendtype     legend =" );%@NL@%
  749.     iRow = ShowLegendType( iRow, 1, ce.legend );%@NL@%
  750.     SprintAt( ++iRow, 1, szContinue );%@NL@%
  751.     if( getch() == ESCAPE )%@NL@%
  752.         return;%@NL@%
  753. %@NL@%
  754.     iRow = 2;%@NL@%
  755.     _clearscreen( _GCLEARSCREEN );%@NL@%
  756.     if( _getfontinfo( &fd ) != -1 )%@NL@%
  757.     {%@NL@%
  758.         SprintAt( iRow++, 1, "struct _fontinfo =" );%@NL@%
  759.         iRow = ShowFontInfo( iRow, 1, fd );%@NL@%
  760.         SprintAt( ++iRow, 1, "Press any key to continue . . ." );%@NL@%
  761.         getch();%@NL@%
  762.     }%@NL@%
  763. }%@NL@%
  764. %@NL@%
  765. %@AB@%/*  ShowAxisType - Displays data in a variable of type "axistype".%@NL@%
  766. %@AB@% *%@NL@%
  767. %@AB@% *  Params: iRow - Row at which to start%@NL@%
  768. %@AB@% *          iCol - Column from which to indent%@NL@%
  769. %@AB@% *          theAxis - Variable of type "axistype" to display%@NL@%
  770. %@AB@% *%@NL@%
  771. %@AB@% *  Return: Next available row%@NL@%
  772. %@AB@% */%@AE@%%@NL@%
  773. int ShowAxisType( int iRow, int iCol, axistype theAxis )%@NL@%
  774. {%@NL@%
  775.     SprintAt( iRow++, iCol + 5, "short        .grid = %d", theAxis.grid );%@NL@%
  776.     SprintAt( iRow++, iCol + 5, "short   .gridstyle = %d", theAxis.gridstyle );%@NL@%
  777.     SprintAt( iRow++, iCol + 5, "titletype axistitle=" );%@NL@%
  778.     iRow = ShowTitleType( iRow, iCol + 5, theAxis.axistitle );%@NL@%
  779.     SprintAt( iRow++, iCol + 5, "short   .axiscolor = %d", theAxis.axiscolor );%@NL@%
  780.     SprintAt( iRow++, iCol + 5, "short     .labeled = %s",%@NL@%
  781.         (theAxis.labeled) ? "TRUE" : "FALSE" );%@NL@%
  782.     SprintAt( iRow++, iCol + 5, "short   .rangetype = %d", theAxis.rangetype );%@NL@%
  783.     SprintAt( iRow++, iCol + 5, "float     .logbase = %f", theAxis.logbase );%@NL@%
  784.     SprintAt( iRow++, iCol + 5, "short   .autoscale = %s",%@NL@%
  785.         (theAxis.autoscale) ? "TRUE" : "FALSE" );%@NL@%
  786.     SprintAt( iRow++, iCol + 5, "float    .scalemin = %f", theAxis.scalemin );%@NL@%
  787.     SprintAt( iRow++, iCol + 5, "float    .scalemax = %f", theAxis.scalemax );%@NL@%
  788.     SprintAt( iRow++, iCol + 5, "float .scalefactor = %f", theAxis.scalefactor );%@NL@%
  789.     iRow = ShowTitleType( iRow, iCol + 5, theAxis.scaletitle );%@NL@%
  790.     SprintAt( iRow++, iCol + 5, "float  .ticinterval = %f", theAxis.ticinterval );%@NL@%
  791.     SprintAt( iRow++, iCol + 5, "short    .ticformat = %d", theAxis.ticformat );%@NL@%
  792.     SprintAt( iRow++, iCol + 5, "short  .ticdecimals = %d", theAxis.ticdecimals );%@NL@%
  793. %@NL@%
  794.     return iRow;%@NL@%
  795. }%@NL@%
  796. %@NL@%
  797. %@AB@%/*  ShowFontInfo - Displays data in a variable of type "_fontinfo".%@NL@%
  798. %@AB@% *%@NL@%
  799. %@AB@% *  Params: iRow - Row at which to start%@NL@%
  800. %@AB@% *          iCol - Column from which to indent%@NL@%
  801. %@AB@% *          theFont - Variable of type "_fontinfo" to display%@NL@%
  802. %@AB@% *%@NL@%
  803. %@AB@% *  Return: Next available row%@NL@%
  804. %@AB@% */%@AE@%%@NL@%
  805. int ShowFontInfo( int iRow, int iCol, struct _fontinfo theFont )%@NL@%
  806. {%@NL@%
  807.     SprintAt( iRow++, iCol + 5, "int          .type = %d", theFont.type );%@NL@%
  808.     SprintAt( iRow++, iCol + 5, "int        .ascent = %d", theFont.ascent );%@NL@%
  809.     SprintAt( iRow++, iCol + 5, "int      .pixwidth = %d", theFont.pixwidth );%@NL@%
  810.     SprintAt( iRow++, iCol + 5, "int     .pixheight = %d", theFont.pixheight );%@NL@%
  811.     SprintAt( iRow++, iCol + 5, "int      .avgwidth = %d", theFont.avgwidth );%@NL@%
  812.     SprintAt( iRow++, iCol + 5, "char     .filename = %s", theFont.filename );%@NL@%
  813.     SprintAt( iRow++, iCol + 5, "char     .facename = %s", theFont.facename );%@NL@%
  814. %@NL@%
  815.     return iRow;%@NL@%
  816. }%@NL@%
  817. %@NL@%
  818. %@AB@%/*  ShowLegendType - Displays data in a variable of type "legendtype".%@NL@%
  819. %@AB@% *%@NL@%
  820. %@AB@% *  Params: iRow - Row at which to start%@NL@%
  821. %@AB@% *          iCol - Column from which to indent%@NL@%
  822. %@AB@% *          theLegend - Variable of type "legendtype" to display%@NL@%
  823. %@AB@% *%@NL@%
  824. %@AB@% *  Return: Next available row%@NL@%
  825. %@AB@% */%@AE@%%@NL@%
  826. int ShowLegendType( int iRow, int iCol, legendtype theLegend )%@NL@%
  827. {%@NL@%
  828.     SprintAt( iRow++, iCol + 5, "short      .legend = %s",%@NL@%
  829.               (theLegend.legend) ? "TRUE" : "FALSE" );%@NL@%
  830.     SprintAt( iRow++, iCol + 5, "short       .place = %d", theLegend.place );%@NL@%
  831.     SprintAt( iRow++, iCol + 5, "short   .textcolor = %d", theLegend.textcolor );%@NL@%
  832.     SprintAt( iRow++, iCol + 5, "short    .autosize = %d", theLegend.autosize );%@NL@%
  833.     SprintAt( iRow++, iCol + 5, "windowtype legendwindow =" );%@NL@%
  834.     iRow = ShowWindowType( iRow, iCol + 5, theLegend.legendwindow );%@NL@%
  835. %@NL@%
  836.     return iRow;%@NL@%
  837. }%@NL@%
  838. %@NL@%
  839. %@AB@%/*  ShowSampleData - Displays the sample data for the demo.%@NL@%
  840. %@AB@% *%@NL@%
  841. %@AB@% *  Params: None%@NL@%
  842. %@AB@% */%@AE@%%@NL@%
  843. void ShowSampleData()%@NL@%
  844. {%@NL@%
  845.     int  iCat, y, iSeries, iValue;%@NL@%
  846.     char szTmp[80];%@NL@%
  847. %@NL@%
  848.     %@AB@%/* Display data in table format. */%@AE@%%@NL@%
  849.     _clearscreen( _GCLEARSCREEN );%@NL@%
  850.     PrintAt( 1, 40 - strlen(szTmp) / 2, "Data in Table Format", -1 );%@NL@%
  851. %@NL@%
  852.     %@AB@%/* Write titles and draw separator line. */%@AE@%%@NL@%
  853.     y = 3;%@NL@%
  854.     for( iCat = 1; iCat <= 12; iCat++ )%@NL@%
  855.     PrintAt( y, iCat * 6, aMonths[iCat - 1], -1 );%@NL@%
  856. %@NL@%
  857.     memset( szTmp, '-', 69 );%@NL@%
  858.     szTmp[69] = 0;%@NL@%
  859.     PrintAt( ++y, 6, szTmp, -1 );%@NL@%
  860. %@NL@%
  861.     %@AB@%/* Write data. */%@AE@%%@NL@%
  862.     for( iSeries = 1; iSeries <= 3; iSeries++ )%@NL@%
  863.     {%@NL@%
  864.         PrintAt( y += 2, 4, aSalesTitles[iSeries - 1], -1 );%@NL@%
  865.         y += 2;%@NL@%
  866.         for( iValue = 1; iValue <= 12; iValue++ )%@NL@%
  867.         {%@NL@%
  868.             sprintf( szTmp, "%#3.2f", aSales[iSeries - 1][iValue - 1] );%@NL@%
  869.             PrintAt( y, iValue * 6, (char _far *)szTmp, -1 );%@NL@%
  870.         }%@NL@%
  871.     }%@NL@%
  872.     PrintAt( y += 2, 4, TempTitle, -1 );%@NL@%
  873.     y += 2;%@NL@%
  874.     for( iValue = 1; iValue <= 12; iValue++ )%@NL@%
  875.     {%@NL@%
  876.         sprintf( szTmp, "%#3.1f", aTemperature[iValue - 1] );%@NL@%
  877.         PrintAt( y, iValue * 6, szTmp, -1 );%@NL@%
  878.     }%@NL@%
  879. %@NL@%
  880.     PrintAt( y += 2, 1, "Press any key to continue . . .", -1 );%@NL@%
  881.     getche();%@NL@%
  882. }%@NL@%
  883. %@NL@%
  884. %@AB@%/*  ShowTitleType - Displays data in a variable of type "titletype".%@NL@%
  885. %@AB@% *%@NL@%
  886. %@AB@% *  Params: iRow - Row at which to start%@NL@%
  887. %@AB@% *          iCol - Column from which to indent%@NL@%
  888. %@AB@% *          theTitle - Variable of type "titletype" to display%@NL@%
  889. %@AB@% *%@NL@%
  890. %@AB@% *  Return: Next available row%@NL@%
  891. %@AB@% */%@AE@%%@NL@%
  892. int ShowTitleType( int iRow, int iCol, titletype theTitle )%@NL@%
  893. {%@NL@%
  894.     SprintAt( iRow++, iCol + 5, "char    .title[%d] = \"%s\"", _PG_TITLELEN,%@NL@%
  895.                  theTitle.title );%@NL@%
  896.     SprintAt( iRow++, iCol + 5, "short  .titlecolor = %d", theTitle.titlecolor );%@NL@%
  897.     SprintAt( iRow++, iCol + 5, "short     .justify = %d", theTitle.justify );%@NL@%
  898. %@NL@%
  899.     return iRow;%@NL@%
  900. }%@NL@%
  901. %@NL@%
  902. %@AB@%/*  ShowWindowType - Displays data in a variable of type "windowtype".%@NL@%
  903. %@AB@% *%@NL@%
  904. %@AB@% *  Params: iRow - Row at which to start%@NL@%
  905. %@AB@% *          iCol - Column from which to indent%@NL@%
  906. %@AB@% *          theWindow - Variable of type "windowtype" to display%@NL@%
  907. %@AB@% *%@NL@%
  908. %@AB@% *  Return: Next available row%@NL@%
  909. %@AB@% */%@AE@%%@NL@%
  910. int ShowWindowType( int iRow, int iCol, windowtype theWindow )%@NL@%
  911. {%@NL@%
  912.     SprintAt( iRow++, iCol + 5, "short          .x1 = %d", theWindow.x1 );%@NL@%
  913.     SprintAt( iRow++, iCol + 5, "short          .y1 = %d", theWindow.y1 );%@NL@%
  914.     SprintAt( iRow++, iCol + 5, "short          .x2 = %d", theWindow.x2 );%@NL@%
  915.     SprintAt( iRow++, iCol + 5, "short          .y2 = %d", theWindow.y2 );%@NL@%
  916.     SprintAt( iRow++, iCol + 5, "short      .border = %d", theWindow.border );%@NL@%
  917.     SprintAt( iRow++, iCol + 5, "short  .background = %d", theWindow.background );%@NL@%
  918.     SprintAt( iRow++, iCol + 5, "short .borderstyle = %d", theWindow.borderstyle );%@NL@%
  919.     SprintAt( iRow++, iCol + 5, "short .bordercolor = %d", theWindow.bordercolor );%@NL@%
  920. %@NL@%
  921.     return iRow;%@NL@%
  922. }%@NL@%
  923. %@NL@%
  924. %@AB@%/*  ShowError - Displays error message for one of the chart library%@NL@%
  925. %@AB@% *  errors.%@NL@%
  926. %@AB@% *%@NL@%
  927. %@AB@% *  Params: iErr - Error number%@NL@%
  928. %@AB@% */%@AE@%%@NL@%
  929. void ShowError( int iErr )%@NL@%
  930. {%@NL@%
  931.     char szTmp[50];%@NL@%
  932. %@NL@%
  933.     %@AB@%/* Change to text screen.  */%@AE@%%@NL@%
  934.     _setvideomode( _DEFAULTMODE );%@NL@%
  935. %@NL@%
  936.     %@AB@%/* Select the error text.  */%@AE@%%@NL@%
  937.     switch( iErr )%@NL@%
  938.     {%@NL@%
  939.         case _PG_NOTINITIALIZED:%@NL@%
  940.             strcpy( szTmp, "Chart Library Not Initialized" );%@NL@%
  941.             break;%@NL@%
  942.         case _PG_BADSCREENMODE:%@NL@%
  943.             strcpy( szTmp, "Invalid Screen Mode" );%@NL@%
  944.             break;%@NL@%
  945.         case _PG_BADCHARTTYPE:%@NL@%
  946.             strcpy( szTmp, "Invalid Chart Type" );%@NL@%
  947.             break;%@NL@%
  948.         case _PG_BADCHARTSTYLE:%@NL@%
  949.             strcpy( szTmp, "Invalid Chart Style" );%@NL@%
  950.             break;%@NL@%
  951.         case _PG_BADLEGENDWINDOW:%@NL@%
  952.             strcpy( szTmp, "Invalid Legend Window" );%@NL@%
  953.             break;%@NL@%
  954.         case _PG_BADDATAWINDOW:%@NL@%
  955.             strcpy( szTmp, "No Room for Data window" );%@NL@%
  956.             break;%@NL@%
  957.         case _PG_BADCHARTWINDOW:%@NL@%
  958.             strcpy( szTmp, "Invalid Chart window coordinates" );%@NL@%
  959.             break;%@NL@%
  960.         case _PG_NOMEMORY:%@NL@%
  961.             strcpy( szTmp, "Not Enough Memory for Data Arrays" );%@NL@%
  962.             break;%@NL@%
  963.         case _PG_BADLOGBASE:%@NL@%
  964.             strcpy( szTmp, "X or Y log base <= 0" );%@NL@%
  965.             break;%@NL@%
  966.         case _PG_BADSCALEFACTOR:%@NL@%
  967.             strcpy( szTmp, "X or Y scale factor = 0" );%@NL@%
  968.             break;%@NL@%
  969.         case _PG_TOOSMALLN:%@NL@%
  970.             strcpy( szTmp, "Too few data values" );%@NL@%
  971.             break;%@NL@%
  972.         case _PG_TOOFEWSERIES:%@NL@%
  973.             strcpy( szTmp, "No data series specified" );%@NL@%
  974.             break;%@NL@%
  975.         default:%@NL@%
  976.             strcpy( szTmp, "Unknown error" );%@NL@%
  977.     }%@NL@%
  978. %@NL@%
  979.     ErrorMsg( szTmp );%@NL@%
  980. }%@NL@%
  981. %@NL@%
  982. %@AB@%/*  ViewChart - Draws the current chart.%@NL@%
  983. %@AB@% *%@NL@%
  984. %@AB@% *  Params: None%@NL@%
  985. %@AB@% */%@AE@%%@NL@%
  986. int ViewChart()%@NL@%
  987. {%@NL@%
  988.     int cValue, iValue, iSeries, iErr;%@NL@%
  989. %@NL@%
  990.     %@AB@%/* Make sure some data exists.  */%@AE@%%@NL@%
  991.     if( cSeries <= 0 )%@NL@%
  992.     {%@NL@%
  993.         fDefault = TRUE;%@NL@%
  994.         DefaultData( ce.charttype, ce.chartstyle, FALSE );%@NL@%
  995.     }%@NL@%
  996. %@NL@%
  997.     %@AB@%/* Find the longest series.  */%@AE@%%@NL@%
  998.     cValue = 0;%@NL@%
  999.     for( iSeries = 0; iSeries < cSeries; iSeries++ )%@NL@%
  1000.         if( acSeries[iSeries] > cValue )%@NL@%
  1001.             cValue = acSeries[iSeries];%@NL@%
  1002. %@NL@%
  1003.     _setvideomode( si.mode );%@NL@%
  1004. %@NL@%
  1005. %@NL@%
  1006.     %@AB@%/* Process depending on the type of chart.  */%@AE@%%@NL@%
  1007.     switch( ce.charttype )%@NL@%
  1008.     {%@NL@%
  1009. %@NL@%
  1010.         case _PG_PIECHART:%@NL@%
  1011.         case _PG_BARCHART:%@NL@%
  1012.         case _PG_COLUMNCHART:%@NL@%
  1013.         case _PG_LINECHART:%@NL@%
  1014. %@NL@%
  1015.             %@AB@%/* Initialize data and draw pie chart or single-series bar,%@NL@%
  1016. %@AB@%             * column, or line chart.%@NL@%
  1017. %@AB@%             */%@AE@%%@NL@%
  1018.             if( (cSeries == 1) ||( ce.charttype == _PG_PIECHART) )%@NL@%
  1019.             {%@NL@%
  1020. %@NL@%
  1021.                 %@AB@%/* Transfer data into a single-dimension array.  */%@AE@%%@NL@%
  1022.                 for( iValue = 0; iValue < cValue; iValue++ )%@NL@%
  1023.                    axValue[iValue] = aValue[0][iValue];%@NL@%
  1024. %@NL@%
  1025.                 %@AB@%/* Draw chart.  */%@AE@%%@NL@%
  1026.                 if( ce.charttype == _PG_PIECHART )%@NL@%
  1027.                     iErr = _pg_chartpie( &ce, aCat, axValue,%@NL@%
  1028.                                          aExplode, cValue );%@NL@%
  1029.                 else%@NL@%
  1030.                     iErr = _pg_chart( &ce, aCat, axValue, cValue );%@NL@%
  1031.             }%@NL@%
  1032.             %@AB@%/* If multiple-series, then data is OK. Just draw chart.  */%@AE@%%@NL@%
  1033.             else%@NL@%
  1034.                 iErr = _pg_chartms( &ce, aCat, (float _far *)aValue,%@NL@%
  1035.                                     cSeries, cValue, cValue, aSeriesName );%@NL@%
  1036.             break;%@NL@%
  1037. %@NL@%
  1038.         case _PG_SCATTERCHART:%@NL@%
  1039. %@NL@%
  1040.             %@AB@%/* Make sure there are enough data sets.  */%@AE@%%@NL@%
  1041.             if( cSeries == 1 )%@NL@%
  1042.             {%@NL@%
  1043.                 _setvideomode( _DEFAULTMODE );%@NL@%
  1044.                 si.help = 10;%@NL@%
  1045.                 ErrorMsg( "Too few value data columns for scatter chart." );%@NL@%
  1046.                 return 1;%@NL@%
  1047. %@NL@%
  1048.             }%@NL@%
  1049.             %@AB@%/* If it's a single-series scatter, transfer data to one-%@NL@%
  1050. %@AB@%             * dimensional arrays and make chart call.%@NL@%
  1051. %@AB@%             */%@AE@%%@NL@%
  1052.             else if( cSeries == 2 )%@NL@%
  1053.             {%@NL@%
  1054.                 for( iValue = 0; iValue < cValue; iValue++ )%@NL@%
  1055.                 {%@NL@%
  1056.                     axValue[iValue] = aValue[0][iValue];%@NL@%
  1057.                     ayValue[iValue] = aValue[1][iValue];%@NL@%
  1058.                 }%@NL@%
  1059.                 cSeries = 1;%@NL@%
  1060.                 iErr = _pg_chartscatter( &ce, axValue, ayValue, cValue );%@NL@%
  1061. %@NL@%
  1062.             }%@NL@%
  1063.             %@AB@%/* If it's a multiple-series scatter, transfer odd columns to%@NL@%
  1064. %@AB@%             * X-axis data array and even columns to Y-axis array and make%@NL@%
  1065. %@AB@%             * chart call.%@NL@%
  1066. %@AB@%             */%@AE@%%@NL@%
  1067.             else%@NL@%
  1068.             {%@NL@%
  1069. %@NL@%
  1070.                 for( iSeries = 1; iSeries < cSeries; iSeries += 2 )%@NL@%
  1071.                 {%@NL@%
  1072.                     aSeriesName[iSeries / 2] = aSeriesName[iSeries - 1];%@NL@%
  1073.                     for( iValue = 0; iValue < cValue; iValue++ )%@NL@%
  1074.                     {%@NL@%
  1075.                         axValueMS[iSeries / 2][iValue] =%@NL@%
  1076.                             aValue[iSeries - 1][iValue];%@NL@%
  1077.                         ayValueMS[iSeries / 2][iValue] =%@NL@%
  1078.                             aValue[iSeries][iValue];%@NL@%
  1079.                     }%@NL@%
  1080.                 }%@NL@%
  1081.                 cSeries /= 2;%@NL@%
  1082. %@NL@%
  1083.                 iErr = _pg_chartscatterms( &ce, (float _far *)axValueMS,%@NL@%
  1084.                                            (float _far *)ayValueMS,%@NL@%
  1085.                                            cSeries, cValue, cValue,%@NL@%
  1086.                                            aSeriesName );%@NL@%
  1087.             }%@NL@%
  1088.     }%@NL@%
  1089. %@NL@%
  1090.     if( !fDefault )%@NL@%
  1091.         ClearData( FALSE );%@NL@%
  1092. %@NL@%
  1093.     %@AB@%/* If error, show it, else wait for keypress with chart on screen.  */%@AE@%%@NL@%
  1094.     if( iErr )%@NL@%
  1095.     {%@NL@%
  1096.         ShowError( iErr );%@NL@%
  1097.         return iErr;%@NL@%
  1098.     }%@NL@%
  1099.     else%@NL@%
  1100.         return ( getch() == ESCAPE );   %@AB@%/* ESCAPE means stop demo */%@AE@%%@NL@%
  1101. }%@NL@%
  1102. %@NL@%
  1103. %@NL@%
  1104. %@2@%%@AH@%CHRTOPT.C%@AE@%%@EH@%%@NL@%
  1105. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\MSC60\CHRTOPT.C%@AE@%%@NL@%
  1106. %@NL@%
  1107. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  1108. %@AI@%#include %@AE@%<conio.h> %@NL@%
  1109. %@AI@%#include %@AE@%<string.h> %@NL@%
  1110. %@AI@%#include %@AE@%<graph.h> %@NL@%
  1111. %@AI@%#include %@AE@%<pgchart.h> %@NL@%
  1112. %@AI@%#include %@AE@%"chrtdemo.h" %@NL@%
  1113. %@NL@%
  1114. %@AB@%/* Structures for system configuration and chart environment. */%@AE@%%@NL@%
  1115. extern struct videoconfig vc;%@NL@%
  1116. extern chartenv ce;%@NL@%
  1117. %@NL@%
  1118. %@AB@%/* Variable used to track control and screen position.  */%@AE@%%@NL@%
  1119. extern struct SCREENINFO si;%@NL@%
  1120. %@NL@%
  1121. %@AB@%/* Colors of menus and prompts. */%@AE@%%@NL@%
  1122. extern struct tagColor co;%@NL@%
  1123. %@NL@%
  1124. %@AB@%/* Arrays of strings used by the Menu function. The first string is the%@NL@%
  1125. %@AB@% * menu title. The next non-null strings are the menu selections. A null%@NL@%
  1126. %@AB@% * string indicates the end of the list.%@NL@%
  1127. %@AB@% */%@AE@%%@NL@%
  1128. char *pszAxes[] =%@NL@%
  1129.     { "Axis", "X Axis", "Y Axis", "" };%@NL@%
  1130. %@NL@%
  1131. char *pszAxis[] =%@NL@%
  1132.     { "? Options", "Grid", "Axis Title", "Color",%@NL@%
  1133.       "Range Type", "Scale", "Tic Marks", "" };%@NL@%
  1134. %@NL@%
  1135. char *pszAuto[] =%@NL@%
  1136.     { "Auto", "Auto", "Manual", "" };%@NL@%
  1137. %@NL@%
  1138. char *pszBorder[] =%@NL@%
  1139.     { "Type", "Color", "Style", "" };%@NL@%
  1140. %@NL@%
  1141. char *pszChartWindow[] =%@NL@%
  1142.     { "Chart", "Size", "Color (Background)", "Border", "" };%@NL@%
  1143. %@NL@%
  1144. char *pszDataWindow[] =%@NL@%
  1145.     { "Data", "Color (Background)", "Border", "" };%@NL@%
  1146. %@NL@%
  1147. char * pszFontOpt[] =%@NL@%
  1148.     { "Font Options", "Change Typeface", "Set Character Size", "" };%@NL@%
  1149. %@NL@%
  1150. char *pszJustify[] =%@NL@%
  1151.     { "Justify", "Left", "Center", "Right", "" };%@NL@%
  1152. %@NL@%
  1153. char *pszLegendWindow[] =%@NL@%
  1154.     { "Options", "Place", "Text Color", "Size", "Color (Background)",%@NL@%
  1155.       "Border", "" };%@NL@%
  1156. %@NL@%
  1157. char *pszPlace[] =%@NL@%
  1158.     { "Place", "Right", "Bottom", "Overlay", "" };%@NL@%
  1159. %@NL@%
  1160. char *pszScale[] =%@NL@%
  1161.     { "Scale", "Low (Min)", "High (Max)", "Scale Factor", "Title", "" };%@NL@%
  1162. %@NL@%
  1163. char *pszSize[] =%@NL@%
  1164.     { "Size", "Top", "Left", "Bottom", "Right", "" };%@NL@%
  1165. %@NL@%
  1166. char *pszTic[] =%@NL@%
  1167.     { "Tic Type", "Interval", "Format", "Decimals", "" };%@NL@%
  1168. %@NL@%
  1169. char *pszTitleOpt[] =%@NL@%
  1170.     { "", "Text", "Color", "Justify", "" };%@NL@%
  1171. %@NL@%
  1172. char *pszTitles[] =%@NL@%
  1173.     { "Title", "Main Title", "Sub Title", "" };%@NL@%
  1174. %@NL@%
  1175. char *pszTypeface[] =%@NL@%
  1176.     { "Type Faces", "Courier", "Helv", "Tms Rmn", "Modern", "Script",%@NL@%
  1177.            "Roman", "None", "" };%@NL@%
  1178. %@NL@%
  1179. char *pszWindows[] =%@NL@%
  1180.     { "Window", "Chart Window", "Data Window", "" };%@NL@%
  1181. %@NL@%
  1182. %@AB@%/*  Axes - Selects X or Y axis.%@NL@%
  1183. %@AB@% *%@NL@%
  1184. %@AB@% *  Params: none%@NL@%
  1185. %@AB@% */%@AE@%%@NL@%
  1186. void Axes()%@NL@%
  1187. {%@NL@%
  1188.     int iChoice;%@NL@%
  1189.     static axistype *patAxis[2] = { &ce.xaxis, &ce.yaxis };%@NL@%
  1190. %@NL@%
  1191.     %@AB@%/* Get menu choice and call appropriate axis Menu. */%@AE@%%@NL@%
  1192.     PushTitle( pszAxes[0] );%@NL@%
  1193.     Help( "Choose 'X' or 'Y' Axis", co.InputColor );%@NL@%
  1194.     while( (iChoice = Menu( pszAxes )) != ESCAPE )%@NL@%
  1195.     {%@NL@%
  1196.         %@AB@%/* Modify axis title, depending on choice. */%@AE@%%@NL@%
  1197.         pszAxis[0][0] = (--iChoice == 0) ? 'X' : 'Y';%@NL@%
  1198. %@NL@%
  1199.         %@AB@%/* Obtain axis information for appropriate axis. */%@AE@%%@NL@%
  1200.         Axis( patAxis[iChoice] );%@NL@%
  1201.     }%@NL@%
  1202.     PopTitle();%@NL@%
  1203. }%@NL@%
  1204. %@NL@%
  1205. %@AB@%/*  Axis - Selects axis options.%@NL@%
  1206. %@AB@% *%@NL@%
  1207. %@AB@% *  Params: pat - Pointer to axistype variable%@NL@%
  1208. %@AB@% */%@AE@%%@NL@%
  1209. void Axis( axistype *pat )%@NL@%
  1210. {%@NL@%
  1211.     int iChoice;%@NL@%
  1212. %@NL@%
  1213.     PushTitle( pszAxis[0] );%@NL@%
  1214.     while( (iChoice = Menu( pszAxis )) != ESCAPE )%@NL@%
  1215.     {%@NL@%
  1216. %@NL@%
  1217.         %@AB@%/* Get Axis option.  */%@AE@%%@NL@%
  1218.         switch( iChoice )%@NL@%
  1219.         {%@NL@%
  1220.             case 1:%@NL@%
  1221.                 %@AB@%/* Grid or not? */%@AE@%%@NL@%
  1222.                 iChoice = BlankMenu( "Grid", "Grid", "No Grid" );%@NL@%
  1223.                 switch( iChoice )%@NL@%
  1224.                 {%@NL@%
  1225. %@NL@%
  1226.                     case 1:%@NL@%
  1227.                         %@AB@%/* If yes, set grid flag and get the grid style. */%@AE@%%@NL@%
  1228.                         pat->grid = TRUE;%@NL@%
  1229.                         Help( "Enter a number in the range 0-10.",%@NL@%
  1230.                             co.InputColor );%@NL@%
  1231.                         pat->gridstyle =%@NL@%
  1232.                             InputInt( "Grid Style? ", pat->gridstyle, 0, 10 );%@NL@%
  1233.                         break;%@NL@%
  1234. %@NL@%
  1235.                     case 2:%@NL@%
  1236.                         %@AB@%/* If no, clear grid flag.  */%@AE@%%@NL@%
  1237.                         pat->grid = FALSE;%@NL@%
  1238.                 }%@NL@%
  1239.                 PopTitle();%@NL@%
  1240.                 break;%@NL@%
  1241. %@NL@%
  1242.             case 2:%@NL@%
  1243.                 %@AB@%/* Select axis title options. */%@AE@%%@NL@%
  1244.                 pszTitleOpt[0] = "Axis Title";%@NL@%
  1245.                 TitleOpt( &pat->axistitle );%@NL@%
  1246.                 break;%@NL@%
  1247. %@NL@%
  1248.             case 3:%@NL@%
  1249.                 %@AB@%/* Select color. */%@AE@%%@NL@%
  1250.                 Help( "Enter a number in the range 0-15.", co.InputColor );%@NL@%
  1251.                 pat->axiscolor =%@NL@%
  1252.                     InputInt( "Axis Color? ", pat->axiscolor, 0, 15 );%@NL@%
  1253.                 break;%@NL@%
  1254. %@NL@%
  1255.             case 4:%@NL@%
  1256.                 %@AB@%/* Get the axis range.  */%@AE@%%@NL@%
  1257.                 AxisRange( pat );%@NL@%
  1258.                 break;%@NL@%
  1259. %@NL@%
  1260.             case 5:%@NL@%
  1261.                 %@AB@%/* Get the axis scale.  */%@AE@%%@NL@%
  1262.                 AxisScale( pat );%@NL@%
  1263.                 break;%@NL@%
  1264. %@NL@%
  1265.             case 6:%@NL@%
  1266.                 %@AB@%/* Get axis tic mark options.  */%@AE@%%@NL@%
  1267.                 AxisTics( pat );%@NL@%
  1268.                 break;%@NL@%
  1269. %@NL@%
  1270.         }%@NL@%
  1271.     }%@NL@%
  1272.     PopTitle();%@NL@%
  1273. }%@NL@%
  1274. %@NL@%
  1275. %@AB@%/*  AxisRange - Selects range for an axis.%@NL@%
  1276. %@AB@% *%@NL@%
  1277. %@AB@% *  Params: pat - pointer to axistype variable%@NL@%
  1278. %@AB@% */%@AE@%%@NL@%
  1279. void AxisRange( axistype *pat )%@NL@%
  1280. {%@NL@%
  1281.     int iChoice;%@NL@%
  1282. %@NL@%
  1283.     iChoice = BlankMenu( "Range Type", "Normal", "Log" );%@NL@%
  1284.     switch( iChoice )%@NL@%
  1285.     {%@NL@%
  1286.         case 1:%@NL@%
  1287.             %@AB@%/* Set range type to linear.  */%@AE@%%@NL@%
  1288.             pat->rangetype = _PG_LINEARAXIS;%@NL@%
  1289.             break;%@NL@%
  1290. %@NL@%
  1291.         case 2:%@NL@%
  1292.             %@AB@%/* Set range type to log, then query for log base.  */%@AE@%%@NL@%
  1293.             pat->rangetype = _PG_LOGAXIS;%@NL@%
  1294.             Help( "Enter a value greater than or equal 2.", co.InputColor );%@NL@%
  1295.             pat->logbase = (float)InputInt( "Log base? ",%@NL@%
  1296.                                             (int)pat->logbase, 2, 0 );%@NL@%
  1297.             break;%@NL@%
  1298.     }%@NL@%
  1299.     PopTitle();%@NL@%
  1300. }%@NL@%
  1301. %@NL@%
  1302. %@AB@%/*  AxisScale - Selects scale options for an axis.%@NL@%
  1303. %@AB@% *%@NL@%
  1304. %@AB@% *  Params: pat - pointer to axistype variable%@NL@%
  1305. %@AB@% */%@AE@%%@NL@%
  1306. void AxisScale( axistype *pat )%@NL@%
  1307. {%@NL@%
  1308.     int iChoice;%@NL@%
  1309. %@NL@%
  1310.     PushTitle( pszAuto[0] );%@NL@%
  1311.     iChoice = Menu( pszAuto );%@NL@%
  1312.     switch( iChoice )%@NL@%
  1313.     {%@NL@%
  1314. %@NL@%
  1315.         case 1:%@NL@%
  1316.             %@AB@%/* Set AutoScale flag.  */%@AE@%%@NL@%
  1317.             pat->autoscale = TRUE;%@NL@%
  1318.             break;%@NL@%
  1319. %@NL@%
  1320.         case 2:%@NL@%
  1321. %@NL@%
  1322.             %@AB@%/* Clear AutoScale flag and get scale options.  */%@AE@%%@NL@%
  1323.             pat->autoscale = FALSE;%@NL@%
  1324.             PushTitle( pszScale[0] );%@NL@%
  1325.             while( (iChoice = Menu( pszScale )) != ESCAPE )%@NL@%
  1326.             {%@NL@%
  1327. %@NL@%
  1328.                 switch( iChoice )%@NL@%
  1329.                 {%@NL@%
  1330. %@NL@%
  1331.                     case 1:  %@NL@%
  1332.                         %@AB@%/* Query for scale minimum.  */%@AE@%%@NL@%
  1333.                         Help( "Enter the range minimum value.", co.InputColor );%@NL@%
  1334.                         pat->scalemin =%@NL@%
  1335.                             (float)InputInt( "Minimum? ",%@NL@%
  1336.                                              (int)pat->scalemin, 1, 0 );%@NL@%
  1337.                         break;%@NL@%
  1338. %@NL@%
  1339.                     case 2:  %@NL@%
  1340.                         %@AB@%/* Query for scale maximum.  */%@AE@%%@NL@%
  1341.                         Help( "Enter the range maximum value.", co.InputColor );%@NL@%
  1342.                         pat->scalemin =%@NL@%
  1343.                             (float)InputInt( "Minimum? ",%@NL@%
  1344.                                              (int)pat->scalemin, 1, 0 );%@NL@%
  1345.                         break;%@NL@%
  1346. %@NL@%
  1347.                     case 3:%@NL@%
  1348.                         %@AB@%/* Query for scale factor.  */%@AE@%%@NL@%
  1349.                         Help( "Enter scale factor (must be 1 or greater).",%@NL@%
  1350.                                  co.InputColor );%@NL@%
  1351.                         pat->scalefactor =%@NL@%
  1352.                             (float)InputInt( "Scale Factor? ",%@NL@%
  1353.                                              (int)pat->scalefactor, 1, 0 );%@NL@%
  1354.                         break;%@NL@%
  1355. %@NL@%
  1356.                     case 4:  %@NL@%
  1357.                         %@AB@%/* Modify scale title, then use menu to get%@NL@%
  1358. %@AB@%                         * title options.%@NL@%
  1359. %@AB@%                         */%@AE@%%@NL@%
  1360.                         pszTitleOpt[0] = "Scale Title";%@NL@%
  1361.                         TitleOpt( &pat->scaletitle );%@NL@%
  1362. %@NL@%
  1363.                 }%@NL@%
  1364.             }%@NL@%
  1365.             PopTitle();%@NL@%
  1366.     }%@NL@%
  1367.     PopTitle();%@NL@%
  1368. }%@NL@%
  1369. %@NL@%
  1370. %@AB@%/*  AxisTics - Selects tic options for an axis.%@NL@%
  1371. %@AB@% *%@NL@%
  1372. %@AB@% *  Params: pat - pointer to axistype variable%@NL@%
  1373. %@AB@% */%@AE@%%@NL@%
  1374. void AxisTics( axistype *pat )%@NL@%
  1375. {%@NL@%
  1376.     int iChoice;%@NL@%
  1377. %@NL@%
  1378.     PushTitle( pszTic[0] );%@NL@%
  1379.     while( (iChoice = Menu( pszTic )) != ESCAPE )%@NL@%
  1380.     {%@NL@%
  1381.         switch( iChoice )%@NL@%
  1382.         {%@NL@%
  1383. %@NL@%
  1384.             case 1:%@NL@%
  1385.                 %@AB@%/* Query for tic interval.  */%@AE@%%@NL@%
  1386.                 Help( "Enter distance in data units.", co.InputColor );%@NL@%
  1387.                 pat->ticinterval =%@NL@%
  1388.                     InputFloat( "Distance between tic marks? ",%@NL@%
  1389.                                pat->ticinterval );%@NL@%
  1390.                 pat->autoscale = FALSE;%@NL@%
  1391.                 break;%@NL@%
  1392. %@NL@%
  1393.             case 2:%@NL@%
  1394.                 %@AB@%/* Query for tic format.  */%@AE@%%@NL@%
  1395.                 iChoice = BlankMenu( "Tic Format", "Normal", "Log" );%@NL@%
  1396.                 if( iChoice != ESCAPE )%@NL@%
  1397.                     pat->ticformat = iChoice;%@NL@%
  1398.                 break;%@NL@%
  1399. %@NL@%
  1400.             case 3:%@NL@%
  1401.                 %@AB@%/* Query for number of decimal places per tic.  */%@AE@%%@NL@%
  1402.                 pat->ticdecimals =%@NL@%
  1403.                     InputInt( "Enter decimal places (0 to 9). ",%@NL@%
  1404.                                pat->ticdecimals, 0, 9 );%@NL@%
  1405.                 pat->autoscale = FALSE;%@NL@%
  1406.                 break;%@NL@%
  1407.         }%@NL@%
  1408. %@NL@%
  1409.     }%@NL@%
  1410.     PopTitle();%@NL@%
  1411. }%@NL@%
  1412. %@NL@%
  1413. %@AB@%/*  Border - Specifies border information for a window.%@NL@%
  1414. %@AB@% *%@NL@%
  1415. %@AB@% *  Params: pwt - Pointer to windowtype variable%@NL@%
  1416. %@AB@% */%@AE@%%@NL@%
  1417. void Border( windowtype *pwt )%@NL@%
  1418. {%@NL@%
  1419.     int iChoice;%@NL@%
  1420. %@NL@%
  1421.     %@AB@%/* Ask whether a border is wanted.  */%@AE@%%@NL@%
  1422.     iChoice = BlankMenu( "Border", "Border", "No Border" );%@NL@%
  1423.     switch( iChoice )%@NL@%
  1424.     {%@NL@%
  1425. %@NL@%
  1426.         case 1:%@NL@%
  1427. %@NL@%
  1428.             %@AB@%/* If border, set Border flag and query for border options.  */%@AE@%%@NL@%
  1429.             pwt->border= TRUE;%@NL@%
  1430.             PushTitle( pszBorder[0] );%@NL@%
  1431.             while( (iChoice = Menu( pszBorder )) != ESCAPE )%@NL@%
  1432.             {%@NL@%
  1433.                 switch( iChoice )%@NL@%
  1434.                 {%@NL@%
  1435.                     case 1:%@NL@%
  1436.                         %@AB@%/* Query for border color.  */%@AE@%%@NL@%
  1437.                         Help( "Enter a color in the range 0-15.",%@NL@%
  1438.                             co.InputColor );%@NL@%
  1439.                         pwt->bordercolor =%@NL@%
  1440.                             InputInt( "Border color? ",%@NL@%
  1441.                                        pwt->bordercolor, 0, 15 );%@NL@%
  1442.                         break;%@NL@%
  1443. %@NL@%
  1444.                     case 2:%@NL@%
  1445.                         %@AB@%/* Query for border style.  */%@AE@%%@NL@%
  1446.                         Help( "Enter a style in the range 0-10.", co.InputColor );%@NL@%
  1447.                         pwt->borderstyle =%@NL@%
  1448.                             InputInt( "Border style? ",%@NL@%
  1449.                                        pwt->borderstyle, 0, 10 );%@NL@%
  1450.                 }%@NL@%
  1451.             }%@NL@%
  1452.             PopTitle();%@NL@%
  1453.             break;%@NL@%
  1454. %@NL@%
  1455.         case 2:%@NL@%
  1456.             %@AB@%/* If no border, clear Border flag.  */%@AE@%%@NL@%
  1457.             pwt->border= FALSE;%@NL@%
  1458.     }%@NL@%
  1459.     PopTitle();%@NL@%
  1460. }%@NL@%
  1461. %@NL@%
  1462. %@AB@%/*  ChangeTypeface - Allow the user to specify a new type face.%@NL@%
  1463. %@AB@% *%@NL@%
  1464. %@AB@% *  Params: iFaceIndex - index of last typeface%@NL@%
  1465. %@AB@% *%@NL@%
  1466. %@AB@% *  Return: index of new typeface%@NL@%
  1467. %@AB@% */%@AE@%%@NL@%
  1468. %@NL@%
  1469. int ChangeTypeface( int iFaceIndex )%@NL@%
  1470. {%@NL@%
  1471.     int iChoice;%@NL@%
  1472. %@NL@%
  1473.     %@AB@%/* Get menu choice and call appropriate axis Menu. */%@AE@%%@NL@%
  1474.     PushTitle( pszFontOpt[0] );%@NL@%
  1475.     Help( "Choose one of the type faces listed.", co.InputColor );%@NL@%
  1476. %@NL@%
  1477.     if( (iChoice = Menu( pszTypeface ) - 1) != ESCAPE )%@NL@%
  1478.     {%@NL@%
  1479.         %@AB@%/* If the user wants the system font, unregister the other fonts. */%@AE@%%@NL@%
  1480.         if( iChoice == NOFONT )%@NL@%
  1481.             _unregisterfonts();%@NL@%
  1482. %@NL@%
  1483.         %@AB@%/* If the user wants any font but the system font, make sure the%@NL@%
  1484. %@AB@%         *   fonts are registered.%@NL@%
  1485. %@AB@%         */%@AE@%%@NL@%
  1486.         else%@NL@%
  1487.         {%@NL@%
  1488.             %@AB@%/* If last face was NOFONT, register fonts. */%@AE@%%@NL@%
  1489.             if( iFaceIndex == NOFONT )%@NL@%
  1490.             {%@NL@%
  1491.                 %@AB@%/* Assumes font files are in current directory.%@NL@%
  1492. %@AB@%                 * Could be enhanced to handle any directory.%@NL@%
  1493. %@AB@%                 */%@AE@%%@NL@%
  1494.                 if( _registerfonts( "*.FON" ) < 0 )%@NL@%
  1495.                     ErrorMsg( "Font files must be in current directory" );%@NL@%
  1496.                 else%@NL@%
  1497.                     iFaceIndex = iChoice;%@NL@%
  1498.             }%@NL@%
  1499.             else%@NL@%
  1500.                 iFaceIndex = iChoice;%@NL@%
  1501.         }%@NL@%
  1502.     }%@NL@%
  1503. %@NL@%
  1504.     PopTitle();%@NL@%
  1505.     return iFaceIndex;%@NL@%
  1506. }%@NL@%
  1507. %@NL@%
  1508. %@AB@%/*  ChooseFont - Chooses a font from the font library.%@NL@%
  1509. %@AB@% *%@NL@%
  1510. %@AB@% *  Params: WhichFont - A member of the set [COURIER, HELV, TMS_RMN,%@NL@%
  1511. %@AB@% *                        MODERN, SCRIPT, ROMAN, NOFONT]%@NL@%
  1512. %@AB@% *          Height    - The desired height of the text (in pixels)%@NL@%
  1513. %@AB@% */%@AE@%%@NL@%
  1514. %@NL@%
  1515. void ChooseFont( int WhichFont, int Height )%@NL@%
  1516. {%@NL@%
  1517.     static char *FontIds[] =%@NL@%
  1518.     {%@NL@%
  1519.         "courier", "helv", "tms rmn", "modern", "script", "roman"%@NL@%
  1520.     };%@NL@%
  1521.     char SetCommand[30];%@NL@%
  1522. %@NL@%
  1523.     %@AB@%/* Construct the command to send to _setfont. */%@AE@%%@NL@%
  1524.     sprintf( SetCommand, "t'%s'h%dw0b", FontIds[WhichFont], Height );%@NL@%
  1525. %@NL@%
  1526.     if( _setfont( SetCommand ) < 0 )%@NL@%
  1527.     {%@NL@%
  1528.         _outtext( "Could not set. Try different font or size" );%@NL@%
  1529.         getch();%@NL@%
  1530.     }%@NL@%
  1531. }%@NL@%
  1532. %@NL@%
  1533. %@AB@%/*  ChartWindow - Gets chart window information.%@NL@%
  1534. %@AB@% *%@NL@%
  1535. %@AB@% *  Params: None%@NL@%
  1536. %@AB@% */%@AE@%%@NL@%
  1537. void ChartWindow()%@NL@%
  1538. {%@NL@%
  1539.     int iChoice;%@NL@%
  1540. %@NL@%
  1541.     PushTitle( pszChartWindow[0] );%@NL@%
  1542.     while( (iChoice = Menu( pszChartWindow )) != ESCAPE )%@NL@%
  1543.     {%@NL@%
  1544. %@NL@%
  1545.         %@AB@%/* Get window options.  */%@AE@%%@NL@%
  1546.         switch( iChoice )%@NL@%
  1547.         {%@NL@%
  1548. %@NL@%
  1549.             case 1:%@NL@%
  1550.                 %@AB@%/* Get window size.  */%@AE@%%@NL@%
  1551.                 WindowSize( &ce.chartwindow );%@NL@%
  1552.                 break;%@NL@%
  1553. %@NL@%
  1554.             case 2:%@NL@%
  1555.                 %@AB@%/* Query for background color.  */%@AE@%%@NL@%
  1556.                 Help( "Enter a number in the range 0-15", co.InputColor );%@NL@%
  1557.                 ce.chartwindow.background =%@NL@%
  1558.                     InputInt( "Background Color? ", ce.chartwindow.background,%@NL@%
  1559.                               0, 15 );%@NL@%
  1560.                 break;%@NL@%
  1561. %@NL@%
  1562.             case 3:%@NL@%
  1563. %@NL@%
  1564.                 %@AB@%/* Get border options.  */%@AE@%%@NL@%
  1565.                 Border( &ce.chartwindow );%@NL@%
  1566. %@NL@%
  1567.         }%@NL@%
  1568.     }%@NL@%
  1569.     PopTitle();%@NL@%
  1570. }%@NL@%
  1571. %@NL@%
  1572. %@AB@%/*  DataWindow - Geta data window information.%@NL@%
  1573. %@AB@% *%@NL@%
  1574. %@AB@% *  Params: None%@NL@%
  1575. %@AB@% */%@AE@%%@NL@%
  1576. void DataWindow()%@NL@%
  1577. {%@NL@%
  1578.     int iChoice;%@NL@%
  1579. %@NL@%
  1580.     PushTitle( pszDataWindow[0] );%@NL@%
  1581.     while( (iChoice = Menu( pszDataWindow )) != ESCAPE )%@NL@%
  1582.     {%@NL@%
  1583. %@NL@%
  1584.         %@AB@%/* Get data window menu options.  */%@AE@%%@NL@%
  1585.         switch( iChoice )%@NL@%
  1586.         {%@NL@%
  1587. %@NL@%
  1588.             case 1: %@NL@%
  1589.                 %@AB@%/* Query for background color.  */%@AE@%%@NL@%
  1590.                 Help( "Enter a number in the range 0-15", co.InputColor );%@NL@%
  1591.                 ce.datawindow.background =%@NL@%
  1592.                     InputInt( "Background Color? ",%@NL@%
  1593.                                ce.datawindow.background,%@NL@%
  1594.                                0, 15 );%@NL@%
  1595.                 break;%@NL@%
  1596. %@NL@%
  1597.             case 2:%@NL@%
  1598.                 %@AB@%/* Get border options.  */%@AE@%%@NL@%
  1599.                 Border( &ce.datawindow );%@NL@%
  1600.                 break;%@NL@%
  1601. %@NL@%
  1602.         }%@NL@%
  1603.     }%@NL@%
  1604.     PopTitle();%@NL@%
  1605. }%@NL@%
  1606. %@NL@%
  1607. %@AB@%/*  FontOptions - Allows the user to modify the font used for display.%@NL@%
  1608. %@AB@% *%@NL@%
  1609. %@AB@% *  Params: None%@NL@%
  1610. %@AB@% */%@AE@%%@NL@%
  1611. %@NL@%
  1612. void FontOptions()%@NL@%
  1613. {%@NL@%
  1614.     int iChoice;%@NL@%
  1615.     static int iFaceIndex = NOFONT;%@NL@%
  1616.     static int iTypeSize = 8;%@NL@%
  1617. %@NL@%
  1618.     %@AB@%/* Get menu choice and call appropriate axis Menu. */%@AE@%%@NL@%
  1619.     PushTitle( pszFontOpt[0] );%@NL@%
  1620. %@NL@%
  1621.     while( (iChoice = Menu( pszFontOpt )) != ESCAPE )%@NL@%
  1622.     {%@NL@%
  1623.         switch( iChoice )%@NL@%
  1624.         {%@NL@%
  1625.             %@AB@%/* Change Typeface. */%@AE@%%@NL@%
  1626.             case 1:%@NL@%
  1627.                 iFaceIndex = ChangeTypeface( iFaceIndex );%@NL@%
  1628.                 ChooseFont( iFaceIndex, iTypeSize );%@NL@%
  1629.                 break;%@NL@%
  1630. %@NL@%
  1631.             %@AB@%/* Change Type Size. */%@AE@%%@NL@%
  1632.             case 2:%@NL@%
  1633. %@NL@%
  1634.                 if( iFaceIndex == NOFONT )%@NL@%
  1635.                 {%@NL@%
  1636.                     ErrorMsg( "Select a font first" );%@NL@%
  1637.                     break;%@NL@%
  1638.                 }%@NL@%
  1639. %@NL@%
  1640.                 iTypeSize = InputInt( "Enter a type size. ", iTypeSize,%@NL@%
  1641.                                        8, 128 );%@NL@%
  1642. %@NL@%
  1643.                 ChooseFont( iFaceIndex, iTypeSize );%@NL@%
  1644.                 break;%@NL@%
  1645. %@NL@%
  1646.             default:%@NL@%
  1647.                 break;%@NL@%
  1648.         }%@NL@%
  1649.     }%@NL@%
  1650.     PopTitle();%@NL@%
  1651. }%@NL@%
  1652. %@NL@%
  1653. %@AB@%/*  Justify - Gets title justification option.%@NL@%
  1654. %@AB@% *%@NL@%
  1655. %@AB@% *  Params: Pointer to titletype variable%@NL@%
  1656. %@AB@% */%@AE@%%@NL@%
  1657. void Justify( titletype *ptt )%@NL@%
  1658. {%@NL@%
  1659.     int iChoice;%@NL@%
  1660. %@NL@%
  1661.     PushTitle( pszJustify[0] );%@NL@%
  1662.     iChoice = Menu( pszJustify );%@NL@%
  1663.     switch( iChoice )%@NL@%
  1664.     {%@NL@%
  1665. %@NL@%
  1666.         %@AB@%/* Set justification.  */%@AE@%%@NL@%
  1667.         case 1:%@NL@%
  1668.         case 2:%@NL@%
  1669.         case 3:%@NL@%
  1670.             ptt->justify = iChoice;%@NL@%
  1671.     }%@NL@%
  1672.     PopTitle();%@NL@%
  1673. }%@NL@%
  1674. %@NL@%
  1675. %@AB@%/*  Legend - Asks whether a legend is desired, and if so, gets%@NL@%
  1676. %@AB@% *  legend options.%@NL@%
  1677. %@AB@% *%@NL@%
  1678. %@AB@% *  Params: None%@NL@%
  1679. %@AB@% */%@AE@%%@NL@%
  1680. void Legend()%@NL@%
  1681. {%@NL@%
  1682.     int iChoice;%@NL@%
  1683. %@NL@%
  1684.     %@AB@%/* Is legend desired?  */%@AE@%%@NL@%
  1685.     iChoice = BlankMenu( "Legend", "Legend", "No Legend" );%@NL@%
  1686.     switch( iChoice )%@NL@%
  1687.     {%@NL@%
  1688.         case 1:%@NL@%
  1689.             %@AB@%/* If legend, set legend flag and get options.  */%@AE@%%@NL@%
  1690.             ce.legend.legend = TRUE;%@NL@%
  1691.             PushTitle( pszLegendWindow[0] );%@NL@%
  1692.             do%@NL@%
  1693.             {%@NL@%
  1694.                 iChoice = Menu( pszLegendWindow );%@NL@%
  1695.                 switch( iChoice )%@NL@%
  1696.                 {%@NL@%
  1697. %@NL@%
  1698.                     case 1:%@NL@%
  1699.                         %@AB@%/* Get legend place.  */%@AE@%%@NL@%
  1700.                         LegendPlace();%@NL@%
  1701.                         break;%@NL@%
  1702. %@NL@%
  1703.                     case 2:%@NL@%
  1704.                         %@AB@%/* Query for legend color.  */%@AE@%%@NL@%
  1705.                         Help( "Enter a number in the range 0-15.", co.InputColor );%@NL@%
  1706.                         ce.legend.textcolor =%@NL@%
  1707.                             InputInt( "Text color? ",%@NL@%
  1708.                                        ce.legend.textcolor,%@NL@%
  1709.                                        0, 15 );%@NL@%
  1710.                         break;%@NL@%
  1711. %@NL@%
  1712.                     case 3:%@NL@%
  1713.                         %@AB@%/* Get auto or manual sizing.  */%@AE@%%@NL@%
  1714.                         PushTitle( "Auto Legend" );%@NL@%
  1715.                         iChoice = Menu( pszAuto );%@NL@%
  1716. %@NL@%
  1717.                         %@AB@%/* Set or clear the autosize flag. If manual%@NL@%
  1718. %@AB@%                         * sizing was selected, get legend size.%@NL@%
  1719. %@AB@%                         */%@AE@%%@NL@%
  1720.                         switch( iChoice )%@NL@%
  1721.                         {%@NL@%
  1722.                             case 1:%@NL@%
  1723.                                 ce.legend.autosize = TRUE;%@NL@%
  1724.                                 break;%@NL@%
  1725. %@NL@%
  1726.                             case 2:%@NL@%
  1727.                                 ce.legend.autosize = FALSE;%@NL@%
  1728.                                 WindowSize( &ce.legend.legendwindow );%@NL@%
  1729.                         }%@NL@%
  1730.                         PopTitle();%@NL@%
  1731.                         break;%@NL@%
  1732. %@NL@%
  1733.                     case 4:%@NL@%
  1734.                         %@AB@%/* Query for background color.  */%@AE@%%@NL@%
  1735.                         Help( "Type a number in the range 0-15.", co.InputColor );%@NL@%
  1736.                         ce.legend.legendwindow.background =%@NL@%
  1737.                             InputInt( "Background color? ",%@NL@%
  1738.                                        ce.legend.legendwindow.background,%@NL@%
  1739.                                        0, 15 );%@NL@%
  1740.                         break;%@NL@%
  1741. %@NL@%
  1742.                     case 5:%@NL@%
  1743.                         %@AB@%/* Get border options for legend window.  */%@AE@%%@NL@%
  1744.                         Border( &ce.legend.legendwindow );%@NL@%
  1745.                 }%@NL@%
  1746. %@NL@%
  1747.             } while( iChoice != ESCAPE );%@NL@%
  1748.             PopTitle();%@NL@%
  1749.             break;%@NL@%
  1750. %@NL@%
  1751.         case 2:%@NL@%
  1752.             %@AB@%/* If no legend wanted, clear flag.  */%@AE@%%@NL@%
  1753.             ce.legend.legend = FALSE;%@NL@%
  1754. %@NL@%
  1755.     }%@NL@%
  1756.     PopTitle();%@NL@%
  1757. }%@NL@%
  1758. %@NL@%
  1759. %@AB@%/*  LegendPlace - Gets legend placement option.%@NL@%
  1760. %@AB@% *%@NL@%
  1761. %@AB@% *  Params: None%@NL@%
  1762. %@AB@% */%@AE@%%@NL@%
  1763. void LegendPlace()%@NL@%
  1764. {%@NL@%
  1765.     int iChoice;%@NL@%
  1766. %@NL@%
  1767.     %@AB@%/* Get legend placement.  */%@AE@%%@NL@%
  1768.     PushTitle( pszPlace[0] );%@NL@%
  1769.     iChoice = Menu( pszPlace );%@NL@%
  1770.     switch( iChoice )%@NL@%
  1771.     {%@NL@%
  1772. %@NL@%
  1773.         case 1:%@NL@%
  1774.             ce.legend.place = _PG_RIGHT;%@NL@%
  1775.             break;%@NL@%
  1776. %@NL@%
  1777.         case 2:%@NL@%
  1778.             ce.legend.place = _PG_BOTTOM;%@NL@%
  1779.             break;%@NL@%
  1780. %@NL@%
  1781.         case 3:%@NL@%
  1782.             ce.legend.place = _PG_OVERLAY;%@NL@%
  1783.     }%@NL@%
  1784.     PopTitle();%@NL@%
  1785. }%@NL@%
  1786. %@NL@%
  1787. %@AB@%/*  ScreenMode - Gets a new screen mode.%@NL@%
  1788. %@AB@% *%@NL@%
  1789. %@AB@% *  Params: None%@NL@%
  1790. %@AB@% */%@AE@%%@NL@%
  1791. void ScreenMode()%@NL@%
  1792. {%@NL@%
  1793.     int iMode, i;%@NL@%
  1794.     char szTmp[80], szHlp[80];%@NL@%
  1795.     static int iLegal[5][11] =%@NL@%
  1796.     {%@NL@%
  1797.         { 3, 4, 5, 6 },%@NL@%
  1798.         { 4, 4, 5, 6, 64 },%@NL@%
  1799.         { 4, 4, 5, 6, 19 },%@NL@%
  1800.         { 7, 4, 5, 6, 13, 14, 15, 16 },%@NL@%
  1801.         { 10, 4, 5, 6, 13, 14, 15, 16, 17, 18, 19 }%@NL@%
  1802.     };%@NL@%
  1803.     int iAdaptor;%@NL@%
  1804. %@NL@%
  1805.     PushTitle( "Screen Mode" );%@NL@%
  1806. %@NL@%
  1807.     %@AB@%/* Show appropriate help line for adaptor.  */%@AE@%%@NL@%
  1808.     switch( vc.adapter )%@NL@%
  1809.     {%@NL@%
  1810.         case _HGC:%@NL@%
  1811.             PopTitle();%@NL@%
  1812.             return;%@NL@%
  1813.         case _CGA:%@NL@%
  1814.             iAdaptor = 0;%@NL@%
  1815.             break;%@NL@%
  1816.         case _OCGA:%@NL@%
  1817.             iAdaptor = 1;%@NL@%
  1818.             break;%@NL@%
  1819.         case _MCGA:%@NL@%
  1820.             iAdaptor = 2;%@NL@%
  1821.             break;%@NL@%
  1822.         case _EGA:%@NL@%
  1823.         case _OEGA:%@NL@%
  1824.             if( vc.adapter == _MONO )%@NL@%
  1825.             {%@NL@%
  1826.                 PopTitle();%@NL@%
  1827.                 return;%@NL@%
  1828.             }%@NL@%
  1829.             else%@NL@%
  1830.                 iAdaptor = 3;%@NL@%
  1831.             break;%@NL@%
  1832.         case _VGA:%@NL@%
  1833.         case _OVGA:%@NL@%
  1834.             iAdaptor = 4;%@NL@%
  1835.             break;%@NL@%
  1836.     }%@NL@%
  1837. %@NL@%
  1838.     %@AB@%/* Form the help line (which gives the choices legal for%@NL@%
  1839. %@AB@%     * the adaptor sensed in the user's machine).%@NL@%
  1840. %@AB@%     */%@AE@%%@NL@%
  1841.     for( iMode = 0, szHlp[0] = '\0'; iMode <= iLegal[iAdaptor][0]; ++iMode )%@NL@%
  1842.     {%@NL@%
  1843.         if( iMode == 0 )%@NL@%
  1844.             strcpy( szTmp, "Enter " );%@NL@%
  1845.         else if( iMode < iLegal[iAdaptor][0] )%@NL@%
  1846.             sprintf( szTmp, "%d, ", iLegal[iAdaptor][iMode] );%@NL@%
  1847.         else%@NL@%
  1848.             sprintf( szTmp, "or %d", iLegal[iAdaptor][iMode] );%@NL@%
  1849.         strcat( szHlp, szTmp );%@NL@%
  1850.     }%@NL@%
  1851. %@NL@%
  1852.     WrtForm( 18 );%@NL@%
  1853.     Help( szHlp, co.InputColor );%@NL@%
  1854. %@NL@%
  1855.     %@AB@%/* Query for screen mode. */%@AE@%%@NL@%
  1856.     for( ;; )%@NL@%
  1857.     {%@NL@%
  1858.         iMode = InputInt( "Screen Mode? ", si.mode, 1, 64 );%@NL@%
  1859.         for( i = 1; i <= iLegal[iAdaptor][0]; ++i ) %@AB@%/* Test legal values    */%@AE@%%@NL@%
  1860.             if( iMode == iLegal[iAdaptor][i] )      %@AB@%/* If a match is found  */%@AE@%%@NL@%
  1861.                 break;                              %@AB@%/* Terminate for loop   */%@AE@%%@NL@%
  1862.         if( iMode == iLegal[iAdaptor][i] )          %@AB@%/* If it's a match,     */%@AE@%%@NL@%
  1863.             break;                                  %@AB@%/* terminate do loop,   */%@AE@%%@NL@%
  1864.         else                                        %@AB@%/* otherwise BEEP, and  */%@AE@%%@NL@%
  1865.             putchar( BEEP );                        %@AB@%/* solicit correct data */%@AE@%%@NL@%
  1866.     }%@NL@%
  1867. %@NL@%
  1868.     PopTitle();%@NL@%
  1869.     if( SetGraphMode( iMode ) )%@NL@%
  1870.         _setvideomode( _DEFAULTMODE );%@NL@%
  1871.     else%@NL@%
  1872.         ShowError( _PG_BADSCREENMODE );%@NL@%
  1873. %@NL@%
  1874.     %@AB@%/* Force rescaling of the chart by resetting the window%@NL@%
  1875. %@AB@%     * rectangles for the chart and data windows to zero size.%@NL@%
  1876. %@AB@%     */%@AE@%%@NL@%
  1877.     ce.chartwindow.x1 = ce.chartwindow.x2 = ce.chartwindow.y1 =%@NL@%
  1878.         ce.chartwindow.y2 = 0;%@NL@%
  1879.     ce.datawindow = ce.chartwindow;%@NL@%
  1880. }%@NL@%
  1881. %@NL@%
  1882. %@AB@%/*  TitleOpt - Gets title options.%@NL@%
  1883. %@AB@% *%@NL@%
  1884. %@AB@% *  Params: ptt - Pointer to titletype variable%@NL@%
  1885. %@AB@% */%@AE@%%@NL@%
  1886. void TitleOpt( titletype *ptt )%@NL@%
  1887. {%@NL@%
  1888.     int iChoice;%@NL@%
  1889. %@NL@%
  1890.     PushTitle( pszTitleOpt[0] );%@NL@%
  1891.     do%@NL@%
  1892.     {%@NL@%
  1893.         iChoice = Menu( pszTitleOpt );%@NL@%
  1894.         switch( iChoice )%@NL@%
  1895.         {%@NL@%
  1896. %@NL@%
  1897.             case 1: %@NL@%
  1898.                 %@AB@%/* Query for title text.  */%@AE@%%@NL@%
  1899.                 Help( "70 characters maximum length.", co.InputColor );%@NL@%
  1900.                 InputStr( "Enter Text: ", ptt->title );%@NL@%
  1901.                 break;%@NL@%
  1902. %@NL@%
  1903.             case 2: %@NL@%
  1904.                 %@AB@%/* Query for title color color.  */%@AE@%%@NL@%
  1905.                 Help( "Enter a number in the range 0-15.", co.InputColor );%@NL@%
  1906.                 ptt->titlecolor =%@NL@%
  1907.                     InputInt( "Title Color? ", ptt->titlecolor, 0, 15 );%@NL@%
  1908.                 break;%@NL@%
  1909. %@NL@%
  1910.             case 3:%@NL@%
  1911.                 %@AB@%/* Get justify option.  */%@AE@%%@NL@%
  1912.                 Justify( ptt );%@NL@%
  1913.         }%@NL@%
  1914.         ClrHelp();%@NL@%
  1915. %@NL@%
  1916.     } while( iChoice != ESCAPE );%@NL@%
  1917.     PopTitle();%@NL@%
  1918. }%@NL@%
  1919. %@NL@%
  1920. %@AB@%/*  Titles - Manages Main and Sub title menus.%@NL@%
  1921. %@AB@% *%@NL@%
  1922. %@AB@% *  Params: None%@NL@%
  1923. %@AB@% */%@AE@%%@NL@%
  1924. void Titles()%@NL@%
  1925. {%@NL@%
  1926.     int iChoice;%@NL@%
  1927. %@NL@%
  1928.     PushTitle( pszTitles[0] );%@NL@%
  1929.     do%@NL@%
  1930.     {%@NL@%
  1931.         iChoice = Menu( pszTitles );%@NL@%
  1932.         switch( iChoice )%@NL@%
  1933.         {%@NL@%
  1934. %@NL@%
  1935.             case 1:%@NL@%
  1936.                 %@AB@%/* Fix menu title and get options for main title.  */%@AE@%%@NL@%
  1937.                 pszTitleOpt[0] = "MainTitle";%@NL@%
  1938.                 TitleOpt( &ce.maintitle );%@NL@%
  1939.                 break;%@NL@%
  1940. %@NL@%
  1941.             case 2:%@NL@%
  1942.                 %@AB@%/* Fix menu title and get options for subtitle.  */%@AE@%%@NL@%
  1943.                 pszTitleOpt[0] = "Sub Title";%@NL@%
  1944.                 TitleOpt( &ce.subtitle );%@NL@%
  1945.         }%@NL@%
  1946.     } while( iChoice != ESCAPE );%@NL@%
  1947.     PopTitle();%@NL@%
  1948. }%@NL@%
  1949. %@NL@%
  1950. %@AB@%/*  Windows - Selects chart or data window, and gets options for either.%@NL@%
  1951. %@AB@% *%@NL@%
  1952. %@AB@% *  Params: None%@NL@%
  1953. %@AB@% */%@AE@%%@NL@%
  1954. void Windows()%@NL@%
  1955. {%@NL@%
  1956.     int iChoice;%@NL@%
  1957. %@NL@%
  1958.     PushTitle( pszWindows[0] );%@NL@%
  1959.     do%@NL@%
  1960.     {%@NL@%
  1961. %@NL@%
  1962.         %@AB@%/* Select window and get options for it.  */%@AE@%%@NL@%
  1963.         iChoice = Menu( pszWindows );%@NL@%
  1964.         switch( iChoice )%@NL@%
  1965.         {%@NL@%
  1966. %@NL@%
  1967.             case 1:%@NL@%
  1968.                 ChartWindow();%@NL@%
  1969.                 break;%@NL@%
  1970. %@NL@%
  1971.             case 2:%@NL@%
  1972.                 DataWindow();%@NL@%
  1973. %@NL@%
  1974.         }%@NL@%
  1975.     } while( iChoice != ESCAPE );%@NL@%
  1976.     PopTitle();%@NL@%
  1977. }%@NL@%
  1978. %@NL@%
  1979. %@AB@%/*  WindowSize - Gets coordinates for window location and size.%@NL@%
  1980. %@AB@% *%@NL@%
  1981. %@AB@% *  Params: pwt - pointer to windowtype variable%@NL@%
  1982. %@AB@% */%@AE@%%@NL@%
  1983. void WindowSize( windowtype *pwt )%@NL@%
  1984. {%@NL@%
  1985.     int iChoice;%@NL@%
  1986. %@NL@%
  1987.     %@AB@%/* Get window size settings.  */%@AE@%%@NL@%
  1988.     PushTitle( pszSize[0] );%@NL@%
  1989.     do%@NL@%
  1990.     {%@NL@%
  1991.         %@AB@%/* Query for top, bottom, left, or right of window.  */%@AE@%%@NL@%
  1992.         iChoice = Menu( pszSize );%@NL@%
  1993.         switch( iChoice )%@NL@%
  1994.         {%@NL@%
  1995. %@NL@%
  1996.             case 1:%@NL@%
  1997.                 Help( "Enter window top in pixels.", co.InputColor );%@NL@%
  1998.                 pwt->y1 = InputInt( "Top? ", pwt->y1, 0, si.yMax );%@NL@%
  1999.                 break;%@NL@%
  2000. %@NL@%
  2001.             case 2:%@NL@%
  2002.                 Help( "Enter window Left in pixels.", co.InputColor );%@NL@%
  2003.                 pwt->x1 = InputInt( "Left? ", pwt->x1, 0, si.xMax );%@NL@%
  2004.                 break;%@NL@%
  2005. %@NL@%
  2006.             case 3:%@NL@%
  2007.                 Help( "Enter window bottom in pixels.", co.InputColor );%@NL@%
  2008.                 pwt->y2 = InputInt( "Bottom? ", pwt->y2, 0, si.yMax );%@NL@%
  2009.                 break;%@NL@%
  2010. %@NL@%
  2011.             case 4:%@NL@%
  2012.                 Help( "Enter window right in pixels.", co.InputColor );%@NL@%
  2013.                 pwt->x2 = InputInt( "Right? ", pwt->x2, 0, si.xMax );%@NL@%
  2014.         }%@NL@%
  2015.     } while( iChoice != ESCAPE );%@NL@%
  2016.     PopTitle();%@NL@%
  2017. }%@NL@%
  2018. %@NL@%
  2019. %@NL@%
  2020. %@2@%%@AH@%CHRTSUPT.C%@AE@%%@EH@%%@NL@%
  2021. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\MSC60\CHRTSUPT.C%@AE@%%@NL@%
  2022. %@NL@%
  2023. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  2024. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  2025. %@AI@%#include %@AE@%<stdarg.h> %@NL@%
  2026. %@AI@%#include %@AE@%<string.h> %@NL@%
  2027. %@AI@%#include %@AE@%<conio.h> %@NL@%
  2028. %@AI@%#include %@AE@%<graph.h> %@NL@%
  2029. %@AI@%#include %@AE@%<pgchart.h> %@NL@%
  2030. %@AI@%#include %@AE@%"chrtdemo.h" %@NL@%
  2031. %@NL@%
  2032. %@AB@%/* Variables to manage menus.  */%@AE@%%@NL@%
  2033. int cMenuLevel = 0;                 %@AB@%/* Current menu level   */%@AE@%%@NL@%
  2034. char *szMenuTitles[10];             %@AB@%/* Stack of menu titles */%@AE@%%@NL@%
  2035. %@NL@%
  2036. char *pszBlankMenu[4];%@NL@%
  2037. %@NL@%
  2038. %@AB@%/* Variables used to track control and screen position.  */%@AE@%%@NL@%
  2039. extern struct SCREENINFO si;%@NL@%
  2040. %@NL@%
  2041. %@AB@%/* Colors of menus and prompts. */%@AE@%%@NL@%
  2042. extern struct tagColor co;%@NL@%
  2043. %@NL@%
  2044. %@AB@%/*  BlankMenu - Gets responses to two specified choices.%@NL@%
  2045. %@AB@% *%@NL@%
  2046. %@AB@% *  Params: pchTitle - Menu title string%@NL@%
  2047. %@AB@% *          pchChoice1 - Selection 1 string%@NL@%
  2048. %@AB@% *          pchChoice2 - Selection 2 string%@NL@%
  2049. %@AB@% *%@NL@%
  2050. %@AB@% *  Return: Number of choice, or ESCAPE%@NL@%
  2051. %@AB@% */%@AE@%%@NL@%
  2052. int BlankMenu( char *pchTitle, char *pchChoice1, char *pchChoice2 )%@NL@%
  2053. {%@NL@%
  2054.     int iChoice;%@NL@%
  2055. %@NL@%
  2056.     %@AB@%/* Initialize title and selections.  */%@AE@%%@NL@%
  2057.     pszBlankMenu[0] = pchTitle;%@NL@%
  2058.     pszBlankMenu[1] = pchChoice1;%@NL@%
  2059.     pszBlankMenu[2] = pchChoice2;%@NL@%
  2060.     pszBlankMenu[3] = "\0";%@NL@%
  2061.     PushTitle( pszBlankMenu[0]);%@NL@%
  2062. %@NL@%
  2063.     while( TRUE )%@NL@%
  2064.     {%@NL@%
  2065.         %@AB@%/* Accept only first letter of either selection, or ESC.  */%@AE@%%@NL@%
  2066.         iChoice = Menu( pszBlankMenu );%@NL@%
  2067.         switch( iChoice )%@NL@%
  2068.         {%@NL@%
  2069.             case 1:%@NL@%
  2070.             case 2:%@NL@%
  2071.             case ESCAPE:%@NL@%
  2072.                 return iChoice;%@NL@%
  2073.         }%@NL@%
  2074.     }%@NL@%
  2075. }%@NL@%
  2076. %@NL@%
  2077. %@AB@%/*  ClrForm - Clears the center of the screen form.%@NL@%
  2078. %@AB@% *%@NL@%
  2079. %@AB@% *  Params: None%@NL@%
  2080. %@AB@% */%@AE@%%@NL@%
  2081. void ClrForm()%@NL@%
  2082. {%@NL@%
  2083. %@NL@%
  2084.     %@AB@%/* Set partial screen window and clear it, then reset full screen.  */%@AE@%%@NL@%
  2085.     _settextwindow( si.top, 1, si.bot, 80 );%@NL@%
  2086.     _clearscreen( _GWINDOW );%@NL@%
  2087.     _settextwindow( 1, 1, 25, 80 );%@NL@%
  2088. %@NL@%
  2089. }%@NL@%
  2090. %@NL@%
  2091. %@AB@%/*  ClrHelp - Clears the current help line.%@NL@%
  2092. %@AB@% *%@NL@%
  2093. %@AB@% *  Params: None%@NL@%
  2094. %@AB@% */%@AE@%%@NL@%
  2095. void ClrHelp()%@NL@%
  2096. {%@NL@%
  2097.     %@AB@%/* Decrement the help line counter and clear the line.  */%@AE@%%@NL@%
  2098.     _settextwindow( --si.help, 1, si.help, 80 );%@NL@%
  2099.     _clearscreen( _GWINDOW );%@NL@%
  2100.     _settextwindow( 1, 1, 25, 80 );%@NL@%
  2101. }%@NL@%
  2102. %@NL@%
  2103. %@AB@%/*  ErrorMsg - Displays an error message.%@NL@%
  2104. %@AB@% *%@NL@%
  2105. %@AB@% *  Params: pchMsg - error message string%@NL@%
  2106. %@AB@% */%@AE@%%@NL@%
  2107. void ErrorMsg( char *pchMsg )%@NL@%
  2108. {%@NL@%
  2109. %@NL@%
  2110.     %@AB@%/* Beep, set error color, and display error message and continue prompt.  */%@AE@%%@NL@%
  2111.     putch( BEEP );%@NL@%
  2112.     Help( pchMsg, co.ErrorColor );%@NL@%
  2113.     Help( "Press any key to continue.", co.ErrorColor );%@NL@%
  2114. %@NL@%
  2115.     %@AB@%/* Wait for keypress and clear help lines.  */%@AE@%%@NL@%
  2116.     getch();%@NL@%
  2117.     ClrHelp();%@NL@%
  2118.     ClrHelp();%@NL@%
  2119. %@NL@%
  2120. }%@NL@%
  2121. %@NL@%
  2122. %@AB@%/*  Help - Displays a help line on the screen.%@NL@%
  2123. %@AB@% *%@NL@%
  2124. %@AB@% *  Params: pchMsg - error message string%@NL@%
  2125. %@AB@% *          sColor - color for message%@NL@%
  2126. %@AB@% */%@AE@%%@NL@%
  2127. void Help( char *pchMsg, short sColor )%@NL@%
  2128. {%@NL@%
  2129. %@NL@%
  2130.     struct rccoord rcCursor;%@NL@%
  2131. %@NL@%
  2132.     %@AB@%/* Save current cursor position.  */%@AE@%%@NL@%
  2133.     rcCursor = _gettextposition();%@NL@%
  2134. %@NL@%
  2135.     %@AB@%/* Print out help line and increment Helpline position variable.  */%@AE@%%@NL@%
  2136.     PrintAt( si.help++, 5, pchMsg, sColor );%@NL@%
  2137. %@NL@%
  2138.     %@AB@%/* Restore cursor position.  */%@AE@%%@NL@%
  2139.     _settextposition( rcCursor.row, rcCursor.col );%@NL@%
  2140. %@NL@%
  2141. }%@NL@%
  2142. %@NL@%
  2143. %@AB@%/*  InputCh - Prompts for and returns a character of input.%@NL@%
  2144. %@AB@% *%@NL@%
  2145. %@AB@% *  Params: pchPrompt - Prompt string%@NL@%
  2146. %@AB@% *          pchAccept - String of acceptable characters (case insensitive)%@NL@%
  2147. %@AB@% *%@NL@%
  2148. %@AB@% *  Return: Character entered%@NL@%
  2149. %@AB@% */%@AE@%%@NL@%
  2150. int InputCh( char *pchPrompt, char *pchAccept )%@NL@%
  2151. {%@NL@%
  2152.     int chResponse;%@NL@%
  2153. %@NL@%
  2154.     %@AB@%/* Display prompt.  */%@AE@%%@NL@%
  2155.     PrintAt( si.mid, 10, pchPrompt, co.InputColor );%@NL@%
  2156. %@NL@%
  2157.     %@AB@%/* Loop until response is valid.  */%@AE@%%@NL@%
  2158.     while( TRUE )%@NL@%
  2159.     {%@NL@%
  2160.         chResponse = toupper( getch() );%@NL@%
  2161. %@NL@%
  2162.         %@AB@%/* Display and return if acceptable character, or beep if not.  */%@AE@%%@NL@%
  2163.         if( *strchr( pchAccept, chResponse) )%@NL@%
  2164.         {%@NL@%
  2165.             _settextcolor( co.InfoColor );%@NL@%
  2166.             putch( chResponse );%@NL@%
  2167.             return chResponse;%@NL@%
  2168.         }%@NL@%
  2169.         else%@NL@%
  2170.             putch( BEEP );%@NL@%
  2171.     }%@NL@%
  2172. }%@NL@%
  2173. %@NL@%
  2174. %@AB@%/*  InputInt - Prompts for and returns an integer value within a%@NL@%
  2175. %@AB@% *  specified range.%@NL@%
  2176. %@AB@% *%@NL@%
  2177. %@AB@% *  Params: pchPrompt - Prompt string%@NL@%
  2178. %@AB@% *          iOld - Previous value%@NL@%
  2179. %@AB@% *          iMin - Minimum value of range%@NL@%
  2180. %@AB@% *          iMax - Maximum value of range%@NL@%
  2181. %@AB@% *%@NL@%
  2182. %@AB@% *  Return: integer input by user%@NL@%
  2183. %@AB@% */%@AE@%%@NL@%
  2184. int InputInt( char *pchPrompt, int iOld, int iMin, int iMax )%@NL@%
  2185. {%@NL@%
  2186.     int i;%@NL@%
  2187.     char szTmp[70];%@NL@%
  2188. %@NL@%
  2189.     %@AB@%/* Prompt for a string input and convert to an integer until a%@NL@%
  2190. %@AB@%     * value in the specified range is given. Then return the value.%@NL@%
  2191. %@AB@%     */%@AE@%%@NL@%
  2192.     do%@NL@%
  2193.     {%@NL@%
  2194.         InputStr( pchPrompt, itoa( iOld, szTmp, 10) );%@NL@%
  2195.         i = atoi( szTmp );%@NL@%
  2196.     } while( !InRange( i, iMin, iMax) );%@NL@%
  2197.     return i;%@NL@%
  2198. }%@NL@%
  2199. %@NL@%
  2200. %@AB@%/*  InputFloat - Prompts for and returns a float value.%@NL@%
  2201. %@AB@% *%@NL@%
  2202. %@AB@% *  Params: pchPrompt - Prompt string%@NL@%
  2203. %@AB@% *          fOld - Previous value%@NL@%
  2204. %@AB@% *%@NL@%
  2205. %@AB@% *  Return: float input by user%@NL@%
  2206. %@AB@% */%@AE@%%@NL@%
  2207. float InputFloat( char *pchPrompt, float fOld )%@NL@%
  2208. {%@NL@%
  2209.     char szTmp[70];%@NL@%
  2210. %@NL@%
  2211.     %@AB@%/* Prompt for a string input and convert to a float. */%@AE@%%@NL@%
  2212.     sprintf( szTmp, "%f", fOld );%@NL@%
  2213.     InputStr( pchPrompt, szTmp );%@NL@%
  2214.     return (float)atof( szTmp );%@NL@%
  2215. }%@NL@%
  2216. %@NL@%
  2217. %@AB@%/*  InputStr - Prompts for a string. Displays the previous string%@NL@%
  2218. %@AB@% *  until the first character is given. Then replaces it with new%@NL@%
  2219. %@AB@% *  entry.%@NL@%
  2220. %@AB@% *%@NL@%
  2221. %@AB@% *  Params: pchPrompt - Prompt string%@NL@%
  2222. %@AB@% *          pchOld - Charater buffer containing previous string; it%@NL@%
  2223. %@AB@% *            must be long enough to hold new string%@NL@%
  2224. %@AB@% *%@NL@%
  2225. %@AB@% *  Return: pointer to pchOld, which now contains new string%@NL@%
  2226. %@AB@% */%@AE@%%@NL@%
  2227. char *InputStr( char *pchPrompt, char *pchOld )%@NL@%
  2228. {%@NL@%
  2229.     char szTmp[81];%@NL@%
  2230.     int x = 5, y = si.mid, ch;%@NL@%
  2231. %@NL@%
  2232.     %@AB@%/* Display prompt in center of form.  */%@AE@%%@NL@%
  2233.     ClrForm();%@NL@%
  2234.     PrintAt( y, x, pchPrompt, co.InputColor );%@NL@%
  2235.     x += strlen( pchPrompt );%@NL@%
  2236. %@NL@%
  2237.     %@AB@%/* Print the old value for reference.  */%@AE@%%@NL@%
  2238.     _outtext( pchOld );%@NL@%
  2239.     _settextposition( y, x );%@NL@%
  2240. %@NL@%
  2241.     %@AB@%/* Wait for input. When received, clear old string.  */%@AE@%%@NL@%
  2242.     while( !(ch = kbhit()) )%@NL@%
  2243.         ;%@NL@%
  2244.     memset( szTmp, ' ', 80 );%@NL@%
  2245.     szTmp[80] = '\0';%@NL@%
  2246.     PrintAt( y, x, szTmp, -1 );%@NL@%
  2247. %@NL@%
  2248.     %@AB@%/* Get new string. If string entered, return it. If null string%@NL@%
  2249. %@AB@%     * (ENTER key pressed), return old value.%@NL@%
  2250. %@AB@%     */%@AE@%%@NL@%
  2251.     _settextcolor( co.InfoColor );%@NL@%
  2252.     _settextposition( y, x );%@NL@%
  2253.     szTmp[0] = 70;             %@AB@%/* Maximum length to be read */%@AE@%%@NL@%
  2254. %@NL@%
  2255.     cgets( szTmp );%@NL@%
  2256.     if( szTmp[1] > 0 )         %@AB@%/* Are any characters read?  */%@AE@%%@NL@%
  2257.     {%@NL@%
  2258.         strcpy( pchOld, &szTmp[2] );%@NL@%
  2259.         return &szTmp[2];%@NL@%
  2260.     }%@NL@%
  2261.     else%@NL@%
  2262.     {%@NL@%
  2263.         _settextposition( y, x );%@NL@%
  2264.         return pchOld;%@NL@%
  2265.     }%@NL@%
  2266. }%@NL@%
  2267. %@NL@%
  2268. %@AB@%/*  InRange - Checks an integer to see if it is in a specified range.%@NL@%
  2269. %@AB@% *%@NL@%
  2270. %@AB@% *  Params: iValue - Integer to check%@NL@%
  2271. %@AB@% *          iMin - Minimum value of range%@NL@%
  2272. %@AB@% *          iMax - Maximim value of range%@NL@%
  2273. %@AB@% *%@NL@%
  2274. %@AB@% *  Return: TRUE if in range, FALSE if not%@NL@%
  2275. %@AB@% */%@AE@%%@NL@%
  2276. BOOL InRange( int Value, int iMin, int iMax )%@NL@%
  2277. {%@NL@%
  2278.     %@AB@%/* Check range and return true if valid, false if not. Note that%@NL@%
  2279. %@AB@%     * (iMin >= iMax) is taken as a signal to check only the minimum%@NL@%
  2280. %@AB@%     * value; there is no maximum.%@NL@%
  2281. %@AB@%     */%@AE@%%@NL@%
  2282.     if( Value >= iMin )%@NL@%
  2283.         if( (Value <= iMax) || (iMin >= iMax) )%@NL@%
  2284.             return TRUE;%@NL@%
  2285.     else%@NL@%
  2286.     {%@NL@%
  2287.         ErrorMsg( "Invalid value." );%@NL@%
  2288.         return FALSE;%@NL@%
  2289.     }%@NL@%
  2290. }%@NL@%
  2291. %@NL@%
  2292. %@AB@%/*  Menu - Draws menu on screen and returns choice number.%@NL@%
  2293. %@AB@% *%@NL@%
  2294. %@AB@% *  Params: array of menu strings%@NL@%
  2295. %@AB@% *%@NL@%
  2296. %@AB@% *  Return: number corresponding to the choice made from the menu%@NL@%
  2297. %@AB@% */%@AE@%%@NL@%
  2298. int Menu( char *pszMenuList[] )%@NL@%
  2299. {%@NL@%
  2300.     int iItem, cItem, yItem, x = 10;%@NL@%
  2301.     int chResponse;%@NL@%
  2302. %@NL@%
  2303.     %@AB@%/* Count menu items.  */%@AE@%%@NL@%
  2304.     for( cItem = 1; *pszMenuList[cItem]; cItem++ )%@NL@%
  2305.         ;%@NL@%
  2306.     --cItem;%@NL@%
  2307. %@NL@%
  2308. %@NL@%
  2309.     %@AB@%/* Clear the form and print the items in the menu.  */%@AE@%%@NL@%
  2310.     WrtForm( 10 + cItem );%@NL@%
  2311.     for( iItem = 1, yItem = 8; iItem <= cItem; iItem++, yItem++ )%@NL@%
  2312.     {%@NL@%
  2313.         PrintAt( yItem, x, pszMenuList[iItem], co.InputColor );%@NL@%
  2314.         PrintChar( yItem, x, pszMenuList[iItem][0], co.HiliteColor );%@NL@%
  2315.     }%@NL@%
  2316.     ++yItem;%@NL@%
  2317. %@NL@%
  2318.     %@AB@%/* Display prompt and help.  */%@AE@%%@NL@%
  2319.     if( strcmpi( pszMenuList[0], "main menu" ) )    %@AB@%/* If not the main menu */%@AE@%%@NL@%
  2320.         Help( "Type the first letter of your selection or ESC to back up.",%@NL@%
  2321.                   co.InputColor );%@NL@%
  2322.     else%@NL@%
  2323.         Help( "Type the first letter of your selection or \"Q\" to quit.",%@NL@%
  2324.                   co.InputColor );%@NL@%
  2325. %@NL@%
  2326.     PrintAt( yItem, x += 5, "Choice? ", co.InfoColor );%@NL@%
  2327.     x += 8;%@NL@%
  2328. %@NL@%
  2329.     %@AB@%/* Loop until a valid choice is made. Beep at invalid choices.  */%@AE@%%@NL@%
  2330.     while( TRUE )%@NL@%
  2331.     {%@NL@%
  2332.         _settextposition( yItem, x );%@NL@%
  2333.         chResponse = toupper( getch() );%@NL@%
  2334. %@NL@%
  2335.         %@AB@%/* Back up for ESC.  */%@AE@%%@NL@%
  2336.         if( chResponse == 27 )%@NL@%
  2337.         {%@NL@%
  2338.             ClrHelp();%@NL@%
  2339.             return ESCAPE;%@NL@%
  2340.         }%@NL@%
  2341. %@NL@%
  2342.         %@AB@%/* Search first letters of choices for a match. If found, return%@NL@%
  2343. %@AB@%         * choice and clear help line.%@NL@%
  2344. %@AB@%         */%@AE@%%@NL@%
  2345.         for( iItem = 1; iItem <= cItem; iItem++ )%@NL@%
  2346.         {%@NL@%
  2347.             if( chResponse == toupper( pszMenuList[iItem][0]) )%@NL@%
  2348.             {%@NL@%
  2349.                 putch( chResponse );%@NL@%
  2350.                 ClrHelp();%@NL@%
  2351.                 return iItem;%@NL@%
  2352.             }%@NL@%
  2353.         }%@NL@%
  2354. %@NL@%
  2355.         %@AB@%/* If we get here, no valid choice was found, so beep and repeat.  */%@AE@%%@NL@%
  2356.         putch( BEEP );%@NL@%
  2357.     }%@NL@%
  2358. }%@NL@%
  2359. %@NL@%
  2360. %@AB@%/*  PopTitle - Pops a menu title from the menu stack.%@NL@%
  2361. %@AB@% *%@NL@%
  2362. %@AB@% *  Params: None%@NL@%
  2363. %@AB@% */%@AE@%%@NL@%
  2364. void PopTitle()%@NL@%
  2365. {%@NL@%
  2366.     szMenuTitles[--cMenuLevel] = "";%@NL@%
  2367. }%@NL@%
  2368. %@NL@%
  2369. %@AB@%/*  PrintAt - Prints a string at the row/column coordinates%@NL@%
  2370. %@AB@% *            specified, in the specified color.%@NL@%
  2371. %@AB@% *%@NL@%
  2372. %@AB@% *  Params: row        - row at which to begin output of string%@NL@%
  2373. %@AB@% *          col        - column at which to begin output of string%@NL@%
  2374. %@AB@% *          lpszString - zero (null) terminated string%@NL@%
  2375. %@AB@% *          sColor     - color in which to output string (-1 if%@NL@%
  2376. %@AB@% *                       PrintAt should leave color alone)%@NL@%
  2377. %@AB@% */%@AE@%%@NL@%
  2378. void PrintAt( int row, int column, char _far *lpszString, short sColor )%@NL@%
  2379. {%@NL@%
  2380.     if( sColor != -1 )%@NL@%
  2381.         _settextcolor( sColor );%@NL@%
  2382.     _settextposition( row, column );%@NL@%
  2383.     _outtext( lpszString );%@NL@%
  2384. }%@NL@%
  2385. %@NL@%
  2386. %@AB@%/*  PrintChar - Prints a character at the row/column coordinates%@NL@%
  2387. %@AB@% *              specified, in the specified color.%@NL@%
  2388. %@AB@% *%@NL@%
  2389. %@AB@% *  Params: row        - row at which to begin output of string%@NL@%
  2390. %@AB@% *          col        - column at which to begin output of string%@NL@%
  2391. %@AB@% *          cChar      - character to print%@NL@%
  2392. %@AB@% *          sColor     - color in which to output string (-1 if%@NL@%
  2393. %@AB@% *                       PrintChar should leave color alone)%@NL@%
  2394. %@AB@% */%@AE@%%@NL@%
  2395. void PrintChar(int row, int column, char cChar, short sColor)%@NL@%
  2396. {%@NL@%
  2397.     char szTiny[2];%@NL@%
  2398. %@NL@%
  2399.     szTiny[0] = cChar;%@NL@%
  2400.     szTiny[1] = '\0';%@NL@%
  2401.     PrintAt( row, column, szTiny, sColor );%@NL@%
  2402. }%@NL@%
  2403. %@NL@%
  2404. %@AB@%/*  PushTitle - Pushes a menu title on to the menu stack.%@NL@%
  2405. %@AB@% *%@NL@%
  2406. %@AB@% *  Params: pchTitle - title string to push%@NL@%
  2407. %@AB@% */%@AE@%%@NL@%
  2408. void PushTitle( char *pchTitle )%@NL@%
  2409. {%@NL@%
  2410.     szMenuTitles[cMenuLevel++] = pchTitle;%@NL@%
  2411. }%@NL@%
  2412. %@NL@%
  2413. %@AB@%/*  SetDisplayColors - Set the colors to values appropriate to the display%@NL@%
  2414. %@AB@% *                     adaptor being used.%@NL@%
  2415. %@AB@% *%@NL@%
  2416. %@AB@% * Parms: None%@NL@%
  2417. %@AB@% */%@AE@%%@NL@%
  2418. void SetDisplayColors()%@NL@%
  2419. {%@NL@%
  2420.     if( ismono( si.mode ) )%@NL@%
  2421.     {%@NL@%
  2422.         co.InputColor  = M_INPUTCOLOR;%@NL@%
  2423.         co.HiliteColor = M_HILITECOLOR;%@NL@%
  2424.         co.FormColor   = M_FORMCOLOR;%@NL@%
  2425.         co.TitleColor  = M_TITLECOLOR;%@NL@%
  2426.         co.ErrorColor  = M_ERRORCOLOR;%@NL@%
  2427.         co.InfoColor   = M_INFOCOLOR;%@NL@%
  2428.     }%@NL@%
  2429.     else%@NL@%
  2430.     {%@NL@%
  2431.         co.InputColor  = C_INPUTCOLOR;%@NL@%
  2432.         co.HiliteColor = C_HILITECOLOR;%@NL@%
  2433.         co.FormColor   = C_FORMCOLOR;%@NL@%
  2434.         co.TitleColor  = C_TITLECOLOR;%@NL@%
  2435.         co.ErrorColor  = C_ERRORCOLOR;%@NL@%
  2436.         co.InfoColor   = C_INFOCOLOR;%@NL@%
  2437.     }%@NL@%
  2438. }%@NL@%
  2439. %@NL@%
  2440. %@AB@%/*  SprintAt - Format a string, using sprintf() and output to screen%@NL@%
  2441. %@AB@% *             using PrintAt.%@NL@%
  2442. %@AB@% *%@NL@%
  2443. %@AB@% *  Parms: iRow  - Row at which to begin display%@NL@%
  2444. %@AB@% *         iCol  - Column at which to begin display%@NL@%
  2445. %@AB@% *         szFmt - Format string (see run-time library documentation for%@NL@%
  2446. %@AB@% *                 correct formation of a format string)%@NL@%
  2447. %@AB@% *         ...   - Variables to output%@NL@%
  2448. %@AB@% */%@AE@%%@NL@%
  2449. void SprintAt( int iRow, int iCol, char * szFmt, ... )%@NL@%
  2450. {%@NL@%
  2451.     char szTmp[81];%@NL@%
  2452.     va_list Marker;%@NL@%
  2453.     va_list saveMarker;%@NL@%
  2454. %@NL@%
  2455.     va_start( Marker, szFmt );%@NL@%
  2456.     saveMarker = Marker;%@NL@%
  2457.     vsprintf( szTmp, szFmt, Marker );%@NL@%
  2458.     va_end( Marker );%@NL@%
  2459. %@NL@%
  2460.     PrintAt( iRow, iCol, szTmp, -1 );%@NL@%
  2461. }%@NL@%
  2462. %@NL@%
  2463. %@AB@%/*  WrtForm - Displays screen form.%@NL@%
  2464. %@AB@% *%@NL@%
  2465. %@AB@% *  Params: yBot - Row number of the bottom row%@NL@%
  2466. %@AB@% */%@AE@%%@NL@%
  2467. void WrtForm( int yBot )%@NL@%
  2468. {%@NL@%
  2469.     int i;%@NL@%
  2470.     char szTmp[81];%@NL@%
  2471. %@NL@%
  2472.     %@AB@%/* Print message in upper right.  */%@AE@%%@NL@%
  2473.     _clearscreen( _GCLEARSCREEN );%@NL@%
  2474.     PrintAt( 1, 55, "Presentation Graphics Demo", co.TitleColor );%@NL@%
  2475. %@NL@%
  2476.     %@AB@%/* Clear the top separator line.  */%@AE@%%@NL@%
  2477.     memset( szTmp, ' ', 79 );%@NL@%
  2478.     szTmp[79] = 0;%@NL@%
  2479. %@NL@%
  2480.     %@AB@%/* Display each level of the menu title.  */%@AE@%%@NL@%
  2481.     _settextposition( 5, 5 );%@NL@%
  2482.     for( i = 0; i < cMenuLevel; i++ )%@NL@%
  2483.     {%@NL@%
  2484.         if( i )%@NL@%
  2485.             _outtext( " - " );%@NL@%
  2486.         _outtext( szMenuTitles[i] );%@NL@%
  2487.     }%@NL@%
  2488. %@NL@%
  2489.     %@AB@%/* Display the top separator line.  */%@AE@%%@NL@%
  2490.     memset( szTmp, 196, 80 );%@NL@%
  2491.     szTmp[80] = 0;%@NL@%
  2492.     PrintAt( 6, 1, szTmp, co.FormColor );%@NL@%
  2493. %@NL@%
  2494.     %@AB@%/* Display the bottom separator line.  */%@AE@%%@NL@%
  2495.     PrintAt( yBot, 1, szTmp, co.FormColor );%@NL@%
  2496. %@NL@%
  2497.     %@AB@%/* Set the global screen variables.  */%@AE@%%@NL@%
  2498. %@NL@%
  2499.     si.help = yBot + 1;%@NL@%
  2500.     si.top = 7;%@NL@%
  2501.     si.bot = yBot - 1;%@NL@%
  2502.     si.mid = (si.top + si.bot) / 2;%@NL@%
  2503. }%@NL@%
  2504. %@NL@%
  2505. %@NL@%
  2506. %@2@%%@AH@%GRDEMO.C%@AE@%%@EH@%%@NL@%
  2507. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\MSC60\GRDEMO.C%@AE@%%@NL@%
  2508. %@NL@%
  2509. %@AB@%/* GRDEMO.C - Demonstrates capabilities of the Microsoft graphics library.%@NL@%
  2510. %@AB@% * Uses MENU module to display menus. Uses TURTLE module for Turtle%@NL@%
  2511. %@AB@% * graphics. This program runs only in DOS and requires GRAPHICS.LIB.%@NL@%
  2512. %@AB@% */%@AE@%%@NL@%
  2513. %@NL@%
  2514. %@AI@%#include %@AE@%<graph.h> %@NL@%
  2515. %@AI@%#include %@AE@%<math.h> %@NL@%
  2516. %@AI@%#include %@AE@%<malloc.h> %@NL@%
  2517. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  2518. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  2519. %@AI@%#include %@AE@%<conio.h> %@NL@%
  2520. %@AI@%#include %@AE@%<time.h> %@NL@%
  2521. %@AI@%#include %@AE@%"turtle.h" %@NL@%
  2522. %@AI@%#include %@AE@%"menu.h" %@NL@%
  2523. %@NL@%
  2524. %@AB@%/* Function prototypes */%@AE@%%@NL@%
  2525. int  main( void );%@NL@%
  2526. void Circles( void );%@NL@%
  2527. void Sphere( void );%@NL@%
  2528. int  Polygons( void );%@NL@%
  2529. int  Spiral( int angle, double inc );%@NL@%
  2530. int  InSpiral( double side, int angle, int inc );%@NL@%
  2531. void Bug( void );%@NL@%
  2532. void Adjust( void );%@NL@%
  2533. void Diamond( double xy );%@NL@%
  2534. %@NL@%
  2535. %@AB@%/* Returns a random number between min and max, which must be in%@NL@%
  2536. %@AB@% * integer range.%@NL@%
  2537. %@AB@% */%@AE@%%@NL@%
  2538. %@AI@%#define %@AE@%getrandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min)) %@NL@%
  2539. %@NL@%
  2540. %@AB@%/* Constants */%@AE@%%@NL@%
  2541. %@AI@%#define %@AE@%PI      3.141593 %@NL@%
  2542. %@AI@%#define %@AE@%LASTATR 15 %@NL@%
  2543. %@AI@%#define %@AE@%NLASTATR 14 %@NL@%
  2544. %@NL@%
  2545. %@AB@%/* Array and enum for main menu */%@AE@%%@NL@%
  2546. ITEM mnuMain[] =%@NL@%
  2547. {                    %@AB@%/* Highlight Char  Pos */%@AE@%%@NL@%
  2548.     { 0, "Quit"            },   %@AB@%/* Q     0  */%@AE@%%@NL@%
  2549.     { 0, "Circles"         },   %@AB@%/* C     0  */%@AE@%%@NL@%
  2550.     { 0, "Rotating Sphere" },   %@AB@%/* R     0  */%@AE@%%@NL@%
  2551.     { 0, "Tunnel"          },   %@AB@%/* T     0  */%@AE@%%@NL@%
  2552.     { 0, "Spiral"          },   %@AB@%/* S     0  */%@AE@%%@NL@%
  2553.     { 0, "Inverted Spiral" },   %@AB@%/* I     0  */%@AE@%%@NL@%
  2554.     { 0, "Bug"             },   %@AB@%/* B     0  */%@AE@%%@NL@%
  2555.     { 0, "Adjust Window"   },   %@AB@%/* A     0  */%@AE@%%@NL@%
  2556.     { 0, "Mode Change"     },   %@AB@%/* M     0  */%@AE@%%@NL@%
  2557.     { 0, ""                }%@NL@%
  2558. };%@NL@%
  2559. %@NL@%
  2560. %@AB@%/* Define constants (0, 1, 2,...) for menu choices */%@AE@%%@NL@%
  2561. enum CHOICES%@NL@%
  2562. {%@NL@%
  2563.     QUIT, CIRCLES, SPHERE, TUNNEL, SPIRAL, INSPIRAL, BUG, ADJUST, CHANGE%@NL@%
  2564. };%@NL@%
  2565. %@NL@%
  2566. %@AB@%/* Arrays of video mode menu items and of corresponding mode numbers.%@NL@%
  2567. %@AB@% * Each has a temporary array containing all items, and a pointer version%@NL@%
  2568. %@AB@% * including all except Olivetti.%@NL@%
  2569. %@AB@% */%@AE@%%@NL@%
  2570. ITEM mnuModesT[] =%@NL@%
  2571. {                    %@AB@%/* Highlight Char  Pos */%@AE@%%@NL@%
  2572.     { 0, "ORESCOLOR "   },      %@AB@%/* O     0  */%@AE@%%@NL@%
  2573.     { 4, "MRES4COLOR "  },      %@AB@%/* 4     4  */%@AE@%%@NL@%
  2574.     { 4, "MRESNOCOLOR"  },      %@AB@%/* N     4  */%@AE@%%@NL@%
  2575.     { 4, "HRESBW"       },      %@AB@%/* B     4  */%@AE@%%@NL@%
  2576.     { 0, "MRES16COLOR"  },      %@AB@%/* M     0  */%@AE@%%@NL@%
  2577.     { 0, "HRES16COLOR"  },      %@AB@%/* H     0  */%@AE@%%@NL@%
  2578.     { 0, "ERESCOLOR"    },      %@AB@%/* E     0  */%@AE@%%@NL@%
  2579.     { 4, "VRES2COLOR"   },      %@AB@%/* 2     4  */%@AE@%%@NL@%
  2580.     { 0, "VRES16COLOR"  },      %@AB@%/* V     0  */%@AE@%%@NL@%
  2581.     { 1, "MRES256COLOR" },      %@AB@%/* R     4  */%@AE@%%@NL@%
  2582.     { 0, ""             }%@NL@%
  2583. };%@NL@%
  2584. ITEM *mnuModes = &mnuModesT[1];  %@AB@%/* Default is no Olivetti mode */%@AE@%%@NL@%
  2585. %@NL@%
  2586. int aModesT[] =%@NL@%
  2587. {%@NL@%
  2588.     _ORESCOLOR,%@NL@%
  2589.     _MRES4COLOR,%@NL@%
  2590.     _MRESNOCOLOR,%@NL@%
  2591.     _HRESBW,%@NL@%
  2592.     _MRES16COLOR,%@NL@%
  2593.     _HRES16COLOR,%@NL@%
  2594.     _ERESCOLOR,%@NL@%
  2595.     _VRES2COLOR,%@NL@%
  2596.     _VRES16COLOR,%@NL@%
  2597.     _MRES256COLOR,%@NL@%
  2598.     _TEXTMONO,%@NL@%
  2599.     _ERESNOCOLOR,%@NL@%
  2600.     _HERCMONO%@NL@%
  2601. };%@NL@%
  2602. int *aModes = &aModesT[1];              %@AB@%/* Default is no Olivetti mode */%@AE@%%@NL@%
  2603. %@NL@%
  2604. %@AB@%/* Global video configuration */%@AE@%%@NL@%
  2605. struct videoconfig vc;%@NL@%
  2606. %@NL@%
  2607. int main()%@NL@%
  2608. {%@NL@%
  2609.     int rowMid, colMid;%@NL@%
  2610.     int fColor, fFirstTime = TRUE;%@NL@%
  2611.     int iMode, iMainCur = 0, iModesCur = 0;%@NL@%
  2612. %@NL@%
  2613.     _displaycursor( _GCURSOROFF );%@NL@%
  2614.     _getvideoconfig( &vc );%@NL@%
  2615.     rowMid = vc.numtextrows / 2;%@NL@%
  2616.     colMid = vc.numtextcols / 2;%@NL@%
  2617. %@NL@%
  2618.     %@AB@%/* Select best graphics mode, adjust menus, set color flag. Note%@NL@%
  2619. %@AB@%     * that this requires checking both the adapter and the mode.%@NL@%
  2620. %@AB@%     */%@AE@%%@NL@%
  2621.     switch( vc.adapter )%@NL@%
  2622.     {%@NL@%
  2623.         case _OCGA:%@NL@%
  2624.             mnuModes = &mnuModesT[0];           %@AB@%/* Turn on Olivetti mode */%@AE@%%@NL@%
  2625.             aModes = &aModesT[0];%@NL@%
  2626.         case _CGA:%@NL@%
  2627.             mnuModesT[4].achItem[0] = '\0';     %@AB@%/* Turn off EGA modes    */%@AE@%%@NL@%
  2628.             iMode = _MRES4COLOR;%@NL@%
  2629.             break;%@NL@%
  2630.         case _HGC:%@NL@%
  2631.             mnuModesT[7].achItem[0] = '\0';%@NL@%
  2632.             iMode = _HERCMONO;%@NL@%
  2633.             break;%@NL@%
  2634.         case _OEGA:%@NL@%
  2635.             mnuModes = &mnuModesT[0];           %@AB@%/* Turn on Olivetti mode */%@AE@%%@NL@%
  2636.             aModes = &aModesT[0];%@NL@%
  2637.         case _EGA:%@NL@%
  2638.             mnuModesT[7].achItem[0] = '\0';     %@AB@%/* Turn off VGA modes    */%@AE@%%@NL@%
  2639.             if( vc.memory > 64 )%@NL@%
  2640.                 iMode = _ERESCOLOR;%@NL@%
  2641.             else%@NL@%
  2642.                 iMode = _HRES16COLOR;%@NL@%
  2643.             break;%@NL@%
  2644.         case _OVGA:%@NL@%
  2645.             mnuModes = &mnuModesT[0];           %@AB@%/* Turn on Olivetti mode */%@AE@%%@NL@%
  2646.             aModes = &aModesT[0];%@NL@%
  2647.         case _VGA:%@NL@%
  2648.             iMode = _VRES16COLOR;%@NL@%
  2649.             break;%@NL@%
  2650.         case _MCGA:%@NL@%
  2651.             iMode = _MRES256COLOR;%@NL@%
  2652.             break;%@NL@%
  2653.         case _MDPA:%@NL@%
  2654.         default:%@NL@%
  2655.             puts( "No graphics mode available.\n" );%@NL@%
  2656.             return TRUE;%@NL@%
  2657.     }%@NL@%
  2658.     switch( vc.mode )%@NL@%
  2659.     {%@NL@%
  2660.         case _TEXTBW80:%@NL@%
  2661.         case _TEXTBW40:%@NL@%
  2662.             fColor = FALSE;%@NL@%
  2663.             break;%@NL@%
  2664.         case _TEXTMONO:%@NL@%
  2665.         case _ERESNOCOLOR:%@NL@%
  2666.         case _HERCMONO:%@NL@%
  2667.             fColor = FALSE;%@NL@%
  2668.             if( iMode != _HERCMONO )%@NL@%
  2669.                 iMode = _ERESNOCOLOR;%@NL@%
  2670.             mnuMain[8].achItem[0] = '\0';       %@AB@%/* Turn off mode change */%@AE@%%@NL@%
  2671.             break;%@NL@%
  2672.         default:%@NL@%
  2673.             fColor = TRUE;%@NL@%
  2674.             break;%@NL@%
  2675.     }%@NL@%
  2676. %@NL@%
  2677.     %@AB@%/* Find current mode in mode array. */%@AE@%%@NL@%
  2678.     for( iModesCur = 0; aModes[iModesCur] != iMode; iModesCur++ )%@NL@%
  2679.         ;%@NL@%
  2680. %@NL@%
  2681.     %@AB@%/* Seed randomizer with time. */%@AE@%%@NL@%
  2682.     srand( (unsigned)time( NULL ) );%@NL@%
  2683. %@NL@%
  2684.     while( TRUE )%@NL@%
  2685.     {%@NL@%
  2686.         %@AB@%/* Set text mode and optionally clear the screen to blue. */%@AE@%%@NL@%
  2687.         _setvideomode(_DEFAULTMODE );%@NL@%
  2688.         if( fColor )%@NL@%
  2689.             _setbkcolor( (long)_TBLUE );%@NL@%
  2690.         _clearscreen( _GCLEARSCREEN );%@NL@%
  2691. %@NL@%
  2692.         %@AB@%/* Select from menu. */%@AE@%%@NL@%
  2693.         iMainCur = Menu( rowMid, colMid, mnuMain, iMainCur );%@NL@%
  2694. %@NL@%
  2695.         %@AB@%/* Set graphics mode and initialize turtle graphics. Put border%@NL@%
  2696. %@AB@%         * on window.%@NL@%
  2697. %@AB@%         */%@AE@%%@NL@%
  2698.         if( iMainCur != CHANGE )%@NL@%
  2699.         {%@NL@%
  2700.             _setvideomode( iMode );%@NL@%
  2701.             _displaycursor( _GCURSOROFF );%@NL@%
  2702.             _getvideoconfig( &vc );%@NL@%
  2703.             InitTurtle( &vc );%@NL@%
  2704.             Rectangle( 2 * tc.xMax, 2 * tc.yMax );%@NL@%
  2705.         }%@NL@%
  2706. %@NL@%
  2707.         %@AB@%/* Branch to menu choice. */%@AE@%%@NL@%
  2708.         switch( iMainCur )%@NL@%
  2709.         {%@NL@%
  2710.             case QUIT:%@NL@%
  2711.                 _setvideomode( _DEFAULTMODE );%@NL@%
  2712.                 return FALSE;%@NL@%
  2713.             case CIRCLES:%@NL@%
  2714.                 Circles();%@NL@%
  2715.                 break;%@NL@%
  2716.             case SPHERE:%@NL@%
  2717.                 Sphere();%@NL@%
  2718.                 break;%@NL@%
  2719.             case TUNNEL:%@NL@%
  2720.                 PenDown( FALSE );%@NL@%
  2721.                 MoveTo( -tc.xMax * .2, tc.yMax * .15 );%@NL@%
  2722.                 PenDown( TRUE );%@NL@%
  2723.                 Polygons();%@NL@%
  2724.                 while( !GetKey( NO_WAIT ) )%@NL@%
  2725.                     NextColorValue( DEFAULT );   %@AB@%/* Rotate palette */%@AE@%%@NL@%
  2726.                 break;%@NL@%
  2727.             case SPIRAL:%@NL@%
  2728.                 if( Spiral( getrandom( 30, 80 ), (double)getrandom( 1, 5 ) ) )%@NL@%
  2729.                     break;%@NL@%
  2730.                 while( !GetKey( NO_WAIT ) )%@NL@%
  2731.                     NextColorValue( DEFAULT );%@NL@%
  2732.                 break;%@NL@%
  2733.             case INSPIRAL:%@NL@%
  2734.                 NextColorIndex( 0 );%@NL@%
  2735.                 if( InSpiral( (double)getrandom( 8, 20 ),%@NL@%
  2736.                               getrandom( 4, 22 ),%@NL@%
  2737.                               getrandom( 3, 31 ) ) )%@NL@%
  2738.                     break;%@NL@%
  2739.                 while( !GetKey( NO_WAIT ) )%@NL@%
  2740.                     NextColorValue( DEFAULT );%@NL@%
  2741.                 break;%@NL@%
  2742.             case BUG:%@NL@%
  2743.                 Bug();%@NL@%
  2744.                 break;%@NL@%
  2745.             case ADJUST:%@NL@%
  2746.                 Adjust();%@NL@%
  2747.                 continue;%@NL@%
  2748.             case CHANGE:%@NL@%
  2749.                 if( fColor )%@NL@%
  2750.                     _setbkcolor( (long)_TBLUE );%@NL@%
  2751.                 _clearscreen( _GCLEARSCREEN );%@NL@%
  2752. %@NL@%
  2753.                 iModesCur = Menu( rowMid, colMid, mnuModes, iModesCur );%@NL@%
  2754.                 iMode = aModes[iModesCur];%@NL@%
  2755.                 if( vc.adapter == _MCGA )%@NL@%
  2756.                     switch( iMode )%@NL@%
  2757.                     {%@NL@%
  2758.                         case _MRES16COLOR:%@NL@%
  2759.                         case _HRES16COLOR:%@NL@%
  2760.                         case _ERESCOLOR:%@NL@%
  2761.                         case _VRES16COLOR:%@NL@%
  2762.                             _settextposition( 1, 22 );%@NL@%
  2763.                             _outtext( "Mode not recognized" );%@NL@%
  2764.                             iMode = _MRES256COLOR;%@NL@%
  2765.                     }%@NL@%
  2766.                 break;%@NL@%
  2767.         }%@NL@%
  2768.     }%@NL@%
  2769. }%@NL@%
  2770. %@NL@%
  2771. %@AB@%/* Circles - Draw circles of varying sizes and colors on screen in a%@NL@%
  2772. %@AB@% * round pattern.%@NL@%
  2773. %@AB@% *%@NL@%
  2774. %@AB@% * Params: None%@NL@%
  2775. %@AB@% *%@NL@%
  2776. %@AB@% * Return: None%@NL@%
  2777. %@AB@% *%@NL@%
  2778. %@AB@% * Uses:   tc%@NL@%
  2779. %@AB@% */%@AE@%%@NL@%
  2780. void Circles()%@NL@%
  2781. {%@NL@%
  2782.     double x, y, xyRadius;%@NL@%
  2783.     int fFill, fPenDown;%@NL@%
  2784. %@NL@%
  2785.     %@AB@%/* Initialize and save pen and fill flags. */%@AE@%%@NL@%
  2786.     if( tc.cci <= 4 )%@NL@%
  2787.         fFill = SetFill( FALSE );%@NL@%
  2788.     else%@NL@%
  2789.         fFill = SetFill( TRUE );%@NL@%
  2790.     fPenDown = PenDown( FALSE );%@NL@%
  2791. %@NL@%
  2792.     while( TRUE )%@NL@%
  2793.     {%@NL@%
  2794.         %@AB@%/* Draw circles. */%@AE@%%@NL@%
  2795.         for( xyRadius = 10.0; xyRadius <= 130.0; xyRadius++ )%@NL@%
  2796.         {%@NL@%
  2797.             x = (tc.xMax - 30) * atan( sin( xyRadius / PI ) );%@NL@%
  2798.             y = (tc.yMax - 30) * atan( cos( xyRadius / PI ) );%@NL@%
  2799.             MoveTo( x, y );%@NL@%
  2800.             PenColor( NextColorIndex( DEFAULT ) );%@NL@%
  2801.             Circle( xyRadius );%@NL@%
  2802.             if( GetKey( NO_WAIT ) )%@NL@%
  2803.             {%@NL@%
  2804.                 PenDown( fPenDown );%@NL@%
  2805.                 SetFill( fFill );%@NL@%
  2806.                 return;%@NL@%
  2807.             }%@NL@%
  2808.         }%@NL@%
  2809. %@NL@%
  2810.         %@AB@%/* For palette modes (except 256 color), start over. */%@AE@%%@NL@%
  2811.         if( tc.ccv == 64 || tc.ccv == 16 )%@NL@%
  2812.         {%@NL@%
  2813.             _clearscreen( _GCLEARSCREEN );%@NL@%
  2814.             SetFill( FALSE );%@NL@%
  2815.             MoveTo( 0.0, 0.0 );%@NL@%
  2816.             PenColor( WHITE );%@NL@%
  2817.             Rectangle( 2 * tc.xMax, 2 * tc.yMax );%@NL@%
  2818.             SetFill( fFill );%@NL@%
  2819.             NextColorValue( DEFAULT );%@NL@%
  2820.         }%@NL@%
  2821.     }%@NL@%
  2822. }%@NL@%
  2823. %@NL@%
  2824. %@AB@%/* Sphere - Draw and fill slices of a sphere. Rotate colors in EGA+ modes%@NL@%
  2825. %@AB@% * with more than 4 color indexes.%@NL@%
  2826. %@AB@% *%@NL@%
  2827. %@AB@% * Params: None%@NL@%
  2828. %@AB@% *%@NL@%
  2829. %@AB@% * Return: None%@NL@%
  2830. %@AB@% *%@NL@%
  2831. %@AB@% * Uses:   tc%@NL@%
  2832. %@AB@% */%@AE@%%@NL@%
  2833. void Sphere()%@NL@%
  2834. {%@NL@%
  2835.     double xCur, xSize, ySize, xInc;%@NL@%
  2836.     short ciBorder, fFill;%@NL@%
  2837. %@NL@%
  2838.     ySize = xSize = tc.yMax * 0.9 * 2;%@NL@%
  2839.     fFill = SetFill( FALSE );%@NL@%
  2840.     NextColorIndex( 0 );%@NL@%
  2841.     xInc = xSize / 14;%@NL@%
  2842.     ciBorder = PenColor( DEFAULT );%@NL@%
  2843.     BorderColor( ciBorder );%@NL@%
  2844. %@NL@%
  2845.     %@AB@%/* Draw slices. */%@AE@%%@NL@%
  2846.     for( xCur = xInc; xCur <= xSize; xCur += xInc * 2 )%@NL@%
  2847.         Ellipse( xCur, ySize );%@NL@%
  2848.     SetFill( TRUE );%@NL@%
  2849.     PenDown( FALSE );%@NL@%
  2850.     Turn( 90 );%@NL@%
  2851.     xSize /= 2;%@NL@%
  2852.     MoveTo( xSize - xInc, 0.0 );%@NL@%
  2853. %@NL@%
  2854.     NextColorValue( LIMITED );%@NL@%
  2855. %@NL@%
  2856.     %@AB@%/* Fill slices. */%@AE@%%@NL@%
  2857.     while( tc.xCur >= (-xSize + xInc))%@NL@%
  2858.     {%@NL@%
  2859.         PenColor( NextColorIndex( DEFAULT ) );%@NL@%
  2860.         FillIn();%@NL@%
  2861.         Move( -xInc );%@NL@%
  2862.     }%@NL@%
  2863. %@NL@%
  2864.     while( !GetKey( NO_WAIT ) )%@NL@%
  2865.         NextColorValue( LIMITED );%@NL@%
  2866. %@NL@%
  2867.     PenDown( TRUE );%@NL@%
  2868.     SetFill( fFill );%@NL@%
  2869. }%@NL@%
  2870. %@NL@%
  2871. %@AB@%/* Polygons - Draws polygons (starting with triangle) of increasing%@NL@%
  2872. %@AB@% * size by incrementing the number of sides without changing the%@NL@%
  2873. %@AB@% * length of sides. Make sure pen is down.%@NL@%
  2874. %@AB@% *%@NL@%
  2875. %@AB@% * Params: None%@NL@%
  2876. %@AB@% *%@NL@%
  2877. %@AB@% * Return: 1 for user interrupt, 0 for edge of screen encountered%@NL@%
  2878. %@AB@% *%@NL@%
  2879. %@AB@% * Uses:   tc%@NL@%
  2880. %@AB@% */%@AE@%%@NL@%
  2881. int Polygons()%@NL@%
  2882. {%@NL@%
  2883.     int cSides = 3, atrib = 1;%@NL@%
  2884.     double dxy = tc.yUnit;%@NL@%
  2885. %@NL@%
  2886.     while( TRUE )%@NL@%
  2887.     {%@NL@%
  2888.         PenColor( NextColorIndex( DEFAULT ) );%@NL@%
  2889.         if( !Poly( cSides++, dxy += 1.5 ) )%@NL@%
  2890.             return FALSE;%@NL@%
  2891.         if( GetKey( NO_WAIT ) )%@NL@%
  2892.             return TRUE;%@NL@%
  2893.     }%@NL@%
  2894. }%@NL@%
  2895. %@NL@%
  2896. %@AB@%/* Spiral - Draw a spiral by incrementing the length of each side%@NL@%
  2897. %@AB@% * of a rotating figure.%@NL@%
  2898. %@AB@% *%@NL@%
  2899. %@AB@% * Params: ang - determines tightness%@NL@%
  2900. %@AB@% *         xyInc - determines size of sides%@NL@%
  2901. %@AB@% *%@NL@%
  2902. %@AB@% * Return: 1 for user interrupt, 0 for edge of screen encountered%@NL@%
  2903. %@AB@% *%@NL@%
  2904. %@AB@% * Uses:   tc%@NL@%
  2905. %@AB@% */%@AE@%%@NL@%
  2906. int Spiral( int ang, double xyInc )%@NL@%
  2907. {%@NL@%
  2908.     double xy = tc.yUnit;%@NL@%
  2909. %@NL@%
  2910.     while( TRUE )%@NL@%
  2911.     {%@NL@%
  2912.         PenColor( NextColorIndex( DEFAULT ) );%@NL@%
  2913.         if( !Move( xy += xyInc ) )%@NL@%
  2914.             return FALSE;%@NL@%
  2915.         Turn( ang );%@NL@%
  2916.         if( GetKey( NO_WAIT ) )%@NL@%
  2917.             return TRUE;%@NL@%
  2918.     }%@NL@%
  2919. }%@NL@%
  2920. %@NL@%
  2921. %@AB@%/* InSpiral - Draw an inverted spiral by increasing each angle%@NL@%
  2922. %@AB@% * of a rotating figure while keeping the length of sides constant.%@NL@%
  2923. %@AB@% *%@NL@%
  2924. %@AB@% * Params: xy - determines size%@NL@%
  2925. %@AB@% *         ang - initial angle determines shape%@NL@%
  2926. %@AB@% *         angInc - determines tightness and shape%@NL@%
  2927. %@AB@% *%@NL@%
  2928. %@AB@% * Return: 1 for user interrupt, 0 for edge of screen encountered%@NL@%
  2929. %@AB@% */%@AE@%%@NL@%
  2930. int InSpiral( double xy, int ang, int angInc )%@NL@%
  2931. {%@NL@%
  2932.     while( TRUE )%@NL@%
  2933.     {%@NL@%
  2934.         PenColor( NextColorIndex( DEFAULT ) );%@NL@%
  2935.         if( !Move( xy ) )%@NL@%
  2936.             return FALSE;%@NL@%
  2937.         Turn( ang += angInc );%@NL@%
  2938.         if( GetKey( NO_WAIT ))%@NL@%
  2939.             return TRUE;%@NL@%
  2940.     }%@NL@%
  2941. }%@NL@%
  2942. %@NL@%
  2943. %@AB@%/* Bug - Draws a winged bug on the screen. Then moves it randomly%@NL@%
  2944. %@AB@% * around the screen.%@NL@%
  2945. %@AB@% *%@NL@%
  2946. %@AB@% * Params: None%@NL@%
  2947. %@AB@% *%@NL@%
  2948. %@AB@% * Return: None%@NL@%
  2949. %@AB@% *%@NL@%
  2950. %@AB@% * Uses:   tc%@NL@%
  2951. %@AB@% */%@AE@%%@NL@%
  2952. void Bug()%@NL@%
  2953. {%@NL@%
  2954. %@NL@%
  2955.     static unsigned char uTopWing[] = { 0x81, 0x3c, 0xc3, 0x66,%@NL@%
  2956.                                         0x66, 0x0f, 0xf0, 0x18 };%@NL@%
  2957.     static unsigned char uBotWing[] = { 0x66, 0x0f, 0xf0, 0x18,%@NL@%
  2958.                                         0x81, 0x3c, 0xc3, 0x66 };%@NL@%
  2959.     char *buffer;               %@AB@%/* Buffer for image */%@AE@%%@NL@%
  2960. %@NL@%
  2961.     %@AB@%/* Draw bug. */%@AE@%%@NL@%
  2962.     PenDown( FALSE );%@NL@%
  2963.     SetFill( TRUE );%@NL@%
  2964.     Move( 40.0 );               %@AB@%/* Draw and fill front wings */%@AE@%%@NL@%
  2965.     Turn( 90 );%@NL@%
  2966.     Move( 80.0 );%@NL@%
  2967.     PenColor( 1 );%@NL@%
  2968.     _setfillmask( uTopWing );%@NL@%
  2969.     Ellipse( 172.0, 70.0 );%@NL@%
  2970.     Turn( 180 );%@NL@%
  2971.     Move( 160.0 );%@NL@%
  2972.     Ellipse( 172.0, 70.0 );%@NL@%
  2973.     Turn(-90 );%@NL@%
  2974.     MoveTo( 0.0, 0.0 );%@NL@%
  2975.     Move( 25.0 );               %@AB@%/* Draw and fill back wings */%@AE@%%@NL@%
  2976.     Turn( 90 );%@NL@%
  2977.     Move( 70.0 );%@NL@%
  2978.     PenColor( 2 );%@NL@%
  2979.     _setfillmask( uBotWing );%@NL@%
  2980.     Ellipse( 150.0, 70.0 );%@NL@%
  2981.     Turn( 180 );%@NL@%
  2982.     Move( 140.0 );%@NL@%
  2983.     Ellipse( 150.0, 70.0 );%@NL@%
  2984.     Turn(-90 );%@NL@%
  2985.     MoveTo( 0.0, 0.0 );%@NL@%
  2986.     _setfillmask( NULL );       %@AB@%/* Draw body */%@AE@%%@NL@%
  2987.     PenColor( 3 );%@NL@%
  2988.     BorderColor( 3 );%@NL@%
  2989.     Ellipse( 52.0, 220.0 );%@NL@%
  2990.     PenColor( 1 );              %@AB@%/* Drill eyes */%@AE@%%@NL@%
  2991.     BorderColor( 1 );%@NL@%
  2992.     SetFill( FALSE );%@NL@%
  2993.     Move( 90.0 );%@NL@%
  2994.     Turn( 90 );%@NL@%
  2995.     Move( 22.0 );%@NL@%
  2996.     Circle( 20.0 );%@NL@%
  2997.     PenColor( 0 );%@NL@%
  2998.     FillIn();%@NL@%
  2999.     PenColor( 1 );%@NL@%
  3000.     Turn( 180 );%@NL@%
  3001.     Move( 44.0 );%@NL@%
  3002.     Circle( 20.0 );%@NL@%
  3003.     PenColor( 0 );%@NL@%
  3004.     FillIn();%@NL@%
  3005. %@NL@%
  3006.     %@AB@%/* Move into position - top-right of image. */%@AE@%%@NL@%
  3007.     MoveTo( 0.0, 0.0 );%@NL@%
  3008.     TurnTo( 0 );%@NL@%
  3009.     Move( 120.0 );%@NL@%
  3010.     Turn( -90 );%@NL@%
  3011.     Move( 175.0 );%@NL@%
  3012.     Turn( 90 );%@NL@%
  3013. %@NL@%
  3014.     %@AB@%/* Size image and allocate memory for it. */%@AE@%%@NL@%
  3015.     buffer = (char *)malloc( (size_t)ImageSize( 350.0, 240.0 ) );%@NL@%
  3016.     GetImage( 350.0, 240.0, buffer );%@NL@%
  3017. %@NL@%
  3018.     %@AB@%/* Move randomly, adjusting at edges. */%@AE@%%@NL@%
  3019.     while( !GetKey( NO_WAIT ) )%@NL@%
  3020.     {%@NL@%
  3021.         if( tc.xCur <= (-tc.xMax + 15.0) )%@NL@%
  3022.             TurnTo( 90 );%@NL@%
  3023.         else if( tc.yCur <= (-tc.yMax + 15.0) )%@NL@%
  3024.             TurnTo( 180 );%@NL@%
  3025.         else if( tc.xCur >= (tc.xMax - 365.0) )%@NL@%
  3026.             TurnTo( 270 );%@NL@%
  3027.         else if( tc.yCur >= (tc.yMax - 255.0) )%@NL@%
  3028.             TurnTo( 0 );%@NL@%
  3029.         else%@NL@%
  3030.             Turn( getrandom( -20, 20 ) );%@NL@%
  3031.         Move( 3.0 );%@NL@%
  3032.         PutImage( buffer, _GPSET );%@NL@%
  3033.     }%@NL@%
  3034.     free( (char *)buffer );%@NL@%
  3035. }%@NL@%
  3036. %@NL@%
  3037. %@AB@%/* Adjust - Allow the user to interactively adjust the display window.%@NL@%
  3038. %@AB@% * Unshifted direction keys adjust the window size. Shifted direction%@NL@%
  3039. %@AB@% * keys move the window. The numeric keypad plus and minus keys adjust%@NL@%
  3040. %@AB@% * aspect without changing the window. A window frame and a diamond give%@NL@%
  3041. %@AB@% * visual feedback on adjustments.%@NL@%
  3042. %@AB@% *%@NL@%
  3043. %@AB@% * Params: None%@NL@%
  3044. %@AB@% *%@NL@%
  3045. %@AB@% * Return: None%@NL@%
  3046. %@AB@% *%@NL@%
  3047. %@AB@% * Uses:   tc and vc%@NL@%
  3048. %@AB@% */%@AE@%%@NL@%
  3049. %@AI@%#define %@AE@%WININC 4 %@NL@%
  3050. void Adjust()%@NL@%
  3051. {%@NL@%
  3052.     short iWriteMode;%@NL@%
  3053.     double xyRadius = 400.0, xEdge, yEdge;%@NL@%
  3054.     char achT[40];%@NL@%
  3055. %@NL@%
  3056.     %@AB@%/* Display instructions. */%@AE@%%@NL@%
  3057.     _clearscreen( _GCLEARSCREEN );%@NL@%
  3058.     _settextposition( 2, 2 );%@NL@%
  3059.     _outtext(" Grey PLUS and MINUS Adjust aspect" );%@NL@%
  3060.     _settextposition( 3, 2 );%@NL@%
  3061.     _outtext(" Cursor keys         Size window" );%@NL@%
  3062.     _settextposition( 4, 2 );%@NL@%
  3063.     _outtext(" SHIFT cursor keys   Move window" );%@NL@%
  3064.     _settextposition( 5, 2 );%@NL@%
  3065.     _outtext(" ENTER               Finished" );%@NL@%
  3066. %@NL@%
  3067.     %@AB@%/* Save old write mode and set XOR so you can erase figures by%@NL@%
  3068. %@AB@%     * redrawing. This allows lines to overwrite text without erasing.%@NL@%
  3069. %@AB@%     */%@AE@%%@NL@%
  3070.     iWriteMode = _getwritemode();%@NL@%
  3071.     _setwritemode( _GXOR );%@NL@%
  3072. %@NL@%
  3073.     while( TRUE )%@NL@%
  3074.     {%@NL@%
  3075.         %@AB@%/* Display data. */%@AE@%%@NL@%
  3076.         _settextposition( 6, 2 );%@NL@%
  3077.         sprintf( achT, " ratio=%1.2f  xMax=%.f  yMax=%.f",%@NL@%
  3078.                  tc.yxRatio, tc.xMax, tc.yMax );%@NL@%
  3079.         _outtext( achT );%@NL@%
  3080. %@NL@%
  3081.         %@AB@%/* Calculate current box edges. */%@AE@%%@NL@%
  3082.         xEdge = 2 * tc.xMax;%@NL@%
  3083.         yEdge = 2 * tc.yMax;%@NL@%
  3084. %@NL@%
  3085.         %@AB@%/* Draw border rectangle and diamond that illustrates ratio. */%@AE@%%@NL@%
  3086.         Rectangle( xEdge, yEdge );%@NL@%
  3087.         Diamond( xyRadius );%@NL@%
  3088. %@NL@%
  3089.         switch( GetKey( CLEAR_WAIT ) )%@NL@%
  3090.         {%@NL@%
  3091.             %@AB@%/* Adjust aspect. */%@AE@%%@NL@%
  3092.             case N_MINUS:%@NL@%
  3093.                 if( tc.yxRatio > 0.4 )%@NL@%
  3094.                     tc.yxRatio = (tc.xMax - (WININC * tc.yUnit)) / tc.yMax;%@NL@%
  3095.                 break;%@NL@%
  3096. %@NL@%
  3097.             case N_PLUS:%@NL@%
  3098.                 if( tc.yxRatio < 8.0 )%@NL@%
  3099.                     tc.yxRatio = (tc.xMax + (WININC * tc.yUnit)) / tc.yMax;%@NL@%
  3100.                 break;%@NL@%
  3101. %@NL@%
  3102.             %@AB@%/* Adjust window size. */%@AE@%%@NL@%
  3103.             case U_RT:%@NL@%
  3104.                 if( tc.xsLeft < (vc.numxpixels / 3) )%@NL@%
  3105.                     tc.xsLeft += WININC;%@NL@%
  3106.                 if( tc.xsRight > (vc.numxpixels - (vc.numxpixels / 3)) )%@NL@%
  3107.                     tc.xsRight -= WININC;%@NL@%
  3108.                 break;%@NL@%
  3109.             case U_LT:%@NL@%
  3110.                 if( tc.xsLeft )%@NL@%
  3111.                     tc.xsLeft -= WININC;%@NL@%
  3112.                 if( tc.xsRight < vc.numxpixels )%@NL@%
  3113.                     tc.xsRight += WININC;%@NL@%
  3114.                 break;%@NL@%
  3115.             case U_DN:%@NL@%
  3116.                 if( tc.ysTop < (vc.numypixels / 3) )%@NL@%
  3117.                     tc.ysTop += WININC;%@NL@%
  3118.                 if( tc.ysBot > (vc.numypixels - (vc.numypixels / 3)) )%@NL@%
  3119.                     tc.ysBot -= WININC;%@NL@%
  3120.                 break;%@NL@%
  3121.             case U_UP:%@NL@%
  3122.                 if( tc.ysTop )%@NL@%
  3123.                     tc.ysTop -= WININC;%@NL@%
  3124.                 if( tc.ysBot < vc.numypixels )%@NL@%
  3125.                     tc.ysBot += WININC;%@NL@%
  3126.                 break;%@NL@%
  3127. %@NL@%
  3128.             %@AB@%/* Adjust window position. */%@AE@%%@NL@%
  3129.             case S_LT:%@NL@%
  3130.                 if( tc.xsLeft )%@NL@%
  3131.                 {%@NL@%
  3132.                     tc.xsLeft -= WININC;%@NL@%
  3133.                     tc.xsRight -= WININC;%@NL@%
  3134.                 }%@NL@%
  3135.                 break;%@NL@%
  3136.             case S_RT:%@NL@%
  3137.                 if( tc.xsRight < vc.numxpixels )%@NL@%
  3138.                 {%@NL@%
  3139.                     tc.xsLeft += WININC;%@NL@%
  3140.                     tc.xsRight += WININC;%@NL@%
  3141.                 }%@NL@%
  3142.                 break;%@NL@%
  3143.             case S_UP:%@NL@%
  3144.                 if( tc.ysTop )%@NL@%
  3145.                 {%@NL@%
  3146.                     tc.ysTop -= WININC;%@NL@%
  3147.                     tc.ysBot -= WININC;%@NL@%
  3148.                 }%@NL@%
  3149.                 break;%@NL@%
  3150.             case S_DN:%@NL@%
  3151.                 if( tc.ysBot < vc.numypixels )%@NL@%
  3152.                 {%@NL@%
  3153.                     tc.ysTop += WININC;%@NL@%
  3154.                     tc.ysBot += WININC;%@NL@%
  3155.                 }%@NL@%
  3156.                 break;%@NL@%
  3157. %@NL@%
  3158.             %@AB@%/* Finished. */%@AE@%%@NL@%
  3159.             case ENTER:%@NL@%
  3160.                 _setwritemode( iWriteMode );%@NL@%
  3161.                 return;%@NL@%
  3162. %@NL@%
  3163.             %@AB@%/* Ignore unknown key. */%@AE@%%@NL@%
  3164.             default:%@NL@%
  3165.                 break;%@NL@%
  3166.         }%@NL@%
  3167.         %@AB@%/* Redraw figures to erase them. Reset defaults. */%@AE@%%@NL@%
  3168.         Rectangle( xEdge, yEdge );%@NL@%
  3169.         Diamond( xyRadius );%@NL@%
  3170.         Home();%@NL@%
  3171.     }%@NL@%
  3172. }%@NL@%
  3173. %@NL@%
  3174. %@AB@%/* Routine used by Adjust to draw its diamond. */%@AE@%%@NL@%
  3175. void Diamond( double xy )%@NL@%
  3176. {%@NL@%
  3177.         PenDown( FALSE );%@NL@%
  3178.         MoveTo( 0.0, xy );%@NL@%
  3179.         PenDown( TRUE );%@NL@%
  3180.         MoveTo( xy, 0.0 );%@NL@%
  3181.         MoveTo( 0.0, -xy );%@NL@%
  3182.         MoveTo( -xy, 0.0 );%@NL@%
  3183.         MoveTo( 0.0, xy );%@NL@%
  3184.         PenDown( FALSE );%@NL@%
  3185.         MoveTo( 0.0, 0.0 );%@NL@%
  3186.         PenDown( TRUE );%@NL@%
  3187. }%@NL@%
  3188. %@NL@%
  3189. %@NL@%
  3190. %@2@%%@AH@%MENU.C%@AE@%%@EH@%%@NL@%
  3191. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\MSC60\MENU.C%@AE@%%@NL@%
  3192. %@NL@%
  3193. %@AB@%/* MENU - Module of functions to put menus on the screen and handle keyboard%@NL@%
  3194. %@AB@% * input. To use it, include the MENU.H file in your program. The following%@NL@%
  3195. %@AB@% * functions are public:%@NL@%
  3196. %@AB@% *%@NL@%
  3197. %@AB@% *   Menu       -   Puts a menu on screen and reads input for it%@NL@%
  3198. %@AB@% *   Box        -   Puts a box on screen (fill it yourself)%@NL@%
  3199. %@AB@% *   GetKey     -   Gets ASCII or function key%@NL@%
  3200. %@AB@% *   _outchar   -   Displays character using current text position and color%@NL@%
  3201. %@AB@% *%@NL@%
  3202. %@AB@% * The following structures are defined:%@NL@%
  3203. %@AB@% *%@NL@%
  3204. %@AB@% *   MENU       -   Defines menu colors, box type, and centering%@NL@%
  3205. %@AB@% *   ITEM       -   Defines text of menu item and index of highlight character%@NL@%
  3206. %@AB@% *%@NL@%
  3207. %@AB@% * The global variable "mnuAtrib" has type MENU. Change this variable to%@NL@%
  3208. %@AB@% * change menu appearance.%@NL@%
  3209. %@AB@% */%@AE@%%@NL@%
  3210. %@NL@%
  3211. %@AI@%#include %@AE@%<string.h> %@NL@%
  3212. %@AI@%#include %@AE@%<stddef.h> %@NL@%
  3213. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  3214. %@AI@%#include %@AE@%<graph.h> %@NL@%
  3215. %@AI@%#include %@AE@%<bios.h> %@NL@%
  3216. %@AI@%#include %@AE@%"menu.h" %@NL@%
  3217. %@NL@%
  3218. %@AB@%/* Prototype for internal function */%@AE@%%@NL@%
  3219. static void Itemize( int row, int col, int fCur, ITEM itm, int cBlank );%@NL@%
  3220. %@NL@%
  3221. %@AB@%/* Default menu attribute. The default works for color or B&W. You can%@NL@%
  3222. %@AB@% * override the default value by defining your own MENU variable and%@NL@%
  3223. %@AB@% * assigning it to mnuAtrib, or you can modify specific fields at%@NL@%
  3224. %@AB@% * run time. For example, you could use a different attribute for color%@NL@%
  3225. %@AB@% * than for black and white.%@NL@%
  3226. %@AB@% */%@AE@%%@NL@%
  3227. MENU mnuAtrib =%@NL@%
  3228. {%@NL@%
  3229.     _TBLACK, _TBLACK, _TWHITE, _TBRIGHTWHITE, _TBRIGHTWHITE,%@NL@%
  3230.     _TWHITE, _TWHITE, _TBLACK, _TWHITE, _TBLACK,%@NL@%
  3231.     TRUE,%@NL@%
  3232.     '┌', '┐', '┘', '└', '│', '─'%@NL@%
  3233. };%@NL@%
  3234. %@NL@%
  3235. %@AB@%/* Menu - Puts menu on screen and reads menu input from keyboard. When a%@NL@%
  3236. %@AB@% * highlighted hot key or ENTER is pressed, returns the index of the%@NL@%
  3237. %@AB@% * selected menu item.%@NL@%
  3238. %@AB@% *%@NL@%
  3239. %@AB@% * Params: row and col - If "fCentered" attribute of "mnuAtrib" is true,%@NL@%
  3240. %@AB@% *           center row and column of menu; otherwise top left of menu%@NL@%
  3241. %@AB@% *         aItem - array of structure containing the text of each item%@NL@%
  3242. %@AB@% *           and the index of the highlighted hot key%@NL@%
  3243. %@AB@% *         iCur - index of the current selection--pass 0 for first item,%@NL@%
  3244. %@AB@% *           or maintain a static value%@NL@%
  3245. %@AB@% *%@NL@%
  3246. %@AB@% * Return: The index of the selected item%@NL@%
  3247. %@AB@% *%@NL@%
  3248. %@AB@% * Uses:   mnuAtrib%@NL@%
  3249. %@AB@% */%@AE@%%@NL@%
  3250. int Menu( int row, int col, ITEM aItem[], int iCur )%@NL@%
  3251. {%@NL@%
  3252.     int cItem, cchItem = 2; %@AB@%/* Counts of items and chars per item       */%@AE@%%@NL@%
  3253.     int i, iPrev;           %@AB@%/* Indexes - temporary and previous         */%@AE@%%@NL@%
  3254.     int acchItem[MAXITEM];  %@AB@%/* Array of counts of character in items    */%@AE@%%@NL@%
  3255.     char *pchT;             %@AB@%/* Temporary character pointer              */%@AE@%%@NL@%
  3256.     char achHilite[36];     %@AB@%/* Array for highlight characters           */%@AE@%%@NL@%
  3257.     unsigned uKey;          %@AB@%/* Unsigned key code                        */%@AE@%%@NL@%
  3258.     long bgColor;           %@AB@%/* Screen color, position, and cursor       */%@AE@%%@NL@%
  3259.     short fgColor;%@NL@%
  3260.     struct rccoord rc;%@NL@%
  3261.     unsigned fCursor;%@NL@%
  3262. %@NL@%
  3263.     %@AB@%/* Save screen information. */%@AE@%%@NL@%
  3264.     fCursor = _displaycursor( _GCURSOROFF );%@NL@%
  3265.     bgColor = _getbkcolor();%@NL@%
  3266.     fgColor = _gettextcolor();%@NL@%
  3267.     rc = _gettextposition();%@NL@%
  3268. %@NL@%
  3269.     %@AB@%/* Count items, find longest, and put count of each in array. Also,%@NL@%
  3270. %@AB@%     * put the highlighted character from each in a string.%@NL@%
  3271. %@AB@%     */%@AE@%%@NL@%
  3272.     for( cItem = 0; aItem[cItem].achItem[0]; cItem++ )%@NL@%
  3273.     {%@NL@%
  3274.         acchItem[cItem] = strlen( aItem[cItem].achItem );%@NL@%
  3275.         cchItem = (acchItem[cItem] > cchItem) ? acchItem[cItem] : cchItem;%@NL@%
  3276.         i = aItem[cItem].iHilite;%@NL@%
  3277.         achHilite[cItem] = aItem[cItem].achItem[i];%@NL@%
  3278.     }%@NL@%
  3279.     cchItem += 2;%@NL@%
  3280.     achHilite[cItem] = 0;          %@AB@%/* Null-terminate and lowercase string  */%@AE@%%@NL@%
  3281.     strlwr( achHilite );%@NL@%
  3282. %@NL@%
  3283.     %@AB@%/* Adjust if centered, and draw menu box. */%@AE@%%@NL@%
  3284.     if( mnuAtrib.fCentered )%@NL@%
  3285.     {%@NL@%
  3286.         row -= cItem / 2;%@NL@%
  3287.         col -= cchItem / 2;%@NL@%
  3288.     }%@NL@%
  3289.     Box( row++, col++, cItem, cchItem );%@NL@%
  3290. %@NL@%
  3291.     %@AB@%/* Put items on menu. */%@AE@%%@NL@%
  3292.     for( i = 0; i < cItem; i++ )%@NL@%
  3293.     {%@NL@%
  3294.         if( i == iCur )%@NL@%
  3295.             Itemize( row + i, col, TRUE, aItem[i], cchItem - acchItem[i] );%@NL@%
  3296.         else%@NL@%
  3297.             Itemize( row + i, col, FALSE, aItem[i], cchItem - acchItem[i] );%@NL@%
  3298.     }%@NL@%
  3299. %@NL@%
  3300.     while( TRUE )%@NL@%
  3301.     {%@NL@%
  3302.         %@AB@%/* Wait until a uKey is pressed, then evaluate it. */%@AE@%%@NL@%
  3303.         uKey = GetKey( WAIT );%@NL@%
  3304.         switch( uKey )%@NL@%
  3305.         {%@NL@%
  3306.             case U_UP:                      %@AB@%/* Up key       */%@AE@%%@NL@%
  3307.                 iPrev = iCur;%@NL@%
  3308.                 iCur = (iCur > 0) ? (--iCur % cItem) : cItem - 1;%@NL@%
  3309.                 break;%@NL@%
  3310.             case U_DN:                      %@AB@%/* Down key     */%@AE@%%@NL@%
  3311.                 iPrev = iCur;%@NL@%
  3312.                 iCur = (iCur < cItem) ? (++iCur % cItem) : 0;%@NL@%
  3313.                 break;%@NL@%
  3314.             default:%@NL@%
  3315.                 if( uKey > 256 )            %@AB@%/* Ignore unknown function key  */%@AE@%%@NL@%
  3316.                     continue;%@NL@%
  3317.                 pchT = strchr( achHilite, (char)tolower( uKey ) );%@NL@%
  3318.                 if( pchT != NULL )          %@AB@%/* If in highlight string,      */%@AE@%%@NL@%
  3319.                     iCur = pchT - achHilite;%@AB@%/*   evaluate and fall through  */%@AE@%%@NL@%
  3320.                 else%@NL@%
  3321.                     continue;               %@AB@%/* Ignore unknown ASCII key     */%@AE@%%@NL@%
  3322.             case ENTER:%@NL@%
  3323.                 _setbkcolor( bgColor );%@NL@%
  3324.                 _settextcolor( fgColor );%@NL@%
  3325.                 _settextposition( rc.row, rc.col );%@NL@%
  3326.                 _displaycursor( fCursor );%@NL@%
  3327.                 return iCur;%@NL@%
  3328.         }%@NL@%
  3329.         %@AB@%/* Redisplay current and previous. */%@AE@%%@NL@%
  3330.         Itemize( row + iCur, col,%@NL@%
  3331.                  TRUE, aItem[iCur], cchItem - acchItem[iCur] );%@NL@%
  3332.         Itemize( row + iPrev, col,%@NL@%
  3333.                  FALSE, aItem[iPrev], cchItem - acchItem[iPrev] );%@NL@%
  3334.     }%@NL@%
  3335. }%@NL@%
  3336. %@NL@%
  3337. %@AB@%/* Box - Draw menu box, filling interior with blanks of the border color.%@NL@%
  3338. %@AB@% *%@NL@%
  3339. %@AB@% * Params: row and col - upper left of box%@NL@%
  3340. %@AB@% *         rowLast and colLast - height and width%@NL@%
  3341. %@AB@% *%@NL@%
  3342. %@AB@% * Return: None%@NL@%
  3343. %@AB@% *%@NL@%
  3344. %@AB@% * Uses:   mnuAtrib%@NL@%
  3345. %@AB@% */%@AE@%%@NL@%
  3346. void Box( int row, int col, int rowLast, int colLast )%@NL@%
  3347. {%@NL@%
  3348.     int i;%@NL@%
  3349.     char achT[MAXITEM + 2];         %@AB@%/* Temporary array of characters */%@AE@%%@NL@%
  3350. %@NL@%
  3351.     %@AB@%/* Set color and position. */%@AE@%%@NL@%
  3352.     _settextposition( row, col );%@NL@%
  3353.     _settextcolor( mnuAtrib.fgBorder );%@NL@%
  3354.     _setbkcolor( mnuAtrib.bgBorder );%@NL@%
  3355. %@NL@%
  3356.     %@AB@%/* Draw box top. */%@AE@%%@NL@%
  3357.     achT[0] = mnuAtrib.chNW;%@NL@%
  3358.     memset( achT + 1, mnuAtrib.chEW, colLast );%@NL@%
  3359.     achT[colLast + 1] = mnuAtrib.chNE;%@NL@%
  3360.     achT[colLast + 2] = 0;%@NL@%
  3361.     _outtext( achT );%@NL@%
  3362. %@NL@%
  3363.     %@AB@%/* Draw box sides and center. */%@AE@%%@NL@%
  3364.     achT[0] = mnuAtrib.chNS;%@NL@%
  3365.     memset( achT + 1, ' ', colLast );%@NL@%
  3366.     achT[colLast + 1] = mnuAtrib.chNS;%@NL@%
  3367.     achT[colLast + 2] = 0;%@NL@%
  3368.     for( i = 1; i <= rowLast; ++i )%@NL@%
  3369.     {%@NL@%
  3370.         _settextposition( row + i, col );%@NL@%
  3371.         _outtext( achT );%@NL@%
  3372.     }%@NL@%
  3373. %@NL@%
  3374.     %@AB@%/* Draw box bottom. */%@AE@%%@NL@%
  3375.     _settextposition( row + rowLast + 1, col );%@NL@%
  3376.     achT[0] = mnuAtrib.chSW;%@NL@%
  3377.     memset( achT + 1, mnuAtrib.chEW, colLast );%@NL@%
  3378.     achT[colLast + 1] = mnuAtrib.chSE;%@NL@%
  3379.     achT[colLast + 2] = 0;%@NL@%
  3380.     _outtext( achT );%@NL@%
  3381. }%@NL@%
  3382. %@NL@%
  3383. %@AB@%/* Itemize - Display one selection (item) of a menu. This function%@NL@%
  3384. %@AB@% * is normally only used internally by Menu.%@NL@%
  3385. %@AB@% *%@NL@%
  3386. %@AB@% * Params: row and col - top left of menu%@NL@%
  3387. %@AB@% *         fCur - flag set if item is current selection%@NL@%
  3388. %@AB@% *         itm - structure containing item text and index of highlight%@NL@%
  3389. %@AB@% *         cBlank - count of blanks to fill%@NL@%
  3390. %@AB@% *%@NL@%
  3391. %@AB@% * Return: none%@NL@%
  3392. %@AB@% *%@NL@%
  3393. %@AB@% * Uses:   mnuAtrib%@NL@%
  3394. %@AB@% */%@AE@%%@NL@%
  3395. void Itemize( int row, int col, int fCur, ITEM itm, int cBlank )%@NL@%
  3396. {%@NL@%
  3397.     int i;%@NL@%
  3398.     char achT[MAXITEM];             %@AB@%/* Temporary array of characters */%@AE@%%@NL@%
  3399. %@NL@%
  3400.     %@AB@%/* Set text position and color. */%@AE@%%@NL@%
  3401.     _settextposition( row, col );%@NL@%
  3402.     if( fCur )%@NL@%
  3403.     {%@NL@%
  3404.         _settextcolor( mnuAtrib.fgSelect );%@NL@%
  3405.         _setbkcolor( mnuAtrib.bgSelect );%@NL@%
  3406.     }%@NL@%
  3407.     else%@NL@%
  3408.     {%@NL@%
  3409.         _settextcolor( mnuAtrib.fgNormal );%@NL@%
  3410.         _setbkcolor( mnuAtrib.bgNormal );%@NL@%
  3411.     }%@NL@%
  3412. %@NL@%
  3413.     %@AB@%/* Display item and fill blanks. */%@AE@%%@NL@%
  3414.     strcat( strcpy( achT, " " ), itm.achItem );%@NL@%
  3415.     _outtext( achT );%@NL@%
  3416.     memset( achT, ' ', cBlank-- );%@NL@%
  3417.     achT[cBlank] = 0;%@NL@%
  3418.     _outtext( achT );%@NL@%
  3419. %@NL@%
  3420.     %@AB@%/* Set position and color of highlight character, then display it. */%@AE@%%@NL@%
  3421.     i = itm.iHilite;%@NL@%
  3422.     _settextposition( row, col + i + 1 );%@NL@%
  3423.     if( fCur )%@NL@%
  3424.     {%@NL@%
  3425.         _settextcolor( mnuAtrib.fgSelHilite );%@NL@%
  3426.         _setbkcolor( mnuAtrib.bgSelHilite );%@NL@%
  3427.     }%@NL@%
  3428.     else%@NL@%
  3429.     {%@NL@%
  3430.         _settextcolor( mnuAtrib.fgNormHilite );%@NL@%
  3431.         _setbkcolor( mnuAtrib.bgNormHilite );%@NL@%
  3432.     }%@NL@%
  3433.     _outchar( itm.achItem[i] );%@NL@%
  3434. }%@NL@%
  3435. %@NL@%
  3436. %@AB@%/* GetKey - Gets a key from the keyboard. This routine distinguishes%@NL@%
  3437. %@AB@% * between ASCII keys and function or control keys with different shift%@NL@%
  3438. %@AB@% * states. It also accepts a flag to return immediately if no key is%@NL@%
  3439. %@AB@% * available.%@NL@%
  3440. %@AB@% *%@NL@%
  3441. %@AB@% * Params: fWait - Code to indicate how to handle keyboard buffer:%@NL@%
  3442. %@AB@% *   NO_WAIT     Return 0 if no key in buffer, else return key%@NL@%
  3443. %@AB@% *   WAIT        Return first key if available, else wait for key%@NL@%
  3444. %@AB@% *   CLEAR_WAIT  Throw away any key in buffer and wait for new key%@NL@%
  3445. %@AB@% *%@NL@%
  3446. %@AB@% * Return: One of the following:%@NL@%
  3447. %@AB@% *%@NL@%
  3448. %@AB@% *   Keytype                                High Byte    Low Byte%@NL@%
  3449. %@AB@% *   -------                                ---------    --------%@NL@%
  3450. %@AB@% *   No key available (only with NO_WAIT)       0           0%@NL@%
  3451. %@AB@% *   ASCII value                                0        ASCII code%@NL@%
  3452. %@AB@% *   Unshifted function or keypad               1        scan code%@NL@%
  3453. %@AB@% *   Shifted function or keypad                 2        scan code%@NL@%
  3454. %@AB@% *   CTRL function or keypad                    3        scan code%@NL@%
  3455. %@AB@% *   ALT function or keypad                     4        scan code%@NL@%
  3456. %@AB@% *%@NL@%
  3457. %@AB@% * Note:   getkey cannot return codes for keys not recognized by BIOS%@NL@%
  3458. %@AB@% *         int 16, such as the CTRL-UP or the 5 key on the numeric keypad.%@NL@%
  3459. %@AB@% */%@AE@%%@NL@%
  3460. unsigned GetKey( int fWait )%@NL@%
  3461. {%@NL@%
  3462.     unsigned uKey, uShift;%@NL@%
  3463. %@NL@%
  3464.     %@AB@%/* If CLEAR_WAIT, drain the keyboard buffer. */%@AE@%%@NL@%
  3465.     if( fWait == CLEAR_WAIT )%@NL@%
  3466.         while( _bios_keybrd( _KEYBRD_READY ) )%@NL@%
  3467.             _bios_keybrd( _KEYBRD_READ );%@NL@%
  3468. %@NL@%
  3469.     %@AB@%/* If NO_WAIT, return 0 if there is no key ready. */%@AE@%%@NL@%
  3470.     if( !fWait && !_bios_keybrd( _KEYBRD_READY ) )%@NL@%
  3471.         return FALSE;%@NL@%
  3472. %@NL@%
  3473.     %@AB@%/* Get key code. */%@AE@%%@NL@%
  3474.     uKey = _bios_keybrd( _KEYBRD_READ );%@NL@%
  3475. %@NL@%
  3476.     %@AB@%/* If low byte is not zero, it's an ASCII key. Check scan code to see%@NL@%
  3477. %@AB@%     * if it's on the numeric keypad. If not, clear high byte and return.%@NL@%
  3478. %@AB@%     */%@AE@%%@NL@%
  3479.     if( uKey & 0x00ff )%@NL@%
  3480.         if( (uKey >> 8) < 69 )%@NL@%
  3481.             return( uKey & 0x00ff );%@NL@%
  3482. %@NL@%
  3483.     %@AB@%/* For function keys and numeric keypad, put scan code in low byte%@NL@%
  3484. %@AB@%     * and shift state codes in high byte.%@NL@%
  3485. %@AB@%     */%@AE@%%@NL@%
  3486.     uKey >>= 8;%@NL@%
  3487.     uShift = _bios_keybrd( _KEYBRD_SHIFTSTATUS ) & 0x000f;%@NL@%
  3488.     switch( uShift )%@NL@%
  3489.     {%@NL@%
  3490.         case 0:%@NL@%
  3491.             return( 0x0100 | uKey );  %@AB@%/* None (1)    */%@AE@%%@NL@%
  3492.         case 1:%@NL@%
  3493.         case 2:%@NL@%
  3494.         case 3:%@NL@%
  3495.             return( 0x0200 | uKey );  %@AB@%/* Shift (2)   */%@AE@%%@NL@%
  3496.         case 4:%@NL@%
  3497.             return( 0x0300 | uKey );  %@AB@%/* Control (3) */%@AE@%%@NL@%
  3498.         case 8:%@NL@%
  3499.             return( 0x0400 | uKey );  %@AB@%/* Alt (4)     */%@AE@%%@NL@%
  3500.     }%@NL@%
  3501. }%@NL@%
  3502. %@NL@%
  3503. %@AB@%/* _outchar - Display a character. This is the character equivalent of%@NL@%
  3504. %@AB@% * _outtext. It is affected by _settextposition, _settextcolor, and%@NL@%
  3505. %@AB@% * _setbkcolor. It should not be used in loops. Build strings and then%@NL@%
  3506. %@AB@% * _outtext to show multiple characters.%@NL@%
  3507. %@AB@% *%@NL@%
  3508. %@AB@% * Params: out - character to be displayed%@NL@%
  3509. %@AB@% *%@NL@%
  3510. %@AB@% * Return: none%@NL@%
  3511. %@AB@% */%@AE@%%@NL@%
  3512. void _outchar( char out )%@NL@%
  3513. {%@NL@%
  3514.     static char achT[2] = " ";      %@AB@%/* Temporary array of characters */%@AE@%%@NL@%
  3515. %@NL@%
  3516.     achT[0] = out;%@NL@%
  3517.     _outtext( achT );%@NL@%
  3518. }%@NL@%
  3519. %@NL@%
  3520. %@NL@%
  3521. %@2@%%@AH@%SNAP.C%@AE@%%@EH@%%@NL@%
  3522. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\MSC60\SNAP.C%@AE@%%@NL@%
  3523. %@NL@%
  3524. %@AB@%/* Snap - An OS/2 screen capture utility%@NL@%
  3525. %@AB@% *%@NL@%
  3526. %@AB@% * Snap starts a background process containing a keyboard monitor.%@NL@%
  3527. %@AB@% * The monitor checks for a hot key (ALT-*). If found, a thread is%@NL@%
  3528. %@AB@% * launched to write the screen to a file. Various command line options%@NL@%
  3529. %@AB@% * allow you to specify capture behavior or to deinstall the program.%@NL@%
  3530. %@AB@% *%@NL@%
  3531. %@AB@% * To compile, use the following command line:%@NL@%
  3532. %@AB@% *%@NL@%
  3533. %@AB@% *   cl /MT /G2s snap.c%@NL@%
  3534. %@AB@% */%@AE@%%@NL@%
  3535. %@NL@%
  3536. %@AB@%/* Function prototypes */%@AE@%%@NL@%
  3537. int  Monitor( void );%@NL@%
  3538. void Snap( unsigned long _far *arg );%@NL@%
  3539. void BackError( char *msgErr );%@NL@%
  3540. void Syntax( void );%@NL@%
  3541. void EvalOptions( int argc, char **argv );%@NL@%
  3542. %@NL@%
  3543. %@AB@%/* Define constants to enable function groups in OS2 include files */%@AE@%%@NL@%
  3544. %@AI@%#define %@AE@%INCL_NOCOMMON %@NL@%
  3545. %@AI@%#define %@AE@%INCL_NOPM %@NL@%
  3546. %@AI@%#define %@AE@%INCL_KBD            // KBDKEYINFO %@NL@%
  3547. %@AI@%#define %@AE@%INCL_VIO            // Vio functions %@NL@%
  3548. %@AI@%#define %@AE@%INCL_DOSMEMMGR      // DosGetShrSeg, DosAllocShrSeg, %@NL@%
  3549. %@AI@%#define %@AE@%INCL_DOSMONITORS    // DosMon functions %@NL@%
  3550. %@AI@%#define %@AE@%INCL_DOSMISC        // DosGetEnv %@NL@%
  3551. %@AI@%#define %@AE@%INCL_DOSSEMAPHORES  // DosSem functions %@NL@%
  3552. %@AI@%#define %@AE@%INCL_DOSPROCESS     // DosBeep, DosSetPrty %@NL@%
  3553. %@AI@%#define %@AE@%INCL_DOSINFOSEG     // DosGetInfoSeg %@NL@%
  3554. %@AI@%#include %@AE@%<os2.h> %@NL@%
  3555. %@NL@%
  3556. %@AI@%#include %@AE@%<malloc.h>         // malloc, free %@NL@%
  3557. %@AI@%#include %@AE@%<process.h>        // _beginthread, _endthread, exit, spawnl %@NL@%
  3558. %@AI@%#include %@AE@%<string.h>         // strcpy, strcat %@NL@%
  3559. %@AI@%#include %@AE@%<stdlib.h>         // atoi, itoa, _MAX_PATH %@NL@%
  3560. %@AI@%#include %@AE@%<stddef.h>         // _threadid variable %@NL@%
  3561. %@AI@%#include %@AE@%<stdio.h>          // puts, fopen, fwrite, etc. %@NL@%
  3562. %@AI@%#include %@AE@%<conio.h>          // kbhit %@NL@%
  3563. %@NL@%
  3564. %@AI@%#define %@AE@%CON  0              // Handle for the console device %@NL@%
  3565. %@AI@%#define %@AE@%FAIL -1             // Fail to start thread %@NL@%
  3566. %@NL@%
  3567. %@AI@%#define %@AE@%STAR    0x37        // Scan code for * on numeric keypad %@NL@%
  3568. %@AI@%#define %@AE@%RELEASE 0x40        // Bit mask for key release %@NL@%
  3569. %@NL@%
  3570. %@AB@%/* Name and structure for shared memory data */%@AE@%%@NL@%
  3571. char szShrSeg[] = { "\\SHAREMEM\\SNAP.DAT" };%@NL@%
  3572. struct SHARED%@NL@%
  3573. {%@NL@%
  3574.     BOOL  fSound;               // Sound flag%@NL@%
  3575.     BOOL  fAppend;              // Append flag%@NL@%
  3576.     BOOL  fInstall;             // Install flag%@NL@%
  3577.     SHORT cScreen;              // Count of screens%@NL@%
  3578.     LONG  lfWait;               // Wait semaphore%@NL@%
  3579.     CHAR  achSnap[_MAX_PATH];   // Snap file name%@NL@%
  3580. } _far *pshrSnap = 0;           // Initialize offset to 0. Segment will%@NL@%
  3581.                                 // be initialized by system call.%@NL@%
  3582. %@NL@%
  3583. %@AB@%/* Count in bytes of shared segment */%@AE@%%@NL@%
  3584. %@AI@%#define %@AE@%C_SHARESEG sizeof( struct SHARED ) %@NL@%
  3585. %@NL@%
  3586. void main( int argc, char **argv )%@NL@%
  3587. {%@NL@%
  3588.     USHORT offCmd;              // Dummy for DosGetEnv%@NL@%
  3589.     CHAR   *pchSnapExe = 0;     // Pointer to name of executable file%@NL@%
  3590.                                 //   (offset initialized to 0)%@NL@%
  3591. %@NL@%
  3592.     %@AB@%/* Try to get shared segment (note how selector value is placed%@NL@%
  3593. %@AB@%     * directly in the segment word of the pointer address). There are%@NL@%
  3594. %@AB@%     * three possibilities:%@NL@%
  3595. %@AB@%     *%@NL@%
  3596. %@AB@%     *   - We can't get memory. This means SNAP is not installed,%@NL@%
  3597. %@AB@%     *     so we must allocate the memory and exec ourself in the%@NL@%
  3598. %@AB@%     *     background to install the monitor.%@NL@%
  3599. %@AB@%     *   - We can get memory and we are not installed. This means%@NL@%
  3600. %@AB@%     *     we have been execed by previous process to install monitor.%@NL@%
  3601. %@AB@%     *   - We can get memory and we are already installed. This means%@NL@%
  3602. %@AB@%     *     we were just called to modify options.%@NL@%
  3603. %@AB@%     */%@AE@%%@NL@%
  3604.     if( DosGetShrSeg( szShrSeg, (PSEL)&pshrSnap + 1 ) )%@NL@%
  3605.     {%@NL@%
  3606.         %@AB@%/* Segment doesn't exist, so try to allocate it. */%@AE@%%@NL@%
  3607.         if( DosAllocShrSeg( C_SHARESEG, szShrSeg, (PSEL)&pshrSnap + 1 ) )%@NL@%
  3608.         {%@NL@%
  3609.             puts( "Can't allocate shared memory" );%@NL@%
  3610.             exit( 1 );%@NL@%
  3611.         }%@NL@%
  3612.         else%@NL@%
  3613.         {%@NL@%
  3614.             %@AB@%/* This is the first time through, so we must execute%@NL@%
  3615. %@AB@%             * ourself to do installation. First set defaults, then%@NL@%
  3616. %@AB@%             * modify for options.%@NL@%
  3617. %@AB@%             */%@AE@%%@NL@%
  3618.             pshrSnap->fSound = TRUE;%@NL@%
  3619.             pshrSnap->fAppend = TRUE;%@NL@%
  3620.             pshrSnap->cScreen = 0;%@NL@%
  3621.             pshrSnap->fInstall = FALSE;%@NL@%
  3622.             strcpy( pshrSnap->achSnap, "SNAP.IMG" );%@NL@%
  3623.             DosSemSet( &pshrSnap->lfWait );%@NL@%
  3624.             EvalOptions( argc, argv );%@NL@%
  3625. %@NL@%
  3626.             %@AB@%/* Get our own path name from the end of the environment%@NL@%
  3627. %@AB@%             * segment. This is the most reliable way to get the full%@NL@%
  3628. %@AB@%             * path name of the current file, since the extension is%@NL@%
  3629. %@AB@%             * ommitted from argv[0].%@NL@%
  3630. %@AB@%             */%@AE@%%@NL@%
  3631.             DosGetEnv( (PUSHORT)&pchSnapExe + 1, &offCmd );%@NL@%
  3632. %@NL@%
  3633.             %@AB@%/* Adjust forward until we point to full path of .EXE file. */%@AE@%%@NL@%
  3634.             while( *pchSnapExe++ || *pchSnapExe )%@NL@%
  3635.                 ;%@NL@%
  3636.             ++pchSnapExe;%@NL@%
  3637. %@NL@%
  3638.             %@AB@%/* Spawn ourself as a background process. Can't install%@NL@%
  3639. %@AB@%             * monitor now because we are in foreground. A background%@NL@%
  3640. %@AB@%             * process needs to install the monitor.%@NL@%
  3641. %@AB@%             */%@AE@%%@NL@%
  3642.             if( spawnl( P_DETACH, pchSnapExe, pchSnapExe, NULL ) == -1 )%@NL@%
  3643.             {%@NL@%
  3644.                 puts( "Can't start background process" );%@NL@%
  3645.                 exit( 1 );%@NL@%
  3646.             }%@NL@%
  3647.             puts( "Snap installed" );%@NL@%
  3648.             Syntax();%@NL@%
  3649. %@NL@%
  3650.             %@AB@%/* Wait for background child process to report receiving%@NL@%
  3651. %@AB@%             * shared data.%@NL@%
  3652. %@AB@%             */%@AE@%%@NL@%
  3653.             DosSemWait( &pshrSnap->lfWait, SEM_INDEFINITE_WAIT );%@NL@%
  3654.         }%@NL@%
  3655.     }%@NL@%
  3656.     else%@NL@%
  3657.     {%@NL@%
  3658.         %@AB@%/* Already installed. We are being run to evaluate options and%@NL@%
  3659. %@AB@%         * modify behavior accordingly.%@NL@%
  3660. %@AB@%         */%@AE@%%@NL@%
  3661.         if( pshrSnap->fInstall )%@NL@%
  3662.             if( argc == 1 )%@NL@%
  3663.                 puts( "Snap already installed" );%@NL@%
  3664.             else%@NL@%
  3665.                 EvalOptions( argc, argv );%@NL@%
  3666.         else%@NL@%
  3667.         {%@NL@%
  3668.             %@AB@%/* Not installed, so we were execed by original SNAP to%@NL@%
  3669. %@AB@%             * install monitor. Tell parent we have received data, set%@NL@%
  3670. %@AB@%             * install flag, and install monitor.%@NL@%
  3671. %@AB@%             */%@AE@%%@NL@%
  3672.             DosSemClear( &pshrSnap->lfWait );%@NL@%
  3673. %@NL@%
  3674.             %@AB@%/* Set installed flag and start monitor. */%@AE@%%@NL@%
  3675.             pshrSnap->fInstall = TRUE;%@NL@%
  3676. %@NL@%
  3677.             exit( Monitor() );%@NL@%
  3678.         }%@NL@%
  3679.     }%@NL@%
  3680. }%@NL@%
  3681. %@NL@%
  3682. %@AB@%/* Monitor routine checks keystrokes as they occur and calls%@NL@%
  3683. %@AB@% * the Snap thread if the hot key is pressed.%@NL@%
  3684. %@AB@% *%@NL@%
  3685. %@AB@% * Params: None%@NL@%
  3686. %@AB@% *%@NL@%
  3687. %@AB@% * Return: 1 if error, 0 if deinstalled%@NL@%
  3688. %@AB@% *%@NL@%
  3689. %@AB@% * Uses:   pshrSnap - Shared memory structure%@NL@%
  3690. %@AB@% */%@AE@%%@NL@%
  3691. int Monitor()%@NL@%
  3692. {%@NL@%
  3693.     #define BUFSIZE 128             // Size for monitor buffers:%@NL@%
  3694.                                     //   64 minimum, 128 recommended%@NL@%
  3695.     #define STACKSIZE 2048          // 2K minimum for any system call%@NL@%
  3696. %@NL@%
  3697.     PMONIN pmnin;%@NL@%
  3698.     PMONOUT pmnout;%@NL@%
  3699. %@NL@%
  3700.     struct KEYPACKET                // KBD monitor data record%@NL@%
  3701.     {%@NL@%
  3702.         USHORT fMon;%@NL@%
  3703.         KBDKEYINFO kki;%@NL@%
  3704.         USHORT fDD;%@NL@%
  3705.     } keyBuff;%@NL@%
  3706.     USHORT ckeyBuff = sizeof( keyBuff );%@NL@%
  3707. %@NL@%
  3708.     HMONITOR hKeyMon;               // Keyboard handle from monitor open%@NL@%
  3709.     PGINFOSEG pGIS = 0, pLIS = 0;   // Information segment structures%@NL@%
  3710.     LONG  lfSnap = FALSE;           // Semaphore for each Snap thread%@NL@%
  3711. %@NL@%
  3712.     %@AB@%/* Allocate space for monitor read/write buffers and mark size. */%@AE@%%@NL@%
  3713.     pmnin = (PMONIN)malloc( BUFSIZE );%@NL@%
  3714.     pmnin->cb = BUFSIZE;%@NL@%
  3715.     pmnout = (PMONOUT)malloc( BUFSIZE );%@NL@%
  3716.     pmnout->cb = BUFSIZE;%@NL@%
  3717. %@NL@%
  3718.     %@AB@%/* Register monitor to the keyboard device (KBD$). */%@AE@%%@NL@%
  3719.     if( DosMonOpen( "KBD$", &hKeyMon ) )%@NL@%
  3720.     {%@NL@%
  3721.         BackError( "Can't open monitor" );%@NL@%
  3722.         return 1;%@NL@%
  3723.     }%@NL@%
  3724. %@NL@%
  3725.     %@AB@%/* Get information segments (all we really need is ID of current%@NL@%
  3726. %@AB@%     * screen group from Global Information Segment).%@NL@%
  3727. %@AB@%     */%@AE@%%@NL@%
  3728.     DosGetInfoSeg( (PSEL)&pGIS + 1, (PSEL)&pLIS + 1 );%@NL@%
  3729. %@NL@%
  3730.     %@AB@%/* Register the monitor buffers to the current screen group */%@AE@%%@NL@%
  3731.     if( DosMonReg( hKeyMon, (PBYTE)pmnin, (PBYTE)pmnout,%@NL@%
  3732.                    MONITOR_DEFAULT, pGIS->sgCurrent ) )%@NL@%
  3733.     {%@NL@%
  3734.         BackError( "Can't register monitor" );%@NL@%
  3735.         return 1;%@NL@%
  3736.     }%@NL@%
  3737. %@NL@%
  3738.     %@AB@%/* Make process time critical so keys are interpreted without delay. */%@AE@%%@NL@%
  3739.     DosSetPrty( PRTYS_PROCESS, PRTYC_TIMECRITICAL, 0, 0 );%@NL@%
  3740. %@NL@%
  3741.     %@AB@%/* Monitor loop - read into monitor buffer and examine. Take action%@NL@%
  3742. %@AB@%     * if hot key, otherwise pass on to device driver.%@NL@%
  3743. %@AB@%     */%@AE@%%@NL@%
  3744.     while( pshrSnap->fInstall )%@NL@%
  3745.     {%@NL@%
  3746.         DosMonRead( (PBYTE)pmnin, IO_WAIT, (PBYTE)&keyBuff, &ckeyBuff );%@NL@%
  3747. %@NL@%
  3748.         %@AB@%/* Snap if ALT+STAR is down. */%@AE@%%@NL@%
  3749.         if( ((keyBuff.kki.chScan == STAR) || (keyBuff.kki.chScan == 0x2a)) &&%@NL@%
  3750.             (keyBuff.kki.fsState & ALT) &&%@NL@%
  3751.             (!(keyBuff.fDD & RELEASE)) )%@NL@%
  3752.         {%@NL@%
  3753.             %@AB@%/* Make sure last thread is finished */%@AE@%%@NL@%
  3754.             DosSemWait( &lfSnap, SEM_INDEFINITE_WAIT );%@NL@%
  3755.             if( (_beginthread( Snap, NULL, STACKSIZE,%@NL@%
  3756.                                (PVOID)&lfSnap )) == FAIL )%@NL@%
  3757.                 BackError( "Can't start screen capture thread" );%@NL@%
  3758.             else%@NL@%
  3759.                 DosSemSet( &lfSnap );%@NL@%
  3760.         }%@NL@%
  3761.         else%@NL@%
  3762.             %@AB@%/* Pass the key through if it is not the hot key */%@AE@%%@NL@%
  3763.             DosMonWrite( (PBYTE)pmnout, (PBYTE)&keyBuff, ckeyBuff );%@NL@%
  3764.     }%@NL@%
  3765. %@NL@%
  3766.     %@AB@%/* Close monitor */%@AE@%%@NL@%
  3767.     free( pmnin );%@NL@%
  3768.     free( pmnout );%@NL@%
  3769.     DosMonClose( hKeyMon );%@NL@%
  3770.     return 0;%@NL@%
  3771. }%@NL@%
  3772. %@NL@%
  3773. %@AB@%/* Screen capture routine (run as a thread). Does a pop-up to get access%@NL@%
  3774. %@AB@% * to the current screen. Reads characters from the screen into a buffer.%@NL@%
  3775. %@AB@% * Then filters trailing spaces as it writes buffer to a file.%@NL@%
  3776. %@AB@% *%@NL@%
  3777. %@AB@% * Params: plfSnap - pointer to flag indicated snap status%@NL@%
  3778. %@AB@% *%@NL@%
  3779. %@AB@% * Return: none%@NL@%
  3780. %@AB@% *%@NL@%
  3781. %@AB@% * Uses:   pshrSnap - Shared memory structure%@NL@%
  3782. %@AB@% */%@AE@%%@NL@%
  3783. void Snap( ULONG _far *plfSnap )%@NL@%
  3784. {%@NL@%
  3785.     enum { NOTE_B = 494, NOTE_C = 523, NOTE_F = 698 };%@NL@%
  3786.     CHAR   *pchScreen;              // Buffer for captured screen%@NL@%
  3787.     USHORT cbScreen;                // Count of bytes in buffer%@NL@%
  3788.     FILE   *sFile;                  // File stream%@NL@%
  3789.     USHORT usLine, usPos, usWidth;%@NL@%
  3790.     CHAR   ach[5];%@NL@%
  3791.     USHORT fWait = VP_WAIT | VP_TRANSPARENT;%@NL@%
  3792.     VIOMODEINFO vmi = { sizeof( vmi ) };%@NL@%
  3793. %@NL@%
  3794.     if( pshrSnap->fSound )%@NL@%
  3795.         DosBeep( NOTE_F, NOTE_C );%@NL@%
  3796. %@NL@%
  3797.     %@AB@%/* Pop up to current screen and check its size. */%@AE@%%@NL@%
  3798.     VioPopUp( &fWait, CON );%@NL@%
  3799.     VioGetMode( &vmi, CON );%@NL@%
  3800. %@NL@%
  3801.     %@AB@%/* Allocate memory for a full screen plus one byte */%@AE@%%@NL@%
  3802.     cbScreen = vmi.col * vmi.row;%@NL@%
  3803.     pchScreen = malloc( cbScreen + 1 );%@NL@%
  3804. %@NL@%
  3805.     %@AB@%/* Read screen and end popup */%@AE@%%@NL@%
  3806.     VioReadCharStr( pchScreen, &cbScreen, 0, 0, CON );%@NL@%
  3807.     VioEndPopUp( 0 );%@NL@%
  3808. %@NL@%
  3809.     %@AB@%/* Increment screen count (4 digits or less) and convert to string. */%@AE@%%@NL@%
  3810.     pshrSnap->cScreen = (pshrSnap->cScreen + 1) % 9999;%@NL@%
  3811.     itoa( pshrSnap->cScreen, ach, 10 );%@NL@%
  3812. %@NL@%
  3813.     %@AB@%/* Make numbered file name if appropriate. */%@AE@%%@NL@%
  3814.     if( !pshrSnap->fAppend )%@NL@%
  3815.         strcat( strcat( strcpy( pshrSnap->achSnap, "SNAP" ), ach ), ".IMG" );%@NL@%
  3816. %@NL@%
  3817.     %@AB@%/* Open file and write buffer to it a line at a time */%@AE@%%@NL@%
  3818.     if( (sFile = fopen( pshrSnap->achSnap, "at" )) == NULL )%@NL@%
  3819.     {%@NL@%
  3820.         BackError( "Can't open file" );%@NL@%
  3821.         --pshrSnap->cScreen;%@NL@%
  3822.     }%@NL@%
  3823.     else%@NL@%
  3824.     {%@NL@%
  3825.         if( pshrSnap->fAppend )%@NL@%
  3826.         {%@NL@%
  3827.             %@AB@%/* Not using fprintf reduces overhead. */%@AE@%%@NL@%
  3828.             fputs( "**** Screen ", sFile );%@NL@%
  3829.             fputs( ach, sFile );%@NL@%
  3830.             fputs( " ****\n", sFile );%@NL@%
  3831.         }%@NL@%
  3832. %@NL@%
  3833.         for( usLine = 0, usPos = 0; usLine < vmi.row; usLine++ )%@NL@%
  3834.         {%@NL@%
  3835.             %@AB@%/* Throw away trailing spaces */%@AE@%%@NL@%
  3836.             for( usWidth = vmi.col;%@NL@%
  3837.                  (pchScreen[usPos + usWidth - 1] == ' ' ) && usWidth;%@NL@%
  3838.                  usWidth-- )%@NL@%
  3839.                 ;%@NL@%
  3840.             %@AB@%/* Write line and newline */%@AE@%%@NL@%
  3841.             fwrite( pchScreen + usPos, 1, usWidth, sFile );%@NL@%
  3842.             fputc( '\n', sFile );%@NL@%
  3843.             usPos += vmi.col;%@NL@%
  3844.         }%@NL@%
  3845.         fclose( sFile );%@NL@%
  3846.     }%@NL@%
  3847.     if( pshrSnap->fSound )%@NL@%
  3848.         DosBeep( NOTE_C, NOTE_B );%@NL@%
  3849. %@NL@%
  3850.     %@AB@%/* Free memory and let parent know we are done */%@AE@%%@NL@%
  3851.     free( pchScreen );%@NL@%
  3852.     DosSemClear( plfSnap );%@NL@%
  3853. }%@NL@%
  3854. %@NL@%
  3855. %@AB@%/* Displays an error message from within a background process or thread.%@NL@%
  3856. %@AB@% * The monitor is in the background and has no screen group, so it must%@NL@%
  3857. %@AB@% * use VioPopUp to get access to the screen.%@NL@%
  3858. %@AB@% *%@NL@%
  3859. %@AB@% * Params: msgErr - error message string%@NL@%
  3860. %@AB@% *%@NL@%
  3861. %@AB@% * Return: None%@NL@%
  3862. %@AB@% */%@AE@%%@NL@%
  3863. void BackError( char *pchErr )%@NL@%
  3864. {%@NL@%
  3865.     USHORT fWait = VP_WAIT | VP_TRANSPARENT;%@NL@%
  3866. %@NL@%
  3867.     VioPopUp( &fWait, CON );%@NL@%
  3868.     puts( pchErr );%@NL@%
  3869.     puts( "Press any key to continue . . ." );%@NL@%
  3870.     while( !kbhit() )%@NL@%
  3871.         ;%@NL@%
  3872.     VioEndPopUp( CON );%@NL@%
  3873. }%@NL@%
  3874. %@NL@%
  3875. %@AB@%/* Displays syntax.%@NL@%
  3876. %@AB@% *%@NL@%
  3877. %@AB@% * Params: None%@NL@%
  3878. %@AB@% *%@NL@%
  3879. %@AB@% * Return: None%@NL@%
  3880. %@AB@% */%@AE@%%@NL@%
  3881. void Syntax()%@NL@%
  3882. {%@NL@%
  3883.     puts( "\nOptions: " );%@NL@%
  3884.     puts( "\t/H\t  Display help." );%@NL@%
  3885.     puts( "\t/S\t  Turn sound on (default)." );%@NL@%
  3886.     puts( "\t/Q\t  Turn sound off." );%@NL@%
  3887.     puts( "\t/D\t  Deinstall." );%@NL@%
  3888.     puts( "\t/A [path] Append each screen to a file (complete path allowed)." );%@NL@%
  3889.     puts( "\t\t  If no file given, default is SNAP.IMG in current directory" );%@NL@%
  3890.     puts( "\t\t  Resets screen number to 1." );%@NL@%
  3891.     puts( "\t/N [num]  Create numbered file for each screen." );%@NL@%
  3892.     puts( "\t\t  Example: SNAP1.IMG, SNAP2.IMG in current directory." );%@NL@%
  3893.     puts( "\t\t  Resets screen number to 1 or to num if given." );%@NL@%
  3894. }%@NL@%
  3895. %@NL@%
  3896. %@AB@%/* Evaluate command-line options.%@NL@%
  3897. %@AB@% *%@NL@%
  3898. %@AB@% * Params: argc - Number of arguments%@NL@%
  3899. %@AB@% *         argv - Pointer to argument list%@NL@%
  3900. %@AB@% *%@NL@%
  3901. %@AB@% * Return: none%@NL@%
  3902. %@AB@% *%@NL@%
  3903. %@AB@% * Uses: Shared memory structure - pshrSnap%@NL@%
  3904. %@AB@% */%@AE@%%@NL@%
  3905. void EvalOptions( int argc, char **argv )%@NL@%
  3906. {%@NL@%
  3907.     SHORT i;%@NL@%
  3908. %@NL@%
  3909.     %@AB@%/* Look for and handle arguments */%@AE@%%@NL@%
  3910.     for(  i = 1; i < argc; i++ )%@NL@%
  3911.     {%@NL@%
  3912.         if( argv[i][0] == '/' || argv[i][0] == '-' )%@NL@%
  3913.         {%@NL@%
  3914.             switch( argv[i][1] )%@NL@%
  3915.             {%@NL@%
  3916.                 case 'A':%@NL@%
  3917.                 case 'a':%@NL@%
  3918.                     pshrSnap->fAppend = TRUE;%@NL@%
  3919.                     pshrSnap->cScreen = 0;%@NL@%
  3920.                     if( (argv[++i]) &&%@NL@%
  3921.                         (argv[i][0] != '/') &&%@NL@%
  3922.                         (argv[i][0] != '-') )%@NL@%
  3923. %@NL@%
  3924.                     {%@NL@%
  3925.                         strcpy( pshrSnap->achSnap, argv[i] );%@NL@%
  3926.                         puts( "Append mode - name set" );%@NL@%
  3927.                     }%@NL@%
  3928.                     else%@NL@%
  3929.                         puts( "Append mode" );%@NL@%
  3930.                     break;%@NL@%
  3931. %@NL@%
  3932.                 case 'N':%@NL@%
  3933.                 case 'n':%@NL@%
  3934.                     pshrSnap->fAppend = FALSE;%@NL@%
  3935.                     puts( "Numbered file mode" );%@NL@%
  3936.                     if( (argv[++i]) &&%@NL@%
  3937.                         (argv[i][0] != '/') &&%@NL@%
  3938.                         (argv[i][0] != '-') )%@NL@%
  3939.                     {%@NL@%
  3940.                         pshrSnap->cScreen = (atoi( argv[i] ) % 9999) - 1;%@NL@%
  3941.                     }%@NL@%
  3942.                     else%@NL@%
  3943.                         pshrSnap->cScreen = 0;%@NL@%
  3944.                     break;%@NL@%
  3945. %@NL@%
  3946.                 case 'Q':%@NL@%
  3947.                 case 'q':%@NL@%
  3948.                     pshrSnap->fSound = FALSE;%@NL@%
  3949.                     puts( "Sound off" );%@NL@%
  3950.                     break;%@NL@%
  3951. %@NL@%
  3952.                 case 'S'  :%@NL@%
  3953.                 case 's'  :%@NL@%
  3954.                     pshrSnap->fSound = TRUE;%@NL@%
  3955.                     puts( "Sound on" );%@NL@%
  3956.                     break;%@NL@%
  3957. %@NL@%
  3958.                 case 'D':%@NL@%
  3959.                 case 'd':%@NL@%
  3960.                     if( pshrSnap->fInstall )%@NL@%
  3961.                     {%@NL@%
  3962.                         pshrSnap->fInstall = FALSE;%@NL@%
  3963.                         puts( "Deinstalling" );%@NL@%
  3964.                     }%@NL@%
  3965.                     else%@NL@%
  3966.                         exit( 0 );%@NL@%
  3967.                     break;%@NL@%
  3968. %@NL@%
  3969.                 case 'H'  :%@NL@%
  3970.                 case 'h'  :%@NL@%
  3971.                     if( pshrSnap->fInstall )%@NL@%
  3972.                         Syntax();%@NL@%
  3973.                     break;%@NL@%
  3974.             }%@NL@%
  3975.         }%@NL@%
  3976.     }%@NL@%
  3977. }%@NL@%
  3978. %@NL@%
  3979. %@NL@%
  3980. %@2@%%@AH@%SORTDEMO.C%@AE@%%@EH@%%@NL@%
  3981. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\MSC60\SORTDEMO.C%@AE@%%@NL@%
  3982. %@NL@%
  3983. %@AB@%/* SORTDEMO -This program graphically demonstrates six common sorting%@NL@%
  3984. %@AB@% * algorithms. It prints 25 or 43 horizontal bars of different lengths%@NL@%
  3985. %@AB@% * in random order, then sorts the bars from shortest to longest.%@NL@%
  3986. %@AB@% * The program can beep to generate different pitches, depending on the%@NL@%
  3987. %@AB@% * length and position of the bar.%@NL@%
  3988. %@AB@% *%@NL@%
  3989. %@AB@% * The program can be created for DOS or OS/2. To create for OS/2, define%@NL@%
  3990. %@AB@% * the symbol OS2. Command lines for DOS and OS/2 are shown below:%@NL@%
  3991. %@AB@% *%@NL@%
  3992. %@AB@% * DOS:%@NL@%
  3993. %@AB@% *    cl /Lr sortdemo.c graphics.lib%@NL@%
  3994. %@AB@% *%@NL@%
  3995. %@AB@% * OS/2:%@NL@%
  3996. %@AB@% *    cl /Lp /DOS2 sortdemo.c grtextp.lib%@NL@%
  3997. %@AB@% */%@AE@%%@NL@%
  3998. %@NL@%
  3999. %@AI@%#include %@AE@%<graph.h>          // _outtext, _settextcolor, _settextposition %@NL@%
  4000. %@AI@%#include %@AE@%<string.h>         // strlen %@NL@%
  4001. %@AI@%#include %@AE@%<stdio.h>          // sprintf %@NL@%
  4002. %@AI@%#include %@AE@%<conio.h>          // getch %@NL@%
  4003. %@AI@%#include %@AE@%<stdlib.h>         // srand, rand, toupper %@NL@%
  4004. %@AI@%#include %@AE@%<malloc.h>         // malloc, free %@NL@%
  4005. %@AI@%#include %@AE@%<time.h>           // time, clock %@NL@%
  4006. %@NL@%
  4007. enum BOOL { FALSE, TRUE };%@NL@%
  4008. %@NL@%
  4009. %@AB@%/* Structure type for colored bars */%@AE@%%@NL@%
  4010. typedef struct _BAR%@NL@%
  4011. {%@NL@%
  4012.     char len;%@NL@%
  4013.     char clr;%@NL@%
  4014. } BAR;%@NL@%
  4015. %@NL@%
  4016. %@AB@%/* Structure type for screen cells */%@AE@%%@NL@%
  4017. typedef struct _CELL%@NL@%
  4018. {%@NL@%
  4019.     char chChar;%@NL@%
  4020.     char chAttr;%@NL@%
  4021. } CELL;%@NL@%
  4022. %@NL@%
  4023. %@AB@%/* Function prototypes */%@AE@%%@NL@%
  4024. void main( void  );%@NL@%
  4025. void Cls( void  );%@NL@%
  4026. void InitMenu( void  );             // Menu Functions%@NL@%
  4027. void DrawFrame( int iTop, int Left, int Width, int Height );%@NL@%
  4028. void RunMenu( void  );%@NL@%
  4029. void DrawTime( int iCurrentRow );%@NL@%
  4030. void InitBars( void  );             // Bar functions%@NL@%
  4031. void ReInitBars( void  );%@NL@%
  4032. void DrawBar( int iRow );%@NL@%
  4033. void SwapBars( int iRow1, int iRow2 );%@NL@%
  4034. void Swaps( BAR *one, BAR *two );%@NL@%
  4035. void InsertionSort( void  );        // Sort functions%@NL@%
  4036. void BubbleSort( void  );%@NL@%
  4037. void HeapSort( void  );%@NL@%
  4038. void PercolateUp( int iMaxLevel );%@NL@%
  4039. void PercolateDown( int iMaxLevel );%@NL@%
  4040. void ExchangeSort( void  );%@NL@%
  4041. void ShellSort( void  );%@NL@%
  4042. void QuickSort( int iLow, int iHigh );%@NL@%
  4043. void Beep( int frequency, int duration );%@NL@%
  4044. void Sleep( clock_t wait );%@NL@%
  4045. %@NL@%
  4046. %@AB@%/* Macro */%@AE@%%@NL@%
  4047. %@AI@%#define %@AE@%GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min)) %@NL@%
  4048. %@AI@%#define %@AE@%_outtextxy( ach, x, y )   { _settextposition( y, x ); \ %@NL@%
  4049.                                     _outtext( ach ); }%@NL@%
  4050. %@NL@%
  4051. %@AB@%/* Constants */%@AE@%%@NL@%
  4052. %@AI@%#define %@AE@%ESC        27 %@NL@%
  4053. %@AI@%#define %@AE@%BLANK      32 %@NL@%
  4054. %@AI@%#define %@AE@%BLOCK      223 %@NL@%
  4055. %@AI@%#define %@AE@%TOP        1 %@NL@%
  4056. %@AI@%#define %@AE@%FIRSTMENU  (TOP + 6) %@NL@%
  4057. %@AI@%#define %@AE@%LEFTCOLUMN 48 %@NL@%
  4058. %@AI@%#define %@AE@%PROMPTPOS  27 %@NL@%
  4059. %@AI@%#define %@AE@%WIDTH      (80 - LEFTCOLUMN) %@NL@%
  4060. %@AI@%#define %@AE@%HEIGHT     (cszMenu + 2) %@NL@%
  4061. %@AI@%#define %@AE@%MENUCOLOR  15 %@NL@%
  4062. %@AI@%#define %@AE@%BLANKCOLOR 7 %@NL@%
  4063. %@AI@%#define %@AE@%BACKCOLOR  0L %@NL@%
  4064. %@AI@%#define %@AE@%PAUSELIMIT 900 %@NL@%
  4065. %@NL@%
  4066. %@AB@%/* Global variables */%@AE@%%@NL@%
  4067. clock_t clStart, clFinish, clPause = 30L;%@NL@%
  4068. int fSound, iCurChoice, iSwaps, iCompares, cRow;%@NL@%
  4069. %@NL@%
  4070. BAR abarWork[43], abarPerm[43]; // Temporary and permanent sort arrays%@NL@%
  4071. %@NL@%
  4072. char *aszMenu[] =%@NL@%
  4073. {%@NL@%
  4074.     "",%@NL@%
  4075.     "       C Sorting Demo",%@NL@%
  4076.     "",%@NL@%
  4077.     "              Time  Swap  Comp",%@NL@%
  4078.     "",%@NL@%
  4079.     "Insertion",%@NL@%
  4080.     "Bubble",%@NL@%
  4081.     "Heap",%@NL@%
  4082.     "Exchange",%@NL@%
  4083.     "Shell",%@NL@%
  4084.     "Quick",%@NL@%
  4085.     "",%@NL@%
  4086.     "Toggle Sound: ",%@NL@%
  4087.     "",%@NL@%
  4088.     "Pause Factor: ",%@NL@%
  4089.     "<   (Slower)",%@NL@%
  4090.     ">   (Faster)",%@NL@%
  4091.     "",%@NL@%
  4092.     "Type first character of",%@NL@%
  4093.     "choice ( I B H E S Q T < > )",%@NL@%
  4094.     "or ESC key to end program: "%@NL@%
  4095.     "",%@NL@%
  4096. };%@NL@%
  4097. int cszMenu = sizeof( aszMenu ) / sizeof( aszMenu[0] );%@NL@%
  4098. %@NL@%
  4099. void main()%@NL@%
  4100. {%@NL@%
  4101.     cRow = _settextrows( 43 );%@NL@%
  4102.     _clearscreen( _GCLEARSCREEN );%@NL@%
  4103.     _displaycursor( _GCURSOROFF );%@NL@%
  4104.     InitBars();%@NL@%
  4105.     InitMenu();%@NL@%
  4106.     RunMenu();                     // Respond to menu until quit%@NL@%
  4107.     _setvideomode( _DEFAULTMODE );%@NL@%
  4108. }%@NL@%
  4109. %@NL@%
  4110. %@NL@%
  4111. %@AB@%/* InitMenu - Calls the DrawFrame procedure to draw the frame around the%@NL@%
  4112. %@AB@% * sort menu, then prints the different options stored in the menu array.%@NL@%
  4113. %@AB@% */%@AE@%%@NL@%
  4114. void InitMenu()%@NL@%
  4115. {%@NL@%
  4116.     int  i;%@NL@%
  4117.     char ach[15];%@NL@%
  4118. %@NL@%
  4119.     _settextcolor( MENUCOLOR );%@NL@%
  4120.     _setbkcolor( BACKCOLOR );%@NL@%
  4121.     DrawFrame( TOP, LEFTCOLUMN - 3, WIDTH + 3, HEIGHT );%@NL@%
  4122.     for( i = 0; i < cszMenu; i++ )%@NL@%
  4123.         _outtextxy( aszMenu[i], LEFTCOLUMN, TOP + i + 1 );%@NL@%
  4124. %@NL@%
  4125.     %@AB@%/* Print the current value for Sound. */%@AE@%%@NL@%
  4126.     if( fSound )%@NL@%
  4127.         strcpy( ach, "ON " );%@NL@%
  4128.     else%@NL@%
  4129.         strcpy( ach, "OFF" );%@NL@%
  4130. %@NL@%
  4131.     _outtextxy( ach, LEFTCOLUMN + 14, cszMenu - 7 );%@NL@%
  4132.     sprintf( ach, "%3.3u", clPause / 30 );%@NL@%
  4133.     _outtextxy( ach, LEFTCOLUMN + 14, cszMenu - 5 );%@NL@%
  4134. %@NL@%
  4135.     %@AB@%/* Erase the speed option if the length of the pause is at a limit. */%@AE@%%@NL@%
  4136.     strcpy( ach, "            " );%@NL@%
  4137.     if( clPause == PAUSELIMIT )%@NL@%
  4138.         _outtextxy( ach, LEFTCOLUMN, cszMenu - 4 );%@NL@%
  4139.     if( clPause == 0L )%@NL@%
  4140.         _outtextxy( ach, LEFTCOLUMN, cszMenu - 3 );%@NL@%
  4141. }%@NL@%
  4142. %@NL@%
  4143. %@NL@%
  4144. %@AB@%/* DrawFrame - Draws a rectangular frame using the double-line box%@NL@%
  4145. %@AB@% * characters. The parameters iTop, iLeft, iWidth, and iHeight are%@NL@%
  4146. %@AB@% * the row and column arguments for the upper-left and lower-right%@NL@%
  4147. %@AB@% * corners of the frame.%@NL@%
  4148. %@AB@% */%@AE@%%@NL@%
  4149. void DrawFrame( int iTop, int iLeft, int iWidth, int iHeight )%@NL@%
  4150. {%@NL@%
  4151.     enum { ULEFT = 201, URIGHT = 187,%@NL@%
  4152.            LLEFT = 200, LRIGHT = 188, VERTICAL = 186, HORIZONTAL = 205%@NL@%
  4153.          };%@NL@%
  4154.     int iRow;%@NL@%
  4155.     char achTmp[80];%@NL@%
  4156. %@NL@%
  4157.     memset( achTmp, HORIZONTAL, iWidth );%@NL@%
  4158.     achTmp[0] = ULEFT;%@NL@%
  4159.     achTmp[iWidth - 1] = URIGHT;%@NL@%
  4160.     achTmp[iWidth] = '\0';%@NL@%
  4161.     _outtextxy( achTmp, iLeft, iTop );%@NL@%
  4162. %@NL@%
  4163.     memset( achTmp, BLANK, iWidth );%@NL@%
  4164.     achTmp[0] = VERTICAL;%@NL@%
  4165.     achTmp[iWidth - 1] = VERTICAL;%@NL@%
  4166.     for(  iRow = iTop + 1; iRow <= iHeight; iRow++ )%@NL@%
  4167.         _outtextxy( achTmp, iLeft, iRow );%@NL@%
  4168. %@NL@%
  4169.     memset( achTmp, HORIZONTAL, iWidth );%@NL@%
  4170.     achTmp[0] = LLEFT;%@NL@%
  4171.     achTmp[iWidth - 1] = LRIGHT;%@NL@%
  4172.     _outtextxy( achTmp, iLeft, iTop + iHeight );%@NL@%
  4173. }%@NL@%
  4174. %@NL@%
  4175. %@NL@%
  4176. %@AB@%/* RunMenu - The RunMenu procedure first calls the ReInitBars%@NL@%
  4177. %@AB@% * procedure to make sure the abarWork is in its unsorted form, then%@NL@%
  4178. %@AB@% * prompts the user to make one of the following choices:%@NL@%
  4179. %@AB@% *%@NL@%
  4180. %@AB@% *  - Run one of the sorting algorithms%@NL@%
  4181. %@AB@% *  - Toggle sound on or off%@NL@%
  4182. %@AB@% *  - Increase or decrease speed%@NL@%
  4183. %@AB@% *  - End the program%@NL@%
  4184. %@AB@% */%@AE@%%@NL@%
  4185. void RunMenu()%@NL@%
  4186. {%@NL@%
  4187.     char    ch;%@NL@%
  4188. %@NL@%
  4189.     while( TRUE )%@NL@%
  4190.     {%@NL@%
  4191.         iSwaps = iCompares = 0;%@NL@%
  4192.         _settextposition( TOP + cszMenu, LEFTCOLUMN + PROMPTPOS );%@NL@%
  4193.         _displaycursor( _GCURSORON );%@NL@%
  4194.         ch = getch();%@NL@%
  4195.         _displaycursor( _GCURSOROFF );%@NL@%
  4196. %@NL@%
  4197.         %@AB@%/* Branch to the appropriate procedure depending on the key. */%@AE@%%@NL@%
  4198.         switch( toupper( ch ) )%@NL@%
  4199.         {%@NL@%
  4200.             case 'I':%@NL@%
  4201.                 iCurChoice = 0;%@NL@%
  4202.                 ReInitBars();%@NL@%
  4203.                 InsertionSort();%@NL@%
  4204.                 DrawTime( 0 );   // Print final time%@NL@%
  4205.                 break;%@NL@%
  4206.             case 'B':%@NL@%
  4207.                 iCurChoice = 1;%@NL@%
  4208.                 ReInitBars();%@NL@%
  4209.                 BubbleSort();%@NL@%
  4210.                 DrawTime( 0 );%@NL@%
  4211.                 break;%@NL@%
  4212. %@NL@%
  4213.             case 'H':%@NL@%
  4214.                 iCurChoice = 2;%@NL@%
  4215.                 ReInitBars();%@NL@%
  4216.                 HeapSort();%@NL@%
  4217.                 DrawTime( 0 );%@NL@%
  4218.                 break;%@NL@%
  4219. %@NL@%
  4220.             case 'E':%@NL@%
  4221.                 iCurChoice = 3;%@NL@%
  4222.                 ReInitBars();%@NL@%
  4223.                 ExchangeSort();%@NL@%
  4224.                 DrawTime( 0 );%@NL@%
  4225.                 break;%@NL@%
  4226. %@NL@%
  4227.             case 'S':%@NL@%
  4228.                 iCurChoice = 4;%@NL@%
  4229.                 ReInitBars();%@NL@%
  4230.                 ShellSort();%@NL@%
  4231.                 DrawTime( 0 );%@NL@%
  4232.                 break;%@NL@%
  4233. %@NL@%
  4234.             case 'Q':%@NL@%
  4235.                 iCurChoice = 5;%@NL@%
  4236.                 ReInitBars();%@NL@%
  4237.                 QuickSort( 0, cRow );%@NL@%
  4238.                 DrawTime( 0 );%@NL@%
  4239.                 break;%@NL@%
  4240. %@NL@%
  4241.             case '>':%@NL@%
  4242.                 %@AB@%/* Decrease pause length to speed up sorting time, then%@NL@%
  4243. %@AB@%                 * redraw the menu to clear any timing results (since%@NL@%
  4244. %@AB@%                 * they won't compare with future results).%@NL@%
  4245. %@AB@%                 */%@AE@%%@NL@%
  4246.                 if( clPause )%@NL@%
  4247.                     clPause -= 30L;%@NL@%
  4248.                 InitMenu();%@NL@%
  4249.                 break;%@NL@%
  4250. %@NL@%
  4251.             case '<':%@NL@%
  4252.                 %@AB@%/* Increase pause length to slow down sorting time. */%@AE@%%@NL@%
  4253.                 if( clPause <= 900L )%@NL@%
  4254.                     clPause += 30L;%@NL@%
  4255.                 InitMenu();%@NL@%
  4256.                 break;%@NL@%
  4257. %@NL@%
  4258.             case 'T':%@NL@%
  4259.                 fSound = !fSound;%@NL@%
  4260.                 InitMenu();%@NL@%
  4261.                 break;%@NL@%
  4262. %@NL@%
  4263.             case ESC:%@NL@%
  4264.                 return;%@NL@%
  4265. %@NL@%
  4266.             default:        // Unknown key ignored%@NL@%
  4267.                 break;%@NL@%
  4268.         }%@NL@%
  4269.     }%@NL@%
  4270. }%@NL@%
  4271. %@NL@%
  4272. %@NL@%
  4273. %@AB@%/* DrawTime - Prints seconds elapsed since the given sorting routine%@NL@%
  4274. %@AB@% * started. Note that this time includes both the time it takes to redraw%@NL@%
  4275. %@AB@% * the bars plus the pause while Beep plays a note, and thus is not an%@NL@%
  4276. %@AB@% * accurate indication of sorting speed.%@NL@%
  4277. %@AB@% */%@AE@%%@NL@%
  4278. void DrawTime( int iCurrentRow )%@NL@%
  4279. {%@NL@%
  4280.     char achTiming[80];%@NL@%
  4281. %@NL@%
  4282.     _settextcolor( MENUCOLOR );%@NL@%
  4283.     clFinish = clock();%@NL@%
  4284. %@NL@%
  4285.     sprintf( achTiming, "%7.2f  %4.i  %4.i",%@NL@%
  4286.              (float)(clFinish - clStart) / CLOCKS_PER_SEC,%@NL@%
  4287.              iSwaps, iCompares );%@NL@%
  4288. %@NL@%
  4289.     %@AB@%/* Print the number of seconds elapsed */%@AE@%%@NL@%
  4290.     _outtextxy( achTiming, LEFTCOLUMN + 11, FIRSTMENU + iCurChoice );%@NL@%
  4291.     if( fSound )%@NL@%
  4292.     {%@NL@%
  4293.         Beep( 60 * iCurrentRow, 75 );   // Play note%@NL@%
  4294.         Sleep( clPause - 75L );         // Pause adjusted for note duration%@NL@%
  4295.     }%@NL@%
  4296.     else%@NL@%
  4297.         Sleep( clPause );               // Pause%@NL@%
  4298. }%@NL@%
  4299. %@NL@%
  4300. %@NL@%
  4301. %@AB@%/* InitBars - Initializes the bar arrays and the menu box.%@NL@%
  4302. %@AB@% */%@AE@%%@NL@%
  4303. void InitBars()%@NL@%
  4304. {%@NL@%
  4305.     struct videoconfig vc;%@NL@%
  4306.     int aTemp[43], iRow, iRowMax, iRand, iColorMax, iLength;%@NL@%
  4307. %@NL@%
  4308.     %@AB@%/* Seed the random-number generator. */%@AE@%%@NL@%
  4309.     srand( (unsigned)time( NULL ) );%@NL@%
  4310.     fSound = TRUE;%@NL@%
  4311.     clPause = 30L;%@NL@%
  4312. %@NL@%
  4313.     %@AB@%/* If monochrome or color burst disabled, use one color */%@AE@%%@NL@%
  4314.     _getvideoconfig( &vc );%@NL@%
  4315.     if( (vc.monitor == _MONO) || (vc.mode == _TEXTBW80) ||%@NL@%
  4316.                                  (vc.mode == _TEXTBW40) )%@NL@%
  4317.         iColorMax = 1;%@NL@%
  4318.     else%@NL@%
  4319.         iColorMax = 15;%@NL@%
  4320. %@NL@%
  4321.     %@AB@%/* Randomize an array of rows. */%@AE@%%@NL@%
  4322.     for( iRow = 0; iRow < cRow; iRow++ )%@NL@%
  4323.         aTemp[iRow] = iRow + 1;%@NL@%
  4324.     iRowMax = cRow - 1;%@NL@%
  4325.     for( iRow = 0; iRow < cRow; iRow++ )%@NL@%
  4326.     {%@NL@%
  4327.         %@AB@%/* Find a random element in aTemp between 0 and iRowMax,%@NL@%
  4328. %@AB@%         * then assign the value in that element to iLength.%@NL@%
  4329. %@AB@%         */%@AE@%%@NL@%
  4330.         iRand = GetRandom( 0, iRowMax );%@NL@%
  4331.         iLength = aTemp[iRand];%@NL@%
  4332. %@NL@%
  4333.         %@AB@%/* Overwrite the value in aTemp[iRand] with the value in%@NL@%
  4334. %@AB@%         * aTemp[iRowMax] so the value in aTemp[iRand] is%@NL@%
  4335. %@AB@%         * chosen only once.%@NL@%
  4336. %@AB@%         */%@AE@%%@NL@%
  4337.         aTemp[iRand] = aTemp[iRowMax];%@NL@%
  4338. %@NL@%
  4339.         %@AB@%/* Decrease the value of iRowMax so that aTemp[iRowMax] can't%@NL@%
  4340. %@AB@%         * be chosen on the next pass through the loop.%@NL@%
  4341. %@AB@%         */%@AE@%%@NL@%
  4342.         --iRowMax;%@NL@%
  4343.         abarPerm[iRow].len = iLength;%@NL@%
  4344.         if( iColorMax == 1 )%@NL@%
  4345.             abarPerm[iRow].clr = BLANKCOLOR;%@NL@%
  4346.         else%@NL@%
  4347.             abarPerm[iRow].clr = iLength % iColorMax + 1;%@NL@%
  4348.     }%@NL@%
  4349. %@NL@%
  4350.     %@AB@%/* Assign permanent sort values to temporary and draw unsorted bars. */%@AE@%%@NL@%
  4351.     ReInitBars();%@NL@%
  4352. }%@NL@%
  4353. %@NL@%
  4354. %@NL@%
  4355. %@AB@%/* ReInitBars - Restores the array abarWork to its original unsorted%@NL@%
  4356. %@AB@% * state and draws the unsorted bars.%@NL@%
  4357. %@AB@% */%@AE@%%@NL@%
  4358. void ReInitBars()%@NL@%
  4359. {%@NL@%
  4360.     int iRow;%@NL@%
  4361. %@NL@%
  4362.     clStart = clock();%@NL@%
  4363.     for( iRow = 0; iRow < cRow; iRow++ )%@NL@%
  4364.     {%@NL@%
  4365.         abarWork[iRow] = abarPerm[iRow];%@NL@%
  4366.         DrawBar( iRow );%@NL@%
  4367.     }%@NL@%
  4368. }%@NL@%
  4369. %@NL@%
  4370. %@NL@%
  4371. %@AB@%/* DrawBar - Prints a bar at a specified row by first blanking the%@NL@%
  4372. %@AB@% * old bar, then drawing a new bar having the length and color given in%@NL@%
  4373. %@AB@% * the work array.%@NL@%
  4374. %@AB@% */%@AE@%%@NL@%
  4375. void DrawBar( int iRow )%@NL@%
  4376. {%@NL@%
  4377.     int  cSpace;%@NL@%
  4378.     char achT[43];%@NL@%
  4379. %@NL@%
  4380.     memset( achT, BLOCK, abarWork[iRow].len );%@NL@%
  4381.     cSpace = cRow - abarWork[iRow].len;%@NL@%
  4382.     memset( achT + abarWork[iRow].len, ' ', cSpace );%@NL@%
  4383.     achT[cRow] = '\0';%@NL@%
  4384.     _settextcolor( abarWork[iRow].clr );%@NL@%
  4385.     _outtextxy( achT, 0, iRow + 1 );%@NL@%
  4386. }%@NL@%
  4387. %@NL@%
  4388. %@NL@%
  4389. %@AB@%/* SwapBars - Calls DrawBar twice to switch the two bars in iRow1 and%@NL@%
  4390. %@AB@% * iRow2, then calls DrawTime to update the time.%@NL@%
  4391. %@AB@% */%@AE@%%@NL@%
  4392. void SwapBars( int iRow1, int iRow2 )%@NL@%
  4393. {%@NL@%
  4394.     DrawBar( iRow1 );%@NL@%
  4395.     DrawBar( iRow2 );%@NL@%
  4396.     DrawTime( iRow1 );%@NL@%
  4397. }%@NL@%
  4398. %@NL@%
  4399. %@NL@%
  4400. %@AB@%/* Swaps - Exchanges two bar structures.%@NL@%
  4401. %@AB@% */%@AE@%%@NL@%
  4402. void Swaps( BAR *bar1, BAR *bar2 )%@NL@%
  4403. {%@NL@%
  4404.     BAR barTmp;%@NL@%
  4405. %@NL@%
  4406.     ++iSwaps;%@NL@%
  4407.     barTmp = *bar1;%@NL@%
  4408.     *bar1  = *bar2;%@NL@%
  4409.     *bar2  = barTmp;%@NL@%
  4410. }%@NL@%
  4411. %@NL@%
  4412. %@NL@%
  4413. %@AB@%/* InsertionSort - InsertionSort compares the length of each element%@NL@%
  4414. %@AB@% * with the lengths of all the preceding elements. When the appropriate%@NL@%
  4415. %@AB@% * place for the new element is found, the element is inserted and%@NL@%
  4416. %@AB@% * all the other elements are moved down one place.%@NL@%
  4417. %@AB@% */%@AE@%%@NL@%
  4418. void InsertionSort()%@NL@%
  4419. {%@NL@%
  4420.     BAR barTemp;%@NL@%
  4421.     int iRow, iRowTmp, iLength;%@NL@%
  4422. %@NL@%
  4423.     %@AB@%/* Start at the top. */%@AE@%%@NL@%
  4424.     for( iRow = 0; iRow < cRow; iRow++ )%@NL@%
  4425.     {%@NL@%
  4426.         barTemp = abarWork[iRow];%@NL@%
  4427.         iLength = barTemp.len;%@NL@%
  4428. %@NL@%
  4429.         %@AB@%/* As long as the length of the temporary element is greater than%@NL@%
  4430. %@AB@%         * the length of the original, keep shifting the elements down.%@NL@%
  4431. %@AB@%         */%@AE@%%@NL@%
  4432.         for( iRowTmp = iRow; iRowTmp; iRowTmp-- )%@NL@%
  4433.         {%@NL@%
  4434.             iCompares++;%@NL@%
  4435.             if( abarWork[iRowTmp - 1].len > iLength )%@NL@%
  4436.             {%@NL@%
  4437.                 ++iSwaps;%@NL@%
  4438.                 abarWork[iRowTmp] = abarWork[iRowTmp - 1];%@NL@%
  4439.                 DrawBar( iRowTmp );         // Print the new bar%@NL@%
  4440.                 DrawTime( iRowTmp );        // Print the elapsed time%@NL@%
  4441. %@NL@%
  4442.             }%@NL@%
  4443.             else                            // Otherwise, exit%@NL@%
  4444.                 break;%@NL@%
  4445.         }%@NL@%
  4446. %@NL@%
  4447.         %@AB@%/* Insert the original bar in the temporary position. */%@AE@%%@NL@%
  4448.         abarWork[iRowTmp] = barTemp;%@NL@%
  4449.         DrawBar( iRowTmp );%@NL@%
  4450.         DrawTime( iRowTmp );%@NL@%
  4451.     }%@NL@%
  4452. }%@NL@%
  4453. %@NL@%
  4454. %@NL@%
  4455. %@AB@%/* BubbleSort - BubbleSort cycles through the elements, comparing%@NL@%
  4456. %@AB@% * adjacent elements and swapping pairs that are out of order. It%@NL@%
  4457. %@AB@% * continues to do this until no out-of-order pairs are found.%@NL@%
  4458. %@AB@% */%@AE@%%@NL@%
  4459. void BubbleSort()%@NL@%
  4460. {%@NL@%
  4461.     int iRow, iSwitch, iLimit = cRow;%@NL@%
  4462. %@NL@%
  4463.     %@AB@%/* Move the longest bar down to the bottom until all are in order. */%@AE@%%@NL@%
  4464.     do%@NL@%
  4465.     {%@NL@%
  4466.         iSwitch = 0;%@NL@%
  4467.         for( iRow = 0; iRow < iLimit; iRow++ )%@NL@%
  4468.         {%@NL@%
  4469.             %@AB@%/* If two adjacent elements are out of order, swap their values%@NL@%
  4470. %@AB@%             * and redraw those two bars.%@NL@%
  4471. %@AB@%             */%@AE@%%@NL@%
  4472.             iCompares++;%@NL@%
  4473.             if( abarWork[iRow].len > abarWork[iRow + 1].len )%@NL@%
  4474.             {%@NL@%
  4475.                 Swaps( &abarWork[iRow], &abarWork[iRow + 1] );%@NL@%
  4476.                 SwapBars( iRow, iRow + 1 );%@NL@%
  4477.                 iSwitch = iRow;%@NL@%
  4478.             }%@NL@%
  4479.         }%@NL@%
  4480. %@NL@%
  4481.         %@AB@%/* Sort on next pass only to where the last switch was made. */%@AE@%%@NL@%
  4482.         iLimit = iSwitch;%@NL@%
  4483.     } while( iSwitch );%@NL@%
  4484. }%@NL@%
  4485. %@NL@%
  4486. %@NL@%
  4487. %@AB@%/* HeapSort - HeapSort (also called TreeSort) works by calling%@NL@%
  4488. %@AB@% * PercolateUp and PercolateDown. PercolateUp organizes the elements%@NL@%
  4489. %@AB@% * into a "heap" or "tree," which has the properties shown below:%@NL@%
  4490. %@AB@% *%@NL@%
  4491. %@AB@% *                             element[1]%@NL@%
  4492. %@AB@% *                           /            \%@NL@%
  4493. %@AB@% *                element[2]                element[3]%@NL@%
  4494. %@AB@% *               /          \              /          \%@NL@%
  4495. %@AB@% *         element[4]     element[5]   element[6]    element[7]%@NL@%
  4496. %@AB@% *         /        \     /        \   /        \    /        \%@NL@%
  4497. %@AB@% *        ...      ...   ...      ... ...      ...  ...      ...%@NL@%
  4498. %@AB@% *%@NL@%
  4499. %@AB@% *%@NL@%
  4500. %@AB@% * Each "parent node" is greater than each of its "child nodes"; for%@NL@%
  4501. %@AB@% * example, element[1] is greater than element[2] or element[3];%@NL@%
  4502. %@AB@% * element[4] is greater than element[5] or element[6], and so forth.%@NL@%
  4503. %@AB@% * Therefore, once the first loop in HeapSort is finished, the%@NL@%
  4504. %@AB@% * largest element is in element[1].%@NL@%
  4505. %@AB@% *%@NL@%
  4506. %@AB@% * The second loop rebuilds the heap (with PercolateDown), but starts%@NL@%
  4507. %@AB@% * at the top and works down, moving the largest elements to the bottom.%@NL@%
  4508. %@AB@% * This has the effect of moving the smallest elements to the top and%@NL@%
  4509. %@AB@% * sorting the heap.%@NL@%
  4510. %@AB@% */%@AE@%%@NL@%
  4511. void HeapSort()%@NL@%
  4512. {%@NL@%
  4513.     int i;%@NL@%
  4514. %@NL@%
  4515.     for( i = 1; i < cRow; i++ )%@NL@%
  4516.         PercolateUp( i );%@NL@%
  4517. %@NL@%
  4518.     for( i = cRow - 1; i > 0; i-- )%@NL@%
  4519.     {%@NL@%
  4520.         Swaps( &abarWork[0], &abarWork[i] );%@NL@%
  4521.         SwapBars( 0, i );%@NL@%
  4522.         PercolateDown( i - 1 );%@NL@%
  4523.     }%@NL@%
  4524. }%@NL@%
  4525. %@NL@%
  4526. %@NL@%
  4527. %@AB@%/* PercolateUp - Converts elements into a "heap" with the largest%@NL@%
  4528. %@AB@% * element at the top (see the diagram above).%@NL@%
  4529. %@AB@% */%@AE@%%@NL@%
  4530. void PercolateUp( int iMaxLevel )%@NL@%
  4531. {%@NL@%
  4532.     int i = iMaxLevel, iParent;%@NL@%
  4533. %@NL@%
  4534.     %@AB@%/* Move the value in abarWork[iMaxLevel] up the heap until it has%@NL@%
  4535. %@AB@%     * reached its proper node (that is, until it is greater than either%@NL@%
  4536. %@AB@%     * of its child nodes, or until it has reached 1, the top of the heap).%@NL@%
  4537. %@AB@%     */%@AE@%%@NL@%
  4538.     while( i )%@NL@%
  4539.     {%@NL@%
  4540.         iParent = i / 2;    // Get the subscript for the parent node%@NL@%
  4541. %@NL@%
  4542.         iCompares++;%@NL@%
  4543.         if( abarWork[i].len > abarWork[iParent].len )%@NL@%
  4544.         {%@NL@%
  4545.             %@AB@%/* The value at the current node is bigger than the value at%@NL@%
  4546. %@AB@%             * its parent node, so swap these two array elements.%@NL@%
  4547. %@AB@%             */%@AE@%%@NL@%
  4548.             Swaps( &abarWork[iParent], &abarWork[i] );%@NL@%
  4549.             SwapBars( iParent, i );%@NL@%
  4550.             i = iParent;%@NL@%
  4551.         }%@NL@%
  4552.         else%@NL@%
  4553.             %@AB@%/* Otherwise, the element has reached its proper place in the%@NL@%
  4554. %@AB@%             * heap, so exit this procedure.%@NL@%
  4555. %@AB@%             */%@AE@%%@NL@%
  4556.             break;%@NL@%
  4557.     }%@NL@%
  4558. }%@NL@%
  4559. %@NL@%
  4560. %@NL@%
  4561. %@AB@%/* PercolateDown - Converts elements to a "heap" with the largest elements%@NL@%
  4562. %@AB@% * at the bottom. When this is done to a reversed heap (largest elements%@NL@%
  4563. %@AB@% * at top), it has the effect of sorting the elements.%@NL@%
  4564. %@AB@% */%@AE@%%@NL@%
  4565. void PercolateDown( int iMaxLevel )%@NL@%
  4566. {%@NL@%
  4567.     int iChild, i = 0;%@NL@%
  4568. %@NL@%
  4569.     %@AB@%/* Move the value in abarWork[0] down the heap until it has reached%@NL@%
  4570. %@AB@%     * its proper node (that is, until it is less than its parent node%@NL@%
  4571. %@AB@%     * or until it has reached iMaxLevel, the bottom of the current heap).%@NL@%
  4572. %@AB@%     */%@AE@%%@NL@%
  4573.     while( TRUE )%@NL@%
  4574.     {%@NL@%
  4575.         %@AB@%/* Get the subscript for the child node. */%@AE@%%@NL@%
  4576.         iChild = 2 * i;%@NL@%
  4577. %@NL@%
  4578.         %@AB@%/* Reached the bottom of the heap, so exit this procedure. */%@AE@%%@NL@%
  4579.         if( iChild > iMaxLevel )%@NL@%
  4580.             break;%@NL@%
  4581. %@NL@%
  4582.         %@AB@%/* If there are two child nodes, find out which one is bigger. */%@AE@%%@NL@%
  4583.         if( iChild + 1 <= iMaxLevel )%@NL@%
  4584.         {%@NL@%
  4585.             iCompares++;%@NL@%
  4586.             if( abarWork[iChild + 1].len > abarWork[iChild].len )%@NL@%
  4587.                 iChild++;%@NL@%
  4588.         }%@NL@%
  4589. %@NL@%
  4590.         iCompares++;%@NL@%
  4591.         if( abarWork[i].len < abarWork[iChild].len )%@NL@%
  4592.         {%@NL@%
  4593.             %@AB@%/* Move the value down since it is still not bigger than%@NL@%
  4594. %@AB@%             * either one of its children.%@NL@%
  4595. %@AB@%             */%@AE@%%@NL@%
  4596.             Swaps( &abarWork[i], &abarWork[iChild] );%@NL@%
  4597.             SwapBars( i, iChild );%@NL@%
  4598.             i = iChild;%@NL@%
  4599.         }%@NL@%
  4600.         else%@NL@%
  4601.             %@AB@%/* Otherwise, abarWork has been restored to a heap from 1 to%@NL@%
  4602. %@AB@%             * iMaxLevel, so exit.%@NL@%
  4603. %@AB@%             */%@AE@%%@NL@%
  4604.             break;%@NL@%
  4605.     }%@NL@%
  4606. }%@NL@%
  4607. %@NL@%
  4608. %@NL@%
  4609. %@AB@%/* ExchangeSort - The ExchangeSort compares each element--starting with%@NL@%
  4610. %@AB@% * the first--with every following element. If any of the following%@NL@%
  4611. %@AB@% * elements is smaller than the current element, it is exchanged with%@NL@%
  4612. %@AB@% * the current element and the process is repeated for the next element.%@NL@%
  4613. %@AB@% */%@AE@%%@NL@%
  4614. void ExchangeSort()%@NL@%
  4615. {%@NL@%
  4616.     int iRowCur, iRowMin, iRowNext;%@NL@%
  4617. %@NL@%
  4618.     for( iRowCur = 0; iRowCur < cRow; iRowCur++ )%@NL@%
  4619.     {%@NL@%
  4620.         iRowMin = iRowCur;%@NL@%
  4621.         for( iRowNext = iRowCur; iRowNext < cRow; iRowNext++ )%@NL@%
  4622.         {%@NL@%
  4623.             iCompares++;%@NL@%
  4624.             if( abarWork[iRowNext].len < abarWork[iRowMin].len )%@NL@%
  4625.             {%@NL@%
  4626.                 iRowMin = iRowNext;%@NL@%
  4627.                 DrawTime( iRowNext );%@NL@%
  4628.             }%@NL@%
  4629.         }%@NL@%
  4630. %@NL@%
  4631.         %@AB@%/* If a row is shorter than the current row, swap those two%@NL@%
  4632. %@AB@%         * array elements.%@NL@%
  4633. %@AB@%         */%@AE@%%@NL@%
  4634.         if( iRowMin > iRowCur )%@NL@%
  4635.         {%@NL@%
  4636.             Swaps( &abarWork[iRowCur], &abarWork[iRowMin] );%@NL@%
  4637.             SwapBars( iRowCur, iRowMin );%@NL@%
  4638.         }%@NL@%
  4639.     }%@NL@%
  4640. }%@NL@%
  4641. %@NL@%
  4642. %@NL@%
  4643. %@AB@%/* ShellSort - ShellSort is similar to the BubbleSort. However, it%@NL@%
  4644. %@AB@% * begins by comparing elements that are far apart (separated by the%@NL@%
  4645. %@AB@% * value of the iOffset variable, which is initially half the distance%@NL@%
  4646. %@AB@% * between the first and last element), then comparing elements that%@NL@%
  4647. %@AB@% * are closer together. When iOffset is one, the last iteration of%@NL@%
  4648. %@AB@% * is merely a bubble sort.%@NL@%
  4649. %@AB@% */%@AE@%%@NL@%
  4650. void ShellSort()%@NL@%
  4651. {%@NL@%
  4652.     int iOffset, iSwitch, iLimit, iRow;%@NL@%
  4653. %@NL@%
  4654.     %@AB@%/* Set comparison offset to half the number of bars. */%@AE@%%@NL@%
  4655.     iOffset = cRow / 2;%@NL@%
  4656. %@NL@%
  4657.     while( iOffset )%@NL@%
  4658.     {%@NL@%
  4659.         %@AB@%/* Loop until offset gets to zero. */%@AE@%%@NL@%
  4660.         iLimit = cRow - iOffset;%@NL@%
  4661.         do%@NL@%
  4662.         {%@NL@%
  4663.             iSwitch = FALSE;     // Assume no switches at this offset.%@NL@%
  4664. %@NL@%
  4665.             %@AB@%/* Compare elements and switch ones out of order. */%@AE@%%@NL@%
  4666.             for( iRow = 0; iRow <= iLimit; iRow++ )%@NL@%
  4667.             {%@NL@%
  4668.                 iCompares++;%@NL@%
  4669.                 if( abarWork[iRow].len > abarWork[iRow + iOffset].len )%@NL@%
  4670.                 {%@NL@%
  4671.                     Swaps( &abarWork[iRow], &abarWork[iRow + iOffset] );%@NL@%
  4672.                     SwapBars( iRow, iRow + iOffset );%@NL@%
  4673.                     iSwitch = iRow;%@NL@%
  4674.                 }%@NL@%
  4675.             }%@NL@%
  4676. %@NL@%
  4677.             %@AB@%/* Sort on next pass only to where last switch was made. */%@AE@%%@NL@%
  4678.             iLimit = iSwitch - iOffset;%@NL@%
  4679.         } while( iSwitch );%@NL@%
  4680. %@NL@%
  4681.         %@AB@%/* No switches at last offset, try one half as big. */%@AE@%%@NL@%
  4682.         iOffset = iOffset / 2;%@NL@%
  4683.     }%@NL@%
  4684. }%@NL@%
  4685. %@NL@%
  4686. %@NL@%
  4687. %@AB@%/* QuickSort - QuickSort works by picking a random "pivot" element,%@NL@%
  4688. %@AB@% * then moving every element that is bigger to one side of the pivot,%@NL@%
  4689. %@AB@% * and every element that is smaller to the other side. QuickSort is%@NL@%
  4690. %@AB@% * then called recursively with the two subdivisions created by the pivot.%@NL@%
  4691. %@AB@% * Once the number of elements in a subdivision reaches two, the recursive%@NL@%
  4692. %@AB@% * calls end and the array is sorted.%@NL@%
  4693. %@AB@% */%@AE@%%@NL@%
  4694. void QuickSort( int iLow, int iHigh )%@NL@%
  4695. {%@NL@%
  4696.     int iUp, iDown, iBreak;%@NL@%
  4697. %@NL@%
  4698.     if( iLow < iHigh )%@NL@%
  4699.     {%@NL@%
  4700.         %@AB@%/* Only two elements in this subdivision; swap them if they are%@NL@%
  4701. %@AB@%         * out of order, then end recursive calls.%@NL@%
  4702. %@AB@%         */%@AE@%%@NL@%
  4703.         if( (iHigh - iLow) == 1 )%@NL@%
  4704.         {%@NL@%
  4705.             iCompares++;%@NL@%
  4706.             if( abarWork[iLow].len > abarWork[iHigh].len )%@NL@%
  4707.             {%@NL@%
  4708.                 Swaps( &abarWork[iLow], &abarWork[iHigh] );%@NL@%
  4709.                 SwapBars( iLow, iHigh );%@NL@%
  4710.             }%@NL@%
  4711.         }%@NL@%
  4712.         else%@NL@%
  4713.         {%@NL@%
  4714.             iBreak = abarWork[iHigh].len;%@NL@%
  4715.             do%@NL@%
  4716.             {%@NL@%
  4717.                 %@AB@%/* Move in from both sides towards the pivot element. */%@AE@%%@NL@%
  4718.                 iUp = iLow;%@NL@%
  4719.                 iDown = iHigh;%@NL@%
  4720.                 iCompares++;%@NL@%
  4721.                 while( (iUp < iDown) && (abarWork[iUp].len <= iBreak) )%@NL@%
  4722.                     iUp++;%@NL@%
  4723.                 iCompares++;%@NL@%
  4724.                 while( (iDown > iUp) && (abarWork[iDown].len >= iBreak) )%@NL@%
  4725.                     iDown--;%@NL@%
  4726.                 %@AB@%/* If we haven't reached the pivot, it means that two%@NL@%
  4727. %@AB@%                 * elements on either side are out of order, so swap them.%@NL@%
  4728. %@AB@%                 */%@AE@%%@NL@%
  4729.                 if( iUp < iDown )%@NL@%
  4730.                 {%@NL@%
  4731.                     Swaps( &abarWork[iUp], &abarWork[iDown] );%@NL@%
  4732.                     SwapBars( iUp, iDown );%@NL@%
  4733.                 }%@NL@%
  4734.             } while ( iUp < iDown );%@NL@%
  4735. %@NL@%
  4736.             %@AB@%/* Move pivot element back to its proper place in the array. */%@AE@%%@NL@%
  4737.             Swaps( &abarWork[iUp], &abarWork[iHigh] );%@NL@%
  4738.             SwapBars( iUp, iHigh );%@NL@%
  4739. %@NL@%
  4740.             %@AB@%/* Recursively call the QuickSort procedure (pass the smaller%@NL@%
  4741. %@AB@%             * subdivision first to use less stack space).%@NL@%
  4742. %@AB@%             */%@AE@%%@NL@%
  4743.             if( (iUp - iLow) < (iHigh - iUp) )%@NL@%
  4744.             {%@NL@%
  4745.                 QuickSort( iLow, iUp - 1 );%@NL@%
  4746.                 QuickSort( iUp + 1, iHigh );%@NL@%
  4747.             }%@NL@%
  4748.             else%@NL@%
  4749.             {%@NL@%
  4750.                 QuickSort( iUp + 1, iHigh );%@NL@%
  4751.                 QuickSort( iLow, iUp - 1 );%@NL@%
  4752.             }%@NL@%
  4753.         }%@NL@%
  4754.     }%@NL@%
  4755. }%@NL@%
  4756. %@NL@%
  4757. %@AB@%/* Beep - Sounds the speaker for a time specified in microseconds by%@NL@%
  4758. %@AB@% * duration at a pitch specified in hertz by frequency.%@NL@%
  4759. %@AB@% */%@AE@%%@NL@%
  4760. void Beep( int frequency, int duration )%@NL@%
  4761. {%@NL@%
  4762. %@AB@%/* Use system call for OS/2 */%@AE@%%@NL@%
  4763. %@AI@%#if %@AE@%defined( OS2 ) %@NL@%
  4764. %@AI@%#define %@AE@%INCL_NOCOMMON %@NL@%
  4765. %@AI@%#define %@AE@%INCL_NOPM %@NL@%
  4766. %@AI@%#define %@AE@%INCL_DOSPROCESS %@NL@%
  4767. %@AI@%#include %@AE@%<os2.h> %@NL@%
  4768.     DosBeep( frequency, duration );%@NL@%
  4769. %@AI@%#else %@AE@%%@NL@%
  4770. %@AB@%/* Define procedure for DOS */%@AE@%%@NL@%
  4771.     int control;%@NL@%
  4772. %@NL@%
  4773.     %@AB@%/* If frequency is 0, Beep doesn't try to make a sound. It%@NL@%
  4774. %@AB@%     * just sleeps for the duration.%@NL@%
  4775. %@AB@%     */%@AE@%%@NL@%
  4776.     if( frequency )%@NL@%
  4777.     {%@NL@%
  4778.         %@AB@%/* 75 is about the shortest reliable duration of a sound. */%@AE@%%@NL@%
  4779.         if( duration < 75 )%@NL@%
  4780.             duration = 75;%@NL@%
  4781. %@NL@%
  4782.         %@AB@%/* Prepare timer by sending 10111100 to port 43. */%@AE@%%@NL@%
  4783.         outp( 0x43, 0xb6 );%@NL@%
  4784. %@NL@%
  4785.         %@AB@%/* Divide input frequency by timer ticks per second and%@NL@%
  4786. %@AB@%         * write (byte by byte) to timer.%@NL@%
  4787. %@AB@%         */%@AE@%%@NL@%
  4788.         frequency = (unsigned)(1193180L / frequency);%@NL@%
  4789.         outp( 0x42, (char)frequency );%@NL@%
  4790.         outp( 0x42, (char)(frequency >> 8) );%@NL@%
  4791. %@NL@%
  4792.         %@AB@%/* Save speaker control byte. */%@AE@%%@NL@%
  4793.         control = inp( 0x61 );%@NL@%
  4794. %@NL@%
  4795.         %@AB@%/* Turn on the speaker (with bits 0 and 1). */%@AE@%%@NL@%
  4796.         outp( 0x61, control | 0x3 );%@NL@%
  4797.     }%@NL@%
  4798. %@NL@%
  4799.     Sleep( (clock_t)duration );%@NL@%
  4800. %@NL@%
  4801.     %@AB@%/* Turn speaker back on if necessary. */%@AE@%%@NL@%
  4802.     if( frequency )%@NL@%
  4803.         outp( 0x61, control );%@NL@%
  4804. %@NL@%
  4805.              %@AB@%/* DOS version */%@AE@%%@NL@%
  4806. }%@NL@%
  4807. %@NL@%
  4808. %@AB@%/* Pauses for a specified number of microseconds. */%@AE@%%@NL@%
  4809. void Sleep( clock_t wait )%@NL@%
  4810. {%@NL@%
  4811.     clock_t goal;%@NL@%
  4812. %@NL@%
  4813.     goal = wait + clock();%@NL@%
  4814.     while( goal >= clock() )%@NL@%
  4815.         ;%@NL@%
  4816. }%@NL@%
  4817. %@NL@%
  4818. %@NL@%
  4819. %@2@%%@AH@%TURTLE.C%@AE@%%@EH@%%@NL@%
  4820. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\MSC60\TURTLE.C%@AE@%%@NL@%
  4821. %@NL@%
  4822. %@AB@%/* TURTLE - Module of functions to implement turtle graphics. Turtle graphics%@NL@%
  4823. %@AB@% * is a model for specifying relative movements of an imaginary pointer whose%@NL@%
  4824. %@AB@% * direction, color, visibility, and other attributes are given default%@NL@%
  4825. %@AB@% * values using turtle functions. To use the turtle module, include TURTLE.H%@NL@%
  4826. %@AB@% * in your program. The following functions (many defined as macros)%@NL@%
  4827. %@AB@% * are public :%@NL@%
  4828. %@AB@% *%@NL@%
  4829. %@AB@% *   InitTurtle      - Initiate turtle graphics%@NL@%
  4830. %@AB@% *   Home            - Reset turtle defaults%@NL@%
  4831. %@AB@% *   PenDown         - Set pen visibility%@NL@%
  4832. %@AB@% *   SetFill         - Set fill state%@NL@%
  4833. %@AB@% *   PenColor        - Set pen color index%@NL@%
  4834. %@AB@% *   BorderColor     - Set border color index%@NL@%
  4835. %@AB@% *   Turn            - Set direction relative to current%@NL@%
  4836. %@AB@% *   TurnTo          - Set absolute direction%@NL@%
  4837. %@AB@% *   Move            - Move in current direction%@NL@%
  4838. %@AB@% *   MoveTo          - Move to absolute location%@NL@%
  4839. %@AB@% *   Poly            - Draw a polygon%@NL@%
  4840. %@AB@% *   Circle          - Draw a circle with center at current location%@NL@%
  4841. %@AB@% *   Ellipse         - Draw an ellipse with center at current location%@NL@%
  4842. %@AB@% *   Rectangle       - Draw a rectangle with center at current location%@NL@%
  4843. %@AB@% *   ImageSize       - Get size of rectangle with top-left origin%@NL@%
  4844. %@AB@% *   GetImage        - Get rectangular image with top-left origin%@NL@%
  4845. %@AB@% *   PutImage        - Put rectangular image with top-left origin%@NL@%
  4846. %@AB@% *   FillIn          - Fill from the current location to border%@NL@%
  4847. %@AB@% *   NextColorIndex  - Rotate to next color index%@NL@%
  4848. %@AB@% *   NextColorValue  - Rotate to next color value%@NL@%
  4849. %@AB@% *   OnScreen        - Report whether current location is on screen%@NL@%
  4850. %@AB@% *   RGB             - Combine Red, Green, and Blue elements of color value%@NL@%
  4851. %@AB@% *%@NL@%
  4852. %@AB@% * The TURTLE structure, the "tc" global variable (having TURTLE type), and%@NL@%
  4853. %@AB@% * "vlColors" variable are defined. However, these are not normally used%@NL@%
  4854. %@AB@% * directly by the programmer.%@NL@%
  4855. %@AB@% */%@AE@%%@NL@%
  4856. %@NL@%
  4857. %@AI@%#include %@AE@%<graph.h> %@NL@%
  4858. %@AI@%#include %@AE@%<math.h> %@NL@%
  4859. %@AI@%#include %@AE@%<string.h> %@NL@%
  4860. %@AI@%#include %@AE@%"turtle.h" %@NL@%
  4861. %@NL@%
  4862. %@AI@%#define %@AE@%PI       3.141593 %@NL@%
  4863. %@NL@%
  4864. long cvlColors[256];            %@AB@%/* Array of long color values              */%@AE@%%@NL@%
  4865. %@NL@%
  4866. TURTLE tc = { 1.39 };    %@AB@%/* Initial aspect - adjust for your screen */%@AE@%%@NL@%
  4867. %@NL@%
  4868. %@AB@%/* InitTurtle - Initializes all turtle defaults. This function should be%@NL@%
  4869. %@AB@% * called at least once (after _setvideomode and _getvideoconfig) and%@NL@%
  4870. %@AB@% * additionally after any change to a new graphics mode.%@NL@%
  4871. %@AB@% *%@NL@%
  4872. %@AB@% * Params: vc - pointer to videoconfig structure%@NL@%
  4873. %@AB@% *%@NL@%
  4874. %@AB@% * Return: 0 if fail, 1 if success%@NL@%
  4875. %@AB@% *%@NL@%
  4876. %@AB@% * Uses:   tc structure variable cvlColors array%@NL@%
  4877. %@AB@% */%@AE@%%@NL@%
  4878. short InitTurtle( struct videoconfig *vc )%@NL@%
  4879. {%@NL@%
  4880.     int i;%@NL@%
  4881.     unsigned cvuInc, cvuRed, cvuGreen, cvuBlue; %@AB@%/* Unsigned portions of */%@AE@%%@NL@%
  4882.     static int mode = -1;                       %@AB@%/*   color values       */%@AE@%%@NL@%
  4883. %@NL@%
  4884.     %@AB@%/* Terminate if not graphics mode. */%@AE@%%@NL@%
  4885.     if( !vc->numxpixels )%@NL@%
  4886.         return 0;%@NL@%
  4887. %@NL@%
  4888.     %@AB@%/* If mode has changed, set window coordinates. */%@AE@%%@NL@%
  4889.     if( mode != vc->mode )%@NL@%
  4890.     {%@NL@%
  4891.         mode = vc->mode;%@NL@%
  4892.         tc.xsLeft = tc.ysTop = 0;%@NL@%
  4893.         tc.xsRight = vc->numxpixels - 1;%@NL@%
  4894.         tc.ysBot = vc->numypixels - 1;%@NL@%
  4895.     }%@NL@%
  4896. %@NL@%
  4897.     %@AB@%/* Set palette flag. */%@AE@%%@NL@%
  4898.     switch( vc->adapter )%@NL@%
  4899.     {%@NL@%
  4900.         case _MDPA:%@NL@%
  4901.         case _CGA:%@NL@%
  4902.         case _OCGA:%@NL@%
  4903.         case _HGC:%@NL@%
  4904.             tc.fPalette = FALSE;%@NL@%
  4905.             break;%@NL@%
  4906.         default:%@NL@%
  4907.             tc.fPalette = TRUE;%@NL@%
  4908.             break;%@NL@%
  4909.     }%@NL@%
  4910. %@NL@%
  4911.     %@AB@%/* Set palette defaults. */%@AE@%%@NL@%
  4912.     switch( vc->mode )%@NL@%
  4913.     {%@NL@%
  4914.         case _HRESBW:%@NL@%
  4915.         case _HERCMONO:%@NL@%
  4916.         case _ERESNOCOLOR:%@NL@%
  4917.         case _ORESCOLOR:%@NL@%
  4918.         case _VRES2COLOR:%@NL@%
  4919.             tc.ccv = 0;%@NL@%
  4920.             tc.cci = 2;%@NL@%
  4921.             return Home();%@NL@%
  4922.         case _MRES256COLOR:        %@AB@%/* Active bits in this order:          */%@AE@%%@NL@%
  4923.             cvuInc = 12;%@NL@%
  4924.             tc.ccv = tc.cci = 125; %@AB@%/* ???????? ??bbbbbb ??gggggg ??rrrrrr */%@AE@%%@NL@%
  4925.             break;%@NL@%
  4926.         case _ERESCOLOR:%@NL@%
  4927.             if( vc->memory == 64 )%@NL@%
  4928.             {%@NL@%
  4929.                 cvuInc = 32;%@NL@%
  4930.                 tc.ccv = 16;       %@AB@%/* ???????? ??????Bb ??????Gg ??????Rr */%@AE@%%@NL@%
  4931.                 tc.cci = 4;%@NL@%
  4932.                 break;%@NL@%
  4933.             } %@AB@%/* Else fall through */%@AE@%%@NL@%
  4934.         case _VRES16COLOR:%@NL@%
  4935.             cvuInc = 16;%@NL@%
  4936.             tc.ccv = 64;           %@AB@%/* ???????? ??????bb ??????gg ??????rr */%@AE@%%@NL@%
  4937.             tc.cci = 16;%@NL@%
  4938.             break;%@NL@%
  4939.         case _MRES4COLOR:%@NL@%
  4940.         case _MRESNOCOLOR:%@NL@%
  4941.             cvuInc = 32;%@NL@%
  4942.             tc.ccv = 16;           %@AB@%/* ???????? ??????Bb ??????Gg ??????Rr */%@AE@%%@NL@%
  4943.             tc.cci = 4;%@NL@%
  4944.             break;%@NL@%
  4945.         case _MRES16COLOR:%@NL@%
  4946.         case _HRES16COLOR:%@NL@%
  4947.             cvuInc = 32;%@NL@%
  4948.             tc.cci = tc.ccv = 16;  %@AB@%/* ???????? ??????Bb ??????Gg ??????Rr */%@AE@%%@NL@%
  4949.             break;%@NL@%
  4950.     }%@NL@%
  4951. %@NL@%
  4952.     %@AB@%/* Fill palette arrays. */%@AE@%%@NL@%
  4953.     for( i = 0, cvuBlue = 0; cvuBlue < 64; cvuBlue += cvuInc )%@NL@%
  4954.         for( cvuGreen = 0; cvuGreen < 64; cvuGreen += cvuInc )%@NL@%
  4955.             for( cvuRed = 0; cvuRed < 64; cvuRed += cvuInc )%@NL@%
  4956.                 {%@NL@%
  4957.                     cvlColors[i] = RGB( cvuRed, cvuGreen, cvuBlue );%@NL@%
  4958.                     %@AB@%/* Special case of 6 bits for 16 colors (RGBI).%@NL@%
  4959. %@AB@%                     * If both bits are on for any color, intensity is set.%@NL@%
  4960. %@AB@%                     * If one bit is set for a color, that color is on.%@NL@%
  4961. %@AB@%                     */%@AE@%%@NL@%
  4962.                     if( cvuInc == 32 )%@NL@%
  4963.                         cvlColors[i + 8] = cvlColors[i] | (cvlColors[i] >> 1);%@NL@%
  4964.                     i++;%@NL@%
  4965.                 }%@NL@%
  4966.     cvlColors[tc.ccv - 1] = _BRIGHTWHITE;%@NL@%
  4967.     NextColorValue( DEFAULT );%@NL@%
  4968.     return Home();%@NL@%
  4969. }%@NL@%
  4970. %@NL@%
  4971. %@AB@%/* Home - Resets turtle defaults. This function can be called if you have%@NL@%
  4972. %@AB@% * not changed the video mode, but you want to put the turtle back in%@NL@%
  4973. %@AB@% * the center of the window and restore all defaults. For example, you can%@NL@%
  4974. %@AB@% * change the absolute window corners and then call it to set a new%@NL@%
  4975. %@AB@% * turtle window.%@NL@%
  4976. %@AB@% *%@NL@%
  4977. %@AB@% * Params: None%@NL@%
  4978. %@AB@% *%@NL@%
  4979. %@AB@% * Return: 0 if fail, 1 if success%@NL@%
  4980. %@AB@% *%@NL@%
  4981. %@AB@% * Uses:   tc%@NL@%
  4982. %@AB@% */%@AE@%%@NL@%
  4983. short Home()%@NL@%
  4984. {%@NL@%
  4985.     struct _wxycoord xy1, xy2;%@NL@%
  4986. %@NL@%
  4987.     _setviewport( tc.xsLeft, tc.ysTop, tc.xsRight, tc.ysBot );%@NL@%
  4988. %@NL@%
  4989.     %@AB@%/* Set the window based on screen height 1000 and width based on%@NL@%
  4990. %@AB@%     * aspect ratio.%@NL@%
  4991. %@AB@%     */%@AE@%%@NL@%
  4992.     tc.yMax = 500.0;%@NL@%
  4993.     tc.xMax = tc.yMax * tc.yxRatio;%@NL@%
  4994.     if( !_setwindow( FALSE, -tc.xMax, -tc.yMax, tc.xMax, tc.yMax ) )%@NL@%
  4995.         return 0;%@NL@%
  4996. %@NL@%
  4997.     %@AB@%/* Calculate the unit size of 1 pixel using Y axis. */%@AE@%%@NL@%
  4998.     xy1 = _getwindowcoord( 1, 1 );%@NL@%
  4999.     xy2 = _getwindowcoord( 1, 2 );%@NL@%
  5000.     tc.yUnit = xy2.wy - xy1.wy;%@NL@%
  5001. %@NL@%
  5002.     %@AB@%/* Set defaults for current pixel, angle, pen state and fill state. */%@AE@%%@NL@%
  5003.     tc.xCur = tc.yCur = 0.0;%@NL@%
  5004.     _moveto_w( tc.xCur, tc.yCur );%@NL@%
  5005.     TurnTo( 0 );%@NL@%
  5006.     PenDown( TRUE );%@NL@%
  5007.     SetFill( FALSE );%@NL@%
  5008. %@NL@%
  5009.     %@AB@%/* Make white the last color index and set pen and border to it. */%@AE@%%@NL@%
  5010.     _remappalette( WHITE, _BRIGHTWHITE );%@NL@%
  5011.     BorderColor( WHITE );%@NL@%
  5012.     PenColor( WHITE );%@NL@%
  5013.     _setbkcolor( _BLACK );%@NL@%
  5014.     return 1;%@NL@%
  5015. }%@NL@%
  5016. %@NL@%
  5017. %@AB@%/* PenDown - Sets the visibility of the pen used by Move and MoveTo. The%@NL@%
  5018. %@AB@% * state can be TRUE (visible), FALSE (invisible), or DEFAULT (return%@NL@%
  5019. %@AB@% * current without changing).%@NL@%
  5020. %@AB@% *%@NL@%
  5021. %@AB@% * Params: fPenDown%@NL@%
  5022. %@AB@% *%@NL@%
  5023. %@AB@% * Return: current pen state%@NL@%
  5024. %@AB@% *%@NL@%
  5025. %@AB@% * Uses:   tc%@NL@%
  5026. %@AB@% */%@AE@%%@NL@%
  5027. int PenDown( int fPenDown )%@NL@%
  5028. {%@NL@%
  5029.     switch( fPenDown )%@NL@%
  5030.     {%@NL@%
  5031.         case DEFAULT:%@NL@%
  5032.             break;%@NL@%
  5033.         case FALSE:%@NL@%
  5034.             tc.fPenDown = FALSE;%@NL@%
  5035.             break;%@NL@%
  5036.         default:%@NL@%
  5037.             tc.fPenDown = TRUE;%@NL@%
  5038.             break;%@NL@%
  5039.     }%@NL@%
  5040.     return tc.fPenDown;%@NL@%
  5041. }%@NL@%
  5042. %@NL@%
  5043. %@AB@%/* SetFill - Determines the state of filling figures such as Rectangle,%@NL@%
  5044. %@AB@% * Circle, and Ellipse. State can be TRUE (fill inside), FALSE (border%@NL@%
  5045. %@AB@% * only), or DEFAULT (return current fill state).%@NL@%
  5046. %@AB@% *%@NL@%
  5047. %@AB@% * Params: fFill%@NL@%
  5048. %@AB@% *%@NL@%
  5049. %@AB@% * Return: current fill state%@NL@%
  5050. %@AB@% *%@NL@%
  5051. %@AB@% * Uses:   tc%@NL@%
  5052. %@AB@% */%@AE@%%@NL@%
  5053. short SetFill( short fFill )%@NL@%
  5054. {%@NL@%
  5055.     switch( fFill )%@NL@%
  5056.     {%@NL@%
  5057.         case DEFAULT:%@NL@%
  5058.             break;%@NL@%
  5059.         case _GBORDER:%@NL@%
  5060.         case FALSE:%@NL@%
  5061.             tc.fFill = _GBORDER;%@NL@%
  5062.             break;%@NL@%
  5063.         default:%@NL@%
  5064.             tc.fFill = _GFILLINTERIOR;%@NL@%
  5065.             break;%@NL@%
  5066.     }%@NL@%
  5067.     return tc.fFill;%@NL@%
  5068. }%@NL@%
  5069. %@NL@%
  5070. %@AB@%/* PenColor - Sets the color index of the pen.%@NL@%
  5071. %@AB@% *%@NL@%
  5072. %@AB@% * Params: ciCur - any color index of DEFAULT to return without changing%@NL@%
  5073. %@AB@% *%@NL@%
  5074. %@AB@% * Return: current pen color index%@NL@%
  5075. %@AB@% *%@NL@%
  5076. %@AB@% * Uses:   tc%@NL@%
  5077. %@AB@% */%@AE@%%@NL@%
  5078. short PenColor( short ciCur )%@NL@%
  5079. {%@NL@%
  5080.     if( ciCur != DEFAULT )%@NL@%
  5081.         _setcolor( tc.ciCur = ciCur );%@NL@%
  5082.     return tc.ciCur;%@NL@%
  5083. }%@NL@%
  5084. %@NL@%
  5085. %@AB@%/* BorderColor - Sets the color index of the border that will be recognized%@NL@%
  5086. %@AB@% * by fills.%@NL@%
  5087. %@AB@% *%@NL@%
  5088. %@AB@% * Params: ciBorder - any color index of DEFAULT to return without changing%@NL@%
  5089. %@AB@% *%@NL@%
  5090. %@AB@% * Return: current border color index%@NL@%
  5091. %@AB@% *%@NL@%
  5092. %@AB@% * Uses:   tc%@NL@%
  5093. %@AB@% */%@AE@%%@NL@%
  5094. short BorderColor( short border )%@NL@%
  5095. {%@NL@%
  5096.     if( border != DEFAULT )%@NL@%
  5097.         tc.ciBorder = border;%@NL@%
  5098.     return tc.ciBorder;%@NL@%
  5099. }%@NL@%
  5100. %@NL@%
  5101. %@AB@%/* Turn - Sets a new direction relative to the current direction.%@NL@%
  5102. %@AB@% *%@NL@%
  5103. %@AB@% * Params: angCur - a positive (clockwise) or negative (counterclockwise)%@NL@%
  5104. %@AB@% *           angle in degrees%@NL@%
  5105. %@AB@% *%@NL@%
  5106. %@AB@% * Return: new current absolute angle%@NL@%
  5107. %@AB@% *%@NL@%
  5108. %@AB@% * Uses:   tc%@NL@%
  5109. %@AB@% */%@AE@%%@NL@%
  5110. short Turn( short angCur )%@NL@%
  5111. {%@NL@%
  5112.     return( tc.angCur = ((tc.angCur + angCur) % CIRCUMFERENCE) );%@NL@%
  5113. }%@NL@%
  5114. %@NL@%
  5115. %@AB@%/* TurnTo - Sets a new absolute direction.%@NL@%
  5116. %@AB@% *%@NL@%
  5117. %@AB@% * Params: angCur - a positive (clockwise) or negative (counterclockwise)%@NL@%
  5118. %@AB@% *           angle in degrees (0 points to 12 o'clock)%@NL@%
  5119. %@AB@% *%@NL@%
  5120. %@AB@% * Return: new current absolute angle%@NL@%
  5121. %@AB@% *%@NL@%
  5122. %@AB@% * Uses:   tc%@NL@%
  5123. %@AB@% */%@AE@%%@NL@%
  5124. short TurnTo( short angCur )%@NL@%
  5125. {%@NL@%
  5126.     if( angCur < 0 )%@NL@%
  5127.         return( tc.angCur = 360 - (angCur % CIRCUMFERENCE) );%@NL@%
  5128.     else%@NL@%
  5129.         return( tc.angCur = angCur % CIRCUMFERENCE );%@NL@%
  5130. }%@NL@%
  5131. %@NL@%
  5132. %@AB@%/* Move - Moves from the current position in the current direction for a%@NL@%
  5133. %@AB@% * specified distance. A line is drawn if the pen is down. The current%@NL@%
  5134. %@AB@% * position is reset to the destination.%@NL@%
  5135. %@AB@% *%@NL@%
  5136. %@AB@% * Params: dxy - difference between current xy and new xy%@NL@%
  5137. %@AB@% *%@NL@%
  5138. %@AB@% * Return: 0 if new position is off screen, nonzero if on screen%@NL@%
  5139. %@AB@% *%@NL@%
  5140. %@AB@% * Uses:   tc%@NL@%
  5141. %@AB@% */%@AE@%%@NL@%
  5142. short Move( double dxy )%@NL@%
  5143. {%@NL@%
  5144.     double dx, dy;          %@AB@%/* Differences of X and Y */%@AE@%%@NL@%
  5145.     double angT;%@NL@%
  5146. %@NL@%
  5147.     %@AB@%/* Calculate new X and Y positions. */%@AE@%%@NL@%
  5148.     angT = (tc.angCur - 90) * (PI / HALFCIRCUMFERENCE);%@NL@%
  5149.     dx = dxy * cos( angT );%@NL@%
  5150.     dy = dxy * sin( angT );%@NL@%
  5151. %@NL@%
  5152.     %@AB@%/* Move, drawing if pen down, then update position */%@AE@%%@NL@%
  5153.     if( tc.fPenDown )%@NL@%
  5154.         _lineto_w( tc.xCur + dx, tc.yCur + dy );%@NL@%
  5155.     else%@NL@%
  5156.         _moveto_w( tc.xCur + dx, tc.yCur + dy );%@NL@%
  5157.     tc.xCur += dx;%@NL@%
  5158.     tc.yCur += dy;%@NL@%
  5159.     return OnScreen();%@NL@%
  5160. }%@NL@%
  5161. %@NL@%
  5162. %@AB@%/* MoveTo - Moves from the current position to a specified position. A%@NL@%
  5163. %@AB@% * line is drawn if the pen is down. The current position is reset to the%@NL@%
  5164. %@AB@% * destination. The current direction is not changed.%@NL@%
  5165. %@AB@% *%@NL@%
  5166. %@AB@% * Params: x and y - destination position%@NL@%
  5167. %@AB@% *%@NL@%
  5168. %@AB@% * Return: 0 if new position is off screen, nonzero if on screen%@NL@%
  5169. %@AB@% *%@NL@%
  5170. %@AB@% * Uses:   tc%@NL@%
  5171. %@AB@% */%@AE@%%@NL@%
  5172. short MoveTo( double x, double y )%@NL@%
  5173. {%@NL@%
  5174.     if( tc.fPenDown )%@NL@%
  5175.         _lineto_w( x, y );%@NL@%
  5176.     else%@NL@%
  5177.         _moveto_w( x, y );%@NL@%
  5178.     tc.xCur = x;%@NL@%
  5179.     tc.yCur = y;%@NL@%
  5180.     return OnScreen();%@NL@%
  5181. }%@NL@%
  5182. %@NL@%
  5183. %@AB@%/* Poly - Draws a polygon.%@NL@%
  5184. %@AB@% *%@NL@%
  5185. %@AB@% * Params: cSide - count of polygon sides%@NL@%
  5186. %@AB@% *         dxySide - distance of each side%@NL@%
  5187. %@AB@% *%@NL@%
  5188. %@AB@% * Return: 0 if any part of polygon is off screen, nonzero if on screen%@NL@%
  5189. %@AB@% */%@AE@%%@NL@%
  5190. short Poly( int cSide, double dxySide )%@NL@%
  5191. {%@NL@%
  5192.     short i, angT, fPen, ret = TRUE;%@NL@%
  5193. %@NL@%
  5194.     %@AB@%/* Make sure pen is down (restore pen state when done). */%@AE@%%@NL@%
  5195.     fPen = PenDown( TRUE );%@NL@%
  5196. %@NL@%
  5197.     %@AB@%/* Calculate angle, then draw each side. */%@AE@%%@NL@%
  5198.     angT = 360 / cSide;%@NL@%
  5199.     for( i = 1; i <= cSide; i++ )%@NL@%
  5200.     {%@NL@%
  5201.         ret = Move( dxySide ) && ret;%@NL@%
  5202.         Turn( angT );%@NL@%
  5203.     }%@NL@%
  5204.     PenDown( fPen );%@NL@%
  5205.     return ret;%@NL@%
  5206. }%@NL@%
  5207. %@NL@%
  5208. %@AB@%/* NextColorIndex - Rotate to next color index. First attribute (normally%@NL@%
  5209. %@AB@% * background) and last attribute (white) are skipped.%@NL@%
  5210. %@AB@% *%@NL@%
  5211. %@AB@% * Params: ciCur - Specify DEFAULT to use color index from last call,%@NL@%
  5212. %@AB@% *           or specify a new color to rotate from%@NL@%
  5213. %@AB@% *%@NL@%
  5214. %@AB@% * Return: rotated color index%@NL@%
  5215. %@AB@% *%@NL@%
  5216. %@AB@% * Uses:   tc%@NL@%
  5217. %@AB@% */%@AE@%%@NL@%
  5218. short NextColorIndex( short ciCur )%@NL@%
  5219. {%@NL@%
  5220.     static short ciPrev = 0;    %@AB@%/* Static to retain index between calls */%@AE@%%@NL@%
  5221. %@NL@%
  5222.     %@AB@%/* Assign new current if given. */%@AE@%%@NL@%
  5223.     if( ciCur != DEFAULT )%@NL@%
  5224.         ciPrev = ciCur;%@NL@%
  5225. %@NL@%
  5226.     %@AB@%/* Toggle for two-color modes, rotate for multi-color modes. */%@AE@%%@NL@%
  5227.     if( tc.cci == 2 )%@NL@%
  5228.         return( ciPrev = !ciPrev );%@NL@%
  5229.     else%@NL@%
  5230.         return( ciPrev = (++ciPrev % (tc.cci - 1)) );%@NL@%
  5231. }%@NL@%
  5232. %@NL@%
  5233. %@AB@%/* NextColorValue - Rotate to next color value for adapters (EGA%@NL@%
  5234. %@AB@% * and higher) that support remappable palettes.%@NL@%
  5235. %@AB@% *%@NL@%
  5236. %@AB@% * Params: fAction - DEFAULT (rotate all) or LIMITED (rotate first 14 only)%@NL@%
  5237. %@AB@% *%@NL@%
  5238. %@AB@% * Return: None%@NL@%
  5239. %@AB@% *%@NL@%
  5240. %@AB@% * Uses:   tc%@NL@%
  5241. %@AB@% */%@AE@%%@NL@%
  5242. void NextColorValue( int fAction )%@NL@%
  5243. {%@NL@%
  5244.     static int icvCur = 1;  %@AB@%/* Current index into color value array */%@AE@%%@NL@%
  5245.     static int ciCur = 1;   %@AB@%/* Current color index                  */%@AE@%%@NL@%
  5246.     int icvT;               %@AB@%/* Temporary index into color values    */%@AE@%%@NL@%
  5247.     int i;%@NL@%
  5248. %@NL@%
  5249.     %@AB@%/* Ignore modes with no palette values. */%@AE@%%@NL@%
  5250.     if( !tc.fPalette || !tc.ccv )%@NL@%
  5251.         return;%@NL@%
  5252. %@NL@%
  5253.     %@AB@%/* Increment and rotate color value index. */%@AE@%%@NL@%
  5254.     icvT = (++icvCur % (tc.ccv - 2)) + 1;%@NL@%
  5255. %@NL@%
  5256. %@NL@%
  5257.     %@AB@%/* DEFAULT - Remap all color indexes, 14 at a time. For most modes,%@NL@%
  5258. %@AB@%     * this is all the indexes  except first and last. For 256-color%@NL@%
  5259. %@AB@%     * mode, rotating all available indexes would be too slow.%@NL@%
  5260. %@AB@%     */%@AE@%%@NL@%
  5261.     if( fAction == DEFAULT )%@NL@%
  5262.         for( i = 1; i <= 14; i++ )%@NL@%
  5263.             _remappalette( (ciCur++ % (tc.cci - 2)) + 1,%@NL@%
  5264.                            cvlColors[(icvT++ % (tc.ccv - 2)) + 1] );%@NL@%
  5265. %@NL@%
  5266.     %@AB@%/* LIMITED - Rotate only the first 14 color indexes. */%@AE@%%@NL@%
  5267.     else%@NL@%
  5268.         for( i = 1; i <= 14; i++ )%@NL@%
  5269.             _remappalette( i, cvlColors[(icvT++ % (tc.ccv - 1)) + 1] );%@NL@%
  5270. }%@NL@%
  5271. %@1@%%@AH@%Microsoft Quick-C Sample Code%@EH@%%@AE@%
  5272. %@NL@%
  5273. %@NL@%
  5274. %@2@%%@AH@%ARGV.C%@AE@%%@EH@%%@NL@%
  5275. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\ARGV.C%@AE@%%@NL@%
  5276. %@NL@%
  5277. %@AB@%/* ARGV.C: Demonstrate accessing command-line arguments. */%@AE@%%@NL@%
  5278. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5279. %@NL@%
  5280. void show_args( char *argument );%@NL@%
  5281. %@NL@%
  5282. int main( int argc, char *argv[] )%@NL@%
  5283. {%@NL@%
  5284.    int count;%@NL@%
  5285.    for( count=0; count < argc; count++ )%@NL@%
  5286.       show_args( argv[count] );%@NL@%
  5287.    return 0;%@NL@%
  5288. }%@NL@%
  5289. %@NL@%
  5290. void show_args( char *argument )%@NL@%
  5291. {%@NL@%
  5292.    printf( "%s\n", argument );%@NL@%
  5293. }%@NL@%
  5294. %@NL@%
  5295. %@NL@%
  5296. %@2@%%@AH@%ARGV1.C%@AE@%%@EH@%%@NL@%
  5297. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\ARGV1.C%@AE@%%@NL@%
  5298. %@NL@%
  5299. %@AB@%/* ARGV1.C: Demonstrate null pointers. */%@AE@%%@NL@%
  5300. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5301. %@NL@%
  5302. void show_args( char *argument );%@NL@%
  5303. %@NL@%
  5304. int main( int argc, char **argv )%@NL@%
  5305. {%@NL@%
  5306.    while( *argv )%@NL@%
  5307.       show_args( *(argv++) );%@NL@%
  5308.    return 0;%@NL@%
  5309. }%@NL@%
  5310. %@NL@%
  5311. void show_args( char *argument )%@NL@%
  5312. {%@NL@%
  5313.    printf( "%s\n", argument );%@NL@%
  5314. }%@NL@%
  5315. %@NL@%
  5316. %@NL@%
  5317. %@2@%%@AH@%ARRAY.C%@AE@%%@EH@%%@NL@%
  5318. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\ARRAY.C%@AE@%%@NL@%
  5319. %@NL@%
  5320. %@AB@%/* ARRAY.C: Demonstrate one-dimensional array. */%@AE@%%@NL@%
  5321. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5322. %@NL@%
  5323. main()%@NL@%
  5324. {%@NL@%
  5325.    int j;%@NL@%
  5326.    int i_array[3];%@NL@%
  5327. %@NL@%
  5328.    i_array[0] = 176;%@NL@%
  5329.    i_array[1] = 4069;%@NL@%
  5330.    i_array[2] = 303;%@NL@%
  5331. %@NL@%
  5332.    printf( "--- Values --------     --- Addresses -------\n\n" );%@NL@%
  5333. %@NL@%
  5334.    for( j = 0; j < 3; j = j + 1 )%@NL@%
  5335.    {%@NL@%
  5336.       printf( "i_array[%d] = %d", j, i_array[j] );%@NL@%
  5337.       printf( "\t&i_array[%d] = %u\n", j, &i_array[j] );%@NL@%
  5338.    }%@NL@%
  5339. %@NL@%
  5340. }%@NL@%
  5341. %@NL@%
  5342. %@NL@%
  5343. %@2@%%@AH@%BAR.C%@AE@%%@EH@%%@NL@%
  5344. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\BAR.C%@AE@%%@NL@%
  5345. %@NL@%
  5346. %@AB@%/* BAR.C:  Create sample bar chart. */%@AE@%%@NL@%
  5347. %@AI@%#include %@AE@%<conio.h> %@NL@%
  5348. %@AI@%#include %@AE@%<string.h> %@NL@%
  5349. %@AI@%#include %@AE@%<graph.h> %@NL@%
  5350. %@AI@%#include %@AE@%<pgchart.h> %@NL@%
  5351. %@AI@%#define %@AE@%MONTHS 12 %@NL@%
  5352. typedef enum {FALSE, TRUE} boolean;%@NL@%
  5353. float far value[MONTHS] = %@NL@%
  5354. { %@NL@%
  5355.    33.0, 27.0, 42.0, 64.0,106.0,157.0,%@NL@%
  5356.   182.0,217.0,128.0, 62.0, 43.0, 36.0%@NL@%
  5357. };%@NL@%
  5358. char far *category[MONTHS] =%@NL@%
  5359. {%@NL@%
  5360.   "Jan", "Feb", "Mar", "Apr",%@NL@%
  5361.   "May", "Jun", "Jly", "Aug",%@NL@%
  5362.   "Sep", "Oct", "Nov", "Dec"%@NL@%
  5363. };%@NL@%
  5364. %@NL@%
  5365. main()%@NL@%
  5366. {%@NL@%
  5367.   chartenv env;%@NL@%
  5368.   int mode = _VRES16COLOR;%@NL@%
  5369.   %@AB@%/* Set highest video mode available */%@AE@%%@NL@%
  5370.   while(!_setvideomode( mode ))%@NL@%
  5371.      mode--;%@NL@%
  5372.   if(mode == _TEXTMONO)%@NL@%
  5373.      return(0);%@NL@%
  5374. %@NL@%
  5375.   %@AB@%/* Initialize chart library and a default bar chart */%@AE@%%@NL@%
  5376.   _pg_initchart();%@NL@%
  5377. %@NL@%
  5378.   _pg_defaultchart( &env, _PG_BARCHART, _PG_PLAINBARS %@NL@%
  5379. );%@NL@%
  5380.   %@AB@%/* Add titles and some chart options */%@AE@%%@NL@%
  5381.   strcpy( env.maintitle.title, "Good Neighbor Grocery" );%@NL@%
  5382.   env.maintitle.titlecolor = 6;%@NL@%
  5383.   env.maintitle.justify = _PG_RIGHT;%@NL@%
  5384.   strcpy( env.subtitle.title, "Orange Juice Sales" );%@NL@%
  5385.   env.subtitle.titlecolor = 6;%@NL@%
  5386.   env.subtitle.justify = _PG_RIGHT;%@NL@%
  5387.   strcpy( env.yaxis.axistitle.title, "Months" );%@NL@%
  5388.   strcpy( env.xaxis.axistitle.title, "Quantity (cases)" );%@NL@%
  5389.   env.chartwindow.border = FALSE;%@NL@%
  5390. %@NL@%
  5391.   %@AB@%/* Parameters for call to _pg_chart are:%@NL@%
  5392. %@AB@%   *    env        - Environment variable%@NL@%
  5393. %@AB@%   *    category   - Category labels%@NL@%
  5394. %@AB@%   *    value      - Data to chart%@NL@%
  5395. %@AB@%   *    MONTHS     - Number of data values */%@AE@%%@NL@%
  5396. %@NL@%
  5397.   if(_pg_chart( &env, category, value, MONTHS ))%@NL@%
  5398.   {%@NL@%
  5399.      _setvideomode( _DEFAULTMODE );%@NL@%
  5400.      _outtext( "Error:  can't draw chart" );%@NL@%
  5401.   }%@NL@%
  5402.   else%@NL@%
  5403.   {%@NL@%
  5404.       getch(); %@NL@%
  5405.       _setvideomode( _DEFAULTMODE );%@NL@%
  5406.    }%@NL@%
  5407.    return(0);%@NL@%
  5408. }%@NL@%
  5409. %@NL@%
  5410. %@NL@%
  5411. %@2@%%@AH@%BEEP.C%@AE@%%@EH@%%@NL@%
  5412. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\BEEP.C%@AE@%%@NL@%
  5413. %@NL@%
  5414. %@AB@%/* BEEP.C: Demonstrate simple function */%@AE@%%@NL@%
  5415. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5416. %@NL@%
  5417. void beep( void);%@NL@%
  5418. %@NL@%
  5419. main()%@NL@%
  5420. {%@NL@%
  5421.    printf( "Time to beep\n" );%@NL@%
  5422.    beep();%@NL@%
  5423.    printf( "All done\n" );%@NL@%
  5424. }%@NL@%
  5425. %@NL@%
  5426. void beep( void )%@NL@%
  5427. {%@NL@%
  5428.    printf( "Beep!\a\n" );%@NL@%
  5429. }%@NL@%
  5430. %@NL@%
  5431. %@NL@%
  5432. %@2@%%@AH@%BEEP1.C%@AE@%%@EH@%%@NL@%
  5433. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\BEEP1.C%@AE@%%@NL@%
  5434. %@NL@%
  5435. %@AB@%/* BEEP1.C: Demonstrate passing arguments */%@AE@%%@NL@%
  5436. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5437. %@NL@%
  5438. void beep( int num_beep );%@NL@%
  5439. %@NL@%
  5440. main()%@NL@%
  5441. {%@NL@%
  5442.    printf( "Time to beep\n" );%@NL@%
  5443.    beep( 5 );%@NL@%
  5444.    printf( "All done\n" );%@NL@%
  5445. }%@NL@%
  5446. %@NL@%
  5447. void beep( int num_beep )%@NL@%
  5448. {%@NL@%
  5449.    while( num_beep > 0  )%@NL@%
  5450.    {%@NL@%
  5451.       printf( "Beep!\a\n" );%@NL@%
  5452.       num_beep = num_beep - 1;%@NL@%
  5453.    }%@NL@%
  5454. }%@NL@%
  5455. %@NL@%
  5456. %@NL@%
  5457. %@2@%%@AH@%BEEPER.C%@AE@%%@EH@%%@NL@%
  5458. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\BEEPER.C%@AE@%%@NL@%
  5459. %@NL@%
  5460. %@AB@%/* BEEPER.C: Demonstrate simple function */%@AE@%%@NL@%
  5461. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5462. %@NL@%
  5463. void beep( void);%@NL@%
  5464. %@NL@%
  5465. main()%@NL@%
  5466. {%@NL@%
  5467.    printf( "Time to beep\n" );%@NL@%
  5468.    beep();%@NL@%
  5469.    printf( "All done\n" );%@NL@%
  5470. }%@NL@%
  5471. %@NL@%
  5472. void beep( void )%@NL@%
  5473. {%@NL@%
  5474.    printf( "Beep!\a\n" );%@NL@%
  5475. }%@NL@%
  5476. %@NL@%
  5477. %@NL@%
  5478. %@2@%%@AH@%BEEPER1.C%@AE@%%@EH@%%@NL@%
  5479. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\BEEPER1.C%@AE@%%@NL@%
  5480. %@NL@%
  5481. %@AB@%/* BEEPER1.C: Demonstrate passing arguments */%@AE@%%@NL@%
  5482. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5483. %@NL@%
  5484. void beep( int num_beep );%@NL@%
  5485. %@NL@%
  5486. main()%@NL@%
  5487. {%@NL@%
  5488.    printf( "Time to beep\n" );%@NL@%
  5489.    beep( 5 );%@NL@%
  5490.    printf( "All done\n" );%@NL@%
  5491. }%@NL@%
  5492. %@NL@%
  5493. void beep( int num_beep )%@NL@%
  5494. {%@NL@%
  5495.    while( num_beep > 0  )%@NL@%
  5496.    {%@NL@%
  5497.       printf( "Beep!\a\n" );%@NL@%
  5498.       num_beep = num_beep - 1;%@NL@%
  5499.    }%@NL@%
  5500. }%@NL@%
  5501. %@NL@%
  5502. %@NL@%
  5503. %@2@%%@AH@%BITWISE.C%@AE@%%@EH@%%@NL@%
  5504. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\BITWISE.C%@AE@%%@NL@%
  5505. %@NL@%
  5506. %@AB@%/* BITWISE.C: Demonstrate bitwise operators. */%@AE@%%@NL@%
  5507. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5508. %@NL@%
  5509. main()%@NL@%
  5510. {%@NL@%
  5511.    printf( "255 & 15 = %d\n", 255 & 15 );%@NL@%
  5512.    printf( "255 | 15 = %d\n", 255 | 15 );%@NL@%
  5513.    printf( "255 ^ 15 = %d\n", 255 ^ 15 );%@NL@%
  5514.    printf( "2 << 2   = %d\n", 2 << 2 );%@NL@%
  5515.    printf( "16 >> 2  = %d\n", 16 >> 2 );%@NL@%
  5516.    printf( "~2       = %d\n", ~2 );%@NL@%
  5517. }%@NL@%
  5518. %@NL@%
  5519. %@NL@%
  5520. %@2@%%@AH@%BREAKER.C%@AE@%%@EH@%%@NL@%
  5521. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\BREAKER.C%@AE@%%@NL@%
  5522. %@NL@%
  5523. %@AB@%/* BREAKER.C: Demonstrate break statement. */%@AE@%%@NL@%
  5524. %@NL@%
  5525. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5526. %@AI@%#include %@AE@%<conio.h> %@NL@%
  5527. %@NL@%
  5528. main()%@NL@%
  5529. {%@NL@%
  5530.    char ch;%@NL@%
  5531.    printf( "Press any key. Press Tab to quit.\n" );%@NL@%
  5532.    while( 1 )%@NL@%
  5533.    {%@NL@%
  5534.       ch = getche();%@NL@%
  5535.       if( ch == '\t' )%@NL@%
  5536.       {%@NL@%
  5537.          printf( "\a\nYou pressed Tab\n" );%@NL@%
  5538.          break;%@NL@%
  5539.       }%@NL@%
  5540.    }%@NL@%
  5541. }%@NL@%
  5542. %@NL@%
  5543. %@NL@%
  5544. %@2@%%@AH@%BREAKER1.C%@AE@%%@EH@%%@NL@%
  5545. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\BREAKER1.C%@AE@%%@NL@%
  5546. %@NL@%
  5547. %@AB@%/* BREAKER1.C: Break only exits one loop. */%@AE@%%@NL@%
  5548. %@NL@%
  5549. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5550. %@AI@%#include %@AE@%<conio.h> %@NL@%
  5551. %@NL@%
  5552. main()%@NL@%
  5553. {%@NL@%
  5554.    char ch;%@NL@%
  5555.    printf( "Press any key. Press Enter to quit.\n" );%@NL@%
  5556.    do%@NL@%
  5557.    {%@NL@%
  5558.       while( ( ch = getche() ) != '\r' )%@NL@%
  5559.       {%@NL@%
  5560.          if( ch == '\t' )%@NL@%
  5561.          {%@NL@%
  5562.             printf( "\a\nYou pressed Tab\n" );%@NL@%
  5563.             break;%@NL@%
  5564.          }%@NL@%
  5565.       }%@NL@%
  5566.    } while( ch != '\r' );%@NL@%
  5567.    printf( "\nBye bye." );%@NL@%
  5568. }%@NL@%
  5569. %@NL@%
  5570. %@NL@%
  5571. %@2@%%@AH@%CGA.C%@AE@%%@EH@%%@NL@%
  5572. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\CGA.C%@AE@%%@NL@%
  5573. %@NL@%
  5574. %@AB@%/* CGA.C: Demonstrates CGA colors */%@AE@%%@NL@%
  5575. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5576. %@AI@%#include %@AE@%<graph.h> %@NL@%
  5577. %@AI@%#include %@AE@%<conio.h> %@NL@%
  5578. %@NL@%
  5579. long bkcolor[8] = %@NL@%
  5580.    {_BLACK, _BLUE, _GREEN, _CYAN,%@NL@%
  5581.     _RED, _MAGENTA, _BROWN, _WHITE};%@NL@%
  5582. %@NL@%
  5583. char *bkcolor_name [] =%@NL@%
  5584.    {"_BLACK", "_BLUE", "_GREEN", "_CYAN",%@NL@%
  5585.    "_RED", "_MAGENTA", "_BROWN", "_WHITE"};%@NL@%
  5586. %@NL@%
  5587. main()%@NL@%
  5588. {%@NL@%
  5589.    int i, j, k;%@NL@%
  5590.    _setvideomode( _MRES4COLOR );%@NL@%
  5591.    for( i=0; i<= 3; i++ )%@NL@%
  5592.    {%@NL@%
  5593.       _selectpalette( i );%@NL@%
  5594.       for( k=0; k <= 7; k++ )%@NL@%
  5595.       {%@NL@%
  5596.          _setbkcolor( bkcolor[k] );%@NL@%
  5597.          for( j=0; j<=3; j++ )%@NL@%
  5598.          {%@NL@%
  5599.             _settextposition( 1, 1 );%@NL@%
  5600.             printf( "background color: %8s\n", bkcolor_name[k] );%@NL@%
  5601.             printf( "palette: %d\ncolor: %d\n", i, j );%@NL@%
  5602.             _setcolor( j );%@NL@%
  5603.             _rectangle( _GFILLINTERIOR, 160, 100, 320, 200 );%@NL@%
  5604.                getch();%@NL@%
  5605.          }%@NL@%
  5606.       }%@NL@%
  5607.    }%@NL@%
  5608.    _setvideomode( _DEFAULTMODE );%@NL@%
  5609. }%@NL@%
  5610. %@NL@%
  5611. %@NL@%
  5612. %@2@%%@AH@%COLOR.C%@AE@%%@EH@%%@NL@%
  5613. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\COLOR.C%@AE@%%@NL@%
  5614. %@NL@%
  5615.  %@AB@%/* COLOR.C: Sets a medium resolution mode with maximum color choices */%@AE@%%@NL@%
  5616. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5617. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  5618. %@AI@%#include %@AE@%<graph.h> %@NL@%
  5619. %@AI@%#include %@AE@%<conio.h> %@NL@%
  5620. struct videoconfig vc;%@NL@%
  5621. %@NL@%
  5622. main()%@NL@%
  5623. {%@NL@%
  5624.    if( _setvideomode( _MRES256COLOR ) )%@NL@%
  5625.       ;%@NL@%
  5626.    else if( _setvideomode( _MRES16COLOR ) )%@NL@%
  5627.       ;%@NL@%
  5628.    else if( _setvideomode( _MRES4COLOR ) )%@NL@%
  5629.       ;%@NL@%
  5630.    else   {%@NL@%
  5631.       printf( "Error: No color graphics capability\n" );%@NL@%
  5632.       exit( 0 );%@NL@%
  5633.    }%@NL@%
  5634. %@NL@%
  5635.    _getvideoconfig( &vc );%@NL@%
  5636. %@NL@%
  5637.    printf( "%d available colors\n", vc.numcolors );%@NL@%
  5638.    printf( "%d horizontal pixels\n", vc.numxpixels );%@NL@%
  5639.    printf( "%d vertical pixels\n", vc.numypixels );%@NL@%
  5640. %@NL@%
  5641.    getch();%@NL@%
  5642.    _clearscreen( _GCLEARSCREEN );%@NL@%
  5643.    _setvideomode( _DEFAULTMODE );%@NL@%
  5644. }%@NL@%
  5645. %@NL@%
  5646. %@NL@%
  5647. %@2@%%@AH@%COLTEXT.C%@AE@%%@EH@%%@NL@%
  5648. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\COLTEXT.C%@AE@%%@NL@%
  5649. %@NL@%
  5650. %@AB@%/* COLTEXT.C: Displays text in color */%@AE@%%@NL@%
  5651. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5652. %@AI@%#include %@AE@%<conio.h> %@NL@%
  5653. %@AI@%#include %@AE@%<graph.h> %@NL@%
  5654. %@NL@%
  5655. char buffer [80];%@NL@%
  5656. %@NL@%
  5657. main()%@NL@%
  5658. {%@NL@%
  5659.    int blink,fgd;%@NL@%
  5660.    long bgd;%@NL@%
  5661. %@NL@%
  5662.    _clearscreen( _GCLEARSCREEN );%@NL@%
  5663.    printf( "Text color attributes:\n" );%@NL@%
  5664. %@NL@%
  5665.    for( blink=0; blink<=16; blink+=16 ) %@NL@%
  5666.    {%@NL@%
  5667.       for( bgd=0; bgd<8; bgd++ ) %@NL@%
  5668.       {%@NL@%
  5669.          _setbkcolor( bgd );%@NL@%
  5670.          _settextposition( bgd + ((blink / 16) * 9) + 3, 1 );%@NL@%
  5671.          _settextcolor( 7 );%@NL@%
  5672.          sprintf( buffer, "Bgd: %d Fgd:", bgd );%@NL@%
  5673.          _outtext( buffer );%@NL@%
  5674.          %@NL@%
  5675.          for( fgd=0; fgd<16; fgd++ ) %@NL@%
  5676.          {%@NL@%
  5677.             _settextcolor( fgd+blink );%@NL@%
  5678.             sprintf( buffer, " %2d ", fgd+blink );%@NL@%
  5679.             _outtext( buffer );%@NL@%
  5680.          }%@NL@%
  5681.       }%@NL@%
  5682.    }%@NL@%
  5683.    getch();%@NL@%
  5684.    _setvideomode( _DEFAULTMODE );%@NL@%
  5685. }%@NL@%
  5686. %@NL@%
  5687. %@NL@%
  5688. %@2@%%@AH@%CONT.C%@AE@%%@EH@%%@NL@%
  5689. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\CONT.C%@AE@%%@NL@%
  5690. %@NL@%
  5691. %@AB@%/* CONT.C: Demonstrate continue statement. */%@AE@%%@NL@%
  5692. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5693. %@NL@%
  5694. main()%@NL@%
  5695. {%@NL@%
  5696.    int count;%@NL@%
  5697.    for( count = 0; count < 10; count++ )%@NL@%
  5698.    {%@NL@%
  5699.       if( count > 3 )%@NL@%
  5700.          continue;%@NL@%
  5701.       printf( "count = %d\n", count );%@NL@%
  5702.    }%@NL@%
  5703.    printf( "Done!\n" );%@NL@%
  5704. }%@NL@%
  5705. %@NL@%
  5706. %@NL@%
  5707. %@2@%%@AH@%CONVERT.C%@AE@%%@EH@%%@NL@%
  5708. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\CONVERT.C%@AE@%%@NL@%
  5709. %@NL@%
  5710. %@AB@%/* CONVERT.C: Demonstrate type conversions. */%@AE@%%@NL@%
  5711. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5712. %@NL@%
  5713. main()%@NL@%
  5714. {%@NL@%
  5715.    char c_val = 10;%@NL@%
  5716.    int i_val = 20;%@NL@%
  5717.    long l_val = 64000;%@NL@%
  5718.    float f_val = 3.1;%@NL@%
  5719.    int result;%@NL@%
  5720. %@NL@%
  5721.    result = c_val + i_val + l_val + f_val;  %@AB@%/* Error! */%@AE@%%@NL@%
  5722. %@NL@%
  5723.    printf( "%d\n", result );%@NL@%
  5724. }%@NL@%
  5725. %@NL@%
  5726. %@NL@%
  5727. %@2@%%@AH@%COPYFILE.C%@AE@%%@EH@%%@NL@%
  5728. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\COPYFILE.C%@AE@%%@NL@%
  5729. %@NL@%
  5730. %@AB@%/* COPYFILE.C: Demonstrate malloc and free functions */%@AE@%%@NL@%
  5731. %@NL@%
  5732. <stdio.h>     %@AB@%/* printf function and NULL */%@AE@%%@NL@%
  5733. <io.h>        %@AB@%/* low-level I/O functions */%@AE@%%@NL@%
  5734. <conio.h>     %@AB@%/* getch function */%@AE@%%@NL@%
  5735. <sys\types.h> %@AB@%/* struct members used in stat.h */%@AE@%%@NL@%
  5736. <sys\stat.h>  %@AB@%/* S_ constants */%@AE@%%@NL@%
  5737. <fcntl.h>     %@AB@%/* O_ constants */%@AE@%%@NL@%
  5738. <malloc.h>    %@AB@%/* malloc function */%@AE@%%@NL@%
  5739. <errno.h>     %@AB@%/* errno global variable */%@AE@%%@NL@%
  5740. %@NL@%
  5741. int copyfile( char *source, char *destin );%@NL@%
  5742. %@NL@%
  5743. main( int argc, char *argv[] )%@NL@%
  5744. {%@NL@%
  5745.    if( argc == 3 )%@NL@%
  5746.       if( copyfile( argv[1], argv[2] ) )%@NL@%
  5747.          printf( "Copy failed\n" );%@NL@%
  5748.       else%@NL@%
  5749.          printf( "Copy successful\n" );%@NL@%
  5750.    else%@NL@%
  5751.       printf( "  SYNTAX: COPYFILE <source> <target>\n" );%@NL@%
  5752. %@NL@%
  5753.    return 0;%@NL@%
  5754. }%@NL@%
  5755. %@NL@%
  5756. int copyfile( char *source, char *target )%@NL@%
  5757. {%@NL@%
  5758.    char *buf;%@NL@%
  5759.    int hsource, htarget, ch;%@NL@%
  5760.    unsigned count = 50000;%@NL@%
  5761. %@NL@%
  5762.    if( (hsource = open( source, O_BINARY | O_RDONLY )) == - 1 )%@NL@%
  5763.       return errno;%@NL@%
  5764.    htarget = open( target, O_BINARY | O_WRONLY | O_CREAT | O_EXCL,%@NL@%
  5765.                            S_IREAD | S_IWRITE );%@NL@%
  5766.    if( errno == EEXIST )%@NL@%
  5767.    {%@NL@%
  5768.       cputs( "Target exists. Overwrite? " );%@NL@%
  5769.       ch = getch();%@NL@%
  5770.       if( (ch == 'y') || (ch == 'Y') )%@NL@%
  5771.          htarget = open( target, O_BINARY | O_WRONLY | O_CREAT | O_TRUNC,%@NL@%
  5772.                                  S_IREAD | S_IWRITE );%@NL@%
  5773.       printf( "\n" );%@NL@%
  5774.    }%@NL@%
  5775.    if( htarget == -1 )%@NL@%
  5776.       return errno;%@NL@%
  5777. %@NL@%
  5778.    if( filelength( hsource ) < count )%@NL@%
  5779.       count = (int)filelength( hsource );%@NL@%
  5780. %@NL@%
  5781.    buf = (char *)malloc( (size_t)count );%@NL@%
  5782. %@NL@%
  5783.    if( buf == NULL )%@NL@%
  5784.    {%@NL@%
  5785.       count = _memmax();%@NL@%
  5786.       buf = (char *)malloc( (size_t)count );%@NL@%
  5787.       if( buf == NULL )%@NL@%
  5788.          return ENOMEM;%@NL@%
  5789.    }%@NL@%
  5790. %@NL@%
  5791.    while( !eof( hsource ) )%@NL@%
  5792.    {%@NL@%
  5793.       if( (count = read( hsource, buf, count )) == -1 )%@NL@%
  5794.          return errno;%@NL@%
  5795.       if( (count = write( htarget, buf, count )) == - 1 )%@NL@%
  5796.          return errno;%@NL@%
  5797.    }%@NL@%
  5798. %@NL@%
  5799.    close( hsource );%@NL@%
  5800.    close( htarget );%@NL@%
  5801.    free( buf );%@NL@%
  5802.    return 0;%@NL@%
  5803. }%@NL@%
  5804. %@NL@%
  5805. %@NL@%
  5806. %@2@%%@AH@%DECRMENT.C%@AE@%%@EH@%%@NL@%
  5807. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\DECRMENT.C%@AE@%%@NL@%
  5808. %@NL@%
  5809. %@AB@%/* DECRMENT.C: Demonstrate prefix and postfix operators. */%@AE@%%@NL@%
  5810. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5811. %@NL@%
  5812. main()%@NL@%
  5813. {%@NL@%
  5814.    int val, sample = 3, proton = 3;%@NL@%
  5815.    val = sample--;%@NL@%
  5816.    printf( "val = %d  sample = %d\n", val, sample );%@NL@%
  5817.    val = --proton;%@NL@%
  5818.    printf( "val = %d  proton = %d\n", val, proton );%@NL@%
  5819. }%@NL@%
  5820. %@NL@%
  5821. %@NL@%
  5822. %@2@%%@AH@%DEFINED.C%@AE@%%@EH@%%@NL@%
  5823. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\DEFINED.C%@AE@%%@NL@%
  5824. %@NL@%
  5825. %@AB@%/* DEFINED.C: Demonstrate defined operator. */%@AE@%%@NL@%
  5826. %@NL@%
  5827. %@AI@%#define %@AE@%DEBUG 12345 %@NL@%
  5828. %@NL@%
  5829. main()%@NL@%
  5830. {%@NL@%
  5831.    #if defined( DEBUG )%@NL@%
  5832.       printf( "Hi\n" );%@NL@%
  5833.    #endif%@NL@%
  5834. }%@NL@%
  5835. %@NL@%
  5836. %@NL@%
  5837. %@2@%%@AH@%DO.C%@AE@%%@EH@%%@NL@%
  5838. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\DO.C%@AE@%%@NL@%
  5839. %@NL@%
  5840. %@AB@%/* DO.C: Demonstrate do loop. */%@AE@%%@NL@%
  5841. %@NL@%
  5842. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5843. %@NL@%
  5844. main()%@NL@%
  5845. {%@NL@%
  5846.    int test = 10;%@NL@%
  5847.    do%@NL@%
  5848.    {%@NL@%
  5849.       printf( "test = %d\n", test );%@NL@%
  5850.       test = test - 2;%@NL@%
  5851.    }  while( test > 0 );%@NL@%
  5852. }%@NL@%
  5853. %@NL@%
  5854. %@NL@%
  5855. %@2@%%@AH@%EGA.C%@AE@%%@EH@%%@NL@%
  5856. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\EGA.C%@AE@%%@NL@%
  5857. %@NL@%
  5858. %@AB@%/* EGA.C: Demonstrates EGA palettes */%@AE@%%@NL@%
  5859. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5860. %@AI@%#include %@AE@%<conio.h> %@NL@%
  5861. %@AI@%#include %@AE@%<graph.h> %@NL@%
  5862. %@NL@%
  5863. main()%@NL@%
  5864. {%@NL@%
  5865.    _setvideomode( _ERESCOLOR );%@NL@%
  5866.    _setcolor( 4 );%@NL@%
  5867.    _rectangle( _GFILLINTERIOR, 50, 50, 200, 200 );%@NL@%
  5868. %@NL@%
  5869.    _settextposition( 1, 1 );%@NL@%
  5870.    printf( "Normal palette\n" );%@NL@%
  5871.    printf( "Press a key" );%@NL@%
  5872.    getch();%@NL@%
  5873. %@NL@%
  5874.    _remappalette( 4, _BLUE );%@NL@%
  5875. %@NL@%
  5876.    _settextposition( 1, 1 );%@NL@%
  5877.    printf( "Remapped palette\n" );%@NL@%
  5878.    printf( "Press a key" );%@NL@%
  5879.    getch();%@NL@%
  5880. %@NL@%
  5881.    _remappalette( 4, _RED );%@NL@%
  5882. %@NL@%
  5883.    _settextposition( 1, 1 );%@NL@%
  5884.    printf( "Restored palette\n" );%@NL@%
  5885.    printf( "Press a key to clear the screen" );%@NL@%
  5886.    getch();%@NL@%
  5887. %@NL@%
  5888.    _clearscreen( _GCLEARSCREEN );%@NL@%
  5889.    _setvideomode( _DEFAULTMODE );%@NL@%
  5890. }%@NL@%
  5891. %@NL@%
  5892. %@NL@%
  5893. %@2@%%@AH@%ELSE.C%@AE@%%@EH@%%@NL@%
  5894. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\ELSE.C%@AE@%%@NL@%
  5895. %@NL@%
  5896. %@AB@%/* ELSE.C: Demonstrate else clause. */%@AE@%%@NL@%
  5897. %@NL@%
  5898. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5899. %@AI@%#include %@AE@%<conio.h> %@NL@%
  5900. %@NL@%
  5901. main()%@NL@%
  5902. {%@NL@%
  5903.    char ch;%@NL@%
  5904.    printf( "Press the b key to hear a bell.\n" );%@NL@%
  5905.    ch = getch();%@NL@%
  5906.    if( ch == 'b' )%@NL@%
  5907.       printf( "Beep!\a\n" );%@NL@%
  5908.    else%@NL@%
  5909.       printf( "Bye bye\n" );%@NL@%
  5910. }%@NL@%
  5911. %@NL@%
  5912. %@NL@%
  5913. %@2@%%@AH@%ELSE1.C%@AE@%%@EH@%%@NL@%
  5914. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\ELSE1.C%@AE@%%@NL@%
  5915. %@NL@%
  5916. %@AB@%/* ELSE1.C: Demonstrate else-if construct. */%@AE@%%@NL@%
  5917. %@NL@%
  5918. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5919. %@AI@%#include %@AE@%<conio.h> %@NL@%
  5920. %@NL@%
  5921. main()%@NL@%
  5922. {%@NL@%
  5923.    char ch;%@NL@%
  5924.    printf( "Press the b key to hear a bell.\n" );%@NL@%
  5925.    ch = getch();%@NL@%
  5926.    if( ch == 'b' )%@NL@%
  5927.       printf( "Beep!\a\n" );%@NL@%
  5928.    else%@NL@%
  5929.       if( ch == '\r' )%@NL@%
  5930.          printf( "Enter\n" );%@NL@%
  5931.    else%@NL@%
  5932.       printf( "Bye bye\n" );%@NL@%
  5933. }%@NL@%
  5934. %@NL@%
  5935. %@NL@%
  5936. %@2@%%@AH@%EMPLOY1.C%@AE@%%@EH@%%@NL@%
  5937. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\EMPLOY1.C%@AE@%%@NL@%
  5938. %@NL@%
  5939. %@AB@%/* EMPLOY1.C: Demonstrate structure pointers. */%@AE@%%@NL@%
  5940. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5941. %@NL@%
  5942. %@NL@%
  5943. struct employee%@NL@%
  5944. {%@NL@%
  5945.    char name[10];%@NL@%
  5946.    int months;%@NL@%
  5947.    float wage;%@NL@%
  5948. };%@NL@%
  5949. %@NL@%
  5950. void display( struct employee *e_ptr  );%@NL@%
  5951. %@NL@%
  5952. main()%@NL@%
  5953. {%@NL@%
  5954.    static struct employee jones =%@NL@%
  5955.       {%@NL@%
  5956.       "Jones, J",%@NL@%
  5957.       77,%@NL@%
  5958.       13.68%@NL@%
  5959.       };%@NL@%
  5960. %@NL@%
  5961.    display( &jones );%@NL@%
  5962. }%@NL@%
  5963. %@NL@%
  5964. void display( struct employee *e_ptr )%@NL@%
  5965. {%@NL@%
  5966.    printf( "Name: %s\n", e_ptr->name );%@NL@%
  5967.    printf( "Months of service: %d\n", e_ptr->months );%@NL@%
  5968.    printf( "Hourly wage: %6.2f\n", e_ptr->wage );%@NL@%
  5969. }%@NL@%
  5970. %@NL@%
  5971. %@NL@%
  5972. %@2@%%@AH@%EMPLOYEE.C%@AE@%%@EH@%%@NL@%
  5973. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\EMPLOYEE.C%@AE@%%@NL@%
  5974. %@NL@%
  5975. %@AB@%/* EMPLOYEE.C: Demonstrate structures. */%@AE@%%@NL@%
  5976. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  5977. %@AI@%#include %@AE@%<string.h> %@NL@%
  5978. %@NL@%
  5979. struct employee%@NL@%
  5980. {%@NL@%
  5981.    char name[10];%@NL@%
  5982.    int months;%@NL@%
  5983.    float wage;%@NL@%
  5984. };%@NL@%
  5985. %@NL@%
  5986. void display( struct employee show );%@NL@%
  5987. %@NL@%
  5988. main()%@NL@%
  5989. {%@NL@%
  5990.    struct employee jones;%@NL@%
  5991. %@NL@%
  5992.    strcpy( jones.name, "Jones, J" );%@NL@%
  5993.    jones.months = 77;%@NL@%
  5994.    jones.wage = 13.68;%@NL@%
  5995. %@NL@%
  5996.    display( jones );%@NL@%
  5997. }%@NL@%
  5998. %@NL@%
  5999. void display( struct employee show )%@NL@%
  6000. {%@NL@%
  6001.    printf( "Name: %s\n", show.name );%@NL@%
  6002.    printf( "Months of service: %d\n", show.months );%@NL@%
  6003.    printf( "Hourly wage: %6.2f\n", show.wage );%@NL@%
  6004. }%@NL@%
  6005. %@NL@%
  6006. %@NL@%
  6007. %@2@%%@AH@%FILE1.C%@AE@%%@EH@%%@NL@%
  6008. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\FILE1.C%@AE@%%@NL@%
  6009. %@NL@%
  6010. %@AB@%/* FILE1.C: Visibility in multiple source files.%@NL@%
  6011. %@AB@%*/%@AE@%%@NL@%
  6012. %@NL@%
  6013. int chico = 20, harpo = 30;%@NL@%
  6014. extern void yonder( void );%@NL@%
  6015. %@NL@%
  6016. main()%@NL@%
  6017. {%@NL@%
  6018.    yonder();%@NL@%
  6019. }%@NL@%
  6020. %@NL@%
  6021. %@NL@%
  6022. %@2@%%@AH@%FILE2.C%@AE@%%@EH@%%@NL@%
  6023. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\FILE2.C%@AE@%%@NL@%
  6024. %@NL@%
  6025. %@AB@%/* FILE2.C: Visibility in multiple source files.%@NL@%
  6026. %@AB@%*/%@AE@%%@NL@%
  6027. %@NL@%
  6028. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6029. %@NL@%
  6030. void yonder( void )%@NL@%
  6031. {%@NL@%
  6032.    extern int chico, harpo;%@NL@%
  6033.    printf( "chico = %d, harpo = %d\n", chico, harpo );%@NL@%
  6034. }%@NL@%
  6035. %@NL@%
  6036. %@NL@%
  6037. %@2@%%@AH@%FORLOOP.C%@AE@%%@EH@%%@NL@%
  6038. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\FORLOOP.C%@AE@%%@NL@%
  6039. %@NL@%
  6040. %@AB@%/* FORLOOP.C: Demonstrate for loop. */%@AE@%%@NL@%
  6041. %@NL@%
  6042. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6043. %@NL@%
  6044. main()%@NL@%
  6045. {%@NL@%
  6046.    int test;%@NL@%
  6047.    for( test = 10; test > 0; test = test - 2 )%@NL@%
  6048.       printf( "test = %d\n", test );%@NL@%
  6049. }%@NL@%
  6050. %@NL@%
  6051. %@NL@%
  6052. %@2@%%@AH@%FORLOOP1.C%@AE@%%@EH@%%@NL@%
  6053. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\FORLOOP1.C%@AE@%%@NL@%
  6054. %@NL@%
  6055. %@AB@%/* FORLOOP1.C: Demonstrate multiple expressions. */%@AE@%%@NL@%
  6056. %@NL@%
  6057. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6058. %@NL@%
  6059. main()%@NL@%
  6060. {%@NL@%
  6061.    int a, b;%@NL@%
  6062.    for( a = 256, b = 1; b < 512; a = a / 2, b = b * 2 )%@NL@%
  6063.       printf( "a = %d  b = %d\n", a, b );%@NL@%
  6064. }%@NL@%
  6065. %@NL@%
  6066. %@NL@%
  6067. %@2@%%@AH@%FORLOOP2.C%@AE@%%@EH@%%@NL@%
  6068. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\FORLOOP2.C%@AE@%%@NL@%
  6069. %@NL@%
  6070. %@AB@%/* FORLOOP2.C: Demonstrate similarity of for and while.%@NL@%
  6071. %@AB@%*/%@AE@%%@NL@%
  6072. %@NL@%
  6073. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6074. %@NL@%
  6075. main()%@NL@%
  6076. {%@NL@%
  6077.    int count;%@NL@%
  6078. %@NL@%
  6079.    for( count = 0; count < 10; count++ )%@NL@%
  6080.       printf( "count = %d\n", count );%@NL@%
  6081. %@NL@%
  6082.    count = 0;%@NL@%
  6083.    while( count < 10 )%@NL@%
  6084.    {%@NL@%
  6085.       printf( "count = %d\n", count );%@NL@%
  6086.       count++;%@NL@%
  6087.    }%@NL@%
  6088. %@NL@%
  6089. }%@NL@%
  6090. %@NL@%
  6091. %@NL@%
  6092. %@2@%%@AH@%FUNCPTR.C%@AE@%%@EH@%%@NL@%
  6093. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\FUNCPTR.C%@AE@%%@NL@%
  6094. %@NL@%
  6095. %@AB@%/* FUNCPTR.C: Demonstrate function pointers. */%@AE@%%@NL@%
  6096. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6097. %@NL@%
  6098. main()%@NL@%
  6099. {%@NL@%
  6100.    int (*func_ptr) ();%@NL@%
  6101.    func_ptr = printf;%@NL@%
  6102.    (*func_ptr) ( "Curiouser and curiouser...\n" );%@NL@%
  6103. }%@NL@%
  6104. %@NL@%
  6105. %@NL@%
  6106. %@2@%%@AH@%FUNCPTR1.C%@AE@%%@EH@%%@NL@%
  6107. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\FUNCPTR1.C%@AE@%%@NL@%
  6108. %@NL@%
  6109. %@AB@%/* FUNCPTR1.C: Passing function pointers as arguments. */%@AE@%%@NL@%
  6110. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6111. %@NL@%
  6112. void gimme_func( void (*func_ptr) () );%@NL@%
  6113. %@NL@%
  6114. main()%@NL@%
  6115. {%@NL@%
  6116.    gimme_func( puts );%@NL@%
  6117.    gimme_func( printf );%@NL@%
  6118. }%@NL@%
  6119. %@NL@%
  6120. void gimme_func( void (*func_ptr) () )%@NL@%
  6121. {%@NL@%
  6122.    (*func_ptr) ( "Ausgezeichnet!" );%@NL@%
  6123. }%@NL@%
  6124. %@NL@%
  6125. %@NL@%
  6126. %@2@%%@AH@%GRAPHIC.C%@AE@%%@EH@%%@NL@%
  6127. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\GRAPHIC.C%@AE@%%@NL@%
  6128. %@NL@%
  6129. %@AB@%/* GRAPHIC.C: Displays every graphics mode */%@AE@%%@NL@%
  6130. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6131. %@AI@%#include %@AE@%<graph.h> %@NL@%
  6132. %@AI@%#include %@AE@%<conio.h> %@NL@%
  6133. %@NL@%
  6134. struct videoconfig screen;%@NL@%
  6135. int modes[12] =%@NL@%
  6136. {%@NL@%
  6137.    _MRES4COLOR, _MRESNOCOLOR, _HRESBW,%@NL@%
  6138.    _HERCMONO,%@NL@%
  6139.    _MRES16COLOR, _HRES16COLOR, _ERESNOCOLOR, _ERESCOLOR,%@NL@%
  6140.    _VRES2COLOR, _VRES16COLOR, _MRES256COLOR, _ORESCOLOR%@NL@%
  6141. };%@NL@%
  6142. %@NL@%
  6143. void print_menu( void );%@NL@%
  6144. void show_mode( char );%@NL@%
  6145. %@NL@%
  6146. main()%@NL@%
  6147. {%@NL@%
  6148.    char key;%@NL@%
  6149.    print_menu();%@NL@%
  6150.    while( (key = getch()) != 'x' )%@NL@%
  6151.       show_mode( key );%@NL@%
  6152. }%@NL@%
  6153. %@NL@%
  6154. void print_menu( void )%@NL@%
  6155. {%@NL@%
  6156.    _setvideomode( _DEFAULTMODE );%@NL@%
  6157.    _clearscreen( _GCLEARSCREEN );%@NL@%
  6158.    printf( "Please choose a graphics mode\nType 'x' to exit.\n\n" );%@NL@%
  6159.    printf( "0 _MRES4COLOR\n1 _MRESNOCOLOR\n2 _HRESBW\n" );%@NL@%
  6160.    printf( "3 _HERCMONO\n4 _MRES16COLOR\n5 _HRES16COLOR\n" );%@NL@%
  6161.    printf( "6 _ERESNOCOLOR\n7 _ERESCOLOR\n" );%@NL@%
  6162.    printf( "8 _VRES2COLOR\n9 _VRES16COLOR\na _MRES256COLOR\n" );%@NL@%
  6163.    printf( "b _ORESCOLOR\n" );%@NL@%
  6164. }%@NL@%
  6165. %@NL@%
  6166. void show_mode( char which )%@NL@%
  6167. {%@NL@%
  6168.    int nc, i;%@NL@%
  6169.    int height, width;%@NL@%
  6170.    int mode = which;%@NL@%
  6171. %@NL@%
  6172.    if( mode < '0' || mode > '9' )%@NL@%
  6173.       if( mode == 'a' )%@NL@%
  6174.          mode = '9' + 1;%@NL@%
  6175.       else if( mode == 'b' )%@NL@%
  6176.          mode = '9' + 2;%@NL@%
  6177.       else%@NL@%
  6178.          return;%@NL@%
  6179. %@NL@%
  6180.    if( _setvideomode( modes[mode - '0'] ) )%@NL@%
  6181.    {%@NL@%
  6182.       _getvideoconfig( &screen );%@NL@%
  6183.       nc = screen.numcolors;%@NL@%
  6184.       width = screen.numxpixels/nc;%@NL@%
  6185.       height = screen.numypixels/2;%@NL@%
  6186.       for( i = 0; i < nc; i++ )%@NL@%
  6187.       {%@NL@%
  6188.          _setcolor( i );%@NL@%
  6189.          _rectangle( _GFILLINTERIOR, i * width, 0, (i + 1) * width, height );%@NL@%
  6190.       }%@NL@%
  6191.    }%@NL@%
  6192.    else%@NL@%
  6193.    {%@NL@%
  6194.       printf( " \nVideo mode %c is not available.\n", which);%@NL@%
  6195.       printf( "Please press a key.\n" );%@NL@%
  6196.    }%@NL@%
  6197.    getch();%@NL@%
  6198.    _setvideomode( _DEFAULTMODE );%@NL@%
  6199.    print_menu();%@NL@%
  6200. }%@NL@%
  6201. %@NL@%
  6202. %@NL@%
  6203. %@2@%%@AH@%HORIZON.C%@AE@%%@EH@%%@NL@%
  6204. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\HORIZON.C%@AE@%%@NL@%
  6205. %@NL@%
  6206. %@AB@%/* HORIZON.C: VGA graphics with cycling of 256 colors */%@AE@%%@NL@%
  6207. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6208. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  6209. %@AI@%#include %@AE@%<conio.h> %@NL@%
  6210. %@AI@%#include %@AE@%<graph.h> %@NL@%
  6211. %@NL@%
  6212. %@AI@%#define %@AE@%RED 0x0000003FL %@NL@%
  6213. %@AI@%#define %@AE@%GRN 0x00003F00L %@NL@%
  6214. %@AI@%#define %@AE@%BLU 0x003F0000L %@NL@%
  6215. %@AI@%#define %@AE@%WHT 0x003F3F3FL %@NL@%
  6216. %@AI@%#define %@AE@%STEP 21 %@NL@%
  6217. %@NL@%
  6218. struct videoconfig screen;%@NL@%
  6219. long int rainbow[512];%@NL@%
  6220. %@NL@%
  6221. main()%@NL@%
  6222. {%@NL@%
  6223.    int i;%@NL@%
  6224.    long int col, gray;%@NL@%
  6225. %@NL@%
  6226.    if( _setvideomode( _MRES256COLOR ) == 0 )%@NL@%
  6227.    {%@NL@%
  6228.       printf("This program requires a VGA card.\n" );%@NL@%
  6229.       exit( 0 );%@NL@%
  6230.    }%@NL@%
  6231.    for( col = 0; col < 64; col++ )%@NL@%
  6232.    {%@NL@%
  6233.       gray = col | (col << 8) | (col << 16);%@NL@%
  6234.       rainbow[col] = rainbow[col + 256] = BLU & gray;%@NL@%
  6235.       rainbow[col + 64] = rainbow[col + 64 + 256] = BLU | gray;%@NL@%
  6236.       rainbow[col + 128] = rainbow[col + 128 + 256] = RED | (WHT & ~gray);%@NL@%
  6237.       rainbow[col + 192] = rainbow[col + 192 + 256] = RED & ~gray;%@NL@%
  6238.    }%@NL@%
  6239.    _setvieworg( 160, 85 );%@NL@%
  6240. %@NL@%
  6241.    for( i = 0; i < 255; i++ )%@NL@%
  6242.    {%@NL@%
  6243.       _setcolor( 255 - i );%@NL@%
  6244.       _moveto( i, i - 255 );%@NL@%
  6245.       _lineto( -i, 255 - i );%@NL@%
  6246.       _moveto( -i, i - 255 );%@NL@%
  6247.       _lineto( i, 255 - i );%@NL@%
  6248.       _ellipse( _GBORDER, -i, -i / 2, i, i / 2 );%@NL@%
  6249.    }%@NL@%
  6250.    for( i = 0; !kbhit(); i += STEP, i %= 256 )%@NL@%
  6251.       _remapallpalette( &(rainbow[i]) );%@NL@%
  6252. %@NL@%
  6253.    _setvideomode( _DEFAULTMODE );%@NL@%
  6254. }%@NL@%
  6255. %@NL@%
  6256. %@NL@%
  6257. %@2@%%@AH@%IFF.C%@AE@%%@EH@%%@NL@%
  6258. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\IFF.C%@AE@%%@NL@%
  6259. %@NL@%
  6260. %@AB@%/* IFF.C: Demonstrate if statement. */%@AE@%%@NL@%
  6261. %@NL@%
  6262. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6263. %@AI@%#include %@AE@%<conio.h> %@NL@%
  6264. %@NL@%
  6265. main()%@NL@%
  6266. {%@NL@%
  6267.    char ch;%@NL@%
  6268.    printf( "Press the b key to hear a bell.\n" );%@NL@%
  6269.    ch = getche();%@NL@%
  6270.    if( ch == 'b' )%@NL@%
  6271.       printf( "Beep!\a\n" );%@NL@%
  6272. }%@NL@%
  6273. %@NL@%
  6274. %@NL@%
  6275. %@2@%%@AH@%INPUT.C%@AE@%%@EH@%%@NL@%
  6276. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\INPUT.C%@AE@%%@NL@%
  6277. %@NL@%
  6278. %@AB@%/* INPUT.C: Reads keyboard. */%@AE@%%@NL@%
  6279. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6280. %@AI@%#include %@AE@%<conio.h> %@NL@%
  6281. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  6282. %@NL@%
  6283. main()%@NL@%
  6284. {%@NL@%
  6285.    int num, c;%@NL@%
  6286.    char name[80];%@NL@%
  6287.    float rb;%@NL@%
  6288. %@NL@%
  6289.    puts( "** Type \"Name:\" and your name" );%@NL@%
  6290.    scanf( "Name: %40s", name );%@NL@%
  6291.    printf( "** You typed this:\n%s", name );%@NL@%
  6292.    puts( "\n\n** Try again, with the gets function." );%@NL@%
  6293.    fflush( stdin );%@NL@%
  6294.    gets( name );%@NL@%
  6295.    printf( "** You typed this:\n%s\n", name );%@NL@%
  6296. %@NL@%
  6297.    printf( "\n** Now type an integer.\n" );%@NL@%
  6298.    scanf( "%i", &num );%@NL@%
  6299.    sprintf( name, "** You typed this number: %i\n", num );%@NL@%
  6300.    puts( name );%@NL@%
  6301. %@NL@%
  6302.    fflush( stdin );%@NL@%
  6303.    printf( "** Enter a floating-point value.\n" );%@NL@%
  6304.    scanf( "%f", &rb );%@NL@%
  6305.    printf( "** The answer is %f or %e\n", rb, rb );%@NL@%
  6306. %@NL@%
  6307.    printf( "** Continue? Y or N\n" );%@NL@%
  6308. %@NL@%
  6309.    do%@NL@%
  6310.    {%@NL@%
  6311.       c = getch();%@NL@%
  6312.       c = tolower( c );%@NL@%
  6313.    } while( c != 'y' && c != 'n' );%@NL@%
  6314. }%@NL@%
  6315. %@NL@%
  6316. %@NL@%
  6317. %@2@%%@AH@%MACRO.C%@AE@%%@EH@%%@NL@%
  6318. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\MACRO.C%@AE@%%@NL@%
  6319. %@NL@%
  6320. %@AB@%/* MACRO.C: Demonstrate macros. */%@AE@%%@NL@%
  6321. %@NL@%
  6322. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6323. %@AI@%#define %@AE@%ABS(value)  ( (value) >= 0 ? (value) : -(value) ) %@NL@%
  6324. %@NL@%
  6325. main()%@NL@%
  6326. {%@NL@%
  6327.    int val = -20;%@NL@%
  6328.    printf( "result = %d\n", ABS(val) );%@NL@%
  6329. }%@NL@%
  6330. %@NL@%
  6331. %@NL@%
  6332. %@2@%%@AH@%NFORMAT.C%@AE@%%@EH@%%@NL@%
  6333. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\NFORMAT.C%@AE@%%@NL@%
  6334. %@NL@%
  6335. %@AB@%/* NFORMAT.C: Prints numbers and a string. */%@AE@%%@NL@%
  6336. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6337. %@AI@%#include %@AE@%<string.h> %@NL@%
  6338. %@NL@%
  6339. main()%@NL@%
  6340. {%@NL@%
  6341.    int    a = -765,%@NL@%
  6342.           b = 1,%@NL@%
  6343.           c = 44000,%@NL@%
  6344.           d = 33;%@NL@%
  6345.    float  e = 1.33E8,%@NL@%
  6346.           f = -0.1234567,%@NL@%
  6347.           g = 12345.6789,%@NL@%
  6348.           h = 1.0;%@NL@%
  6349.    char   i[80];%@NL@%
  6350. %@NL@%
  6351.    strcpy( i, "word 1, word 2, word 3, word 4, word 5" );%@NL@%
  6352. %@NL@%
  6353.    printf( "Unformatted:\n%d %d %d %d\n", a, b, c, d );%@NL@%
  6354.    printf( "%f %f %f %f\n", e, f, g, h );%@NL@%
  6355.    printf( "%s\n", i );%@NL@%
  6356. }%@NL@%
  6357. %@NL@%
  6358. %@NL@%
  6359. %@2@%%@AH@%NOT.C%@AE@%%@EH@%%@NL@%
  6360. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\NOT.C%@AE@%%@NL@%
  6361. %@NL@%
  6362. %@AB@%/* NOT.C: Demonstrate logical NOT operator. */%@AE@%%@NL@%
  6363. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6364. %@NL@%
  6365. main()%@NL@%
  6366. {%@NL@%
  6367.   int val = 0;%@NL@%
  6368.   if( !val )%@NL@%
  6369.      printf( "val is zero" );%@NL@%
  6370. }%@NL@%
  6371. %@NL@%
  6372. %@NL@%
  6373. %@2@%%@AH@%OLDSTYLE.C%@AE@%%@EH@%%@NL@%
  6374. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\OLDSTYLE.C%@AE@%%@NL@%
  6375. %@NL@%
  6376. %@AB@%/* OLDSTYLE.C: Old-style function. */%@AE@%%@NL@%
  6377. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6378. %@AI@%#define %@AE@%PI 3.14 %@NL@%
  6379. %@NL@%
  6380. float sphere();%@NL@%
  6381. %@NL@%
  6382. main()%@NL@%
  6383. {%@NL@%
  6384.    float volume;%@NL@%
  6385.    int radius = 3;%@NL@%
  6386.    volume = sphere( radius );%@NL@%
  6387.    printf( "Volume: %f\n", volume );%@NL@%
  6388. }%@NL@%
  6389. %@NL@%
  6390. float sphere( rad )%@NL@%
  6391. int rad;%@NL@%
  6392. {%@NL@%
  6393.    float result;%@NL@%
  6394.    result = rad * rad * rad;%@NL@%
  6395.    result = 4 * PI * result;%@NL@%
  6396.    result = result / 3;%@NL@%
  6397.    return result;%@NL@%
  6398. }%@NL@%
  6399. %@NL@%
  6400. %@NL@%
  6401. %@2@%%@AH@%PARRAY.C%@AE@%%@EH@%%@NL@%
  6402. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PARRAY.C%@AE@%%@NL@%
  6403. %@NL@%
  6404. %@AB@%/* PARRAY.C: Demonstrate pointer to array. */%@AE@%%@NL@%
  6405. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6406. %@NL@%
  6407. int i_array[] = { 25, 300, 2, 12 };%@NL@%
  6408. %@NL@%
  6409. main()%@NL@%
  6410. {%@NL@%
  6411.    int *ptr;%@NL@%
  6412.    int count;%@NL@%
  6413.    ptr = &i_array[0];%@NL@%
  6414.    for( count = 0; count < 4 ; count++ )%@NL@%
  6415.    {%@NL@%
  6416.       printf( "i_array[%d] = %d\n", count, *ptr );%@NL@%
  6417.       ptr++;%@NL@%
  6418.    }%@NL@%
  6419. }%@NL@%
  6420. %@NL@%
  6421. %@NL@%
  6422. %@2@%%@AH@%PARRAY1.C%@AE@%%@EH@%%@NL@%
  6423. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PARRAY1.C%@AE@%%@NL@%
  6424. %@NL@%
  6425. %@AB@%/* PARRAY1.C: Compact version of PARRAY.C. */%@AE@%%@NL@%
  6426. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6427. %@NL@%
  6428. int i_array[] = { 25, 300, 2, 12 };%@NL@%
  6429. %@NL@%
  6430. main()%@NL@%
  6431. {%@NL@%
  6432.    int count;%@NL@%
  6433.    int *ptr = i_array;%@NL@%
  6434.    for( count = 0; count < 4 ; count++ )%@NL@%
  6435.       printf( "i_array[%d] = %d\n", count, *ptr++ );%@NL@%
  6436. }%@NL@%
  6437. %@NL@%
  6438. %@NL@%
  6439. %@2@%%@AH@%PFUNC.C%@AE@%%@EH@%%@NL@%
  6440. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PFUNC.C%@AE@%%@NL@%
  6441. %@NL@%
  6442. %@AB@%/* PFUNC.C: Passing pointers to a function. */%@AE@%%@NL@%
  6443. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6444. %@NL@%
  6445. void swap( int *ptr1, int *ptr2 );%@NL@%
  6446. %@NL@%
  6447. main()%@NL@%
  6448. {%@NL@%
  6449.    int first = 1, second = 3;%@NL@%
  6450.    int *ptr = &second;%@NL@%
  6451.    printf( "first: %d  second: %d\n", first, *ptr );%@NL@%
  6452.    swap( &first, ptr );%@NL@%
  6453.    printf( "first: %d  second: %d\n", first, *ptr );%@NL@%
  6454. }%@NL@%
  6455. %@NL@%
  6456. void swap( int *ptr1, int *ptr2 )%@NL@%
  6457. {%@NL@%
  6458.    int temp;%@NL@%
  6459.    temp = *ptr1;%@NL@%
  6460.    *ptr1 = *ptr2;%@NL@%
  6461.    *ptr2 = temp;%@NL@%
  6462. }%@NL@%
  6463. %@NL@%
  6464. %@NL@%
  6465. %@2@%%@AH@%PIE.C%@AE@%%@EH@%%@NL@%
  6466. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PIE.C%@AE@%%@NL@%
  6467. %@NL@%
  6468. %@AB@%/* PIE.C:  Create sample pie chart.  */%@AE@%%@NL@%
  6469. %@AI@%#include %@AE@%<conio.h> %@NL@%
  6470. %@AI@%#include %@AE@%<string.h> %@NL@%
  6471. %@AI@%#include %@AE@%<graph.h> %@NL@%
  6472. %@AI@%#include %@AE@%<pgchart.h> %@NL@%
  6473. %@NL@%
  6474. %@AI@%#define %@AE@%MONTHS 12 %@NL@%
  6475. %@NL@%
  6476. typedef enum {FALSE, TRUE} boolean;%@NL@%
  6477. %@NL@%
  6478. float far value[MONTHS] = %@NL@%
  6479. {%@NL@%
  6480.    33.0, 27.0, 42.0, 64.0,106.0,157.0,%@NL@%
  6481.   182.0,217.0,128.0, 62.0, 43.0, 36.0%@NL@%
  6482. };%@NL@%
  6483. char far *category[MONTHS] = %@NL@%
  6484. {%@NL@%
  6485.   "Jan", "Feb", "Mar", "Apr",%@NL@%
  6486.   "May", "Jun", "Jly", "Aug",%@NL@%
  6487.   "Sep", "Oct", "Nov", "Dec"%@NL@%
  6488. };%@NL@%
  6489. short far explode[MONTHS] = {0};%@NL@%
  6490. %@NL@%
  6491. main()%@NL@%
  6492. {%@NL@%
  6493.   chartenv env;%@NL@%
  6494.   int mode = _VRES16COLOR;%@NL@%
  6495. %@NL@%
  6496.   %@AB@%/* Set highest video mode available */%@AE@%%@NL@%
  6497.   while(!_setvideomode( mode ))%@NL@%
  6498.      mode--;%@NL@%
  6499.   if(mode == _TEXTMONO)%@NL@%
  6500.      return( 0 );%@NL@%
  6501. %@NL@%
  6502.   %@AB@%/* Initialize chart library and a default pie chart */%@AE@%%@NL@%
  6503.   _pg_initchart();%@NL@%
  6504.   _pg_defaultchart( &env, _PG_PIECHART, _PG_PERCENT );%@NL@%
  6505. %@NL@%
  6506.   %@AB@%/* Add titles and some chart options */%@AE@%%@NL@%
  6507.   strcpy( env.maintitle.title, "Good Neighbor Grocery" );%@NL@%
  6508.   env.maintitle.titlecolor = 6;%@NL@%
  6509.   env.maintitle.justify = _PG_RIGHT;%@NL@%
  6510.   strcpy( env.subtitle.title, "Orange Juice Sales" ); %@NL@%
  6511.   env.subtitle.titlecolor = 6;%@NL@%
  6512.   env.subtitle.justify = _PG_RIGHT;%@NL@%
  6513.   env.chartwindow.border = FALSE;%@NL@%
  6514. %@NL@%
  6515.   %@AB@%/* Parameters for call to _pg_chartpie are:%@NL@%
  6516. %@AB@%   *  %@NL@%
  6517. %@AB@%   *    env        - Environment variable%@NL@%
  6518. %@AB@%   *    category   - Category labels%@NL@%
  6519. %@AB@%   *    value      - Data to chart%@NL@%
  6520. %@AB@%   *    explode    - Separated pieces%@NL@%
  6521. %@AB@%   *    MONTHS     - Number of data values%@NL@%
  6522. %@AB@%   */%@AE@%%@NL@%
  6523. %@NL@%
  6524.   if(_pg_chartpie( &env, category, value,%@NL@%
  6525.                     explode, MONTHS ))%@NL@%
  6526.   {%@NL@%
  6527.      _setvideomode( _DEFAULTMODE );%@NL@%
  6528.      _outtext( "Error:  can't draw chart" );%@NL@%
  6529.   }%@NL@%
  6530.   else%@NL@%
  6531.   {%@NL@%
  6532.      getch(); %@NL@%
  6533.      _setvideomode( _DEFAULTMODE );%@NL@%
  6534.   }%@NL@%
  6535.   return( 0 );%@NL@%
  6536. }%@NL@%
  6537. %@NL@%
  6538. %@NL@%
  6539. %@2@%%@AH@%POINTER.C%@AE@%%@EH@%%@NL@%
  6540. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\POINTER.C%@AE@%%@NL@%
  6541. %@NL@%
  6542. %@AB@%/* POINTER.C: Demonstrate pointer basics. */%@AE@%%@NL@%
  6543. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6544. %@NL@%
  6545. main()%@NL@%
  6546. {%@NL@%
  6547.    int val = 25;%@NL@%
  6548.    int *ptr;%@NL@%
  6549.    ptr = &val;%@NL@%
  6550.    printf( " val = %d\n", val );%@NL@%
  6551.    printf( "*ptr = %d\n\n", *ptr );%@NL@%
  6552.    printf( "&val = %u\n", &val );%@NL@%
  6553.    printf( " ptr = %d\n", ptr );%@NL@%
  6554. }%@NL@%
  6555. %@NL@%
  6556. %@NL@%
  6557. %@2@%%@AH@%POWER2.C%@AE@%%@EH@%%@NL@%
  6558. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\POWER2.C%@AE@%%@NL@%
  6559. %@NL@%
  6560. %@AB@%/* POWER2.C */%@AE@%%@NL@%
  6561. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6562. %@NL@%
  6563. int power2( int num, int power );%@NL@%
  6564. %@NL@%
  6565. void main( void )%@NL@%
  6566. {%@NL@%
  6567.    printf( "3 times 2 to the power of 5 is %d\n", \%@NL@%
  6568.            power2( 3, 5) );%@NL@%
  6569. }%@NL@%
  6570. %@NL@%
  6571. int power2( int num, int power )%@NL@%
  6572. {%@NL@%
  6573.    _asm%@NL@%
  6574.    {%@NL@%
  6575.       mov ax, num    ; Get first argument%@NL@%
  6576.       mov cx, power  ; Get second argument%@NL@%
  6577.       shl ax, cl     ; AX = AX * ( 2 to the power of CL )%@NL@%
  6578.    }%@NL@%
  6579.    %@AB@%/* Return with result in AX */%@AE@%%@NL@%
  6580. }%@NL@%
  6581. %@NL@%
  6582. %@NL@%
  6583. %@2@%%@AH@%PRTESC.C%@AE@%%@EH@%%@NL@%
  6584. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PRTESC.C%@AE@%%@NL@%
  6585. %@NL@%
  6586. %@AB@%/* PRTESC.C: Prints escape characters \",\n, and \t. */%@AE@%%@NL@%
  6587. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6588. %@AI@%#include %@AE@%<string.h> %@NL@%
  6589. %@NL@%
  6590. main()%@NL@%
  6591. {%@NL@%
  6592.    char b[80];%@NL@%
  6593.    int i,j;%@NL@%
  6594. %@NL@%
  6595.    strcpy( b, "and seven years ago\n" );%@NL@%
  6596.    printf( "\"Four score\n" );%@NL@%
  6597.    printf( b );%@NL@%
  6598.    printf( "\tone tab\n\t\ttwo tabs\n\t\t\tthree tabs\n" );%@NL@%
  6599.    i = sizeof( b );%@NL@%
  6600.    j = strlen( b );%@NL@%
  6601.    printf( "Size is %d\nLength is %d.\n", i, j );%@NL@%
  6602. }%@NL@%
  6603. %@NL@%
  6604. %@NL@%
  6605. %@2@%%@AH@%PRTSTR.C%@AE@%%@EH@%%@NL@%
  6606. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PRTSTR.C%@AE@%%@NL@%
  6607. %@NL@%
  6608. %@AB@%/* PRTSTR.C: Prints strings. */%@AE@%%@NL@%
  6609. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6610. %@AI@%#include %@AE@%<string.h> %@NL@%
  6611. %@NL@%
  6612. main()%@NL@%
  6613. {%@NL@%
  6614.    char aline[80], more[80];%@NL@%
  6615.    char *strptr;%@NL@%
  6616. %@NL@%
  6617.    %@AB@%/* aline = "Another line."; */%@AE@%%@NL@%
  6618.    %@AB@%/* Note: This causes a compiler error */%@AE@%%@NL@%
  6619. %@NL@%
  6620.    strcpy( aline, "Another line." );%@NL@%
  6621.    strcpy( more, aline );%@NL@%
  6622.    strptr = aline;%@NL@%
  6623.    strcat( aline, "dog" );%@NL@%
  6624.    printf( "A line of text." );%@NL@%
  6625.    printf( aline );%@NL@%
  6626.    printf( more );%@NL@%
  6627.    printf( strptr );%@NL@%
  6628. }%@NL@%
  6629. %@NL@%
  6630. %@NL@%
  6631. %@2@%%@AH@%PSTRING.C%@AE@%%@EH@%%@NL@%
  6632. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PSTRING.C%@AE@%%@NL@%
  6633. %@NL@%
  6634. %@AB@%/* PSTRING.C: Demonstrate pointer to a string. */%@AE@%%@NL@%
  6635. %@NL@%
  6636. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6637. %@NL@%
  6638. main()%@NL@%
  6639. {%@NL@%
  6640.    int count;%@NL@%
  6641.    static char name[] = "john";%@NL@%
  6642.    char *ptr = name;%@NL@%
  6643.    for( count = 0; count < 4; count++ )%@NL@%
  6644.    {%@NL@%
  6645.       printf( "name[%d]: %c\n", count, *ptr++ );%@NL@%
  6646.    }%@NL@%
  6647. }%@NL@%
  6648. %@NL@%
  6649. %@NL@%
  6650. %@2@%%@AH@%PSTRING1.C%@AE@%%@EH@%%@NL@%
  6651. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PSTRING1.C%@AE@%%@NL@%
  6652. %@NL@%
  6653. %@AB@%/* PSTRING1.C: Look for null at string's end.  */%@AE@%%@NL@%
  6654. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6655. %@NL@%
  6656. main()%@NL@%
  6657. {%@NL@%
  6658.    char name[] = "john";%@NL@%
  6659.    char *ptr = name;%@NL@%
  6660.    while( *ptr )%@NL@%
  6661.       printf( "*ptr = %c\n", *ptr++ );%@NL@%
  6662. }%@NL@%
  6663. %@NL@%
  6664. %@NL@%
  6665. %@2@%%@AH@%PSTRING2.C%@AE@%%@EH@%%@NL@%
  6666. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PSTRING2.C%@AE@%%@NL@%
  6667. %@NL@%
  6668. %@AB@%/* PSTRING2.C: Demonstrate strings and array notation. */%@AE@%%@NL@%
  6669. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6670. %@AI@%#include %@AE@%<string.h> %@NL@%
  6671. %@NL@%
  6672. main()%@NL@%
  6673. {%@NL@%
  6674.    int count;%@NL@%
  6675.    char name[] = "john";%@NL@%
  6676.    for( count = 0; count < strlen( name ); count++ )%@NL@%
  6677.       printf( "name[%d]: %c\n", count, name[count] );%@NL@%
  6678. }%@NL@%
  6679. %@NL@%
  6680. %@NL@%
  6681. %@2@%%@AH@%PSTRING3.C%@AE@%%@EH@%%@NL@%
  6682. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PSTRING3.C%@AE@%%@NL@%
  6683. %@NL@%
  6684. %@AB@%/* PSTRING3.C: Strings and pointer notation.  */%@AE@%%@NL@%
  6685. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6686. %@AI@%#include %@AE@%<string.h> %@NL@%
  6687. %@NL@%
  6688. main()%@NL@%
  6689. {%@NL@%
  6690.    int count;%@NL@%
  6691.    char name[] = "john";%@NL@%
  6692.    for( count = 0; count < strlen( name ); count++ )%@NL@%
  6693.       printf( "*(name+%d) = %c\n", count, *(name+count) );%@NL@%
  6694. }%@NL@%
  6695. %@NL@%
  6696. %@NL@%
  6697. %@2@%%@AH@%PTRPTR.C%@AE@%%@EH@%%@NL@%
  6698. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\PTRPTR.C%@AE@%%@NL@%
  6699. %@NL@%
  6700. %@AB@%/* PTRPTR.C: Demonstrate a pointer to a pointer. */%@AE@%%@NL@%
  6701. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6702. %@NL@%
  6703. main()%@NL@%
  6704. {%@NL@%
  6705.    int val = 501;%@NL@%
  6706.    int *ptr = &val;%@NL@%
  6707.    int **ptr_ptr = &ptr;%@NL@%
  6708.    printf( "val = %d\n", **ptr_ptr );%@NL@%
  6709. }%@NL@%
  6710. %@NL@%
  6711. %@NL@%
  6712. %@2@%%@AH@%QCSORT.C%@AE@%%@EH@%%@NL@%
  6713. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\QCSORT.C%@AE@%%@NL@%
  6714. %@NL@%
  6715. %@AB@%/* QCSORT.C: Demonstrate sorting array of pointers. */%@AE@%%@NL@%
  6716. %@NL@%
  6717. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6718. %@AI@%#define %@AE@%SIZE 4 %@NL@%
  6719. %@NL@%
  6720. void sort( int size, double *p[] );%@NL@%
  6721. void show( int size, double *p[], double dd[] );%@NL@%
  6722. %@NL@%
  6723. main()%@NL@%
  6724. {%@NL@%
  6725.    int x;%@NL@%
  6726.    double d[] = { 3.333, 1.111, 2.222, 4.444 };%@NL@%
  6727.    double *d_ptr[SIZE];%@NL@%
  6728.    for( x = 0; x < SIZE; x++ )%@NL@%
  6729.       d_ptr[x] = &d[x];%@NL@%
  6730.    show( SIZE, d_ptr, d );%@NL@%
  6731.    sort( SIZE, d_ptr );%@NL@%
  6732.    show( SIZE, d_ptr, d );%@NL@%
  6733. }%@NL@%
  6734. %@NL@%
  6735. void sort( int size, double *p[] )%@NL@%
  6736. {%@NL@%
  6737.    int x, x1;%@NL@%
  6738.    double *temp;%@NL@%
  6739.    for( x = 0; x < size - 1; x++ )%@NL@%
  6740.       for( x1 = x + 1; x1 < size; x1++ )%@NL@%
  6741.       {%@NL@%
  6742.          if( *p[x] > *p[x1] )%@NL@%
  6743.          {%@NL@%
  6744.             temp = p[x1];%@NL@%
  6745.             p[x1] = p[x];%@NL@%
  6746.             p[x] = temp;%@NL@%
  6747.          }%@NL@%
  6748.       }%@NL@%
  6749. }%@NL@%
  6750. %@NL@%
  6751. void show( int size, double *p[], double dd[] )%@NL@%
  6752. {%@NL@%
  6753.    int x;%@NL@%
  6754.    printf( "------------------------" );%@NL@%
  6755.    printf( "------------------------\n" );%@NL@%
  6756.    for( x = 0; x < size; x++ )%@NL@%
  6757.    {%@NL@%
  6758.       printf( "*d_ptr[%d] = %1.3f   ", x, *p[x]);%@NL@%
  6759.       printf( "d_ptr[%d] = %u ", x, p[x]);%@NL@%
  6760.       printf( "  d[%d] = %1.3f\n", x, dd[x] );%@NL@%
  6761.    }%@NL@%
  6762. }%@NL@%
  6763. %@NL@%
  6764. %@NL@%
  6765. %@2@%%@AH@%QCSORT1.C%@AE@%%@EH@%%@NL@%
  6766. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\QCSORT1.C%@AE@%%@NL@%
  6767. %@NL@%
  6768. %@AB@%/* QCSORT1.C: Demonstrate sort with pointer notation. */%@AE@%%@NL@%
  6769. %@NL@%
  6770. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6771. %@AI@%#define %@AE@%SIZE 4 %@NL@%
  6772. %@NL@%
  6773. void sort( int size, double **p );%@NL@%
  6774. void show( int size, double **p, double dd[] );%@NL@%
  6775. %@NL@%
  6776. main()%@NL@%
  6777. {%@NL@%
  6778.    int x;%@NL@%
  6779.    double d[] = { 3.333, 1.111, 2.222, 4.444 };%@NL@%
  6780.    double *d_ptr[SIZE];%@NL@%
  6781.    for( x = 0; x < SIZE; x++ )%@NL@%
  6782.       d_ptr[x] = &d[x];%@NL@%
  6783.    show( SIZE, d_ptr, d );%@NL@%
  6784.    sort( SIZE, d_ptr );%@NL@%
  6785.    show( SIZE, d_ptr, d );%@NL@%
  6786. }%@NL@%
  6787. %@NL@%
  6788. void sort( int size, double **p )%@NL@%
  6789. {%@NL@%
  6790.    int x, x1;%@NL@%
  6791.    double *temp;%@NL@%
  6792.    for( x = 0; x < size - 1; x++ )%@NL@%
  6793.       for( x1 = x + 1; x1 < size; x1++ )%@NL@%
  6794.       {%@NL@%
  6795.          if( **(p+x) > **(p+x1) )%@NL@%
  6796.          {%@NL@%
  6797.             temp = *(p+x1);%@NL@%
  6798.             *(p+x1) = *(p+x);%@NL@%
  6799.             *(p+x) = temp;%@NL@%
  6800.          }%@NL@%
  6801.       }%@NL@%
  6802. }%@NL@%
  6803. %@NL@%
  6804. void show( int size, double **p, double dd[] )%@NL@%
  6805. {%@NL@%
  6806.    int x;%@NL@%
  6807.    printf( "------------------------" );%@NL@%
  6808.    printf( "------------------------\n" );%@NL@%
  6809.    for( x = 0; x < size; x++ )%@NL@%
  6810.    {%@NL@%
  6811.       printf( "*d_ptr[%d] = %1.3f   ", x, **(p+x) );%@NL@%
  6812.       printf( "d_ptr[%d] = %u ", x, *(p+x) );%@NL@%
  6813.       printf( "  d[%d] = %1.3f\n", x, dd[x] );%@NL@%
  6814.    }%@NL@%
  6815. }%@NL@%
  6816. %@NL@%
  6817. %@NL@%
  6818. %@2@%%@AH@%RDFILE.C%@AE@%%@EH@%%@NL@%
  6819. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\RDFILE.C%@AE@%%@NL@%
  6820. %@NL@%
  6821. %@AB@%/* RDFILE.C: Reads a file and prints characters to the screen. */%@AE@%%@NL@%
  6822. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6823. %@NL@%
  6824. main()%@NL@%
  6825. {%@NL@%
  6826.    int c;%@NL@%
  6827.    FILE *fp;%@NL@%
  6828. %@NL@%
  6829.    if( fp = fopen( "c:\\testfile.asc", "rb" ) )%@NL@%
  6830.    {%@NL@%
  6831.       while( (c = fgetc( fp )) != EOF )%@NL@%
  6832.          printf( " %c\t%d\n", c, c );%@NL@%
  6833.       printf( "\nEnd of file marker: %d", c );%@NL@%
  6834.       fclose( fp );%@NL@%
  6835.    }%@NL@%
  6836.    else%@NL@%
  6837.       printf( "Error in opening file\n" );%@NL@%
  6838. }%@NL@%
  6839. %@NL@%
  6840. %@NL@%
  6841. %@2@%%@AH@%REALG.C%@AE@%%@EH@%%@NL@%
  6842. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\REALG.C%@AE@%%@NL@%
  6843. %@NL@%
  6844. %@AB@%/* REALG.C: Example of real-coordinate graphics */%@AE@%%@NL@%
  6845. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6846. %@AI@%#include %@AE@%<conio.h> %@NL@%
  6847. %@AI@%#include %@AE@%<graph.h> %@NL@%
  6848. %@NL@%
  6849. %@AI@%#define %@AE@%TRUE 1 %@NL@%
  6850. %@AI@%#define %@AE@%FALSE 0 %@NL@%
  6851. %@NL@%
  6852. int four_colors( void );%@NL@%
  6853. void three_graphs( void );%@NL@%
  6854. void grid_shape( void );%@NL@%
  6855. %@NL@%
  6856. int halfx, halfy;%@NL@%
  6857. struct videoconfig screen;%@NL@%
  6858. double bananas[] =%@NL@%
  6859. {   -0.3, -0.2, -0.224, -0.1, -0.5, +0.21, +2.9,%@NL@%
  6860.    +0.3, +0.2, 0.0, -0.885, -1.1, -0.3, -0.2,%@NL@%
  6861.    +.001, +.005, +0.14, 0.0, -0.9, -0.13, +0.3%@NL@%
  6862. };%@NL@%
  6863. %@NL@%
  6864. main()%@NL@%
  6865. {%@NL@%
  6866.    if( four_colors() )%@NL@%
  6867.       three_graphs();%@NL@%
  6868.    else%@NL@%
  6869.       printf( "This program requires a CGA, EGA, or VGA graphics card.\n" );%@NL@%
  6870. }%@NL@%
  6871. %@NL@%
  6872. int four_colors( void )%@NL@%
  6873. {%@NL@%
  6874.    _getvideoconfig( &screen );%@NL@%
  6875.    switch( screen.adapter )%@NL@%
  6876.    {%@NL@%
  6877.       case _CGA:%@NL@%
  6878.       case _OCGA:%@NL@%
  6879.          _setvideomode( _MRES4COLOR );%@NL@%
  6880.          break;%@NL@%
  6881.       case _EGA:%@NL@%
  6882.       case _OEGA:%@NL@%
  6883.          _setvideomode( _ERESCOLOR );%@NL@%
  6884.          break;%@NL@%
  6885.       case _VGA:%@NL@%
  6886.       case _OVGA:%@NL@%
  6887.          _setvideomode( _VRES16COLOR );%@NL@%
  6888.          break;%@NL@%
  6889.       default:%@NL@%
  6890.          return( FALSE );%@NL@%
  6891.    }%@NL@%
  6892.    _getvideoconfig( &screen );%@NL@%
  6893.    return( TRUE );%@NL@%
  6894. }%@NL@%
  6895. %@NL@%
  6896. void three_graphs( void )%@NL@%
  6897. {%@NL@%
  6898.    int xwidth, yheight, cols, rows;%@NL@%
  6899.    struct _wxycoord upleft, botright;%@NL@%
  6900. %@NL@%
  6901.    _clearscreen( _GCLEARSCREEN );%@NL@%
  6902.    xwidth = screen.numxpixels;%@NL@%
  6903.    yheight = screen.numypixels;%@NL@%
  6904.    halfx = xwidth/2;%@NL@%
  6905.    halfy = yheight/2;%@NL@%
  6906.    cols = screen.numtextcols;%@NL@%
  6907.    rows = screen.numtextrows;%@NL@%
  6908.    %@NL@%
  6909.    %@AB@%/* first window */%@AE@%%@NL@%
  6910.    _setviewport( 0, 0, halfx-1, halfy-1 );%@NL@%
  6911.    _settextwindow( 1, 1, rows/2, cols/2 );%@NL@%
  6912.    _setwindow( FALSE, -2.0, -2.0, 2.0, 2.0 );%@NL@%
  6913.    grid_shape();%@NL@%
  6914.    _rectangle( _GBORDER, 0, 0, halfx-1, halfy-1 );%@NL@%
  6915.    %@NL@%
  6916.    %@AB@%/* second window */%@AE@%%@NL@%
  6917.    _setviewport( halfx, 0, xwidth-1, halfy-1 );%@NL@%
  6918.    _settextwindow( 1, cols/2+1, rows/2, cols );%@NL@%
  6919.    _setwindow( FALSE, -3.0, -3.0, 3.0, 3.0 );%@NL@%
  6920.    grid_shape();%@NL@%
  6921.    _rectangle_w( _GBORDER, -3.0, -3.0, 3.0, 3.0 );   %@NL@%
  6922.    %@NL@%
  6923.    %@AB@%/* third window */%@AE@%%@NL@%
  6924.    _setviewport( 0, halfy, xwidth-1, yheight-1 );%@NL@%
  6925.    _settextwindow( rows/2+1, 1, rows, cols );%@NL@%
  6926.    _setwindow( TRUE, -3.0, -1.5, 1.5, 1.5 );%@NL@%
  6927.    grid_shape();%@NL@%
  6928.    upleft.wx = -3.0;%@NL@%
  6929.    upleft.wy = -1.5;%@NL@%
  6930.    botright.wx = 1.5;%@NL@%
  6931.    botright.wy = 1.5;%@NL@%
  6932.    _rectangle_wxy( _GBORDER, &upleft, &botright );%@NL@%
  6933.    %@NL@%
  6934.    getch();%@NL@%
  6935.    _setvideomode( _DEFAULTMODE );%@NL@%
  6936. }%@NL@%
  6937. %@NL@%
  6938. void grid_shape( void )%@NL@%
  6939. {%@NL@%
  6940.    int i, numc, x1, y1, x2, y2;%@NL@%
  6941.    double x, y;%@NL@%
  6942.    char txt[80];%@NL@%
  6943.    %@NL@%
  6944.    numc = screen.numcolors;%@NL@%
  6945.    for( i = 1; i < numc; i++ )%@NL@%
  6946.    {%@NL@%
  6947.       _settextposition( i, 2 );%@NL@%
  6948.       _settextcolor( i );%@NL@%
  6949.       sprintf( txt, "Color %d", i );%@NL@%
  6950.       _outtext( txt );%@NL@%
  6951.    }%@NL@%
  6952.    %@NL@%
  6953.    _setcolor( 1 );%@NL@%
  6954.    _rectangle_w( _GBORDER, -1.0, -1.0, 1.0, 1.0 );%@NL@%
  6955.    _rectangle_w( _GBORDER, -1.02, -1.02, 1.02, 1.02 );%@NL@%
  6956.    %@NL@%
  6957.    for( x = -0.9, i = 0; x < 0.9; x += 0.1 )%@NL@%
  6958.    {%@NL@%
  6959.       _setcolor( 2 );%@NL@%
  6960.       _moveto_w( x, -1.0 );%@NL@%
  6961.       _lineto_w( x, 1.0 );%@NL@%
  6962.       _moveto_w( -1.0, x );%@NL@%
  6963.       _lineto_w( 1.0, x );%@NL@%
  6964.    %@NL@%
  6965.       _setcolor( 3 );%@NL@%
  6966.       _moveto_w( x - 0.1, bananas[i++] );%@NL@%
  6967.       _lineto_w( x, bananas[i] );%@NL@%
  6968.    }%@NL@%
  6969.    _moveto_w( 0.9, bananas[i++] );%@NL@%
  6970.    _lineto_w( 1.0, bananas[i] );%@NL@%
  6971. }%@NL@%
  6972. %@NL@%
  6973. %@NL@%
  6974. %@2@%%@AH@%RWFILE.C%@AE@%%@EH@%%@NL@%
  6975. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\RWFILE.C%@AE@%%@NL@%
  6976. %@NL@%
  6977. %@AB@%/* RWFILE.C: Reads and writes a file. */%@AE@%%@NL@%
  6978. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  6979. %@AI@%#include %@AE@%<string.h> %@NL@%
  6980. %@AI@%#include %@AE@%<fcntl.h> %@NL@%
  6981. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  6982. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  6983. %@AI@%#include %@AE@%<io.h> %@NL@%
  6984. %@NL@%
  6985. %@AI@%#define %@AE@%BUFF 512 %@NL@%
  6986. %@NL@%
  6987. main()%@NL@%
  6988. {%@NL@%
  6989.    char inbuffer[BUFF];%@NL@%
  6990.    char outbuffer[BUFF];%@NL@%
  6991.    int infile, outfile, length, num;%@NL@%
  6992. %@NL@%
  6993.    strcpy( outbuffer, "Happy Birthday." );%@NL@%
  6994.    length = strlen( outbuffer );%@NL@%
  6995.    length++;%@NL@%
  6996. %@NL@%
  6997.    if( (outfile = open( "testfile.bin",%@NL@%
  6998.       O_CREAT | O_WRONLY | O_BINARY,  S_IWRITE )) != -1 )%@NL@%
  6999.    {%@NL@%
  7000.       if( (num = write( outfile, outbuffer, length )) == -1 )%@NL@%
  7001.          perror( "Error in writing" );%@NL@%
  7002.       printf( "\nBytes written to file: %d\n", num );%@NL@%
  7003.       close( outfile );%@NL@%
  7004.    }%@NL@%
  7005.    else%@NL@%
  7006.       perror( "Error opening outfile" );%@NL@%
  7007. %@NL@%
  7008.    if( (infile = open( "testfile.bin", O_RDONLY | O_BINARY )) != -1  )%@NL@%
  7009.    {%@NL@%
  7010.       while( length = read( infile, inbuffer, BUFF ) )%@NL@%
  7011.          printf( "%d bytes received so far.\n", length );%@NL@%
  7012.       close( infile );%@NL@%
  7013.       printf( "%s\n", inbuffer );%@NL@%
  7014.    }%@NL@%
  7015.    else%@NL@%
  7016.       perror( "Error opening infile" );%@NL@%
  7017. }%@NL@%
  7018. %@NL@%
  7019. %@NL@%
  7020. %@2@%%@AH@%SAMPLER.C%@AE@%%@EH@%%@NL@%
  7021. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\SAMPLER.C%@AE@%%@NL@%
  7022. %@NL@%
  7023. %@AB@%/* SAMPLER.C: Display sample text in various fonts. */%@AE@%%@NL@%
  7024. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7025. %@AI@%#include %@AE@%<conio.h> %@NL@%
  7026. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  7027. %@AI@%#include %@AE@%<graph.h> %@NL@%
  7028. %@AI@%#include %@AE@%<string.h> %@NL@%
  7029. %@NL@%
  7030. %@AI@%#define %@AE@%NFONTS 6 %@NL@%
  7031. %@NL@%
  7032. main()%@NL@%
  7033. {%@NL@%
  7034.   static unsigned char *text[2*NFONTS] =%@NL@%
  7035.   {%@NL@%
  7036.       "COURIER",        "courier",%@NL@%
  7037.       "HELV",           "helv",%@NL@%
  7038.       "TMS RMN",        "tms rmn",%@NL@%
  7039.       "MODERN",         "modern",%@NL@%
  7040.       "SCRIPT",         "script",%@NL@%
  7041.       "ROMAN",          "roman"%@NL@%
  7042.   };%@NL@%
  7043.   static unsigned char *face[NFONTS] =%@NL@%
  7044.   {%@NL@%
  7045.       "t'courier'",%@NL@%
  7046.       "t'helv'",%@NL@%
  7047.       "t'tms rmn'",%@NL@%
  7048.       "t'modern'",%@NL@%
  7049.       "t'script'",%@NL@%
  7050.       "t'roman'"%@NL@%
  7051.   };%@NL@%
  7052.   static unsigned char list[20];%@NL@%
  7053.   struct videoconfig vc;%@NL@%
  7054.   int mode = _VRES16COLOR;%@NL@%
  7055.   register i;%@NL@%
  7056. %@NL@%
  7057.   %@AB@%/*  Read header info from all .FON files in%@NL@%
  7058. %@AB@%   *  current directory   */%@AE@%%@NL@%
  7059.   if(_registerfonts( "*.FON" )<0 )%@NL@%
  7060.   {%@NL@%
  7061.      _outtext("Error:  can't register fonts");%@NL@%
  7062.      exit( 0 );%@NL@%
  7063.   }%@NL@%
  7064. %@NL@%
  7065.   %@AB@%/*   Set highest available video mode */%@AE@%%@NL@%
  7066.   while( !_setvideomode( mode ) )%@NL@%
  7067.      mode--;%@NL@%
  7068.   if( mode == _TEXTMONO )%@NL@%
  7069.      exit ( 0 );%@NL@%
  7070. %@NL@%
  7071.   %@AB@%/*   Copy video configuration into structure vc */%@AE@%%@NL@%
  7072.   _getvideoconfig( &vc );%@NL@%
  7073. %@NL@%
  7074.   %@AB@%/*   Display six lines of sample text */%@AE@%%@NL@%
  7075.   for( i = 0; i<NFONTS; i++ )%@NL@%
  7076.   {%@NL@%
  7077.      strcpy( list, face[i] );%@NL@%
  7078.      strcat( list, "h30w24b" );%@NL@%
  7079. %@NL@%
  7080.      if( !_setfont( list ) )%@NL@%
  7081.      {%@NL@%
  7082.          _setcolor( i + 1 );%@NL@%
  7083.          _moveto( 0, (i * vc.numypixels) / NFONTS );%@NL@%
  7084.          _outgtext( text[i * 2] );%@NL@%
  7085.          _moveto( vc.numxpixels / 2,%@NL@%
  7086.                      (i * vc.numypixels) / NFONTS );%@NL@%
  7087.          _outgtext( text[(i * 2) + 1] );%@NL@%
  7088.      }%@NL@%
  7089.      else%@NL@%
  7090.      {%@NL@%
  7091.          _setvideomode( _DEFAULTMODE );%@NL@%
  7092.          _outtext( "Error:  can't set font" );%@NL@%
  7093.          exit( 0 );%@NL@%
  7094.      }%@NL@%
  7095.   }%@NL@%
  7096.   getch();%@NL@%
  7097.   _setvideomode( _DEFAULTMODE );%@NL@%
  7098. %@NL@%
  7099.   %@AB@%/* Return memory when finished with fonts */%@AE@%%@NL@%
  7100.   _unregisterfonts();%@NL@%
  7101.   exit( 0 );%@NL@%
  7102. }%@NL@%
  7103. %@NL@%
  7104. %@NL@%
  7105. %@2@%%@AH@%SCATTER.C%@AE@%%@EH@%%@NL@%
  7106. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\SCATTER.C%@AE@%%@NL@%
  7107. %@NL@%
  7108. %@AB@%/* SCATTER.C:  Create sample scatter diagram. */%@AE@%%@NL@%
  7109. %@NL@%
  7110. %@AI@%#include %@AE@%<conio.h> %@NL@%
  7111. %@AI@%#include %@AE@%<string.h> %@NL@%
  7112. %@AI@%#include %@AE@%<graph.h> %@NL@%
  7113. %@AI@%#include %@AE@%<pgchart.h> %@NL@%
  7114. %@NL@%
  7115. %@AI@%#define %@AE@%MONTHS 12 %@NL@%
  7116. typedef enum {FALSE, TRUE} boolean;%@NL@%
  7117. %@NL@%
  7118. %@AB@%/* Orange juice sales */%@AE@%%@NL@%
  7119. %@NL@%
  7120. float far xvalue[MONTHS] = %@NL@%
  7121. { %@NL@%
  7122.    33.0, 27.0, 42.0, 64.0,106.0,157.0,%@NL@%
  7123.   182.0,217.0,128.0, 62.0, 43.0, 36.0%@NL@%
  7124. };%@NL@%
  7125. %@AB@%/* Hot chocolate sales */%@AE@%%@NL@%
  7126. %@NL@%
  7127. float far yvalue[MONTHS] = %@NL@%
  7128. { %@NL@%
  7129.   37.0, 37.0, 30.0, 19.0, 10.0,  5.0,%@NL@%
  7130.    2.0,  1.0,  7.0, 15.0, 28.0, 39.0%@NL@%
  7131. };%@NL@%
  7132. %@NL@%
  7133. main()%@NL@%
  7134. {%@NL@%
  7135.   chartenv env;%@NL@%
  7136.   int mode = _VRES16COLOR;%@NL@%
  7137. %@NL@%
  7138.   %@AB@%/* Set highest video mode available */%@AE@%%@NL@%
  7139. %@NL@%
  7140.   while(!_setvideomode( mode ))%@NL@%
  7141.      mode--;%@NL@%
  7142.   if(mode == _TEXTMONO)%@NL@%
  7143.      return(0);%@NL@%
  7144. %@NL@%
  7145.   %@AB@%/* Initialize chart library and default%@NL@%
  7146. %@AB@%   * scatter diagram%@NL@%
  7147. %@AB@%   */%@AE@%%@NL@%
  7148. %@NL@%
  7149.   _pg_initchart();%@NL@%
  7150. %@NL@%
  7151.   _pg_defaultchart( &env, _PG_SCATTERCHART,%@NL@%
  7152.                     _PG_POINTONLY );%@NL@%
  7153. %@NL@%
  7154.   %@AB@%/* Add titles and some chart options */%@AE@%%@NL@%
  7155. %@NL@%
  7156.   strcpy( env.maintitle.title, "Good Neighbor Grocery" );%@NL@%
  7157.   env.maintitle.titlecolor = 6;%@NL@%
  7158.   env.maintitle.justify = _PG_RIGHT;%@NL@%
  7159.   strcpy( env.subtitle.title,%@NL@%
  7160.           "Orange Juice vs Hot Chocolate" );%@NL@%
  7161.   env.subtitle.titlecolor = 6;%@NL@%
  7162.   env.subtitle.justify = _PG_RIGHT;%@NL@%
  7163.   env.yaxis.grid = TRUE;%@NL@%
  7164.   strcpy( env.xaxis.axistitle.title,%@NL@%
  7165.           "Orange Juice Sales" );%@NL@%
  7166.   strcpy( env.yaxis.axistitle.title,%@NL@%
  7167.           "Hot Chocolate Sales" );%@NL@%
  7168.   env.chartwindow.border = FALSE;%@NL@%
  7169. %@NL@%
  7170.   %@AB@%/* Parameters for call to _pg_chartscatter are:%@NL@%
  7171. %@AB@%   *    env        - Environment variable%@NL@%
  7172. %@AB@%   *    xvalue     - X-axis data%@NL@%
  7173. %@AB@%   *    yvalue     - Y-axis data%@NL@%
  7174. %@AB@%   *    MONTHS     - Number of data values%@NL@%
  7175. %@AB@%   */%@AE@%%@NL@%
  7176. %@NL@%
  7177.   if(_pg_chartscatter( &env, xvalue,%@NL@%
  7178.                         yvalue, MONTHS ))%@NL@%
  7179.   {%@NL@%
  7180.      _setvideomode( _DEFAULTMODE );%@NL@%
  7181.      _outtext( "Error:  can't draw chart" );%@NL@%
  7182.   }%@NL@%
  7183.   else%@NL@%
  7184.   {%@NL@%
  7185.      getch(); %@NL@%
  7186.      _setvideomode( _DEFAULTMODE );%@NL@%
  7187.   }%@NL@%
  7188.   return(0);%@NL@%
  7189. } %@NL@%
  7190. %@NL@%
  7191. %@NL@%
  7192. %@2@%%@AH@%SHOWME.C%@AE@%%@EH@%%@NL@%
  7193. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\SHOWME.C%@AE@%%@NL@%
  7194. %@NL@%
  7195. %@AB@%/* SHOWME.C: Demonstrate passing by value. */%@AE@%%@NL@%
  7196. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7197. %@NL@%
  7198. void showme( int a, int b, int c );%@NL@%
  7199. %@NL@%
  7200. main()%@NL@%
  7201. {%@NL@%
  7202.    int x = 10, y = 20, z = 30;%@NL@%
  7203.    showme( z, y, x );%@NL@%
  7204. }%@NL@%
  7205. %@NL@%
  7206. void showme( int a, int b, int c )%@NL@%
  7207. {%@NL@%
  7208.    printf( "a=%d b=%d c=%d", a, b, c );%@NL@%
  7209. }%@NL@%
  7210. %@NL@%
  7211. %@NL@%
  7212. %@2@%%@AH@%SHOWMORE.C%@AE@%%@EH@%%@NL@%
  7213. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\SHOWMORE.C%@AE@%%@NL@%
  7214. %@NL@%
  7215. %@AB@%/* SHOWMORE.C: Demonstrate passing by value. */%@AE@%%@NL@%
  7216. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7217. %@NL@%
  7218. void showme( int any, int old, int name );%@NL@%
  7219. %@NL@%
  7220. main()%@NL@%
  7221. {%@NL@%
  7222.    int x = 10, y = 20, z = 30;%@NL@%
  7223.    showme( z, y, x );%@NL@%
  7224.    printf( "  z=%d   y=%d    x=%d\n", z, y, x );%@NL@%
  7225. }%@NL@%
  7226. %@NL@%
  7227. void showme( int any, int old, int name )%@NL@%
  7228. {%@NL@%
  7229.    printf( "any=%d old=%d name=%d\n", any, old, name );%@NL@%
  7230.    any = 55;%@NL@%
  7231.    old = 66;%@NL@%
  7232.    name = 77;%@NL@%
  7233.    printf( "any=%d old=%d name=%d\n", any, old, name );%@NL@%
  7234. }%@NL@%
  7235. %@NL@%
  7236. %@NL@%
  7237. %@2@%%@AH@%SINE.C%@AE@%%@EH@%%@NL@%
  7238. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\SINE.C%@AE@%%@NL@%
  7239. %@NL@%
  7240. %@AB@%/*  SINE.C: Illustrates basic graphics commands */%@AE@%%@NL@%
  7241. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7242. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  7243. %@AI@%#include %@AE@%<graph.h> %@NL@%
  7244. %@AI@%#include %@AE@%<math.h> %@NL@%
  7245. %@AI@%#include %@AE@%<conio.h> %@NL@%
  7246. %@AI@%#define %@AE@%PI 3.14159 %@NL@%
  7247. %@NL@%
  7248. void graphics_mode( void );%@NL@%
  7249. void draw_lines( void );%@NL@%
  7250. void sine_wave( void );%@NL@%
  7251. void draw_shapes( void );%@NL@%
  7252. void end_program( void );%@NL@%
  7253. int newx( int );%@NL@%
  7254. int newy( int );%@NL@%
  7255. %@NL@%
  7256. struct videoconfig myscreen;%@NL@%
  7257. int maxx, maxy;%@NL@%
  7258. unsigned char diagmask[8] =%@NL@%
  7259. { 0x93, 0xC9, 0x64, 0xB2, 0x59, 0x2C, 0x96, 0x4B };%@NL@%
  7260. unsigned char linemask[8] =%@NL@%
  7261. { 0xFF, 0x00, 0x7F, 0xFE, 0x00, 0x00, 0x00, 0xCC };%@NL@%
  7262. %@NL@%
  7263. main()%@NL@%
  7264. {%@NL@%
  7265.    graphics_mode();%@NL@%
  7266.    draw_lines();%@NL@%
  7267.    sine_wave();%@NL@%
  7268.    draw_shapes();%@NL@%
  7269.    end_program();%@NL@%
  7270. }%@NL@%
  7271. %@NL@%
  7272. void graphics_mode( void )%@NL@%
  7273. {%@NL@%
  7274.    _getvideoconfig( &myscreen );%@NL@%
  7275.    switch( myscreen.adapter )%@NL@%
  7276.    {%@NL@%
  7277.       case _CGA:%@NL@%
  7278.          _setvideomode( _HRESBW );%@NL@%
  7279.          break;%@NL@%
  7280.       case _OCGA:%@NL@%
  7281.          _setvideomode( _ORESCOLOR );%@NL@%
  7282.          break;%@NL@%
  7283.       case _EGA:%@NL@%
  7284.       case _OEGA:%@NL@%
  7285.          if( myscreen.monitor == _MONO )%@NL@%
  7286.             _setvideomode( _ERESNOCOLOR );%@NL@%
  7287.          else%@NL@%
  7288.             _setvideomode( _ERESCOLOR );%@NL@%
  7289.          break;%@NL@%
  7290.       case _VGA:%@NL@%
  7291.       case _OVGA:%@NL@%
  7292.       case _MCGA:%@NL@%
  7293.          _setvideomode( _VRES2COLOR );%@NL@%
  7294.          break;%@NL@%
  7295.       case _HGC:%@NL@%
  7296.          _setvideomode( _HERCMONO );%@NL@%
  7297.          break;%@NL@%
  7298.       default:%@NL@%
  7299.          printf( "This program requires a CGA, EGA, VGA, or Hercules card\n" );%@NL@%
  7300.          exit( 0 );%@NL@%
  7301.    }%@NL@%
  7302.    _getvideoconfig( &myscreen );%@NL@%
  7303.    maxx = myscreen.numxpixels - 1;%@NL@%
  7304.    maxy = myscreen.numypixels - 1;%@NL@%
  7305. }%@NL@%
  7306. %@NL@%
  7307. int newx( int xcoord )%@NL@%
  7308. {%@NL@%
  7309.    int nx;%@NL@%
  7310.    float tempx;%@NL@%
  7311.    tempx = ((float) maxx)/ 1000.0;%@NL@%
  7312.    tempx = ((float) xcoord) * tempx + 0.5;%@NL@%
  7313.    return( (int) tempx );%@NL@%
  7314. }%@NL@%
  7315. %@NL@%
  7316. int newy( int ycoord )%@NL@%
  7317. {%@NL@%
  7318.    int ny;%@NL@%
  7319.    float tempy;%@NL@%
  7320.    tempy = ((float) maxy)/ 1000.0;%@NL@%
  7321.    tempy = ((float) ycoord) * tempy + 0.5;%@NL@%
  7322.    return( (int) tempy );%@NL@%
  7323. }%@NL@%
  7324. %@NL@%
  7325. void sine_wave( void )%@NL@%
  7326. {%@NL@%
  7327.    int locx, locy;%@NL@%
  7328.    double i, rad;%@NL@%
  7329. %@NL@%
  7330.    for( i = 0.0; i < 1000.0; i += 3.0 )%@NL@%
  7331.    {%@NL@%
  7332.       rad = -sin( (PI * (float) i) / 250.0 );%@NL@%
  7333.       locx = newx( (int) i );%@NL@%
  7334.       locy = newy( (int) (rad * 250.0) );%@NL@%
  7335.       _setpixel( locx, locy );%@NL@%
  7336.    }%@NL@%
  7337. }%@NL@%
  7338. %@NL@%
  7339. void draw_shapes( void )%@NL@%
  7340. {%@NL@%
  7341.    _setlinestyle( 0xFFFF );%@NL@%
  7342.    _setfillmask( diagmask );%@NL@%
  7343.    _rectangle( _GBORDER, newx(50), newy(-325), newx(200), newy(-425) );%@NL@%
  7344.    _rectangle( _GFILLINTERIOR, newx(550), newy(-325), newx(700), newy(-425) );%@NL@%
  7345. %@NL@%
  7346.    _setfillmask( linemask );%@NL@%
  7347.    _ellipse( _GBORDER, newx(50), newy(325), newx(200), newy(425) );%@NL@%
  7348.    _ellipse( _GFILLINTERIOR, newx(550), newy(325), newx(700), newy(425) );%@NL@%
  7349. }%@NL@%
  7350. %@NL@%
  7351. void end_program( void )%@NL@%
  7352. {%@NL@%
  7353.    getch();%@NL@%
  7354.    _setvideomode( _DEFAULTMODE );%@NL@%
  7355. }%@NL@%
  7356. %@NL@%
  7357. void draw_lines( void )%@NL@%
  7358. {%@NL@%
  7359.    _rectangle( _GBORDER, 0, 0, maxx, maxy );%@NL@%
  7360.    %@AB@%/* _setcliprgn( 20, 20, maxx - 20, maxy - 20 ); */%@AE@%%@NL@%
  7361.    _setvieworg( 0, newy( 500 ) );%@NL@%
  7362. %@NL@%
  7363.    _moveto( 0, 0 );%@NL@%
  7364.    _lineto( newx( 1000 ), 0 );%@NL@%
  7365.    _setlinestyle( 0xAA3C );%@NL@%
  7366.    _moveto( 0, newy( -250) );%@NL@%
  7367.    _lineto( newx( 1000 ), newy( -250 ) );%@NL@%
  7368. %@NL@%
  7369.    _setlinestyle( 0x8888 );%@NL@%
  7370.    _moveto( 0, newy( 250 ) );%@NL@%
  7371.    _lineto( newx( 1000 ), newy( 250 ) );%@NL@%
  7372. }%@NL@%
  7373. %@NL@%
  7374. %@NL@%
  7375. %@2@%%@AH@%STATIC.C%@AE@%%@EH@%%@NL@%
  7376. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\STATIC.C%@AE@%%@NL@%
  7377. %@NL@%
  7378. %@AB@%/* STATIC.C: Demonstrate static variables. */%@AE@%%@NL@%
  7379. %@NL@%
  7380. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7381. %@NL@%
  7382. void add_val( int value );%@NL@%
  7383. %@NL@%
  7384. main()%@NL@%
  7385. {%@NL@%
  7386.    add_val( 1 );%@NL@%
  7387.    add_val( 5 );%@NL@%
  7388.    add_val( 20 );%@NL@%
  7389. }%@NL@%
  7390. %@NL@%
  7391. void add_val( int value )%@NL@%
  7392. {%@NL@%
  7393.    static int methuselah;%@NL@%
  7394.    if( value == 1 )%@NL@%
  7395.       methuselah = 0;%@NL@%
  7396.    methuselah = methuselah + value;%@NL@%
  7397.    printf( "methuselah = %d\n", methuselah );%@NL@%
  7398. }%@NL@%
  7399. %@NL@%
  7400. %@NL@%
  7401. %@2@%%@AH@%STRING.C%@AE@%%@EH@%%@NL@%
  7402. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\STRING.C%@AE@%%@NL@%
  7403. %@NL@%
  7404. %@AB@%/* STRING.C: Demonstrate string arrays. */%@AE@%%@NL@%
  7405. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7406. %@NL@%
  7407. main()%@NL@%
  7408. {%@NL@%
  7409.    int j;%@NL@%
  7410.    char c_array[] = "Hello";%@NL@%
  7411. %@NL@%
  7412.    printf( "--- Values --------     --- Addresses -------\n\n" );%@NL@%
  7413. %@NL@%
  7414.    for( j = 0; j < 6; j = j + 1 )%@NL@%
  7415.    {%@NL@%
  7416.       printf( "c_array[%d]   = %x %c", j, c_array[j], c_array[j] );%@NL@%
  7417.       printf( "\t&c_array[%d]    = %u\n", j, &c_array[j] );%@NL@%
  7418.    }%@NL@%
  7419. }%@NL@%
  7420. %@NL@%
  7421. %@NL@%
  7422. %@2@%%@AH@%SVBIN.C%@AE@%%@EH@%%@NL@%
  7423. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\SVBIN.C%@AE@%%@NL@%
  7424. %@NL@%
  7425. %@AB@%/* SVBIN.C: Saves integer variables in binary format. */%@AE@%%@NL@%
  7426. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7427. %@AI@%#define %@AE@%ASIZE 10 %@NL@%
  7428. %@NL@%
  7429. main()%@NL@%
  7430. {%@NL@%
  7431.    FILE *ap;%@NL@%
  7432.    int zebra[ASIZE], acopy[ASIZE], bcopy[ASIZE];%@NL@%
  7433.    int i;%@NL@%
  7434. %@NL@%
  7435.    for( i = 0; i < ASIZE; i++ )%@NL@%
  7436.       zebra[i] = 7700 + i;%@NL@%
  7437. %@NL@%
  7438.    if( (ap = fopen( "binfile", "wb" )) != NULL )%@NL@%
  7439.    {%@NL@%
  7440.       fwrite( zebra, sizeof(zebra), 1, ap );%@NL@%
  7441.       fclose( ap );%@NL@%
  7442.    }%@NL@%
  7443.    else%@NL@%
  7444.       perror( "Write error" );%@NL@%
  7445. %@NL@%
  7446.    if( (ap = fopen( "morebin", "wb" )) != NULL )%@NL@%
  7447.    {%@NL@%
  7448.       fwrite( &zebra[0], sizeof(zebra[0]), ASIZE, ap );%@NL@%
  7449.       fclose( ap );%@NL@%
  7450.    }%@NL@%
  7451.    else%@NL@%
  7452.       perror( "Write error" );%@NL@%
  7453. %@NL@%
  7454.    if( (ap = fopen( "binfile", "rb" )) != NULL )%@NL@%
  7455.    {%@NL@%
  7456.       printf( "Hexadecimal values in binfile:\n" );%@NL@%
  7457.       while( (i = fgetc( ap )) != EOF )%@NL@%
  7458.          printf( "%02X ", i );%@NL@%
  7459.       rewind( ap );%@NL@%
  7460.       fread( acopy, sizeof(acopy), 1, ap );%@NL@%
  7461.       rewind( ap );%@NL@%
  7462.       fread( &bcopy[0], sizeof( bcopy[0] ), ASIZE, ap);%@NL@%
  7463.       for( i=0; i<ASIZE; i++ )%@NL@%
  7464.          printf( "\nItem %d = %d\t%d", i, acopy[i], bcopy[i] );%@NL@%
  7465.       fclose( ap );%@NL@%
  7466. %@NL@%
  7467.    }%@NL@%
  7468.    else%@NL@%
  7469.       perror( "Read error" );%@NL@%
  7470. %@NL@%
  7471. }%@NL@%
  7472. %@NL@%
  7473. %@NL@%
  7474. %@2@%%@AH@%SVTEXT.C%@AE@%%@EH@%%@NL@%
  7475. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\SVTEXT.C%@AE@%%@NL@%
  7476. %@NL@%
  7477. %@AB@%/* SVTEXT.C: Save integer variables as text. */%@AE@%%@NL@%
  7478. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7479. %@NL@%
  7480. int list[] = { 53, -23456, 50, 500, 5000, -99 };%@NL@%
  7481. extern int errno;%@NL@%
  7482. char fname[] = "numtext";%@NL@%
  7483. char temp[81];%@NL@%
  7484. %@NL@%
  7485. main()%@NL@%
  7486. {%@NL@%
  7487.    FILE *fptr;%@NL@%
  7488.    int i;%@NL@%
  7489. %@NL@%
  7490.    if( (fptr = fopen( "numtext","wt" )) != NULL )%@NL@%
  7491.    {%@NL@%
  7492.       for( i=0; i<6; i++ )%@NL@%
  7493.          fprintf( fptr, "Item %d: %6d \n", i, list[i] );%@NL@%
  7494.       fclose( fptr );%@NL@%
  7495.    }%@NL@%
  7496.    else%@NL@%
  7497.       printf( "Error: Couldn't create file.\n" );%@NL@%
  7498. %@NL@%
  7499.    if( (fptr = fopen( "badname", "rt" )) != NULL )%@NL@%
  7500.    {%@NL@%
  7501.       %@AB@%/* do nothing */%@AE@%%@NL@%
  7502.    }%@NL@%
  7503.    else%@NL@%
  7504.    {%@NL@%
  7505.       printf( "Error number: %d\n\t", errno );%@NL@%
  7506.       perror( "Couldn't open file BADNAME\n\t" );%@NL@%
  7507.    }%@NL@%
  7508. %@NL@%
  7509.    if( (fptr = fopen( fname, "rt" )) != NULL )%@NL@%
  7510.    {%@NL@%
  7511.       list[0] = 0;%@NL@%
  7512.       fscanf( fptr, "Item %d: %d \n", &i, &list[0] );%@NL@%
  7513.       printf( "Values read from file:\t %d %d\n", i, list[0] );%@NL@%
  7514.       fgets( temp, 80, fptr );%@NL@%
  7515.       printf( "String from file: \t%s\n", temp );%@NL@%
  7516.       while( (i = fgetc( fptr )) != '\n' )%@NL@%
  7517.          printf( "char: %c \t ASCII: %d \n", i, i );%@NL@%
  7518.       rewind( fptr );%@NL@%
  7519.       printf( "Rewind to start -->\t%s", fgets( temp, 80, fptr ) );%@NL@%
  7520.       fclose( fptr );%@NL@%
  7521.    }%@NL@%
  7522.    else%@NL@%
  7523.       printf( "Trouble opening %s \n", fname );%@NL@%
  7524. }%@NL@%
  7525. %@NL@%
  7526. %@NL@%
  7527. %@2@%%@AH@%SWITCH.C%@AE@%%@EH@%%@NL@%
  7528. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\SWITCH.C%@AE@%%@NL@%
  7529. %@NL@%
  7530. %@AB@%/* SWITCH.C: Demonstrate switch statement. */%@AE@%%@NL@%
  7531. %@NL@%
  7532. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7533. %@AI@%#include %@AE@%<conio.h> %@NL@%
  7534. %@NL@%
  7535. main()%@NL@%
  7536. {%@NL@%
  7537.    char ch;%@NL@%
  7538.    printf( "Press the b key to hear a bell.\n" );%@NL@%
  7539.    ch = getch();%@NL@%
  7540.    switch( ch )%@NL@%
  7541.    {%@NL@%
  7542.       case 'b':%@NL@%
  7543.          printf( "Beep!\a\n" );%@NL@%
  7544.          break;%@NL@%
  7545.       case '\r':%@NL@%
  7546.          printf( "Enter\n" );%@NL@%
  7547.          break;%@NL@%
  7548.       default:%@NL@%
  7549.          printf( "Bye bye" );%@NL@%
  7550.          break;%@NL@%
  7551.    }%@NL@%
  7552. }%@NL@%
  7553. %@NL@%
  7554. %@NL@%
  7555. %@2@%%@AH@%TWODIM.C%@AE@%%@EH@%%@NL@%
  7556. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\TWODIM.C%@AE@%%@NL@%
  7557. %@NL@%
  7558. %@AB@%/* TWODIM.C: Demonstrate multidimensional arrays. */%@AE@%%@NL@%
  7559. %@NL@%
  7560. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7561. %@NL@%
  7562. main()%@NL@%
  7563. {%@NL@%
  7564.    int j, k;%@NL@%
  7565.    int i_array[2][3] = { { 176, 4069, 303 }, { 6, 55, 777 } };%@NL@%
  7566. %@NL@%
  7567.    printf( "--- Values --------     --- Addresses -------\n\n" );%@NL@%
  7568. %@NL@%
  7569.    for( j = 0; j < 2; j = j + 1 )%@NL@%
  7570.    {%@NL@%
  7571.       for( k = 0; k < 3; k = k + 1 )%@NL@%
  7572.       {%@NL@%
  7573.          printf( "i_array[%d][%d] = %d", j, k, i_array[j][k] );%@NL@%
  7574.          printf( "\t&i_array[%d][%d] = %u\n", j, k, &i_array[j][k] );%@NL@%
  7575.       }%@NL@%
  7576.       printf( "\n" );%@NL@%
  7577.    }%@NL@%
  7578. %@NL@%
  7579. }%@NL@%
  7580. %@NL@%
  7581. %@NL@%
  7582. %@2@%%@AH@%TYPES.C%@AE@%%@EH@%%@NL@%
  7583. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\TYPES.C%@AE@%%@NL@%
  7584. %@NL@%
  7585. %@AB@%/* TYPES.C: Illustrate basic data types. */%@AE@%%@NL@%
  7586. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7587. %@NL@%
  7588. main()%@NL@%
  7589. {%@NL@%
  7590.    char char_val        = 'a';%@NL@%
  7591.    int int_val         = 543;%@NL@%
  7592.    float float_val     = 11.1;%@NL@%
  7593.    double double_val   = 66.123456789;%@NL@%
  7594.    printf( "char_val   = %c\n", char_val );%@NL@%
  7595.    printf( "int_val    = %d\n", int_val );%@NL@%
  7596.    printf( "float_val  = %f\n", float_val );%@NL@%
  7597.    printf( "double_val = %2.9f\n", double_val );%@NL@%
  7598. }%@NL@%
  7599. %@NL@%
  7600. %@NL@%
  7601. %@2@%%@AH@%VISIBLE.C%@AE@%%@EH@%%@NL@%
  7602. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\VISIBLE.C%@AE@%%@NL@%
  7603. %@NL@%
  7604. %@AB@%/* VISIBLE.C: Demonstrate local visibility. */%@AE@%%@NL@%
  7605. %@NL@%
  7606. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7607. void be_bop( void );%@NL@%
  7608. %@NL@%
  7609. main()%@NL@%
  7610. {%@NL@%
  7611.    int val = 10;%@NL@%
  7612.    be_bop();%@NL@%
  7613. }%@NL@%
  7614. %@NL@%
  7615. void be_bop( void )%@NL@%
  7616. {%@NL@%
  7617.    printf( "val = %d", val ); %@AB@%/* Error! */%@AE@%%@NL@%
  7618. }%@NL@%
  7619. %@NL@%
  7620. %@NL@%
  7621. %@2@%%@AH@%VISIBLE1.C%@AE@%%@EH@%%@NL@%
  7622. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\VISIBLE1.C%@AE@%%@NL@%
  7623. %@NL@%
  7624. %@AB@%/* VISIBLE1.C: Demonstrate local visibility. */%@AE@%%@NL@%
  7625. %@NL@%
  7626. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7627. %@NL@%
  7628. void be_bop( int param );%@NL@%
  7629. %@NL@%
  7630. main()%@NL@%
  7631. {%@NL@%
  7632.    int val = 10;%@NL@%
  7633.    be_bop( val );%@NL@%
  7634. }%@NL@%
  7635. %@NL@%
  7636. void be_bop( int param )%@NL@%
  7637. {%@NL@%
  7638.    printf( "%d\n", param );%@NL@%
  7639. }%@NL@%
  7640. %@NL@%
  7641. %@NL@%
  7642. %@2@%%@AH@%VISIBLE2.C%@AE@%%@EH@%%@NL@%
  7643. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\VISIBLE2.C%@AE@%%@NL@%
  7644. %@NL@%
  7645. %@AB@%/* VISIBLE2.C: Demonstrate external visibility.%@NL@%
  7646. %@AB@%*/%@AE@%%@NL@%
  7647. %@NL@%
  7648. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7649. %@NL@%
  7650. void be_bop( int param );%@NL@%
  7651. %@NL@%
  7652. main()%@NL@%
  7653. {%@NL@%
  7654.    be_bop( val ); %@AB@%/* Error! */%@AE@%%@NL@%
  7655. }%@NL@%
  7656. %@NL@%
  7657. int val = 10;%@NL@%
  7658. %@NL@%
  7659. void be_bop( int param )%@NL@%
  7660. {%@NL@%
  7661.    printf( "val = %d\n", param );%@NL@%
  7662. }%@NL@%
  7663. %@NL@%
  7664. %@NL@%
  7665. %@2@%%@AH@%VOLUME.C%@AE@%%@EH@%%@NL@%
  7666. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\VOLUME.C%@AE@%%@NL@%
  7667. %@NL@%
  7668. %@AB@%/* VOLUME.C: Calculate sphere's volume. */%@AE@%%@NL@%
  7669. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7670. %@AI@%#define %@AE@%PI 3.14 %@NL@%
  7671. %@NL@%
  7672. float sphere( int rad );%@NL@%
  7673. %@NL@%
  7674. main()%@NL@%
  7675. {%@NL@%
  7676.    float volume;%@NL@%
  7677.    int radius = 3;%@NL@%
  7678.    volume = sphere( radius );%@NL@%
  7679.    printf( "Volume: %f\n", volume );%@NL@%
  7680. }%@NL@%
  7681. %@NL@%
  7682. float sphere( int rad )%@NL@%
  7683. {%@NL@%
  7684.    float result;%@NL@%
  7685.    result = rad * rad * rad;%@NL@%
  7686.    result = 4 * PI * result;%@NL@%
  7687.    result = result / 3;%@NL@%
  7688.    return result;%@NL@%
  7689. }%@NL@%
  7690. %@NL@%
  7691. %@NL@%
  7692. %@2@%%@AH@%WHILE.C%@AE@%%@EH@%%@NL@%
  7693. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\WHILE.C%@AE@%%@NL@%
  7694. %@NL@%
  7695. %@AB@%/* WHILE.C: Demonstrate while loop. */%@AE@%%@NL@%
  7696. %@NL@%
  7697. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7698. %@NL@%
  7699. main()%@NL@%
  7700. {%@NL@%
  7701.    int test = 10;%@NL@%
  7702. %@NL@%
  7703.    while( test > 0 )%@NL@%
  7704.    {%@NL@%
  7705.       printf( "test = %d\n", test );%@NL@%
  7706.       test = test - 2;%@NL@%
  7707.    }%@NL@%
  7708. }%@NL@%
  7709. %@NL@%
  7710. %@NL@%
  7711. %@2@%%@AH@%WRFILE.C%@AE@%%@EH@%%@NL@%
  7712. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC\QC25\WRFILE.C%@AE@%%@NL@%
  7713. %@NL@%
  7714. %@AB@%/* WRFILE.C: Creates and writes to a disk file. */%@AE@%%@NL@%
  7715. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7716. %@NL@%
  7717. main()%@NL@%
  7718. {%@NL@%
  7719.    FILE *fp;%@NL@%
  7720. %@NL@%
  7721.    if( (fp = fopen( "c:\\testfile.asc","w" )) != NULL )%@NL@%
  7722.    {%@NL@%
  7723.       fputs( "Example string", fp );%@NL@%
  7724.       fputc( '\n', fp );%@NL@%
  7725.       fclose( fp );%@NL@%
  7726.    }%@NL@%
  7727.    else%@NL@%
  7728.       printf( "error message\n" );%@NL@%
  7729. }%@NL@%
  7730. %@1@%%@AH@%Proficient C - Sample Code%@EH@%%@AE@%
  7731. %@NL@%
  7732. %@NL@%
  7733. %@2@%%@AH@%ANSI_CPR.C%@AE@%%@EH@%%@NL@%
  7734. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\13ANSI\ANSI_CPR.C%@AE@%%@NL@%
  7735. %@NL@%
  7736. %@AB@%/*%@NL@%
  7737. %@AB@% *        ansi_cpr -- report where the cursor is located%@NL@%
  7738. %@AB@% *        The position information is placed in the keyboard buffer%@NL@%
  7739. %@AB@% *        in the form ESC[rr;ccR where ESC is the value of the%@NL@%
  7740. %@AB@% *        ESCAPE character (\033) and r and c represent%@NL@%
  7741. %@AB@% *        decimal values of row and column data.%@NL@%
  7742. %@AB@% */%@AE@%%@NL@%
  7743. %@NL@%
  7744. %@AI@%#include %@AE@%<local\ansi.h> %@NL@%
  7745. %@NL@%
  7746. void%@NL@%
  7747. ansi_cpr(row, col)%@NL@%
  7748. int        *row,%@NL@%
  7749.         *col;%@NL@%
  7750. {%@NL@%
  7751.         int i;%@NL@%
  7752. %@NL@%
  7753.         %@AB@%/* request a cursor position report */%@AE@%%@NL@%
  7754.         ANSI_DSR;%@NL@%
  7755. %@NL@%
  7756.         %@AB@%/* toss the ESC and '[' */%@AE@%%@NL@%
  7757.         (void) getkey();%@NL@%
  7758.         (void) getkey();%@NL@%
  7759. %@NL@%
  7760.         %@AB@%/* read the row number */%@AE@%%@NL@%
  7761.         *row = 10 * (getkey() - '0');%@NL@%
  7762.         *row = *row + getkey() - '0';%@NL@%
  7763.         %@NL@%
  7764.         %@AB@%/* toss the ';' separator */%@AE@%%@NL@%
  7765.         (void) getkey();%@NL@%
  7766.         %@NL@%
  7767.         %@AB@%/* read the column number */%@AE@%%@NL@%
  7768.         *col = 10 * (getkey() - '0');%@NL@%
  7769.         *col = *col + getkey() - '0';%@NL@%
  7770. %@NL@%
  7771.         %@AB@%/* toss the trailing ('R') and return */%@AE@%%@NL@%
  7772.         (void) getkey();%@NL@%
  7773.         (void) getkey();%@NL@%
  7774.         return;%@NL@%
  7775. }%@NL@%
  7776. %@NL@%
  7777. %@NL@%
  7778. %@2@%%@AH@%ANSI_TST.C%@AE@%%@EH@%%@NL@%
  7779. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\13ANSI\ANSI_TST.C%@AE@%%@NL@%
  7780. %@NL@%
  7781. %@AB@%/*%@NL@%
  7782. %@AB@% *        ansi_tst -- verify that the ANSI.SYS driver is loaded%@NL@%
  7783. %@AB@% *        (prints message and exits is ANSI driver not working)%@NL@%
  7784. %@AB@% */%@AE@%%@NL@%
  7785. %@NL@%
  7786. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7787. %@AI@%#include %@AE@%<local\ansi.h> %@NL@%
  7788. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  7789. %@NL@%
  7790. %@AI@%#define %@AE@%TST_ROW        2 %@NL@%
  7791. %@AI@%#define %@AE@%TST_COL        75 %@NL@%
  7792. %@NL@%
  7793. void%@NL@%
  7794. ansi_tst()%@NL@%
  7795. {%@NL@%
  7796.         int row, col;%@NL@%
  7797.         static char *help[] = {%@NL@%
  7798.                 "\n",%@NL@%
  7799.                 "ANSI.SYS device driver not loaded:\n",%@NL@%
  7800.                 "  1. Copy ANSI.SYS to your system disk.\n",%@NL@%
  7801.                 "  2. Add the line device=ansi.sys to your\n",%@NL@%
  7802.                 "     CONFIG.SYS file and reboot your machine.\n",%@NL@%
  7803.                 NULL%@NL@%
  7804.         };%@NL@%
  7805.         char **msg;%@NL@%
  7806. %@NL@%
  7807.         extern int getstate();%@NL@%
  7808.         extern int readcur(int *, int *, int);%@NL@%
  7809. %@NL@%
  7810.         getstate();%@NL@%
  7811.         ANSI_CUP(TST_ROW, TST_COL);%@NL@%
  7812.         readcur(&row, &col, Vpage);%@NL@%
  7813.         if (row != TST_ROW - 1 || col != TST_COL - 1) {%@NL@%
  7814.                 for (msg = help; *msg != NULL; ++msg)%@NL@%
  7815.                         fputs(*msg, stderr);%@NL@%
  7816.                 exit(1);%@NL@%
  7817.         }%@NL@%
  7818. %@NL@%
  7819.         return;%@NL@%
  7820. }%@NL@%
  7821. %@NL@%
  7822. %@NL@%
  7823. %@2@%%@AH@%BEEP.C%@AE@%%@EH@%%@NL@%
  7824. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\BEEP.C%@AE@%%@NL@%
  7825. %@NL@%
  7826. %@AB@%/*%@NL@%
  7827. %@AB@% *        beep -- sound the terminal beeper%@NL@%
  7828. %@AB@% */%@AE@%%@NL@%
  7829. %@NL@%
  7830. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7831. %@NL@%
  7832. %@AI@%#define %@AE@%BEL        7 %@NL@%
  7833. %@NL@%
  7834. void%@NL@%
  7835. beep()%@NL@%
  7836. {%@NL@%
  7837.         putchar(BEL);%@NL@%
  7838. }%@NL@%
  7839. %@NL@%
  7840. %@NL@%
  7841. %@2@%%@AH@%CAT.C%@AE@%%@EH@%%@NL@%
  7842. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\06USER\CAT.C%@AE@%%@NL@%
  7843. %@NL@%
  7844. %@AB@%/*%@NL@%
  7845. %@AB@% *        cat -- concatenate files%@NL@%
  7846. %@AB@% */%@AE@%%@NL@%
  7847. %@NL@%
  7848. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  7849. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  7850. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  7851. %@NL@%
  7852. main(argc, argv)%@NL@%
  7853. int argc;%@NL@%
  7854. char **argv;%@NL@%
  7855. {%@NL@%
  7856.         int ch;%@NL@%
  7857.         char *cp;%@NL@%
  7858.         FILE *fp;%@NL@%
  7859.         BOOLEAN errflag, silent;%@NL@%
  7860.         static char pgm[MAXNAME + 1] = { "cat" };%@NL@%
  7861. %@NL@%
  7862.         extern void getpname(char *, char *);%@NL@%
  7863.         extern int fcopy(FILE *, FILE *);%@NL@%
  7864.         extern int getopt(int, char **, char *);%@NL@%
  7865.         extern int optind;%@NL@%
  7866.         extern char *optarg;%@NL@%
  7867. %@NL@%
  7868.         %@AB@%/* use an alias if one is given to this program */%@AE@%%@NL@%
  7869.         if (_osmajor >= 3)%@NL@%
  7870.                 getpname(*argv, pgm);%@NL@%
  7871. %@NL@%
  7872.         %@AB@%/* process optional arguments, if any */%@AE@%%@NL@%
  7873.         errflag = FALSE;%@NL@%
  7874.         silent = FALSE;%@NL@%
  7875.         while ((ch = getopt(argc, argv, "s")) != EOF)%@NL@%
  7876.                 switch (ch) {%@NL@%
  7877.                 case 's':%@NL@%
  7878.                         %@AB@%/* don't complain about non-existent files */%@AE@%%@NL@%
  7879.                         silent = TRUE;%@NL@%
  7880.                         break;%@NL@%
  7881.                 case '?':%@NL@%
  7882.                         %@AB@%/* say what? */%@AE@%%@NL@%
  7883.                         errflag = TRUE;%@NL@%
  7884.                         break;%@NL@%
  7885.                 }%@NL@%
  7886.         if (errflag == TRUE) {%@NL@%
  7887.                 fprintf(stderr, "Usage: %s [-s] file...\n", pgm);%@NL@%
  7888.                 exit(1);%@NL@%
  7889.         }%@NL@%
  7890. %@NL@%
  7891.         %@AB@%/* process any remaining arguments */%@AE@%%@NL@%
  7892.         argc -= optind;%@NL@%
  7893.         argv += optind;%@NL@%
  7894.         if (argc == 0)%@NL@%
  7895.                 %@AB@%/* no file names -- use standard input */%@AE@%%@NL@%
  7896.                 if (fcopy(stdin, stdout) != 0) {%@NL@%
  7897.                         fprintf(stderr, "error copying stdin");%@NL@%
  7898.                         exit(2);%@NL@%
  7899.                 }%@NL@%
  7900.                 else%@NL@%
  7901.                         exit(0);%@NL@%
  7902. %@NL@%
  7903.         %@AB@%/* copy the contents of each named file to standard output */%@AE@%%@NL@%
  7904.         for (; argc-- > 0; ++argv) {%@NL@%
  7905.                 if ((fp = fopen(*argv, "r")) == NULL) {%@NL@%
  7906.                         if (silent == FALSE)%@NL@%
  7907.                                 fprintf(stderr, "%s: can't open %s\n",%@NL@%
  7908.                                         pgm, *argv);%@NL@%
  7909.                         continue;%@NL@%
  7910.                 }%@NL@%
  7911.                 if (fcopy(fp, stdout) != 0) {%@NL@%
  7912.                         fprintf(stderr, "%s: Error while copying %s",%@NL@%
  7913.                                 pgm, *argv);%@NL@%
  7914.                         exit(3);%@NL@%
  7915.                 }%@NL@%
  7916.                 if (fclose(fp) != 0) {%@NL@%
  7917.                         fprintf(stderr, "%s: Error closing %s",%@NL@%
  7918.                                 pgm, *argv);%@NL@%
  7919.                         exit(4);%@NL@%
  7920.                 }%@NL@%
  7921.         }%@NL@%
  7922. %@NL@%
  7923.         exit(0);%@NL@%
  7924. }%@NL@%
  7925. %@NL@%
  7926. %@NL@%
  7927. %@2@%%@AH@%CLRSCRN.C%@AE@%%@EH@%%@NL@%
  7928. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\CLRSCRN.C%@AE@%%@NL@%
  7929. %@NL@%
  7930. %@AB@%/*%@NL@%
  7931. %@AB@% *        clrscrn -- clear the "visual" screen page%@NL@%
  7932. %@AB@% */%@AE@%%@NL@%
  7933. %@NL@%
  7934. %@AI@%#include %@AE@%<dos.h> %@NL@%
  7935. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  7936. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  7937. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  7938. %@NL@%
  7939. int%@NL@%
  7940. clrscrn(a)%@NL@%
  7941. unsigned int a;        %@AB@%/* video attribute for new lines */%@AE@%%@NL@%
  7942. {%@NL@%
  7943.         union REGS inregs, outregs;%@NL@%
  7944. %@NL@%
  7945.         inregs.h.ah = SCROLL_UP;%@NL@%
  7946.         inregs.h.al = 0;                %@AB@%/* blank entire window */%@AE@%%@NL@%
  7947.         inregs.h.bh = a;                %@AB@%/* use specified attribute */%@AE@%%@NL@%
  7948.         inregs.h.bl = 0;%@NL@%
  7949.         inregs.x.cx = 0;                %@AB@%/* upper left corner */%@AE@%%@NL@%
  7950.         inregs.h.dh = Maxrow[Vmode] - 1;%@AB@%/* bottom screen row */%@AE@%%@NL@%
  7951.         inregs.h.dl = Maxcol[Vmode] - 1;%@AB@%/* rightmost column */%@AE@%%@NL@%
  7952.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  7953. %@NL@%
  7954.         return (outregs.x.cflag);%@NL@%
  7955. }%@NL@%
  7956. %@NL@%
  7957. %@NL@%
  7958. %@2@%%@AH@%CLRW.C%@AE@%%@EH@%%@NL@%
  7959. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\CLRW.C%@AE@%%@NL@%
  7960. %@NL@%
  7961. %@AB@%/*%@NL@%
  7962. %@AB@% *        clrw -- clear specified region of "visual" screen page%@NL@%
  7963. %@AB@% */%@AE@%%@NL@%
  7964. %@NL@%
  7965. %@AI@%#include %@AE@%<dos.h> %@NL@%
  7966. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  7967. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  7968. %@NL@%
  7969. int%@NL@%
  7970. clrw(t, l, b, r, a)%@NL@%
  7971. int t;                %@AB@%/* top row of region to clear */%@AE@%%@NL@%
  7972. int l;                %@AB@%/* left column */%@AE@%%@NL@%
  7973. int b;                %@AB@%/* bottom row */%@AE@%%@NL@%
  7974. int r;                %@AB@%/* right column */%@AE@%%@NL@%
  7975. unsigned char a;%@AB@%/* attribute for cleared region */%@AE@%%@NL@%
  7976. {%@NL@%
  7977.         union REGS inregs, outregs;%@NL@%
  7978. %@NL@%
  7979.         inregs.h.ah = SCROLL_UP;%@AB@%/* scroll visual page up */%@AE@%%@NL@%
  7980.         inregs.h.al = 0;        %@AB@%/* blank entire window */%@AE@%%@NL@%
  7981.         inregs.h.bh = a;        %@AB@%/* attribute of blank lines */%@AE@%%@NL@%
  7982.         inregs.h.bl = 0;%@NL@%
  7983.         inregs.h.ch = t;        %@AB@%/* upper left of scroll region */%@AE@%%@NL@%
  7984.         inregs.h.cl = l;%@NL@%
  7985.         inregs.h.dh = b;        %@AB@%/* lower right of scroll region */%@AE@%%@NL@%
  7986.         inregs.h.dl = r;%@NL@%
  7987.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  7988. %@NL@%
  7989.         return (outregs.x.cflag);%@NL@%
  7990. }%@NL@%
  7991. %@NL@%
  7992. %@NL@%
  7993. %@2@%%@AH@%COLORNUM.C%@AE@%%@EH@%%@NL@%
  7994. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\13ANSI\COLORNUM.C%@AE@%%@NL@%
  7995. %@NL@%
  7996. %@AB@%/*%@NL@%
  7997. %@AB@% *        colornum -- return the IBM number for the color%@NL@%
  7998. %@AB@% *        presented as a string; return -1 if no match.%@NL@%
  7999. %@AB@% */%@AE@%%@NL@%
  8000. %@NL@%
  8001. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  8002. %@AI@%#include %@AE@%<string.h> %@NL@%
  8003. %@AI@%#include %@AE@%<local\ibmcolor.h> %@NL@%
  8004. %@NL@%
  8005. %@AI@%#define %@AE@%NCHARS        3 %@NL@%
  8006. %@NL@%
  8007. int%@NL@%
  8008. colornum(name)%@NL@%
  8009. char *name;%@NL@%
  8010. {%@NL@%
  8011.         register int n;%@NL@%
  8012.         static struct color_st {%@NL@%
  8013.                 char *c_name;%@NL@%
  8014.                 int c_num;%@NL@%
  8015.         } colortab[] = {%@NL@%
  8016.                 "black",        IBM_BLACK,%@NL@%
  8017.                 "blue",                IBM_BLUE,%@NL@%
  8018.                 "green",        IBM_GREEN,%@NL@%
  8019.                 "cyan",                IBM_CYAN,%@NL@%
  8020.                 "red",                IBM_RED,%@NL@%
  8021.                 "magenta",        IBM_MAGENTA,%@NL@%
  8022.                 "brown",        IBM_BROWN,%@NL@%
  8023.                 "white",        IBM_WHITE,%@NL@%
  8024.                 "normal",        IBM_NORMAL,%@NL@%
  8025.                 "bright",        IBM_BRIGHT,%@NL@%
  8026.                 "light",        IBM_BRIGHT,%@NL@%
  8027.                 "bold",                IBM_BRIGHT,%@NL@%
  8028.                 "yellow",        IBM_BROWN + IBM_BRIGHT,%@NL@%
  8029.                 "blink",        IBM_BLINK,%@NL@%
  8030.                 "reverse",        IBM_REVERSE,%@NL@%
  8031.                 "invisible",        IBM_INVISIBLE,%@NL@%
  8032.                 NULL,                (-1)%@NL@%
  8033.         };%@NL@%
  8034. %@NL@%
  8035.         (void) strlwr(name);%@NL@%
  8036.         for (n = 0; colortab[n].c_name != NULL; ++n)%@NL@%
  8037.                 if ((strncmp(name, colortab[n].c_name, NCHARS)) == 0)%@NL@%
  8038.                         return (colortab[n].c_num);%@NL@%
  8039.         return (-1);%@NL@%
  8040. }%@NL@%
  8041. %@NL@%
  8042. %@NL@%
  8043. %@2@%%@AH@%CP.C%@AE@%%@EH@%%@NL@%
  8044. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\03DOS\CP.C%@AE@%%@NL@%
  8045. %@NL@%
  8046. %@AB@%/*%@NL@%
  8047. %@AB@% *        cp -- a simplified copy command%@NL@%
  8048. %@AB@% */%@AE@%%@NL@%
  8049. %@NL@%
  8050. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  8051. %@NL@%
  8052. main()%@NL@%
  8053. {%@NL@%
  8054.         int ch;%@NL@%
  8055. %@NL@%
  8056.         while ((ch = getc(stdin)) != EOF)%@NL@%
  8057.                 putc(ch, stdout);%@NL@%
  8058.         exit(0);%@NL@%
  8059. }%@NL@%
  8060. %@NL@%
  8061. %@NL@%
  8062. %@2@%%@AH@%CPBLK.C%@AE@%%@EH@%%@NL@%
  8063. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\11SCREEN\CPBLK.C%@AE@%%@NL@%
  8064. %@NL@%
  8065. %@AB@%/*%@NL@%
  8066. %@AB@% *        cpblk -- copy a block of characters and attributes%@NL@%
  8067. %@AB@% *        while eliminating "snow" on a standard CGA display%@NL@%
  8068. %@AB@% */%@AE@%%@NL@%
  8069. %@NL@%
  8070. %@AI@%#include %@AE@%<conio.h> %@NL@%
  8071. %@AI@%#include %@AE@%<memory.h> %@NL@%
  8072. %@NL@%
  8073. %@AI@%#define %@AE@%BLKCNT        10 %@NL@%
  8074. %@AI@%#define %@AE@%VSTAT        0x3DA %@NL@%
  8075. %@AI@%#define %@AE@%VRBIT        8 %@NL@%
  8076. %@AI@%#define %@AE@%WRDCNT        200 %@NL@%
  8077. %@AI@%#define %@AE@%NBYTES        (2 * WRDCNT) %@NL@%
  8078. %@NL@%
  8079. %@AB@%/* macro to synchronize with vertical retrace period */%@AE@%%@NL@%
  8080. %@AI@%#define %@AE@%VSYNC        while ((inp(VSTAT) & VRBIT) == VRBIT); \ %@NL@%
  8081.                 while ((inp(VSTAT) & VRBIT) != VRBIT)%@NL@%
  8082. %@NL@%
  8083. int%@NL@%
  8084. cpblk(src_os, src_seg, dest_os, dest_seg)%@NL@%
  8085. unsigned int src_os, src_seg, dest_os, dest_seg;%@NL@%
  8086. {%@NL@%
  8087.         register int i;%@NL@%
  8088.         int n;%@NL@%
  8089.         register int delta;%@NL@%
  8090. %@NL@%
  8091.         n = 0;%@NL@%
  8092.         delta = 0;%@NL@%
  8093.         for (i = 0; i < BLKCNT ; ++i) {%@NL@%
  8094.                 %@AB@%/* copy a block of words during vertical retrace */%@AE@%%@NL@%
  8095.                 VSYNC;%@NL@%
  8096.                 movedata(src_seg, src_os + delta,%@NL@%
  8097.                         dest_seg, dest_os + delta, NBYTES);%@NL@%
  8098.                 n += WRDCNT;%@NL@%
  8099. %@NL@%
  8100.                 %@AB@%/* adjust buffer offset */%@AE@%%@NL@%
  8101.                 delta += NBYTES;%@NL@%
  8102.         }%@NL@%
  8103. %@NL@%
  8104.         return (n);%@NL@%
  8105. }%@NL@%
  8106. %@NL@%
  8107. %@NL@%
  8108. %@2@%%@AH@%CURBACK.C%@AE@%%@EH@%%@NL@%
  8109. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\CURBACK.C%@AE@%%@NL@%
  8110. %@NL@%
  8111. %@AB@%/*%@NL@%
  8112. %@AB@% *        curback -- move cursor back (left); return the%@NL@%
  8113. %@AB@% *        value of the new column position%@NL@%
  8114. %@AB@% */%@AE@%%@NL@%
  8115. %@NL@%
  8116. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  8117. %@NL@%
  8118. int%@NL@%
  8119. curback(n, pg)%@NL@%
  8120. int n, pg;%@NL@%
  8121. {%@NL@%
  8122.         int r, c;%@NL@%
  8123. %@NL@%
  8124.         %@AB@%/*%@NL@%
  8125. %@AB@%         *        move the cursor left by up to n positions but %@NL@%
  8126. %@AB@%         *        not past the beginning of the current line%@NL@%
  8127. %@AB@%         */%@AE@%%@NL@%
  8128.         readcur(&r, &c, pg);%@NL@%
  8129.         if (c - n < 0)%@NL@%
  8130.                 c = 0;%@NL@%
  8131.         putcur(r, c, pg);%@NL@%
  8132.         return (c);%@NL@%
  8133. }%@NL@%
  8134. %@NL@%
  8135. %@NL@%
  8136. %@2@%%@AH@%CURSOR.C%@AE@%%@EH@%%@NL@%
  8137. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\CURSOR.C%@AE@%%@NL@%
  8138. %@NL@%
  8139. %@AB@%/*%@NL@%
  8140. %@AB@% *        cursor -- interactively set cursor shape%@NL@%
  8141. %@AB@% */%@AE@%%@NL@%
  8142. %@NL@%
  8143. %@AI@%#include %@AE@%<dos.h> %@NL@%
  8144. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  8145. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  8146. %@AI@%#include %@AE@%<local\keydefs.h> %@NL@%
  8147. %@NL@%
  8148. %@AB@%/* additional drawing characters (others are defined in video.h) */%@AE@%%@NL@%
  8149. %@AI@%#define %@AE@%DOT        254 %@NL@%
  8150. %@AI@%#define %@AE@%NO_DOT        196 %@NL@%
  8151. %@AI@%#define %@AE@%D_POINT        31 %@NL@%
  8152. %@AI@%#define %@AE@%R_POINT 16 %@NL@%
  8153. %@AI@%#define %@AE@%L_POINT 17 %@NL@%
  8154. %@NL@%
  8155. %@AB@%/* dimensions of the help frame */%@AE@%%@NL@%
  8156. %@AI@%#define %@AE@%BOX_Y        6 %@NL@%
  8157. %@AI@%#define %@AE@%BOX_X        30 %@NL@%
  8158. %@NL@%
  8159. %@AB@%/* upper-left row and column of big cursor */%@AE@%%@NL@%
  8160. int Ulr;%@NL@%
  8161. int Ulc;%@NL@%
  8162. int Mid;%@NL@%
  8163. %@NL@%
  8164. %@AB@%/* cursor scan-line-selection modes */%@AE@%%@NL@%
  8165. typedef enum { STARTSCAN, ENDSCAN } CMODE;%@NL@%
  8166. %@NL@%
  8167. int%@NL@%
  8168. main()%@NL@%
  8169. {%@NL@%
  8170.         int i, j;%@NL@%
  8171.         int ch;%@NL@%
  8172.         int start, end;%@NL@%
  8173.         int height, width;%@NL@%
  8174.         static char spoint[] = { "Start\020" }; %@AB@%/* contains right pointer */%@AE@%%@NL@%
  8175.         static char epoint[] = { "\021Stop" };        %@AB@%/* contains left pointer */%@AE@%%@NL@%
  8176.         static char title[] = { "CURSOR: Control cursor shape (V1.0)" };%@NL@%
  8177.         unsigned char%@NL@%
  8178.                 oldattr,        %@AB@%/* video attribute upon entry */%@AE@%%@NL@%
  8179.                 headattr,        %@AB@%/* video attribute of header */%@AE@%%@NL@%
  8180.                 attr,                 %@AB@%/* primary video attribute */%@AE@%%@NL@%
  8181.                 standout;        %@AB@%/* highlighting video attribute */%@AE@%%@NL@%
  8182.         CMODE mode;%@NL@%
  8183. %@NL@%
  8184.         static void drawdspy(int, int, int, int, int);%@NL@%
  8185.         static void drawstart(int, char *);%@NL@%
  8186.         static void drawend(int, int, char *);%@NL@%
  8187.         static void drawactive(int, int, CMODE);%@NL@%
  8188.         static void showhelp(int, int);%@NL@%
  8189. %@NL@%
  8190.         %@AB@%/* get video information and initialize */%@AE@%%@NL@%
  8191.         getstate();%@NL@%
  8192.         Mid = Vwidth / 2;%@NL@%
  8193.         readca(&ch, &oldattr, Vpage);        %@AB@%/* preserve user's video attribute */%@AE@%%@NL@%
  8194.         getctype(&start, &end, Vpage);        %@AB@%/* and cursor shape */%@AE@%%@NL@%
  8195.         headattr = (WHT << 4) | BLK;%@NL@%
  8196. %@NL@%
  8197.         %@AB@%/* set parameters based on video mode (default = CGA) */%@AE@%%@NL@%
  8198.         height = width = 8;        %@AB@%/* use an 8 by 8 block character cell */%@AE@%%@NL@%
  8199.         attr = (BLU << 4) | CYAN | BRIGHT;%@NL@%
  8200.         standout = YEL;%@NL@%
  8201.         if (Vmode == MDA_M80) {%@NL@%
  8202.                 %@AB@%/* uses a 14 by 9 dot block character cell */%@AE@%%@NL@%
  8203.                 height = 14;%@NL@%
  8204.                 width = 9;%@NL@%
  8205.                 attr = NORMAL;%@NL@%
  8206.                 standout = BWHT;%@NL@%
  8207.         }%@NL@%
  8208.         setctype(height + 1, height + 1);        %@AB@%/* cursor off */%@AE@%%@NL@%
  8209. %@NL@%
  8210.         %@AB@%/* basic text and layout */%@AE@%%@NL@%
  8211.         Ulr = 2;%@NL@%
  8212.         Ulc = Mid - width / 2;%@NL@%
  8213.         clrscrn(attr);%@NL@%
  8214.         putcur(0, 0, Vpage);%@NL@%
  8215.         writeca(' ', headattr, Vwidth, Vpage);%@NL@%
  8216.         putcur(0, Mid - strlen(title) / 2, Vpage);%@NL@%
  8217.         writestr(title, Vpage);%@NL@%
  8218.         showhelp(Ulr + height + 1, Mid - BOX_X / 2);%@NL@%
  8219. %@NL@%
  8220.         %@AB@%/* interactively select cursor shape */%@AE@%%@NL@%
  8221.         mode = STARTSCAN;%@NL@%
  8222.         drawdspy(start, end, standout, width, height);%@NL@%
  8223.         drawstart(start, spoint);%@NL@%
  8224.         drawend(end, width, epoint);%@NL@%
  8225.         drawactive(height, width, mode);%@NL@%
  8226.         while (1) {%@NL@%
  8227.                 switch (ch = getkey()) {%@NL@%
  8228.                 case K_UP:%@NL@%
  8229.                         %@AB@%/* move up one scan line */%@AE@%%@NL@%
  8230.                         if (mode == STARTSCAN)%@NL@%
  8231.                                 drawstart(start--, "      ");%@NL@%
  8232.                         else%@NL@%
  8233.                                 drawend(end--, width, "     ");%@NL@%
  8234.                         break;%@NL@%
  8235.                 case K_DOWN:%@NL@%
  8236.                         %@AB@%/* move down one scan line */%@AE@%%@NL@%
  8237.                         if (mode == STARTSCAN)%@NL@%
  8238.                                 drawstart(start++, "      ");%@NL@%
  8239.                         else%@NL@%
  8240.                                 drawend(end++, width, "     ");%@NL@%
  8241.                         break;%@NL@%
  8242.                 case K_LEFT:%@NL@%
  8243.                         %@AB@%/* starting scan-line-selection mode */%@AE@%%@NL@%
  8244.                         mode = STARTSCAN;%@NL@%
  8245.                         drawactive(height, width, mode);%@NL@%
  8246.                         continue;%@NL@%
  8247.                 case K_RIGHT:%@NL@%
  8248.                         %@AB@%/* ending scan-line-selection mode */%@AE@%%@NL@%
  8249.                         mode = ENDSCAN;%@NL@%
  8250.                         drawactive(height, width, mode);%@NL@%
  8251.                         continue;%@NL@%
  8252.                 case K_RETURN:%@NL@%
  8253.                         %@AB@%/* set the new cursor shape */%@AE@%%@NL@%
  8254.                         setctype(start, end);%@NL@%
  8255.                         clrscrn(oldattr);%@NL@%
  8256.                         putcur(0, 0, Vpage);%@NL@%
  8257.                         exit(0);%@NL@%
  8258.                 }%@NL@%
  8259. %@NL@%
  8260.                 %@AB@%/* make corrections at cursor image boundaries */%@AE@%%@NL@%
  8261.                 if (start < 0)%@NL@%
  8262.                         start = 0;%@NL@%
  8263.                 else if (start > height)%@NL@%
  8264.                         start = height;%@NL@%
  8265.                 if (end < 0)%@NL@%
  8266.                         end = 0;%@NL@%
  8267.                 else if (end >= height)%@NL@%
  8268.                         end = height - 1;%@NL@%
  8269. %@NL@%
  8270.                 %@AB@%/* show updated cursor shape and pointers */%@AE@%%@NL@%
  8271.                 drawdspy(start, end, standout, width, height);%@NL@%
  8272.                 drawstart(start, spoint);%@NL@%
  8273.                 drawend(end, width, epoint);%@NL@%
  8274.         }%@NL@%
  8275. %@NL@%
  8276.         exit(0);%@NL@%
  8277. } %@AB@%/* end main() */%@AE@%%@NL@%
  8278. %@NL@%
  8279. %@NL@%
  8280. %@AB@%/*%@NL@%
  8281. %@AB@% *        drawdspy -- draw a magnified image of a cursor with the%@NL@%
  8282. %@AB@% *        currently active scan lines depicted as a sequence of dots%@NL@%
  8283. %@AB@% *        and inactive lines depicted as straight lines%@NL@%
  8284. %@AB@% */%@AE@%%@NL@%
  8285. %@NL@%
  8286. static void%@NL@%
  8287. drawdspy(s, e, a, w, h)%@NL@%
  8288. int s;        %@AB@%/* starting scan line */%@AE@%%@NL@%
  8289. int e;        %@AB@%/* ending scan line */%@AE@%%@NL@%
  8290. int a;        %@AB@%/* video attribute */%@AE@%%@NL@%
  8291. int w;        %@AB@%/* width */%@AE@%%@NL@%
  8292. int h;        %@AB@%/* height */%@AE@%%@NL@%
  8293. {%@NL@%
  8294.         int i;%@NL@%
  8295. %@NL@%
  8296.         %@AB@%/* display an exploded image of each scan line */%@AE@%%@NL@%
  8297.         for (i = 0; i < h; ++i) {%@NL@%
  8298.                 putcur(Ulr + i, Ulc, Vpage);%@NL@%
  8299.                 if (s >= h)%@NL@%
  8300.                         %@AB@%/* cursor is effectively off */%@AE@%%@NL@%
  8301.                         writeca(NO_DOT, a, w, Vpage);%@NL@%
  8302.                 else if ((s <= e && i >= s && i <= e) || %@AB@%/* a full block */%@AE@%%@NL@%
  8303.                         (s > e && (i <= e || i >= s)))         %@AB@%/* a split block */%@AE@%%@NL@%
  8304.                         writeca(DOT, a, w, Vpage);%@NL@%
  8305.                 else%@NL@%
  8306.                         %@AB@%/* outside start/end range */%@AE@%%@NL@%
  8307.                         writeca(NO_DOT, a, w, Vpage);%@NL@%
  8308.         }%@NL@%
  8309. } %@AB@%/* end drawdspy() */%@AE@%%@NL@%
  8310. %@NL@%
  8311. %@NL@%
  8312. %@AB@%/*%@NL@%
  8313. %@AB@% *        drawstart -- display a pointer to the displayed starting%@NL@%
  8314. %@AB@% *        scan line in the magnified cursor image%@NL@%
  8315. %@AB@% */%@AE@%%@NL@%
  8316. %@NL@%
  8317. static void%@NL@%
  8318. drawstart(s, sp)%@NL@%
  8319. int s;                %@AB@%/* starting scan line number */%@AE@%%@NL@%
  8320. char *sp;        %@AB@%/* visual pointer to the displayed starting scan line */%@AE@%%@NL@%
  8321. {%@NL@%
  8322.         putcur(Ulr + s, Ulc - strlen(sp), Vpage);%@NL@%
  8323.         putstr(sp, Vpage);%@NL@%
  8324. } %@AB@%/* end drawstart() */%@AE@%%@NL@%
  8325. %@NL@%
  8326. %@NL@%
  8327. %@AB@%/*%@NL@%
  8328. %@AB@% *        drawend -- display a pointer to the displayed ending%@NL@%
  8329. %@AB@% *        scan line in the magnified cursor image%@NL@%
  8330. %@AB@% */%@AE@%%@NL@%
  8331. %@NL@%
  8332. static void%@NL@%
  8333. drawend(e, w, ep)%@NL@%
  8334. int e;                %@AB@%/* ending scan line number */%@AE@%%@NL@%
  8335. int w;                %@AB@%/* width of the cursor image */%@AE@%%@NL@%
  8336. char *ep;        %@AB@%/* visual pointer to the displayed ending scan line */%@AE@%%@NL@%
  8337. {%@NL@%
  8338.         putcur(Ulr + e, Ulc + w, Vpage);%@NL@%
  8339.         putstr(ep, Vpage);%@NL@%
  8340. } %@AB@%/* end drawend() */%@AE@%%@NL@%
  8341. %@NL@%
  8342. static void%@NL@%
  8343. drawactive(h, w, m)%@NL@%
  8344. int h, w;%@NL@%
  8345. CMODE m;%@NL@%
  8346. {%@NL@%
  8347.         int col;%@NL@%
  8348. %@NL@%
  8349.         %@AB@%/* clear active selector row */%@AE@%%@NL@%
  8350.         putcur(Ulr - 1, Ulc, Vpage);%@NL@%
  8351.         writec(' ', w, Vpage);%@NL@%
  8352. %@NL@%
  8353.         %@AB@%/* point to active selector */%@AE@%%@NL@%
  8354.         col = (m == STARTSCAN) ? 0 : w - 1;%@NL@%
  8355.         putcur(Ulr - 1, Ulc + col, Vpage);%@NL@%
  8356.         writec(D_POINT, 1, Vpage);%@NL@%
  8357. } %@AB@%/* end drawactive() */%@AE@%%@NL@%
  8358. %@NL@%
  8359. %@AB@%/*%@NL@%
  8360. %@AB@% *        showhelp -- display a set of instructions about the%@NL@%
  8361. %@AB@% *        use of the cursor program in a fine-ruled box%@NL@%
  8362. %@AB@% */%@AE@%%@NL@%
  8363. %@NL@%
  8364. static void%@NL@%
  8365. showhelp(r, c)%@NL@%
  8366. int r, c;        %@AB@%/* upper-left corner of help frame */%@AE@%%@NL@%
  8367. {%@NL@%
  8368.         static char title[] = { " Instructions " };%@NL@%
  8369.         extern int drawbox(int, int, int, int, int);%@NL@%
  8370. %@NL@%
  8371.         %@AB@%/* fine-ruled box */%@AE@%%@NL@%
  8372.         clrw(r, c, r + BOX_Y, c + BOX_X, (WHT << 4) | GRN | BRIGHT);%@NL@%
  8373.         drawbox(r, c, r + BOX_Y, c + BOX_X, Vpage);%@NL@%
  8374. %@NL@%
  8375.         %@AB@%/* centered title */%@AE@%%@NL@%
  8376.         putcur(r, c + (BOX_X - strlen(title)) / 2, Vpage);%@NL@%
  8377.         putstr(title, Vpage);%@NL@%
  8378. %@NL@%
  8379.         %@AB@%/* display symbols and text using brute-force positioning */%@AE@%%@NL@%
  8380.         putcur(r + 2, c + 2, Vpage);%@NL@%
  8381.         put_ch(LEFTARROW, Vpage);%@NL@%
  8382.         put_ch(RIGHTARROW, Vpage);%@NL@%
  8383.         putstr("  Change selection mode", Vpage);%@NL@%
  8384.         putcur(r + 3, c + 2, Vpage);%@NL@%
  8385.         put_ch(UPARROW, Vpage);%@NL@%
  8386.         put_ch(DOWNARROW, Vpage);%@NL@%
  8387.         putstr("  Select scan lines", Vpage);%@NL@%
  8388.         putcur(r + 4, c + 2, Vpage);%@NL@%
  8389.         put_ch(L_POINT, Vpage);%@NL@%
  8390.         put_ch(LRC11, Vpage);%@NL@%
  8391.         putstr("  Set shape and exit", Vpage);%@NL@%
  8392. } %@AB@%/* end showhelp() */%@AE@%%@NL@%
  8393. %@NL@%
  8394. %@NL@%
  8395. %@2@%%@AH@%DELAY.C%@AE@%%@EH@%%@NL@%
  8396. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\DELAY.C%@AE@%%@NL@%
  8397. %@NL@%
  8398. %@AB@%/*%@NL@%
  8399. %@AB@% *        delay -- provide a delay of ** approximately ** the%@NL@%
  8400. %@AB@% *        specified duration (resolution is about 0.055 second)%@NL@%
  8401. %@AB@% */%@AE@%%@NL@%
  8402. %@NL@%
  8403. %@AI@%#include %@AE@%<local\timer.h> %@NL@%
  8404. %@NL@%
  8405. void%@NL@%
  8406. delay(d)%@NL@%
  8407. float d;        %@AB@%/* duration in seconds and fractional seconds */%@AE@%%@NL@%
  8408. {%@NL@%
  8409.         long ticks, then;%@NL@%
  8410.         extern long getticks();%@NL@%
  8411. %@NL@%
  8412.         %@AB@%/* convert duration to number of PC clock ticks */%@AE@%%@NL@%
  8413.         ticks = d * TICKRATE;%@NL@%
  8414. %@NL@%
  8415.         %@AB@%/* delay for the specified interval */%@AE@%%@NL@%
  8416.         then = getticks() + ticks;%@NL@%
  8417.         while (1)%@NL@%
  8418.                 if (getticks() >= then)%@NL@%
  8419.                         break;%@NL@%
  8420. }%@NL@%
  8421. %@NL@%
  8422. %@NL@%
  8423. %@2@%%@AH@%DRAWBOX.C%@AE@%%@EH@%%@NL@%
  8424. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\DRAWBOX.C%@AE@%%@NL@%
  8425. %@NL@%
  8426. %@AB@%/*%@NL@%
  8427. %@AB@% *        drawbox -- create a box with IBM line-drawing characters%@NL@%
  8428. %@AB@% */%@AE@%%@NL@%
  8429. %@NL@%
  8430. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  8431. %@NL@%
  8432. int%@NL@%
  8433. drawbox(top, lft, btm, rgt, pg)%@NL@%
  8434. int top, lft, btm, rgt, pg;%@NL@%
  8435. {%@NL@%
  8436.         int i;%@NL@%
  8437.         int x;        %@AB@%/* interior line length for top and bottom segments */%@AE@%%@NL@%
  8438. %@NL@%
  8439.         x = rgt - lft - 1;%@NL@%
  8440. %@NL@%
  8441.         %@AB@%/* draw the top row */%@AE@%%@NL@%
  8442.         putcur(top, lft, pg);%@NL@%
  8443.         put_ch(ULC11, pg);%@NL@%
  8444.         writec(HBAR1, x, pg);%@NL@%
  8445.         putcur(top, rgt, pg);%@NL@%
  8446.         put_ch(URC11, pg);%@NL@%
  8447. %@NL@%
  8448.         %@AB@%/* draw the sides */%@AE@%%@NL@%
  8449.         for (i = 1; i < btm - top; ++i)%@NL@%
  8450.         {%@NL@%
  8451.                 putcur(top + i, lft, pg);%@NL@%
  8452.                 put_ch(VBAR1, pg);%@NL@%
  8453.                 putcur(top + i, rgt, pg);%@NL@%
  8454.                 put_ch(VBAR1, pg);%@NL@%
  8455.         }%@NL@%
  8456. %@NL@%
  8457.         %@AB@%/* draw the bottom row */%@AE@%%@NL@%
  8458.         putcur(btm, lft, pg);%@NL@%
  8459.         put_ch(LLC11, pg);%@NL@%
  8460.         writec(HBAR1, x, pg);%@NL@%
  8461.         putcur(btm, rgt, pg);%@NL@%
  8462.         put_ch(LRC11, pg);%@NL@%
  8463. }%@NL@%
  8464. %@NL@%
  8465. %@NL@%
  8466. %@2@%%@AH@%DRVPATH.C%@AE@%%@EH@%%@NL@%
  8467. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\DOS\DRVPATH.C%@AE@%%@NL@%
  8468. %@NL@%
  8469. %@AB@%/*%@NL@%
  8470. %@AB@% *        drvpath -- convert a drive name to a full pathname%@NL@%
  8471. %@AB@% */%@AE@%%@NL@%
  8472. %@NL@%
  8473. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  8474. %@AI@%#include %@AE@%<dos.h> %@NL@%
  8475. %@AI@%#include %@AE@%<string.h> %@NL@%
  8476. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  8477. %@AI@%#include %@AE@%<local\doslib.h> %@NL@%
  8478. %@NL@%
  8479. char *%@NL@%
  8480. drvpath(path)%@NL@%
  8481. char path[];        %@AB@%/* path string */%@AE@%%@NL@%
  8482.                 %@AB@%/* must be large enough to hold a full DOS path + NUL */%@AE@%%@NL@%
  8483. {%@NL@%
  8484.         union REGS inregs, outregs;%@NL@%
  8485.         static int drive(char);%@NL@%
  8486. %@NL@%
  8487.         %@AB@%/* patch root directory onto drive name */%@AE@%%@NL@%
  8488.         strcat(path, "\\");%@NL@%
  8489. %@NL@%
  8490.         %@AB@%/* set current directory path for drive from DOS */%@AE@%%@NL@%
  8491.         inregs.h.ah = GET_CUR_DIR;%@NL@%
  8492.         inregs.h.dl = drive(path[0]);                %@AB@%/* convert to drive number */%@AE@%%@NL@%
  8493.         inregs.x.si = (unsigned)&path[3];        %@AB@%/* start of return string */%@AE@%%@NL@%
  8494.         intdos(&inregs, &outregs);%@NL@%
  8495. %@NL@%
  8496.         return (outregs.x.cflag ? (char *)NULL : path); %@NL@%
  8497. }%@NL@%
  8498. %@NL@%
  8499. static int%@NL@%
  8500. drive(dltr)%@NL@%
  8501. char dltr;        %@AB@%/* drive letter */%@AE@%%@NL@%
  8502. {%@NL@%
  8503.         %@AB@%/* 'A' (or 'a') => 1, 'B' (or 'b') => 2, etc. */%@AE@%%@NL@%
  8504.         return (tolower(dltr) - 'a' + 1);%@NL@%
  8505. }%@NL@%
  8506. %@NL@%
  8507. %@NL@%
  8508. %@2@%%@AH@%DSPYTYPE.C%@AE@%%@EH@%%@NL@%
  8509. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\11SCREEN\DSPYTYPE.C%@AE@%%@NL@%
  8510. %@NL@%
  8511. %@AB@%/*%@NL@%
  8512. %@AB@% *        dspytype -- determine display adapter type%@NL@%
  8513. %@AB@% */%@AE@%%@NL@%
  8514. %@NL@%
  8515. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  8516. %@AI@%#include %@AE@%<dos.h> %@NL@%
  8517. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  8518. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  8519. %@NL@%
  8520. %@AI@%#define %@AE@%MDA_SEG        0xB000 %@NL@%
  8521. %@AI@%#define %@AE@%CGA_SEG 0xB800 %@NL@%
  8522. %@NL@%
  8523. main()%@NL@%
  8524. {%@NL@%
  8525.         extern int memchk(unsigned int, unsigned int);%@NL@%
  8526.         int mdaflag, egaflag, cgaflag;%@NL@%
  8527.         int ega_mem, ega_mode;%@NL@%
  8528.         unsigned int features, switches;%@NL@%
  8529.         static int memtab[] = {%@NL@%
  8530.                 64, 128, 192, 256%@NL@%
  8531.         };%@NL@%
  8532. %@NL@%
  8533.         mdaflag = egaflag = cgaflag = 0;%@NL@%
  8534.         %@NL@%
  8535.         %@AB@%/* look for display adapters */%@AE@%%@NL@%
  8536.         if (ega_info(&ega_mem, &ega_mode, &features, &switches))%@NL@%
  8537.                 ++egaflag;%@NL@%
  8538.         fputs("Enhanced graphics adapter ", stdout);%@NL@%
  8539.         if (egaflag) {%@NL@%
  8540.                 fputs("installed\n", stdout);%@NL@%
  8541.                 fprintf(stdout, "EGA memory size = %d-KB\n", memtab[ega_mem]);%@NL@%
  8542.                 fprintf(stdout, "EGA is in %s mode\n",%@NL@%
  8543.                         ega_mode ? "monochrome" : "color");%@NL@%
  8544.         }%@NL@%
  8545.         else%@NL@%
  8546.                 fputs("not installed\n", stdout);%@NL@%
  8547. %@NL@%
  8548.         if (egaflag && ega_mode == 0) {%@NL@%
  8549.                 %@AB@%/* look for IBM monochrome memory */%@AE@%%@NL@%
  8550.                 if (memchk(MDA_SEG, 0))%@NL@%
  8551.                         ++mdaflag;%@NL@%
  8552.         }%@NL@%
  8553.         else {%@NL@%
  8554.                 %@AB@%/* look for IBM monochrome memory */%@AE@%%@NL@%
  8555.                 if (memchk(CGA_SEG, 0))%@NL@%
  8556.                         ++cgaflag;%@NL@%
  8557.         }%@NL@%
  8558.         fputs("Monochrome adapter ", stdout);%@NL@%
  8559.         if (mdaflag)%@NL@%
  8560.                 fputs("installed\n", stdout);%@NL@%
  8561.         else%@NL@%
  8562.                 fputs("not installed\n", stdout);%@NL@%
  8563.         fputs("Color/graphics adapter ", stdout);%@NL@%
  8564.         if (cgaflag)%@NL@%
  8565.                 fputs("installed\n", stdout);%@NL@%
  8566.         else%@NL@%
  8567.                 fputs("not installed\n", stdout);%@NL@%
  8568. %@NL@%
  8569.         %@AB@%/* report video settings */%@AE@%%@NL@%
  8570.         getstate();%@NL@%
  8571.         fprintf(stdout, "mode=%d width=%d page=%d\n", Vmode, Vwidth, Vpage);%@NL@%
  8572. %@NL@%
  8573.         exit(0);%@NL@%
  8574. }%@NL@%
  8575. %@NL@%
  8576. %@NL@%
  8577. %@2@%%@AH@%DUMP.C%@AE@%%@EH@%%@NL@%
  8578. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\10DUMP\DUMP.C%@AE@%%@NL@%
  8579. %@NL@%
  8580. %@AB@%/*%@NL@%
  8581. %@AB@% *        dump -- display contents of non-ASCII files in hex byte and%@NL@%
  8582. %@AB@% *        ASCII character forms (like the DOS debug dump option)%@NL@%
  8583. %@AB@% */%@AE@%%@NL@%
  8584. %@NL@%
  8585. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  8586. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  8587. %@AI@%#include %@AE@%<fcntl.h> %@NL@%
  8588. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  8589. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  8590. %@AI@%#include %@AE@%<io.h> %@NL@%
  8591. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  8592. %@NL@%
  8593. %@AI@%#define %@AE@%STDINPUT        0 %@NL@%
  8594. %@AI@%#define %@AE@%LINEWIDTH        80 %@NL@%
  8595. %@NL@%
  8596. main(argc,argv)%@NL@%
  8597. int argc;%@NL@%
  8598. char *argv[];%@NL@%
  8599. {%@NL@%
  8600.         int ch;%@NL@%
  8601.         BOOLEAN        sflag = FALSE,%@NL@%
  8602.                 vflag = FALSE,%@NL@%
  8603.                 errflag = FALSE;%@NL@%
  8604.         int fd;%@NL@%
  8605.         static char pgm[MAXNAME + 1] = { "dump" };%@NL@%
  8606. %@NL@%
  8607.         extern int getopt(int, char **, char *);%@NL@%
  8608.         extern char *optarg;%@NL@%
  8609.         extern int optind, opterr;%@NL@%
  8610.         extern void getpname(char *, char *);%@NL@%
  8611.         extern int hexdump(int, BOOLEAN);%@NL@%
  8612.         extern void fatal(char *, char *, int);%@NL@%
  8613. %@NL@%
  8614.         if (_osmajor >= 3)%@NL@%
  8615.                 getpname(*argv, pgm);%@NL@%
  8616. %@NL@%
  8617.         while ((ch = getopt(argc, argv, "sv")) != EOF)%@NL@%
  8618.                 switch (ch) {%@NL@%
  8619.                 case 's': %@AB@%/* strip -- convert all non-ASCII to '.' */%@AE@%%@NL@%
  8620.                         sflag = TRUE;%@NL@%
  8621.                         break;%@NL@%
  8622.                 case 'v': %@AB@%/* verbose -- tell user what's happening */%@AE@%%@NL@%
  8623.                         vflag = TRUE;%@NL@%
  8624.                         break;%@NL@%
  8625.                 case '?': %@AB@%/* bad option */%@AE@%%@NL@%
  8626.                         errflag = TRUE;%@NL@%
  8627.                         break;%@NL@%
  8628.                 }%@NL@%
  8629. %@NL@%
  8630.         if (errflag == TRUE) {%@NL@%
  8631.                 fprintf(stderr, "Usage: %s [-sv] [file...]\n", pgm);%@NL@%
  8632.                 exit(1);%@NL@%
  8633.         }%@NL@%
  8634. %@NL@%
  8635.         if (optind == argc) {%@NL@%
  8636.                 if (setmode(STDINPUT, O_BINARY) == -1)%@NL@%
  8637.                         fatal(pgm, "Cannot set binary mode", 2);%@NL@%
  8638.                 hexdump(STDINPUT, sflag);%@NL@%
  8639.                 exit(0);%@NL@%
  8640.         }%@NL@%
  8641. %@NL@%
  8642.         for ( ; optind < argc; ++optind) {%@NL@%
  8643.                 if ((fd = open(argv[optind], O_BINARY | O_RDONLY)) == -1) {%@NL@%
  8644.                         fprintf(stderr,%@NL@%
  8645.                                 "%s: Error opening %s -- ", pgm, argv[optind]);%@NL@%
  8646.                         perror("");%@NL@%
  8647.                         continue;%@NL@%
  8648.                 }%@NL@%
  8649.                 if (vflag == TRUE)%@NL@%
  8650.                         fprintf(stdout, "\n%s:\n", argv[optind]);%@NL@%
  8651.                 if (hexdump(fd, sflag) == FAILURE) {%@NL@%
  8652.                         fprintf(stderr,%@NL@%
  8653.                                 "%s: Error reading %s -- ", pgm, argv[optind]);%@NL@%
  8654.                         perror("");%@NL@%
  8655.                 }%@NL@%
  8656.                 if (close(fd) == -1)%@NL@%
  8657.                         fatal(pgm, "Error closing input file", 3);%@NL@%
  8658.         }%@NL@%
  8659. %@NL@%
  8660.         exit(0);%@NL@%
  8661. }%@NL@%
  8662. %@NL@%
  8663. %@NL@%
  8664. %@2@%%@AH@%EGA_INFO.C%@AE@%%@EH@%%@NL@%
  8665. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\EGA_INFO.C%@AE@%%@NL@%
  8666. %@NL@%
  8667. %@AB@%/*%@NL@%
  8668. %@AB@% *        ega_info -- gather information about an EGA;%@NL@%
  8669. %@AB@% *        return a non-zero value if one is found%@NL@%
  8670. %@AB@% */%@AE@%%@NL@%
  8671. %@NL@%
  8672. %@AI@%#include %@AE@%<dos.h> %@NL@%
  8673. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  8674. %@NL@%
  8675. %@AI@%#define %@AE@%EGA_INFO 0x10 %@NL@%
  8676. %@AI@%#define %@AE@%NMODES        2 %@NL@%
  8677. %@AI@%#define %@AE@%NMEMSIZ        4 %@NL@%
  8678. %@NL@%
  8679. int%@NL@%
  8680. ega_info(memsize, mode, features, switches)%@NL@%
  8681. int *memsize;                %@AB@%/* EGA memory size indicator: 0 = 64K */%@AE@%%@NL@%
  8682.                         %@AB@%/* 1 = 128K; 2 = 192K; 3 = 256K */%@AE@%%@NL@%
  8683. int *mode;                %@AB@%/* 0 = color mode; 1 = mono mode */%@AE@%%@NL@%
  8684.                         %@AB@%/* use getstate function to find out which mode */%@AE@%%@NL@%
  8685. unsigned int%@NL@%
  8686.         *features,        %@AB@%/* feature bit settings */%@AE@%%@NL@%
  8687.         *switches;        %@AB@%/* EGA switch settings */%@AE@%%@NL@%
  8688. {%@NL@%
  8689.         int result = 0;%@NL@%
  8690.         union REGS inregs, outregs;%@NL@%
  8691. %@NL@%
  8692.         %@AB@%/* request EGA information */%@AE@%%@NL@%
  8693.         inregs.h.ah = ALT_FUNCTION;%@NL@%
  8694.         inregs.h.bl = EGA_INFO;%@NL@%
  8695.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  8696. %@NL@%
  8697.         *memsize = outregs.h.bl;%@NL@%
  8698.         *mode = outregs.h.bh;%@NL@%
  8699.         *features = outregs.h.ch;%@NL@%
  8700.         *switches = outregs.h.cl;%@NL@%
  8701. %@NL@%
  8702.         %@AB@%/* return non-zero if EGA installed */%@AE@%%@NL@%
  8703.         if (*memsize >= 0 && *memsize < NMEMSIZ && *mode >= 0 && *mode < NMODES)%@NL@%
  8704.                 result = 1;%@NL@%
  8705.         return (result);%@NL@%
  8706. }%@NL@%
  8707. %@NL@%
  8708. %@NL@%
  8709. %@2@%%@AH@%EQUIPCHK.C%@AE@%%@EH@%%@NL@%
  8710. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\EQUIPCHK.C%@AE@%%@NL@%
  8711. %@NL@%
  8712. %@AB@%/*%@NL@%
  8713. %@AB@% *        equipchk -- get equipment list%@NL@%
  8714. %@AB@% */%@AE@%%@NL@%
  8715. %@NL@%
  8716. %@AI@%#include %@AE@%<dos.h> %@NL@%
  8717. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  8718. %@AI@%#include %@AE@%<local\equip.h> %@NL@%
  8719. %@NL@%
  8720. struct EQUIP Eq;%@NL@%
  8721. %@NL@%
  8722. int%@NL@%
  8723. equipchk()%@NL@%
  8724. {%@NL@%
  8725.         union REGS inregs, outregs;%@NL@%
  8726. %@NL@%
  8727.         %@AB@%/* call BIOS equipment check routine */%@AE@%%@NL@%
  8728.         int86(EQUIP_CK, &inregs, &outregs);%@NL@%
  8729. %@NL@%
  8730.         %@AB@%/* extract data from returned data word */%@AE@%%@NL@%
  8731.         Eq.nprint  = (outregs.x.ax & 0xC000) / 0x8000;        %@NL@%
  8732.         Eq.game_io = ((outregs.x.ax & 0x1000) / 0x1000) ? 1 : 0;%@NL@%
  8733.         Eq.nrs232  = (outregs.x.ax & 0x0E00) /0x0200;%@NL@%
  8734.         Eq.ndrive  = ((outregs.x.ax & 0x00C0) / 0x0040) + 1;%@NL@%
  8735.         Eq.vmode   = (outregs.x.ax & 0x0030) / 0x0010;%@NL@%
  8736.         Eq.basemem = ((outregs.x.ax & 0x000C) / 0x0004) + 1;%@NL@%
  8737.         Eq.disksys = outregs.x.ax & 0x0001 == 1;%@NL@%
  8738. %@NL@%
  8739.         return (outregs.x.cflag);%@NL@%
  8740. }%@NL@%
  8741. %@NL@%
  8742. %@NL@%
  8743. %@2@%%@AH@%FATAL.C%@AE@%%@EH@%%@NL@%
  8744. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\FATAL.C%@AE@%%@NL@%
  8745. %@NL@%
  8746. %@AB@%/*%@NL@%
  8747. %@AB@% *  fatal -- issue a diagnostic message and terminate%@NL@%
  8748. %@AB@% */%@AE@%%@NL@%
  8749. %@NL@%
  8750. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  8751. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  8752. %@NL@%
  8753. void%@NL@%
  8754. fatal(pname, mesg, errlevel)%@NL@%
  8755. char *pname;        %@AB@%/* program name */%@AE@%%@NL@%
  8756. char *mesg;        %@AB@%/* message text */%@AE@%%@NL@%
  8757. int errlevel;        %@AB@%/* errorlevel (exit code) */%@AE@%%@NL@%
  8758. {%@NL@%
  8759.         %@AB@%/* display error message */%@AE@%%@NL@%
  8760.         fputs(pname, stderr);%@NL@%
  8761.         fputc(':', stderr);%@NL@%
  8762.         fputc(' ', stderr);%@NL@%
  8763.         fputs(mesg, stderr);%@NL@%
  8764. %@NL@%
  8765.         %@AB@%/* return to DOS with the specified errorlevel */%@AE@%%@NL@%
  8766.         exit(errlevel);%@NL@%
  8767. }%@NL@%
  8768. %@NL@%
  8769. %@NL@%
  8770. %@2@%%@AH@%FCONFIG.C%@AE@%%@EH@%%@NL@%
  8771. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\FCONFIG.C%@AE@%%@NL@%
  8772. %@NL@%
  8773. %@AB@%/*%@NL@%
  8774. %@AB@% *        fconfig -- return a FILE pointer to a local or%@NL@%
  8775. %@AB@% *        global configuration file, or NULL if none found%@NL@%
  8776. %@AB@% */%@AE@%%@NL@%
  8777. %@NL@%
  8778. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  8779. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  8780. %@AI@%#include %@AE@%<string.h> %@NL@%
  8781. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  8782. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  8783. %@NL@%
  8784. FILE *%@NL@%
  8785. fconfig(varname, fname)%@NL@%
  8786. char *varname;%@NL@%
  8787. char *fname;%@NL@%
  8788. {%@NL@%
  8789.         FILE *fp;%@NL@%
  8790.         char pname[MAXPATH + 1];%@NL@%
  8791.         char *p;%@NL@%
  8792.         %@NL@%
  8793.         %@AB@%/* look for a local configuration file */%@AE@%%@NL@%
  8794.         if ((fp = fopen(fname, "r")) != NULL)%@NL@%
  8795.                 return (fp);%@NL@%
  8796. %@NL@%
  8797.         %@AB@%/* look for a directory variable */%@AE@%%@NL@%
  8798.         if ((p = getenv(strupr(varname))) != NULL) {%@NL@%
  8799.                 strcpy(pname, p);%@NL@%
  8800.                 strcat(pname, "\\");%@NL@%
  8801.                 strcat(pname, fname);%@NL@%
  8802.                 if ((fp = fopen(pname, "r")) != NULL)%@NL@%
  8803.                         return (fp);%@NL@%
  8804.         }%@NL@%
  8805.         %@NL@%
  8806.         %@AB@%/* didn't find anything to read */%@AE@%%@NL@%
  8807.         return (NULL);%@NL@%
  8808. }%@NL@%
  8809. %@NL@%
  8810. %@NL@%
  8811. %@2@%%@AH@%FCOPY.C%@AE@%%@EH@%%@NL@%
  8812. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\FCOPY.C%@AE@%%@NL@%
  8813. %@NL@%
  8814. %@AB@%/*%@NL@%
  8815. %@AB@% *        fcopy -- copy input stream (fin) to output stream%@NL@%
  8816. %@AB@% *        (fout) and return an indication of success or failure%@NL@%
  8817. %@AB@% */%@AE@%%@NL@%
  8818. %@NL@%
  8819. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  8820. %@NL@%
  8821. %@AI@%#define %@AE@%BUFLEN        1024 %@NL@%
  8822. %@NL@%
  8823. int%@NL@%
  8824. fcopy(fin, fout)%@NL@%
  8825. FILE *fin, *fout;%@NL@%
  8826. {%@NL@%
  8827.         int errcount = 0;%@NL@%
  8828.         char line[BUFLEN];%@NL@%
  8829.         register char *s;%@NL@%
  8830. %@NL@%
  8831.         while ((s = fgets(line, BUFLEN, fin)) != NULL)%@NL@%
  8832.                 if (fputs(s, fout) == EOF)%@NL@%
  8833.                         ++errcount;%@NL@%
  8834.         if (ferror(fin))%@NL@%
  8835.                 ++errcount;%@NL@%
  8836.         return (errcount);        %@AB@%/* 0 if all went well */%@AE@%%@NL@%
  8837. }%@NL@%
  8838. %@NL@%
  8839. %@NL@%
  8840. %@2@%%@AH@%FIRST_FM.C%@AE@%%@EH@%%@NL@%
  8841. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\DOS\FIRST_FM.C%@AE@%%@NL@%
  8842. %@NL@%
  8843. %@AB@%/*%@NL@%
  8844. %@AB@% *        first_fm - find first file match in work directory%@NL@%
  8845. %@AB@% */%@AE@%%@NL@%
  8846. %@NL@%
  8847. %@AI@%#include %@AE@%<dos.h> %@NL@%
  8848. %@AI@%#include %@AE@%<local\doslib.h> %@NL@%
  8849. %@NL@%
  8850. int%@NL@%
  8851. first_fm(path, fa)%@NL@%
  8852. char *path;        %@AB@%/* pathname of directory */%@AE@%%@NL@%
  8853. int fa;                %@AB@%/* attribute(s) of file to match */%@AE@%%@NL@%
  8854. {%@NL@%
  8855.         union REGS inregs, outregs;%@NL@%
  8856. %@NL@%
  8857.         %@AB@%/* find first matching file */%@AE@%%@NL@%
  8858.         inregs.h.ah = FIND_FIRST;%@NL@%
  8859.         inregs.x.cx = fa;                %@NL@%
  8860.         inregs.x.dx = (unsigned int)path;%@NL@%
  8861.         (void)intdos(&inregs, &outregs);%@NL@%
  8862. %@NL@%
  8863.         return (outregs.x.cflag);%@NL@%
  8864. }%@NL@%
  8865. %@NL@%
  8866. %@NL@%
  8867. %@2@%%@AH@%GETCTYPE.C%@AE@%%@EH@%%@NL@%
  8868. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\GETCTYPE.C%@AE@%%@NL@%
  8869. %@NL@%
  8870. %@AB@%/*%@NL@%
  8871. %@AB@% *        getctype -- pass back cursor type info (scan lines) %@NL@%
  8872. %@AB@% */%@AE@%%@NL@%
  8873. %@NL@%
  8874. %@AI@%#include %@AE@%<dos.h> %@NL@%
  8875. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  8876. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  8877. %@NL@%
  8878. %@AI@%#define %@AE@%LO_NIBBLE        0x0F %@NL@%
  8879. %@NL@%
  8880. int%@NL@%
  8881. getctype(start_scan, end_scan, pg)%@NL@%
  8882. int *start_scan;%@AB@%/* starting scan line */%@AE@%%@NL@%
  8883. int *end_scan;        %@AB@%/* ending scan line */%@AE@%%@NL@%
  8884. int pg;                %@AB@%/* "visual" page */%@AE@%%@NL@%
  8885. {%@NL@%
  8886.         union REGS inregs, outregs;%@NL@%
  8887. %@NL@%
  8888.         inregs.h.bh = pg;%@NL@%
  8889.         inregs.h.ah = GET_CUR;%@NL@%
  8890. %@NL@%
  8891.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  8892. %@NL@%
  8893.         %@AB@%/* end_scan = low 4 bits of cl */%@AE@%%@NL@%
  8894.         *end_scan = outregs.h.cl & LO_NIBBLE;%@NL@%
  8895. %@NL@%
  8896.         %@AB@%/* starting_scan = low 4 bits of ah */%@AE@%%@NL@%
  8897.         *start_scan = outregs.h.ch & LO_NIBBLE;%@NL@%
  8898. %@NL@%
  8899.         return (outregs.x.cflag);%@NL@%
  8900. }%@NL@%
  8901. %@NL@%
  8902. %@NL@%
  8903. %@2@%%@AH@%GETDRIVE.C%@AE@%%@EH@%%@NL@%
  8904. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\DOS\GETDRIVE.C%@AE@%%@NL@%
  8905. %@NL@%
  8906. %@AB@%/*%@NL@%
  8907. %@AB@% *        getdrive -- return the number of the default drive%@NL@%
  8908. %@AB@% */%@AE@%%@NL@%
  8909. %@NL@%
  8910. %@AI@%#include %@AE@%<dos.h> %@NL@%
  8911. %@AI@%#include %@AE@%<local\doslib.h> %@NL@%
  8912. %@NL@%
  8913. int getdrive()%@NL@%
  8914. {%@NL@%
  8915.         return (bdos(CURRENT_DISK, 0, 0));%@NL@%
  8916. }%@NL@%
  8917. %@NL@%
  8918. %@NL@%
  8919. %@2@%%@AH@%GETKEY.C%@AE@%%@EH@%%@NL@%
  8920. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\DOS\GETKEY.C%@AE@%%@NL@%
  8921. %@NL@%
  8922. %@AB@%/*%@NL@%
  8923. %@AB@% *        getkey -- return a code for single combo keystrokes%@NL@%
  8924. %@AB@% *        - returns a unique code for each keystroke or combination%@NL@%
  8925. %@AB@% *        - ignores "Ctrl-Break" input%@NL@%
  8926. %@AB@% */%@AE@%%@NL@%
  8927. %@NL@%
  8928. %@AI@%#include %@AE@%<dos.h> %@NL@%
  8929. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  8930. %@AI@%#include %@AE@%<local\doslib.h> %@NL@%
  8931. %@AI@%#include %@AE@%<local\keydefs.h> %@NL@%
  8932. %@NL@%
  8933. int%@NL@%
  8934. getkey()%@NL@%
  8935. {%@NL@%
  8936.         int ch;%@NL@%
  8937. %@NL@%
  8938.         %@AB@%/* normal key codes */%@AE@%%@NL@%
  8939.         if ((ch = bdos(KEYIN, 0, 0) & LOBYTE) != '\0')%@NL@%
  8940.                 return (ch);%@NL@%
  8941. %@NL@%
  8942.         %@AB@%/* convert scan codes to unique internal codes */%@AE@%%@NL@%
  8943.         return ((bdos(KEYIN, 0, 0) & LOBYTE) | XF);%@NL@%
  8944. }%@NL@%
  8945. %@NL@%
  8946. %@NL@%
  8947. %@2@%%@AH@%GETNAME.C%@AE@%%@EH@%%@NL@%
  8948. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\GETNAME.C%@AE@%%@NL@%
  8949. %@NL@%
  8950. %@AB@%/*%@NL@%
  8951. %@AB@% *        getname -- strip drive identifier and directory node%@NL@%
  8952. %@AB@% *        list, if any, from a pathname;  returns a pointer to%@NL@%
  8953. %@AB@% *        the resulting filename[.ext] string or NULL for error%@NL@%
  8954. %@AB@% */%@AE@%%@NL@%
  8955. %@NL@%
  8956. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  8957. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  8958. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  8959. %@NL@%
  8960. %@AI@%#define %@AE@%MAXFSPEC 13 %@NL@%
  8961. %@NL@%
  8962. char *%@NL@%
  8963. getname(path)%@NL@%
  8964. char *path;        %@AB@%/* string to modify */%@AE@%%@NL@%
  8965. {%@NL@%
  8966.         register char *cp;        %@AB@%/* character pointer */%@AE@%%@NL@%
  8967.         struct stat buf;%@NL@%
  8968. %@NL@%
  8969.         %@AB@%/* try to get information about the pathname */%@AE@%%@NL@%
  8970.         if (stat(path, &buf) != 0)%@NL@%
  8971.                 return (NULL);        %@AB@%/* bad pathname */%@AE@%%@NL@%
  8972. %@NL@%
  8973.         %@AB@%/* locate the end of the pathname string */%@AE@%%@NL@%
  8974.         cp = path;%@NL@%
  8975.         while (*cp != '\0')%@NL@%
  8976.                 ++cp;%@NL@%
  8977.         --cp;                %@AB@%/* went one too far */%@AE@%%@NL@%
  8978. %@NL@%
  8979.         %@AB@%/* find the start of the filename part */%@AE@%%@NL@%
  8980.         while (cp > path && *cp != '\\' && *cp != ':' && *cp != '/')%@NL@%
  8981.                 --cp;%@NL@%
  8982.         if (cp > path)%@NL@%
  8983.                 ++cp;        %@AB@%/* on a separator (\, :, or /) -- move one past */%@AE@%%@NL@%
  8984. %@NL@%
  8985.         %@AB@%/* return the full filespec (filename[.ext]) */%@AE@%%@NL@%
  8986.         return (cp);%@NL@%
  8987. }%@NL@%
  8988. %@NL@%
  8989. %@NL@%
  8990. %@2@%%@AH@%GETOPT.C%@AE@%%@EH@%%@NL@%
  8991. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\GETOPT.C%@AE@%%@NL@%
  8992. %@NL@%
  8993. %@AB@%/*%@NL@%
  8994. %@AB@% *      Copyright (c) 1984, 1985 AT&T%@NL@%
  8995. %@AB@% *      All Rights Reserved%@NL@%
  8996. %@AB@% *%@NL@%
  8997. %@AB@% *      ----- Author's Note -----%@NL@%
  8998. %@AB@% *      getopt() is reproduced with permission of the AT&T UNIX(R) System%@NL@%
  8999. %@AB@% *      Toolchest. This is a public domain version of getopt(3) that is%@NL@%
  9000. %@AB@% *      distributed to registered Toolchest participants.%@NL@%
  9001. %@AB@% *      Defining DOS_MODS alters the code slightly to obtain compatibility%@NL@%
  9002. %@AB@% *      with DOS and support libraries provided with most DOS C compilers.%@NL@%
  9003. %@AB@% */%@AE@%%@NL@%
  9004. %@NL@%
  9005. %@AI@%#define %@AE@%DOS_MODS %@NL@%
  9006. %@NL@%
  9007. %@AI@%#if %@AE@%defined (DOS_MODS) %@NL@%
  9008.         %@AB@%/* getopt() for DOS */%@AE@%%@NL@%
  9009. %@AI@%#else %@AE@%%@NL@%
  9010. %@AI@%#ident %@AE@% "@(#)getopt.c   1.9" %@NL@%
  9011. %@AI@%#endif %@AE@%%@NL@%
  9012. %@NL@%
  9013. %@AB@%/*      3.0 SID #       1.2     */%@AE@%%@NL@%
  9014. %@AB@%/*LINTLIBRARY*/%@AE@%%@NL@%
  9015. %@AI@%#define %@AE@%NULL    0 %@NL@%
  9016. %@AI@%#define %@AE@%EOF     (-1) %@NL@%
  9017. %@NL@%
  9018. %@AB@%/*%@NL@%
  9019. %@AB@% *      For this to work under versions of DOS prior to 3.00, argv[0]%@NL@%
  9020. %@AB@% *      must be set in main() to point to a valid program name or a%@NL@%
  9021. %@AB@% *      reasonable substitute string.   (ARH, 10-8-86)%@NL@%
  9022. %@AB@% */%@AE@%%@NL@%
  9023. %@AI@%#define %@AE@%ERR(s, c)       if(opterr){\ %@NL@%
  9024.         char errbuf[2];\%@NL@%
  9025.         errbuf[0] = c; errbuf[1] = '\n';\%@NL@%
  9026.         (void) write(2, argv[0], (unsigned)strlen(argv[0]));\%@NL@%
  9027.         (void) write(2, s, (unsigned)strlen(s));\%@NL@%
  9028.         (void) write(2, errbuf, 2);}%@NL@%
  9029. %@NL@%
  9030. %@AI@%#if %@AE@%defined (DOS_MODS) %@NL@%
  9031. %@AB@%/* permit function prototyping under DOS */%@AE@%%@NL@%
  9032. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  9033. %@AI@%#include %@AE@%<string.h> %@NL@%
  9034. %@AI@%#else %@AE@%%@NL@%
  9035. %@AB@%/* standard UNIX declarations */%@AE@%%@NL@%
  9036. extern int strcmp();%@NL@%
  9037. extern char *strchr();%@NL@%
  9038. %@AB@%/*%@NL@%
  9039. %@AB@% *      The following line was moved here from the ERR definition%@NL@%
  9040. %@AB@% *      to prevent a "duplicate definition" error message when the%@NL@%
  9041. %@AB@% *      code is compiled under DOS.  (ARH, 10-8-86)%@NL@%
  9042. %@AB@% */%@AE@%%@NL@%
  9043. extern int strlen(), write();%@NL@%
  9044. %@AI@%#endif %@AE@%%@NL@%
  9045. %@NL@%
  9046. int     opterr = 1;%@NL@%
  9047. int     optind = 1;%@NL@%
  9048. int     optopt;%@NL@%
  9049. char    *optarg;%@NL@%
  9050. %@NL@%
  9051. int%@NL@%
  9052. getopt(argc, argv, opts)%@NL@%
  9053. int     argc;%@NL@%
  9054. char    **argv, *opts;%@NL@%
  9055. {%@NL@%
  9056.         static int sp = 1;%@NL@%
  9057.         register int c;%@NL@%
  9058.         register char *cp;%@NL@%
  9059. %@NL@%
  9060.         if(sp == 1)%@NL@%
  9061.                 if(optind >= argc ||%@NL@%
  9062.                    argv[optind][0] != '-' || argv[optind][1] == '\0')%@NL@%
  9063.                         return(EOF);%@NL@%
  9064.                 else if(strcmp(argv[optind], "--") == NULL) {%@NL@%
  9065.                         optind++;%@NL@%
  9066.                         return(EOF);%@NL@%
  9067.                 }%@NL@%
  9068.         optopt = c = argv[optind][sp];%@NL@%
  9069.         if(c == ':' || (cp=strchr(opts, c)) == NULL) {%@NL@%
  9070.                 ERR(": illegal option -- ", c);%@NL@%
  9071.                 if(argv[optind][++sp] == '\0') {%@NL@%
  9072.                         optind++;%@NL@%
  9073.                         sp = 1;%@NL@%
  9074.                 }%@NL@%
  9075.                 return('?');%@NL@%
  9076.         }%@NL@%
  9077.         if(*++cp == ':') {%@NL@%
  9078.                 if(argv[optind][sp+1] != '\0')%@NL@%
  9079.                         optarg = &argv[optind++][sp+1];%@NL@%
  9080.                 else if(++optind >= argc) {%@NL@%
  9081.                         ERR(": option requires an argument -- ", c);%@NL@%
  9082.                         sp = 1;%@NL@%
  9083.                         return('?');%@NL@%
  9084.                 } else%@NL@%
  9085.                         optarg = argv[optind++];%@NL@%
  9086.                 sp = 1;%@NL@%
  9087.         } else {%@NL@%
  9088.                 if(argv[optind][++sp] == '\0') {%@NL@%
  9089.                         sp = 1;%@NL@%
  9090.                         optind++;%@NL@%
  9091.                 }%@NL@%
  9092.                 optarg = NULL;%@NL@%
  9093.         }%@NL@%
  9094.         return(c);%@NL@%
  9095. }%@NL@%
  9096. %@NL@%
  9097. %@NL@%
  9098. %@2@%%@AH@%GETPNAME.C%@AE@%%@EH@%%@NL@%
  9099. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\GETPNAME.C%@AE@%%@NL@%
  9100. %@NL@%
  9101. %@AB@%/*%@NL@%
  9102. %@AB@% *        getpname -- extract the base name of a program from%@NL@%
  9103. %@AB@% *        the pathname string (deletes a drive specifier, any%@NL@%
  9104. %@AB@% *        leading path node information, and the extension)%@NL@%
  9105. %@AB@% */%@AE@%%@NL@%
  9106. %@NL@%
  9107. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  9108. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  9109. %@NL@%
  9110. char *%@NL@%
  9111. getpname(path, pname)%@NL@%
  9112. char *path;        %@AB@%/* full or relative pathname */%@AE@%%@NL@%
  9113. char *pname;        %@AB@%/* program name pointer */%@AE@%%@NL@%
  9114. {%@NL@%
  9115.         register char *cp;%@NL@%
  9116. %@NL@%
  9117.         %@AB@%/* find the end of the pathname string */%@AE@%%@NL@%
  9118.         cp = path;         %@AB@%/* start of pathname */%@AE@%%@NL@%
  9119.         while (*cp != '\0')%@NL@%
  9120.                 ++cp;%@NL@%
  9121.         --cp;                %@AB@%/* went one too far */%@AE@%%@NL@%
  9122. %@NL@%
  9123.         %@AB@%/* find the start of the filename part */%@AE@%%@NL@%
  9124.         while (cp > path && *cp != '\\' && *cp != ':' && *cp != '/')%@NL@%
  9125.                 --cp;%@NL@%
  9126.         if (cp > path)%@NL@%
  9127.                 ++cp;        %@AB@%/* move off the pathname separator */%@AE@%%@NL@%
  9128. %@NL@%
  9129.         %@AB@%/* copy the filename part only */%@AE@%%@NL@%
  9130.         while ((*pname = tolower(*cp)) != '.' && *pname != '\0') {%@NL@%
  9131.                 ++cp;%@NL@%
  9132.                 ++pname;%@NL@%
  9133.         }%@NL@%
  9134.         *pname = '\0';%@NL@%
  9135. }%@NL@%
  9136. %@NL@%
  9137. %@NL@%
  9138. %@2@%%@AH@%GETREPLY.C%@AE@%%@EH@%%@NL@%
  9139. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\06USER\GETREPLY.C%@AE@%%@NL@%
  9140. %@NL@%
  9141. %@AB@%/*%@NL@%
  9142. %@AB@% *        getreply -- display a message and wait for a reply%@NL@%
  9143. %@AB@% */%@AE@%%@NL@%
  9144. %@NL@%
  9145. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  9146. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  9147. %@AI@%#include %@AE@%<memory.h> %@NL@%
  9148. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  9149. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  9150. %@AI@%#include %@AE@%<local\keydefs.h> %@NL@%
  9151. %@AI@%#include %@AE@%"linebuf.h" %@NL@%
  9152. %@NL@%
  9153. char *%@NL@%
  9154. getreply(row, col, width, mesg, lp, size, attr, pg)%@NL@%
  9155. short row, col, width;        %@AB@%/* window location and width */%@AE@%%@NL@%
  9156. char *mesg;                %@AB@%/* message text */%@AE@%%@NL@%
  9157. LINEBUF *lp;                %@AB@%/* line pointer */%@AE@%%@NL@%
  9158. short size;                %@AB@%/* size of line buffer */%@AE@%%@NL@%
  9159. short attr;                %@AB@%/* video attribute for response field */%@AE@%%@NL@%
  9160. short pg;                %@AB@%/* active display page */%@AE@%%@NL@%
  9161. {%@NL@%
  9162.         int n, k, len;%@NL@%
  9163.         short mfw;        %@AB@%/* message field width */%@AE@%%@NL@%
  9164.         short rfw;        %@AB@%/* response field width */%@AE@%%@NL@%
  9165.         short ccol;        %@AB@%/* visible cursor column */%@AE@%%@NL@%
  9166.         int msgflag;        %@AB@%/* non-zero after a message is displayed */%@AE@%%@NL@%
  9167.         char *cp;        %@AB@%/* character pointer */%@AE@%%@NL@%
  9168.         char *wp;        %@AB@%/* pointer to window start */%@AE@%%@NL@%
  9169.         char *tmp;        %@AB@%/* temporary char pointer */%@AE@%%@NL@%
  9170. %@NL@%
  9171.         extern int writemsg(short, short, short, char *, char *, short);%@NL@%
  9172. %@NL@%
  9173.         %@AB@%/* display the prompt string and calculate response field width */%@AE@%%@NL@%
  9174.         putcur(row, col, pg);%@NL@%
  9175.         mfw = writemsg(row, col, width, mesg, NULL, pg);%@NL@%
  9176.         rfw = width - mfw;%@NL@%
  9177.         writea(attr, rfw, pg);%@NL@%
  9178. %@NL@%
  9179.         %@AB@%/* collect the user's response */%@AE@%%@NL@%
  9180.         memset(lp->l_buf, '\0', size);%@NL@%
  9181.         wp = cp = lp->l_buf;%@NL@%
  9182.         putcur(row, col + mfw, pg);%@NL@%
  9183.         msgflag = 0;%@NL@%
  9184.         while ((k = getkey()) != K_RETURN) {%@NL@%
  9185.                 if (msgflag) {%@NL@%
  9186.                         %@AB@%/* clear old messages */%@AE@%%@NL@%
  9187.                         errmsg("");%@NL@%
  9188.                         putcur(row, ccol, pg);%@NL@%
  9189.                         msgflag = 0;%@NL@%
  9190.                 }%@NL@%
  9191.                 if (isascii(k) && isprint(k)) {%@NL@%
  9192.                         len = strlen(cp);%@NL@%
  9193.                         if (cp + len - lp->l_buf < size - 1) {%@NL@%
  9194.                                 memcpy(cp + 1, cp, len);%@NL@%
  9195.                                 *cp = k;%@NL@%
  9196.                                 ++cp;%@NL@%
  9197.                         }%@NL@%
  9198.                         else {%@NL@%
  9199.                                 errmsg("input buffer full");%@NL@%
  9200.                                 ++msgflag;%@NL@%
  9201.                         }%@NL@%
  9202.                 }%@NL@%
  9203.                 else%@NL@%
  9204.                         switch (k) {%@NL@%
  9205.                         case K_LEFT:%@NL@%
  9206.                                 %@AB@%/* move left one character */%@AE@%%@NL@%
  9207.                                 if (cp > lp->l_buf)%@NL@%
  9208.                                         --cp;%@NL@%
  9209.                                 break;%@NL@%
  9210.                         case K_RIGHT:%@NL@%
  9211.                                 %@AB@%/* move right one character */%@AE@%%@NL@%
  9212.                                 if (*cp != '\0')%@NL@%
  9213.                                         ++cp;%@NL@%
  9214.                                 break;%@NL@%
  9215.                         case K_UP:%@NL@%
  9216.                                 %@AB@%/* pop a line off the stack */%@AE@%%@NL@%
  9217.                                 if (lp->l_prev != NULL) {%@NL@%
  9218.                                         lp = lp->l_prev;%@NL@%
  9219.                                         wp = cp = lp->l_buf;%@NL@%
  9220.                                 }%@NL@%
  9221.                                 break;%@NL@%
  9222.                         case K_DOWN:%@NL@%
  9223.                                 %@AB@%/* push a line onto the stack */%@AE@%%@NL@%
  9224.                                 if (lp->l_next != NULL) {%@NL@%
  9225.                                         lp = lp->l_next;%@NL@%
  9226.                                         wp = cp = lp->l_buf;%@NL@%
  9227.                                 }%@NL@%
  9228.                                 break;%@NL@%
  9229.                         case K_HOME:%@NL@%
  9230.                                 %@AB@%/* beginning of buffer */%@AE@%%@NL@%
  9231.                                 cp = lp->l_buf;%@NL@%
  9232.                                 break;%@NL@%
  9233.                         case K_END:%@NL@%
  9234.                                 %@AB@%/* end of buffer */%@AE@%%@NL@%
  9235.                                 while (*cp != '\0')%@NL@%
  9236.                                         ++cp;%@NL@%
  9237.                                 break;%@NL@%
  9238.                         case K_CTRLH:%@NL@%
  9239.                                 if (cp > lp->l_buf) {%@NL@%
  9240.                                         tmp = cp - 1;%@NL@%
  9241.                                         memcpy(tmp, cp, strlen(tmp));%@NL@%
  9242.                                         --cp;%@NL@%
  9243.                                 }%@NL@%
  9244.                                 break;%@NL@%
  9245.                         case K_DEL:%@NL@%
  9246.                                 %@AB@%/* delete character at cursor */%@AE@%%@NL@%
  9247.                                 memcpy(cp, cp + 1, strlen(cp));%@NL@%
  9248.                                 break;%@NL@%
  9249.                         case K_ESC:%@NL@%
  9250.                                 %@AB@%/* cancel current input */%@AE@%%@NL@%
  9251.                                 lp->l_buf[0] = '\0';%@NL@%
  9252.                                 putcur(row, col, pg);%@NL@%
  9253.                                 writec(' ', width, pg);%@NL@%
  9254.                                 return (NULL);%@NL@%
  9255.                         default:%@NL@%
  9256.                                 errmsg("unknown command");%@NL@%
  9257.                                 ++msgflag;%@NL@%
  9258.                                 break;%@NL@%
  9259.                         }%@NL@%
  9260. %@NL@%
  9261.                 %@AB@%/* adjust the window pointer if necessary */%@AE@%%@NL@%
  9262.                 if (cp < wp)%@NL@%
  9263.                         wp = cp;%@NL@%
  9264.                 else if (cp >= wp + rfw)%@NL@%
  9265.                         wp = cp + 1 - rfw;%@NL@%
  9266. %@NL@%
  9267.                 %@AB@%/* display the reply window */%@AE@%%@NL@%
  9268.                 ccol = col + mfw;%@NL@%
  9269.                 writemsg(row, ccol, rfw, wp, NULL, pg);%@NL@%
  9270. %@NL@%
  9271.                 %@AB@%/* reposition the cursor */%@AE@%%@NL@%
  9272.                 ccol = col + mfw + (cp - wp);%@NL@%
  9273.                 putcur(row, ccol, pg);%@NL@%
  9274.         }%@NL@%
  9275.         putcur(row, col, pg);%@NL@%
  9276.         writec(' ', width, pg);        %@AB@%/* blank message area */%@AE@%%@NL@%
  9277.         return (lp->l_buf);%@NL@%
  9278. }%@NL@%
  9279. %@NL@%
  9280. %@NL@%
  9281. %@2@%%@AH@%GETSTATE.C%@AE@%%@EH@%%@NL@%
  9282. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\GETSTATE.C%@AE@%%@NL@%
  9283. %@NL@%
  9284. %@AB@%/*%@NL@%
  9285. %@AB@% *        getstate -- update video state structure%@NL@%
  9286. %@AB@% */%@AE@%%@NL@%
  9287. %@NL@%
  9288. %@AI@%#include %@AE@%<dos.h> %@NL@%
  9289. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  9290. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  9291. %@NL@%
  9292. %@AB@%/* current video state/mode information */%@AE@%%@NL@%
  9293. short Vmode;%@NL@%
  9294. short Vwidth;%@NL@%
  9295. short Vpage;%@NL@%
  9296. %@NL@%
  9297. %@AB@%/*%@NL@%
  9298. %@AB@% *        video tables -- these tables of video parameters use%@NL@%
  9299. %@AB@% *        a value of -1 to indicate that an item is not supported%@NL@%
  9300. %@AB@% *        and 0 to indicate that an item has a variable value.%@NL@%
  9301. %@AB@% */%@AE@%%@NL@%
  9302. %@NL@%
  9303. %@AB@%/* video limit tables */%@AE@%%@NL@%
  9304. short Maxrow[] = { %@NL@%
  9305.         %@AB@%/* CGA modes */%@AE@%%@NL@%
  9306.         25, 25, 25, 25, 25, 25, 25,%@NL@%
  9307.         %@AB@%/* MDA mode */%@AE@%%@NL@%
  9308.         25,%@NL@%
  9309.         %@AB@%/* PCjr modes */%@AE@%%@NL@%
  9310.         25, 25, 25,%@NL@%
  9311.         %@AB@%/* not used */%@AE@%%@NL@%
  9312.         -1, -1,%@NL@%
  9313.         %@AB@%/* EGA modes */%@AE@%%@NL@%
  9314.         25, 25, 25, 25, 43%@NL@%
  9315. };%@NL@%
  9316. %@NL@%
  9317. short Maxcol[] = {%@NL@%
  9318.         %@AB@%/* CGA modes */%@AE@%%@NL@%
  9319.         40, 40, 80, 80, 40, 40, 80,%@NL@%
  9320.         %@AB@%/* MDA mode */%@AE@%%@NL@%
  9321.         80,%@NL@%
  9322.         %@AB@%/* PCjr modes */%@AE@%%@NL@%
  9323.         -1, 40, 80,%@NL@%
  9324.         %@AB@%/* not used */%@AE@%%@NL@%
  9325.         -1, -1,%@NL@%
  9326.         %@AB@%/* EGA modes */%@AE@%%@NL@%
  9327.         80, 80, 80, 80%@NL@%
  9328. };%@NL@%
  9329. %@NL@%
  9330. short Maxpage[] = {%@NL@%
  9331.         %@AB@%/* CGA modes */%@AE@%%@NL@%
  9332.         8, 8, 4, 4, 1, 1, 1,%@NL@%
  9333.         %@AB@%/* MDA mode */%@AE@%%@NL@%
  9334.         1,%@NL@%
  9335.         %@AB@%/* PCjr modes */%@AE@%%@NL@%
  9336.         0, 0, 0,%@NL@%
  9337.         %@AB@%/* not used */%@AE@%%@NL@%
  9338.         -1, -1,%@NL@%
  9339.         %@AB@%/* EGA modes */%@AE@%%@NL@%
  9340.         8, 4, 1, 1%@NL@%
  9341. };%@NL@%
  9342. %@NL@%
  9343. int%@NL@%
  9344. getstate()%@NL@%
  9345. {%@NL@%
  9346.         union REGS inregs, outregs;%@NL@%
  9347. %@NL@%
  9348.         inregs.h.ah = GET_STATE;%@NL@%
  9349.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  9350. %@NL@%
  9351.         Vmode = outregs.h.al;%@NL@%
  9352.         Vwidth = outregs.h.ah;%@NL@%
  9353.         Vpage = outregs.h.bh;%@NL@%
  9354. %@NL@%
  9355.         return (outregs.x.cflag);%@NL@%
  9356. }%@NL@%
  9357. %@NL@%
  9358. %@NL@%
  9359. %@2@%%@AH@%GETSTR.C%@AE@%%@EH@%%@NL@%
  9360. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\14VIEW\GETSTR.C%@AE@%%@NL@%
  9361. %@NL@%
  9362. %@AB@%/*%@NL@%
  9363. %@AB@% *        getstr -- get a string from the keyboard%@NL@%
  9364. %@AB@% */%@AE@%%@NL@%
  9365. %@NL@%
  9366. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  9367. %@AI@%#include %@AE@%<string.h> %@NL@%
  9368. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  9369. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  9370. %@AI@%#include %@AE@%<local\keydefs.h> %@NL@%
  9371. %@NL@%
  9372. char *%@NL@%
  9373. getstr(buf, width)%@NL@%
  9374. char *buf;%@NL@%
  9375. int width;%@NL@%
  9376. {%@NL@%
  9377.         int row, col;%@NL@%
  9378.         char *cp;%@NL@%
  9379. %@NL@%
  9380.         %@AB@%/* function prototypes */%@AE@%%@NL@%
  9381.         extern int putcur(int, int, int);%@NL@%
  9382.         extern int readcur(int *, int *, int);%@NL@%
  9383.         extern int writec(char, int, int);%@NL@%
  9384.         extern int getkey();%@NL@%
  9385. %@NL@%
  9386.         %@AB@%/* gather keyboard input into a string buffer */%@AE@%%@NL@%
  9387.         cp = buf;%@NL@%
  9388.         while ((*cp = getkey()) != K_RETURN && cp - buf < width) {%@NL@%
  9389.                 switch (*cp) {%@NL@%
  9390.                 case K_CTRLH:%@NL@%
  9391.                         %@AB@%/* destructive backspace */%@AE@%%@NL@%
  9392.                         if (cp > buf) {%@NL@%
  9393.                                 readcur(&row, &col, Vpage);%@NL@%
  9394.                                 putcur(row, col - 1, Vpage);%@NL@%
  9395.                                 writec(' ', 1, Vpage);%@NL@%
  9396.                                 --cp;%@NL@%
  9397.                         }%@NL@%
  9398.                         continue;%@NL@%
  9399.                 case K_ESC:%@NL@%
  9400.                         %@AB@%/* cancel string input operation */%@AE@%%@NL@%
  9401.                         return (char *)NULL;%@NL@%
  9402.                 }%@NL@%
  9403.                 put_ch(*cp, Vpage);%@NL@%
  9404.                 ++cp;%@NL@%
  9405.         }%@NL@%
  9406.         *cp = '\0';%@NL@%
  9407.         return (buf);%@NL@%
  9408. }%@NL@%
  9409. %@NL@%
  9410. %@NL@%
  9411. %@2@%%@AH@%GETTICKS.C%@AE@%%@EH@%%@NL@%
  9412. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\GETTICKS.C%@AE@%%@NL@%
  9413. %@NL@%
  9414. %@AB@%/*%@NL@%
  9415. %@AB@% *        getticks -- get the current bios clock ticks value%@NL@%
  9416. %@AB@% */%@AE@%%@NL@%
  9417. %@NL@%
  9418. %@AI@%#include %@AE@%<dos.h> %@NL@%
  9419. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  9420. %@NL@%
  9421. long%@NL@%
  9422. getticks()%@NL@%
  9423. {%@NL@%
  9424.         long count;%@NL@%
  9425.         union REGS inregs, outregs;%@NL@%
  9426. %@NL@%
  9427.         %@AB@%/* get BIOS time of day as no. of ticks since midnight */%@AE@%%@NL@%
  9428.         inregs.h.ah = 0;%@NL@%
  9429.         int86(TOD, &inregs, &outregs);%@NL@%
  9430. %@NL@%
  9431.         %@AB@%/* correct for possible rollover at 24 hours */%@AE@%%@NL@%
  9432.         count = (outregs.h.al != 0) ? 0x01800B0L : 0;%@NL@%
  9433. %@NL@%
  9434.         %@AB@%/* add current day ticks */%@AE@%%@NL@%
  9435.         count += (outregs.x.dx + (outregs.x.cx << 16));%@NL@%
  9436. %@NL@%
  9437.         return (count);%@NL@%
  9438. }%@NL@%
  9439. %@NL@%
  9440. %@NL@%
  9441. %@2@%%@AH@%GETXLINE.C%@AE@%%@EH@%%@NL@%
  9442. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\GETXLINE.C%@AE@%%@NL@%
  9443. %@NL@%
  9444. %@AB@%/*%@NL@%
  9445. %@AB@% *        getxline -- get a line of text while expanding tabs,%@NL@%
  9446. %@AB@% *        put text into an array, and return a pointer to the%@NL@%
  9447. %@AB@% *        resulting null-terminated line%@NL@%
  9448. %@AB@% */%@AE@%%@NL@%
  9449. %@NL@%
  9450. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  9451. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  9452. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  9453. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  9454. %@NL@%
  9455. char *%@NL@%
  9456. getxline(buf, size, fin)%@NL@%
  9457. char *buf;%@NL@%
  9458. int size;%@NL@%
  9459. FILE *fin;%@NL@%
  9460. {%@NL@%
  9461.         register int ch;        %@AB@%/* input character */%@AE@%%@NL@%
  9462.         register char *cp;        %@AB@%/* character pointer */%@AE@%%@NL@%
  9463. %@NL@%
  9464.         extern BOOLEAN tabstop(int);%@NL@%
  9465. %@NL@%
  9466.         cp = buf;%@NL@%
  9467.         while (--size > 0 && (ch = fgetc(fin)) != EOF) {%@NL@%
  9468.                 if (ch == '\n') {%@NL@%
  9469.                         *cp++ = ch;%@NL@%
  9470.                         break;%@NL@%
  9471.                 }%@NL@%
  9472.                 else if (ch == '\t')%@NL@%
  9473.                         do {%@NL@%
  9474.                                 *cp = ' ';%@NL@%
  9475.                         } while (--size > 0 && (tabstop(++cp - buf) == FALSE));%@NL@%
  9476.                 else%@NL@%
  9477.                         *cp++ = ch & ASCII;%@NL@%
  9478.         }%@NL@%
  9479.         *cp = '\0';%@NL@%
  9480.         return ((ch == EOF && cp == buf) ? NULL : buf);%@NL@%
  9481. }%@NL@%
  9482. %@NL@%
  9483. %@NL@%
  9484. %@2@%%@AH@%HEX.C%@AE@%%@EH@%%@NL@%
  9485. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\HEX.C%@AE@%%@NL@%
  9486. %@NL@%
  9487. %@AB@%/*%@NL@%
  9488. %@AB@% *        hex.c -- hex conversions routines%@NL@%
  9489. %@AB@% */%@AE@%%@NL@%
  9490. %@NL@%
  9491. %@AI@%#define %@AE@%NIBBLE        0x000F %@NL@%
  9492. %@NL@%
  9493. char hextab[] = {%@NL@%
  9494.         '0', '1', '2', '3', '4', '5', '6', '7',%@NL@%
  9495.         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'%@NL@%
  9496. };%@NL@%
  9497. %@NL@%
  9498. %@AB@%/*%@NL@%
  9499. %@AB@% *        byte2hex -- convert a byte to a string%@NL@%
  9500. %@AB@% *        representation of its hexadecimal value%@NL@%
  9501. %@AB@% */%@AE@%%@NL@%
  9502. %@NL@%
  9503. char *%@NL@%
  9504. byte2hex(data, buf)%@NL@%
  9505. unsigned char data;%@NL@%
  9506. char *buf;%@NL@%
  9507. {%@NL@%
  9508.         register char *cp;%@NL@%
  9509. %@NL@%
  9510.         cp = buf;%@NL@%
  9511.         *cp++ = hextab[(data >> 4) & NIBBLE];%@NL@%
  9512.         *cp++ = hextab[data & NIBBLE];%@NL@%
  9513.         *cp = '\0';%@NL@%
  9514. %@NL@%
  9515.         return (buf);%@NL@%
  9516. } %@AB@%/* end byte2hex() */%@AE@%%@NL@%
  9517. %@NL@%
  9518. %@AB@%/*%@NL@%
  9519. %@AB@% *        word2hex -- convert a word to a string%@NL@%
  9520. %@AB@% *        representation of its hexadecimal value%@NL@%
  9521. %@AB@% */%@AE@%%@NL@%
  9522. %@NL@%
  9523. char *%@NL@%
  9524. word2hex(data, buf)%@NL@%
  9525. unsigned int data;%@NL@%
  9526. char *buf;%@NL@%
  9527. {%@NL@%
  9528.         register char *cp;%@NL@%
  9529. %@NL@%
  9530.         cp = buf;%@NL@%
  9531.         *cp++ = hextab[(data >> 12) & NIBBLE];%@NL@%
  9532.         *cp++ = hextab[(data >> 8) & NIBBLE];%@NL@%
  9533.         *cp++ = hextab[(data >> 4) & NIBBLE];%@NL@%
  9534.         *cp++ = hextab[data & NIBBLE];%@NL@%
  9535.         *cp = '\0';%@NL@%
  9536. %@NL@%
  9537.         return (buf);%@NL@%
  9538. } %@AB@%/* end word2hex() */%@AE@%%@NL@%
  9539. %@NL@%
  9540. %@NL@%
  9541. %@2@%%@AH@%HEX.C%@AE@%%@EH@%%@NL@%
  9542. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\10DUMP\HEX.C%@AE@%%@NL@%
  9543. %@NL@%
  9544. %@AB@%/*%@NL@%
  9545. %@AB@% *        hex.c -- hex conversions routines%@NL@%
  9546. %@AB@% */%@AE@%%@NL@%
  9547. %@NL@%
  9548. %@AI@%#define %@AE@%NIBBLE        0x000F %@NL@%
  9549. %@AI@%#define %@AE@%BYTE        0x00FF %@NL@%
  9550. %@AI@%#define %@AE@%WORD        0xFFFF %@NL@%
  9551. %@NL@%
  9552. char hextab[] = {%@NL@%
  9553.         '0', '1', '2', '3', '4', '5', '6', '7',%@NL@%
  9554.         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'%@NL@%
  9555. };%@NL@%
  9556. %@NL@%
  9557. %@AB@%/*%@NL@%
  9558. %@AB@% *        byte2hex -- convert a byte to a string%@NL@%
  9559. %@AB@% *        representation of its hexadecimal value%@NL@%
  9560. %@AB@% */%@AE@%%@NL@%
  9561. %@NL@%
  9562. char *%@NL@%
  9563. byte2hex(data, buf)%@NL@%
  9564. unsigned char data;%@NL@%
  9565. char *buf;%@NL@%
  9566. {%@NL@%
  9567.         char *cp;%@NL@%
  9568.         unsigned int d;%@NL@%
  9569. %@NL@%
  9570.         d = data & BYTE;%@NL@%
  9571.         cp = buf;%@NL@%
  9572.         *cp++ = hextab[(d >> 4) & NIBBLE];%@NL@%
  9573.         *cp++ = hextab[d & NIBBLE];%@NL@%
  9574.         *cp = '\0';%@NL@%
  9575. %@NL@%
  9576.         return (buf);%@NL@%
  9577. }%@NL@%
  9578. %@NL@%
  9579. %@AB@%/*%@NL@%
  9580. %@AB@% *        word2hex -- convert a word to a string%@NL@%
  9581. %@AB@% *        representation of its hexadecimal value%@NL@%
  9582. %@AB@% */%@AE@%%@NL@%
  9583. %@NL@%
  9584. char *%@NL@%
  9585. word2hex(data, buf)%@NL@%
  9586. unsigned int data;%@NL@%
  9587. char *buf;%@NL@%
  9588. {%@NL@%
  9589.         char *cp;%@NL@%
  9590.         unsigned int d;%@NL@%
  9591. %@NL@%
  9592.         d = data & WORD;%@NL@%
  9593.         cp = buf;%@NL@%
  9594.         *cp++ = hextab[(d >> 12) & NIBBLE];%@NL@%
  9595.         *cp++ = hextab[(d >> 8) & NIBBLE];%@NL@%
  9596.         *cp++ = hextab[(d >> 4) & NIBBLE];%@NL@%
  9597.         *cp++ = hextab[d & NIBBLE];%@NL@%
  9598.         *cp = '\0';%@NL@%
  9599. %@NL@%
  9600.         return (buf);%@NL@%
  9601. }%@NL@%
  9602. %@NL@%
  9603. %@NL@%
  9604. %@2@%%@AH@%HEXDUMP.C%@AE@%%@EH@%%@NL@%
  9605. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\10DUMP\HEXDUMP.C%@AE@%%@NL@%
  9606. %@NL@%
  9607. %@AB@%/*%@NL@%
  9608. %@AB@% *        hexdump -- read data from an open file and "dump"%@NL@%
  9609. %@AB@% *        it in side-by-side hex and ASCII to standard output%@NL@%
  9610. %@AB@% */%@AE@%%@NL@%
  9611. %@NL@%
  9612. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  9613. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  9614. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  9615. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  9616. %@NL@%
  9617. %@AI@%#define %@AE@%LINEWIDTH        80 %@NL@%
  9618. %@AI@%#define %@AE@%NBYTES                16 %@NL@%
  9619. %@AI@%#define %@AE@%WORD                0xFFFF %@NL@%
  9620. %@AI@%#define %@AE@%RGHTMARK        179 %@NL@%
  9621. %@AI@%#define %@AE@%LEFTMARK        179 %@NL@%
  9622. %@AI@%#define %@AE@%DEL                0x7F %@NL@%
  9623. %@NL@%
  9624. int hexdump(fd, strip)%@NL@%
  9625. int fd;%@NL@%
  9626. BOOLEAN strip;%@NL@%
  9627. {%@NL@%
  9628.         unsigned char i;%@NL@%
  9629.         int n;                        %@AB@%/* bytes per read operation */%@AE@%%@NL@%
  9630.         unsigned long offset;        %@AB@%/* bytes from start of file */%@AE@%%@NL@%
  9631.         char inbuf[BUFSIZ + 1], outbuf[LINEWIDTH + 1];%@NL@%
  9632.         char hexbuf[5];%@NL@%
  9633.         register char *inp, *outp;%@NL@%
  9634. %@NL@%
  9635.         extern char *byte2hex(unsigned char, char *);%@NL@%
  9636.         extern char *word2hex(unsigned int, char *);%@NL@%
  9637. %@NL@%
  9638.         offset = 0;%@NL@%
  9639.         while ((n = read(fd, inbuf, BUFSIZ)) != 0) {%@NL@%
  9640.                 if (n == -1)%@NL@%
  9641.                         return FAILURE;%@NL@%
  9642.                 inp = inbuf;%@NL@%
  9643.                 while (inp < inbuf + n) {%@NL@%
  9644.                         outp = outbuf;%@NL@%
  9645. %@NL@%
  9646.                         %@AB@%/* offset in hex */%@AE@%%@NL@%
  9647.                         outp += sprintf(outp, "%08lX",%@NL@%
  9648.                                 offset + (unsigned long)(inp - inbuf));%@NL@%
  9649.                         *outp++ = ' ';%@NL@%
  9650. %@NL@%
  9651.                         %@AB@%/* block of bytes in hex */%@AE@%%@NL@%
  9652.                         for (i = 0; i < NBYTES; ++i) {%@NL@%
  9653.                                 *outp++ = ' ';%@NL@%
  9654.                                 strcpy(outp, byte2hex(*inp++, hexbuf));%@NL@%
  9655.                                 outp += 2;%@NL@%
  9656.                         }%@NL@%
  9657.                         *outp++ = ' ';%@NL@%
  9658.                         *outp++ = ' ';%@NL@%
  9659.                         *outp++ = (strip == TRUE) ? '|' : LEFTMARK;%@NL@%
  9660. %@NL@%
  9661.                         %@AB@%/* same block of bytes in ASCII */%@AE@%%@NL@%
  9662.                         inp -= NBYTES;%@NL@%
  9663.                         for (i = 0; i < NBYTES; ++i) {%@NL@%
  9664.                                 if (strip == TRUE && (*inp < ' ' || *inp >= DEL))%@NL@%
  9665.                                         *outp = '.';%@NL@%
  9666.                                 else if (iscntrl(*inp))%@NL@%
  9667.                                         *outp = '.';%@NL@%
  9668.                                 else%@NL@%
  9669.                                         *outp = *inp;%@NL@%
  9670.                                 ++inp;%@NL@%
  9671.                                 ++outp;%@NL@%
  9672.                         }%@NL@%
  9673.                         *outp++ = (strip == TRUE) ? '|' : RGHTMARK;%@NL@%
  9674.                         *outp++ = '\n';%@NL@%
  9675.                         *outp = '\0';%@NL@%
  9676.                         fputs(outbuf, stdout);%@NL@%
  9677.                 }%@NL@%
  9678.                 offset += n;%@NL@%
  9679.         }%@NL@%
  9680.         return SUCCESS;%@NL@%
  9681. }%@NL@%
  9682. %@NL@%
  9683. %@NL@%
  9684. %@2@%%@AH@%INTERVAL.C%@AE@%%@EH@%%@NL@%
  9685. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\06USER\INTERVAL.C%@AE@%%@NL@%
  9686. %@NL@%
  9687. %@AB@%/*%@NL@%
  9688. %@AB@% *        interval -- report the interval given in seconds as%@NL@%
  9689. %@AB@% *        a human-readable null-terminated string%@NL@%
  9690. %@AB@% */%@AE@%%@NL@%
  9691. %@NL@%
  9692. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  9693. %@NL@%
  9694. char *%@NL@%
  9695. interval(seconds, buf)%@NL@%
  9696. long seconds;%@NL@%
  9697. char *buf;%@NL@%
  9698. {%@NL@%
  9699.         int hh, mm, ss;%@NL@%
  9700.         long remainder;%@NL@%
  9701. %@NL@%
  9702.         %@AB@%/* calculate the values */%@AE@%%@NL@%
  9703.         hh = seconds / 3600;%@NL@%
  9704.         remainder = seconds % 3600;%@NL@%
  9705.         mm = remainder / 60;%@NL@%
  9706.         ss = remainder - (mm * 60);%@NL@%
  9707.         sprintf(buf, "%02d:%02d:%02d\0", hh, mm, ss);%@NL@%
  9708. %@NL@%
  9709.         return (buf);%@NL@%
  9710. }%@NL@%
  9711. %@NL@%
  9712. %@NL@%
  9713. %@2@%%@AH@%ISCOLOR.C%@AE@%%@EH@%%@NL@%
  9714. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\ISCOLOR.C%@AE@%%@NL@%
  9715. %@NL@%
  9716. %@AB@%/*%@NL@%
  9717. %@AB@% *        iscolor -- return TRUE if a color display system is%@NL@%
  9718. %@AB@% *        in use and is set to one of the text modes%@NL@%
  9719. %@AB@% */%@AE@%%@NL@%
  9720. %@NL@%
  9721. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  9722. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  9723. %@NL@%
  9724. BOOLEAN%@NL@%
  9725. iscolor()%@NL@%
  9726. {%@NL@%
  9727.         getstate();%@NL@%
  9728.         if (Vmode != CGA_C40 || Vmode != CGA_C80)%@NL@%
  9729.                 return TRUE;%@NL@%
  9730.         return FALSE;%@NL@%
  9731. }%@NL@%
  9732. %@NL@%
  9733. %@NL@%
  9734. %@2@%%@AH@%KBD_STAT.C%@AE@%%@EH@%%@NL@%
  9735. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\KBD_STAT.C%@AE@%%@NL@%
  9736. %@NL@%
  9737. %@AB@%/*%@NL@%
  9738. %@AB@% *        kbd_stat -- return the keyboard status%@NL@%
  9739. %@AB@% *        word (bit-significant)%@NL@%
  9740. %@AB@% */%@AE@%%@NL@%
  9741. %@NL@%
  9742. %@AI@%#include %@AE@%<dos.h> %@NL@%
  9743. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  9744. %@AI@%#include %@AE@%<local\keybdlib.h> %@NL@%
  9745. %@NL@%
  9746. unsigned char%@NL@%
  9747. kbd_stat()%@NL@%
  9748. {%@NL@%
  9749.         union REGS inregs, outregs;%@NL@%
  9750. %@NL@%
  9751.         inregs.h.ah = KBD_STATUS;%@NL@%
  9752.         int86(KEYBD_IO, &inregs, &outregs);%@NL@%
  9753. %@NL@%
  9754.         return ((unsigned char)(outregs.h.al));%@NL@%
  9755. }%@NL@%
  9756. %@NL@%
  9757. %@NL@%
  9758. %@2@%%@AH@%KEYREADY.C%@AE@%%@EH@%%@NL@%
  9759. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\DOS\KEYREADY.C%@AE@%%@NL@%
  9760. %@NL@%
  9761. %@AB@%/*%@NL@%
  9762. %@AB@% *        keyready -- non-zero if the keyboard buffer%@NL@%
  9763. %@AB@% *        has any codes waiting%@NL@%
  9764. %@AB@% */%@AE@%%@NL@%
  9765. %@NL@%
  9766. %@AI@%#include %@AE@%<dos.h> %@NL@%
  9767. %@AI@%#include %@AE@%<local\doslib.h> %@NL@%
  9768. %@NL@%
  9769. int%@NL@%
  9770. keyready()%@NL@%
  9771. {%@NL@%
  9772.         union REGS inregs, outregs;%@NL@%
  9773. %@NL@%
  9774.         inregs.h.ah = CH_READY;%@NL@%
  9775.         intdos(&inregs, &outregs);%@NL@%
  9776. %@NL@%
  9777.         return (outregs.h.al);%@NL@%
  9778. }%@NL@%
  9779. %@NL@%
  9780. %@NL@%
  9781. %@2@%%@AH@%LAST_CH.C%@AE@%%@EH@%%@NL@%
  9782. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\LAST_CH.C%@AE@%%@NL@%
  9783. %@NL@%
  9784. %@AB@%/*%@NL@%
  9785. %@AB@% *        last_ch -- return a copy of the last character%@NL@%
  9786. %@AB@% *        before the NUL byte in a string%@NL@%
  9787. %@AB@% */%@AE@%%@NL@%
  9788. %@NL@%
  9789. char%@NL@%
  9790. last_ch(s)%@NL@%
  9791. char *s;%@NL@%
  9792. {%@NL@%
  9793.         register char *cp;%@NL@%
  9794. %@NL@%
  9795.         %@AB@%/* find end of s */%@AE@%%@NL@%
  9796.         cp = s;%@NL@%
  9797.         while (*cp != '\0')%@NL@%
  9798.                 ++cp;%@NL@%
  9799. %@NL@%
  9800.         %@AB@%/* return previous character */%@AE@%%@NL@%
  9801.         --cp;%@NL@%
  9802.         return (*cp);%@NL@%
  9803. }%@NL@%
  9804. %@NL@%
  9805. %@NL@%
  9806. %@2@%%@AH@%LINES.C%@AE@%%@EH@%%@NL@%
  9807. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\LINES.C%@AE@%%@NL@%
  9808. %@NL@%
  9809. %@AB@%/*%@NL@%
  9810. %@AB@% *        lines -- send newlines to the output stream%@NL@%
  9811. %@AB@% */%@AE@%%@NL@%
  9812. %@NL@%
  9813. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  9814. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  9815. %@NL@%
  9816. int%@NL@%
  9817. lines(n, fp)%@NL@%
  9818. int n;%@NL@%
  9819. FILE *fp;%@NL@%
  9820. {%@NL@%
  9821.         register int i;%@NL@%
  9822. %@NL@%
  9823.         for (i = 0; i < n; ++i)%@NL@%
  9824.                 if (putc('\n', fp) == EOF && ferror(fp))%@NL@%
  9825.                         break;%@NL@%
  9826. %@NL@%
  9827.         %@AB@%/* return number of newlines emitted */%@AE@%%@NL@%
  9828.         return (i);%@NL@%
  9829. }%@NL@%
  9830. %@NL@%
  9831. %@NL@%
  9832. %@2@%%@AH@%LS.C%@AE@%%@EH@%%@NL@%
  9833. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\08FILE\LS.C%@AE@%%@NL@%
  9834. %@NL@%
  9835. %@AB@%/*%@NL@%
  9836. %@AB@% *        ls -- display a directory listing%@NL@%
  9837. %@AB@% */%@AE@%%@NL@%
  9838. %@NL@%
  9839. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  9840. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  9841. %@AI@%#include %@AE@%<string.h> %@NL@%
  9842. %@AI@%#include %@AE@%<memory.h> %@NL@%
  9843. %@AI@%#include %@AE@%<dos.h> %@NL@%
  9844. %@AI@%#include %@AE@%<direct.h> %@NL@%
  9845. %@AI@%#include %@AE@%<signal.h> %@NL@%
  9846. %@AI@%#include %@AE@%<search.h> %@NL@%
  9847. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  9848. %@AI@%#include %@AE@%<local\doslib.h> %@NL@%
  9849. %@AI@%#include %@AE@%"ls.h" %@NL@%
  9850. %@NL@%
  9851. %@AB@%/* allocation quantities */%@AE@%%@NL@%
  9852. %@AI@%#define %@AE@%N_FILES        256 %@NL@%
  9853. %@AI@%#define %@AE@%N_DIRS        16 %@NL@%
  9854. %@NL@%
  9855. %@AB@%/* global data */%@AE@%%@NL@%
  9856. int Multicol = 0;%@NL@%
  9857. int Filetype = 0;%@NL@%
  9858. int Hidden = 0;%@NL@%
  9859. int Longlist = 0;%@NL@%
  9860. int Reverse = 0;%@NL@%
  9861. int Modtime = 0;%@NL@%
  9862. %@NL@%
  9863. main(argc, argv)%@NL@%
  9864. int argc;%@NL@%
  9865. char *argv[];%@NL@%
  9866. {%@NL@%
  9867.         int ch, i;%@NL@%
  9868.         int errflag;                %@AB@%/* error flag */%@AE@%%@NL@%
  9869.         char *ep;                %@AB@%/* environment pointer */%@AE@%%@NL@%
  9870.         int status = 0;                %@AB@%/* return status value */%@AE@%%@NL@%
  9871.         int fileattr;                %@AB@%/* file attribute number */%@AE@%%@NL@%
  9872.         struct DTA buf;                %@AB@%/* private disk buffer */%@AE@%%@NL@%
  9873.         char path[MAXPATH + 1];        %@AB@%/* working pathname */%@AE@%%@NL@%
  9874.         struct OUTBUF *fp, *fq;        %@AB@%/* pointers to file array */%@AE@%%@NL@%
  9875.         char **dp, **dq;        %@AB@%/* pointer to directory pointer array */%@AE@%%@NL@%
  9876.         int fbc = 1;                %@AB@%/* file memory block allocation count */%@AE@%%@NL@%
  9877.         int dbc = 1;                %@AB@%/* directory memory block allocation count */%@AE@%%@NL@%
  9878.         int nfiles;                %@AB@%/* number of file elements */%@AE@%%@NL@%
  9879.         int ndirs;                %@AB@%/* number of directory elements */%@AE@%%@NL@%
  9880. %@NL@%
  9881.         static char pgm[MAXNAME + 1] = { "ls" };%@NL@%
  9882. %@NL@%
  9883.         %@AB@%/* function prototypes */%@AE@%%@NL@%
  9884.         void getpname(char *, char *);%@NL@%
  9885.         extern int getopt(int, char **, char *);%@NL@%
  9886.         extern int optind, opterr;%@NL@%
  9887.         extern char *optarg;%@NL@%
  9888.         extern char *drvpath(char *);%@NL@%
  9889.         extern void fatal(char *, char *, int);%@NL@%
  9890.         extern void setdta(char *);%@NL@%
  9891.         extern int first_fm(char *, int);%@NL@%
  9892.         extern int next_fm();%@NL@%
  9893.         extern int ls_fcomp(struct OUTBUF *, struct OUTBUF *);%@NL@%
  9894.         extern int ls_dcomp(char *, char *);%@NL@%
  9895.         extern int ls_single(struct OUTBUF *, int);%@NL@%
  9896.         extern int ls_multi(struct OUTBUF *, int);%@NL@%
  9897.         extern int ls_dirx(char *, char *);%@NL@%
  9898.         int bailout();%@NL@%
  9899. %@NL@%
  9900.         %@AB@%/* guarantee that needed DOS services are available */%@AE@%%@NL@%
  9901.         if (_osmajor < 2)%@NL@%
  9902.                 fatal(pgm, "ls requires DOS 2.00 or later", 1);%@NL@%
  9903. %@NL@%
  9904.         %@AB@%/* get program name from DOS (version 3.00 and later) */%@AE@%%@NL@%
  9905.         if (_osmajor >= 3)%@NL@%
  9906.                 getpname(*argv, pgm);%@NL@%
  9907. %@NL@%
  9908.         %@AB@%/* useful aliases (DOS version 3.00 and later) */%@AE@%%@NL@%
  9909.         if (strcmp(pgm, "lc") == 0)%@NL@%
  9910.                 ++Multicol;%@NL@%
  9911.         if (strcmp(pgm, "lf") == 0) {%@NL@%
  9912.                 ++Multicol;%@NL@%
  9913.                 ++Filetype;%@NL@%
  9914.         }%@NL@%
  9915. %@NL@%
  9916.         %@AB@%/* prepare for emergencies */%@AE@%%@NL@%
  9917.         if (signal(SIGINT, bailout) == (int(*)())-1) {%@NL@%
  9918.                 perror("Can't set SIGINT");%@NL@%
  9919.                 exit(2);%@NL@%
  9920.         }%@NL@%
  9921. %@NL@%
  9922.         %@AB@%/* process optional arguments first */%@AE@%%@NL@%
  9923.         errflag = 0;%@NL@%
  9924.         while ((ch = getopt(argc, argv, "aCFlrt")) != EOF)%@NL@%
  9925.                 switch (ch) {%@NL@%
  9926.                 case 'a':%@NL@%
  9927.                         %@AB@%/* all files (hidden, system, etc.) */%@AE@%%@NL@%
  9928.                         ++Hidden;%@NL@%
  9929.                         break;%@NL@%
  9930.                 case 'C':%@NL@%
  9931.                         ++Multicol;%@NL@%
  9932.                         break;%@NL@%
  9933.                 case 'F':%@NL@%
  9934.                         %@AB@%/* show file types (/=directory, *=executable) */%@AE@%%@NL@%
  9935.                         ++Filetype;%@NL@%
  9936.                         break;%@NL@%
  9937.                 case 'l':%@NL@%
  9938.                         %@AB@%/* long list (overrides multicolumn) */%@AE@%%@NL@%
  9939.                         ++Longlist;%@NL@%
  9940.                         break;%@NL@%
  9941.                 case 'r':%@NL@%
  9942.                         %@AB@%/* reverse sort */%@AE@%%@NL@%
  9943.                         ++Reverse;%@NL@%
  9944.                         break;%@NL@%
  9945.                 case 't':%@NL@%
  9946.                         %@AB@%/* sort by file modification time */%@AE@%%@NL@%
  9947.                         ++Modtime;%@NL@%
  9948.                         break;%@NL@%
  9949.                 case '?':%@NL@%
  9950.                         errflag = TRUE;%@NL@%
  9951.                         break;%@NL@%
  9952.                 }%@NL@%
  9953.         argc -= optind;%@NL@%
  9954.         argv += optind;%@NL@%
  9955. %@NL@%
  9956.         %@AB@%/* check for command-line errors */%@AE@%%@NL@%
  9957.         if (argc < 0 || errflag) {%@NL@%
  9958.                 fprintf(stderr, "Usage: %s [-aCFlrt] [pathname ...]", pgm);%@NL@%
  9959.                 exit(3);%@NL@%
  9960.         }%@NL@%
  9961. %@NL@%
  9962.         %@AB@%/* allocate initial file and directory storage areas */%@AE@%%@NL@%
  9963.         dp = dq = (char **)malloc(N_DIRS * sizeof (char *));%@NL@%
  9964.         if (dp == NULL)%@NL@%
  9965.                 fatal(pgm, "Out of memory", 4);%@NL@%
  9966.         fp = fq = (struct OUTBUF *)malloc(N_FILES * sizeof (struct OUTBUF));%@NL@%
  9967.         if (fp == NULL)%@NL@%
  9968.                 fatal(pgm, "Out of memory", 4);%@NL@%
  9969.         nfiles = ndirs = 0;%@NL@%
  9970. %@NL@%
  9971.         %@AB@%/* use current directory if no args */%@AE@%%@NL@%
  9972.         if (argc == 0) {%@NL@%
  9973.                 if (getcwd(path, MAXPATH) == NULL)%@NL@%
  9974.                         fatal(pgm, "Cannot get current directory", 5);%@NL@%
  9975.                 *dq = path;%@NL@%
  9976.                 ndirs = 1;%@NL@%
  9977.         }%@NL@%
  9978.         else {%@NL@%
  9979.                 %@AB@%/* use arguments as file and directory names */%@AE@%%@NL@%
  9980.                 for ( ; argc-- > 0; ++argv) {%@NL@%
  9981.                         strcpy(path, *argv);%@NL@%
  9982.                         if (path[0] == '\\') {%@NL@%
  9983.                                 %@AB@%/* prepend default drive name */%@AE@%%@NL@%
  9984.                                 memcpy(path + 2, path, strlen(path) + 1);%@NL@%
  9985.                                 path[0] = 'a' + getdrive();%@NL@%
  9986.                                 path[1] = ':';%@NL@%
  9987.                         }%@NL@%
  9988.                         if (path[1] == ':' && path[2] == '\0' && drvpath(path) == NULL) {%@NL@%
  9989.                                 fprintf(stderr, "%s: Cannot get drive path", pgm);%@NL@%
  9990.                                 continue;%@NL@%
  9991.                         }%@NL@%
  9992. %@NL@%
  9993.                         %@AB@%/* establish private disk transfer area */%@AE@%%@NL@%
  9994.                         setdta((char *)&buf);%@NL@%
  9995. %@NL@%
  9996.                         %@AB@%/* set file attribute for search */%@AE@%%@NL@%
  9997.                         if (Hidden)%@NL@%
  9998.                                 fileattr = SUBDIR | HIDDEN | SYSTEM | READONLY;%@NL@%
  9999.                         else%@NL@%
  10000.                                 fileattr = SUBDIR;%@NL@%
  10001.                         if (first_fm(path, fileattr) != 0 && path[3] != '\0') {%@NL@%
  10002.                                 fprintf(stderr, "%s -- No such file or directory\n", path);%@NL@%
  10003.                                 continue;%@NL@%
  10004.                         }%@NL@%
  10005.                         if ((buf.d_attr & SUBDIR) == SUBDIR || path[3] == '\0') {%@NL@%
  10006.                                 %@AB@%/* path is a (sub)directory */%@AE@%%@NL@%
  10007.                                 *dq = strdup(path);%@NL@%
  10008.                                 if (++ndirs == dbc * N_DIRS) {%@NL@%
  10009.                                         ++dbc;        %@AB@%/* increase space requirement */%@AE@%%@NL@%
  10010.                                         dp = (char **)realloc(dp, dbc * N_DIRS * sizeof (char *));%@NL@%
  10011.                                         if (dp == NULL)%@NL@%
  10012.                                                 fatal(pgm, "Out of memory", 4);%@NL@%
  10013.                                         dq = dp + dbc * N_DIRS;%@NL@%
  10014.                                 }%@NL@%
  10015.                                 else%@NL@%
  10016.                                         ++dq;%@NL@%
  10017.                         }%@NL@%
  10018.                         else {%@NL@%
  10019.                                 fq->o_name = strdup(path);%@NL@%
  10020.                                 fq->o_mode = buf.d_attr;%@NL@%
  10021.                                 fq->o_date = buf.d_mdate;%@NL@%
  10022.                                 fq->o_time = buf.d_mtime;%@NL@%
  10023.                                 fq->o_size = buf.d_fsize;%@NL@%
  10024.                                 if (++nfiles == fbc * N_FILES) {%@NL@%
  10025.                                         ++fbc;%@NL@%
  10026.                                         fp = (struct OUTBUF *)realloc(fp, fbc * N_FILES * sizeof (struct OUTBUF));%@NL@%
  10027.                                         if (fp == NULL)%@NL@%
  10028.                                                 fatal(pgm, "Out of memory", 4);%@NL@%
  10029.                                         fq = fp + fbc * N_FILES;%@NL@%
  10030.                                 }%@NL@%
  10031.                                 else%@NL@%
  10032.                                         ++fq;%@NL@%
  10033.                         }%@NL@%
  10034.                 }%@NL@%
  10035.         }%@NL@%
  10036. %@NL@%
  10037.         %@AB@%/* output file list, if any */%@AE@%%@NL@%
  10038.         if (nfiles > 0) {%@NL@%
  10039.                 qsort(fp, nfiles, sizeof(struct OUTBUF), ls_fcomp);%@NL@%
  10040.                 if (Longlist)%@NL@%
  10041.                         ls_long(fp, nfiles);%@NL@%
  10042.                 else if (Multicol)%@NL@%
  10043.                         ls_multi(fp, nfiles);%@NL@%
  10044.                 else%@NL@%
  10045.                         ls_single(fp, nfiles);%@NL@%
  10046.                 putchar('\n');%@NL@%
  10047.         }%@NL@%
  10048.         free(fp);%@NL@%
  10049.          %@NL@%
  10050.         %@AB@%/* output directory lists, if any */%@AE@%%@NL@%
  10051.         if (ndirs == 1 && nfiles == 0) {%@NL@%
  10052.                 %@AB@%/* expand directory and output without header */%@AE@%%@NL@%
  10053.                 if (ls_dirx(pgm, *dp))%@NL@%
  10054.                         fprintf(stderr, "%s -- empty directory\n", strlwr(*dp));%@NL@%
  10055.         }%@NL@%
  10056.         else if (ndirs > 0) {%@NL@%
  10057.                 %@AB@%/* expand each directory and output with headers */%@AE@%%@NL@%
  10058.                 dq = dp;%@NL@%
  10059.                 qsort(dp, ndirs, sizeof(char *), ls_dcomp);%@NL@%
  10060.                 while (ndirs-- > 0) {%@NL@%
  10061.                         fprintf(stdout, "%s:\n", strlwr(*dq));%@NL@%
  10062.                         if (ls_dirx(pgm, *dq++))%@NL@%
  10063.                                 fprintf(stderr, "%s -- empty directory\n",%@NL@%
  10064.                                         strlwr(*dq));%@NL@%
  10065.                         putchar('\n');%@NL@%
  10066.                 }%@NL@%
  10067.         }%@NL@%
  10068. %@NL@%
  10069.         exit(0);%@NL@%
  10070. }%@NL@%
  10071. %@NL@%
  10072. %@AB@%/*%@NL@%
  10073. %@AB@% *        bailout -- optionally terminate upon interrupt%@NL@%
  10074. %@AB@% */%@AE@%%@NL@%
  10075. int%@NL@%
  10076. bailout()%@NL@%
  10077. {%@NL@%
  10078.         char ch;%@NL@%
  10079. %@NL@%
  10080.         signal(SIGINT, bailout);%@NL@%
  10081.         printf("\nTerminate directory listing? ");%@NL@%
  10082.         scanf("%1s", &ch);%@NL@%
  10083.         if (ch == 'y' || ch == 'Y')%@NL@%
  10084.                 exit(1);%@NL@%
  10085. }%@NL@%
  10086. %@NL@%
  10087. %@NL@%
  10088. %@2@%%@AH@%LS_DIRX.C%@AE@%%@EH@%%@NL@%
  10089. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\08FILE\LS_DIRX.C%@AE@%%@NL@%
  10090. %@NL@%
  10091. %@AB@%/*%@NL@%
  10092. %@AB@% *        ls_dirx -- expand the contents of a directory using%@NL@%
  10093. %@AB@% *        the DOS first/next matching file functions%@NL@%
  10094. %@AB@% */%@AE@%%@NL@%
  10095. %@NL@%
  10096. %@AI@%#define %@AE@%DEBUG %@NL@%
  10097. %@NL@%
  10098. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  10099. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  10100. %@AI@%#include %@AE@%<string.h> %@NL@%
  10101. %@AI@%#include %@AE@%<malloc.h> %@NL@%
  10102. %@AI@%#include %@AE@%<dos.h> %@NL@%
  10103. %@AI@%#include %@AE@%<direct.h> %@NL@%
  10104. %@AI@%#include %@AE@%<signal.h> %@NL@%
  10105. %@AI@%#include %@AE@%<search.h> %@NL@%
  10106. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  10107. %@AI@%#include %@AE@%<local\doslib.h> %@NL@%
  10108. %@AI@%#include %@AE@%"ls.h" %@NL@%
  10109. %@NL@%
  10110. %@AI@%#define %@AE@%NFILES        1024 %@NL@%
  10111. %@NL@%
  10112. extern int Recursive;%@NL@%
  10113. extern int Longlist;%@NL@%
  10114. extern int Multicol;%@NL@%
  10115. extern int Hidden;%@NL@%
  10116. %@NL@%
  10117. int%@NL@%
  10118. ls_dirx(pname, namep)%@NL@%
  10119. char *pname;%@NL@%
  10120. char *namep;%@NL@%
  10121. {%@NL@%
  10122.         int status = 0;                        %@AB@%/* function return value */%@AE@%%@NL@%
  10123.         int n;                                %@AB@%/* number of items found */%@AE@%%@NL@%
  10124.         int fileattr;                        %@AB@%/* attributes of file-matching */%@AE@%%@NL@%
  10125.         struct DTA buf;                        %@AB@%/* disk transfer area */%@AE@%%@NL@%
  10126.         struct OUTBUF *bp, *bq;                %@AB@%/* output buffer pointers */%@AE@%%@NL@%
  10127.         char path[MAXPATH + 1];                %@AB@%/* working path string */%@AE@%%@NL@%
  10128. %@NL@%
  10129.         extern void setdta(char *);%@NL@%
  10130.         extern int first_fm(char *, int);%@NL@%
  10131.         extern int next_fm();%@NL@%
  10132.         extern int ls_fcomp(struct OUTBUF *, struct OUTBUF *);%@NL@%
  10133.         extern char last_ch(char *);%@NL@%
  10134. %@NL@%
  10135.         %@AB@%/* allocate a buffer */%@AE@%%@NL@%
  10136.         bp = bq = (struct OUTBUF *)malloc(NFILES * sizeof(struct OUTBUF));%@NL@%
  10137.         if (bp == NULL)%@NL@%
  10138.                 fatal(pname, "Out of memory");%@NL@%
  10139. %@NL@%
  10140.         %@AB@%/* form name for directory search */%@AE@%%@NL@%
  10141.         strcpy(path, namep);%@NL@%
  10142.         if (last_ch(path) != '\\')%@NL@%
  10143.                 strcat(path, "\\");%@NL@%
  10144.         strcat(path, "*.*");%@NL@%
  10145. %@NL@%
  10146.         %@AB@%/* list the files found */%@AE@%%@NL@%
  10147.         n = 0;%@NL@%
  10148.         %@AB@%/* establish a private DTA */%@AE@%%@NL@%
  10149.         setdta((char *)&buf);%@NL@%
  10150.         %@AB@%/* select file attributes */%@AE@%%@NL@%
  10151.         if (Hidden)%@NL@%
  10152.                 fileattr = SUBDIR | HIDDEN | SYSTEM | READONLY;%@NL@%
  10153.         else%@NL@%
  10154.                 fileattr = SUBDIR;%@NL@%
  10155.         if (first_fm(path, fileattr) == 0) {%@NL@%
  10156.                 %@AB@%/* add file or directory to the buffer */%@AE@%%@NL@%
  10157.                 do {%@NL@%
  10158.                         if (!Hidden && buf.d_fname[0] == '.')%@NL@%
  10159.                                 continue;%@NL@%
  10160.                         bq->o_name = strdup(buf.d_fname);%@NL@%
  10161.                         bq->o_mode = buf.d_attr;%@NL@%
  10162.                         bq->o_size = buf.d_fsize;%@NL@%
  10163.                         bq->o_date = buf.d_mdate;%@NL@%
  10164.                         bq->o_time = buf.d_mtime;%@NL@%
  10165.                         ++bq;%@NL@%
  10166.                         ++n;%@NL@%
  10167.                         setdta((char *)&buf);        %@AB@%/* reset to our DTA */%@AE@%%@NL@%
  10168.                 } while (next_fm() == 0);%@NL@%
  10169. %@NL@%
  10170.                 if (n > 0) {%@NL@%
  10171.                         %@AB@%/* got some -- sort and list them */%@AE@%%@NL@%
  10172.                         qsort(bp, n, sizeof(struct OUTBUF), ls_fcomp);%@NL@%
  10173.                         if (Longlist)%@NL@%
  10174.                                 ls_long(bp, n);%@NL@%
  10175.                         else if (Multicol)%@NL@%
  10176.                                 ls_multi(bp, n);%@NL@%
  10177.                         else%@NL@%
  10178.                                 ls_single(bp, n);%@NL@%
  10179.                 }%@NL@%
  10180.         }%@NL@%
  10181.         else %@NL@%
  10182.                 ++status;%@NL@%
  10183.         free(bp);%@NL@%
  10184. %@NL@%
  10185.         return (status);%@NL@%
  10186. }%@NL@%
  10187. %@NL@%
  10188. %@NL@%
  10189. %@2@%%@AH@%LS_FCOMP.C%@AE@%%@EH@%%@NL@%
  10190. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\08FILE\LS_FCOMP.C%@AE@%%@NL@%
  10191. %@NL@%
  10192. %@AB@%/*%@NL@%
  10193. %@AB@% *        ls_fcomp -- file and directory comparison functions%@NL@%
  10194. %@AB@% */%@AE@%%@NL@%
  10195. %@NL@%
  10196. %@AI@%#include %@AE@%<string.h> %@NL@%
  10197. %@AI@%#include %@AE@%"ls.h" %@NL@%
  10198. %@NL@%
  10199. extern int Modtime;%@NL@%
  10200. extern int Reverse;%@NL@%
  10201. %@NL@%
  10202. %@NL@%
  10203. %@AB@%/*%@NL@%
  10204. %@AB@% *        ls_fcomp -- compare two "file" items%@NL@%
  10205. %@AB@% */%@AE@%%@NL@%
  10206. %@NL@%
  10207. int%@NL@%
  10208. ls_fcomp(s1, s2)%@NL@%
  10209. struct OUTBUF *s1, *s2;%@NL@%
  10210. {%@NL@%
  10211.         int result;%@NL@%
  10212. %@NL@%
  10213.         if (Modtime) {%@NL@%
  10214.                 if ((result = s1->o_date - s2->o_date) == 0)%@NL@%
  10215.                         result = s1->o_time - s2->o_time;%@NL@%
  10216.         }%@NL@%
  10217.         else%@NL@%
  10218.                 result = strcmp(s1->o_name, s2->o_name);%@NL@%
  10219. %@NL@%
  10220.         return (Reverse ? -result : result);%@NL@%
  10221. } %@AB@%/* end_fcomp() */%@AE@%%@NL@%
  10222. %@NL@%
  10223. %@NL@%
  10224. %@AB@%/*%@NL@%
  10225. %@AB@% *        dcomp -- compare two "directory" items%@NL@%
  10226. %@AB@% */%@AE@%%@NL@%
  10227. %@NL@%
  10228. int%@NL@%
  10229. ls_dcomp(s1, s2)%@NL@%
  10230. char *s1, *s2;%@NL@%
  10231. {%@NL@%
  10232.         int result;%@NL@%
  10233. %@NL@%
  10234.         result = strcmp(s1, s2);%@NL@%
  10235. %@NL@%
  10236.         return (Reverse ? -result : result);%@NL@%
  10237. } %@AB@%/* end ls_dcomp() */%@AE@%%@NL@%
  10238. %@NL@%
  10239. %@NL@%
  10240. %@2@%%@AH@%LS_LIST.C%@AE@%%@EH@%%@NL@%
  10241. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\08FILE\LS_LIST.C%@AE@%%@NL@%
  10242. %@NL@%
  10243. %@AB@%/*%@NL@%
  10244. %@AB@% *        ls_list -- list functions (long, single, multi) for ls%@NL@%
  10245. %@AB@% */%@AE@%%@NL@%
  10246. %@NL@%
  10247. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  10248. %@AI@%#include %@AE@%<string.h> %@NL@%
  10249. %@AI@%#include %@AE@%"ls.h" %@NL@%
  10250. %@NL@%
  10251. %@AI@%#define %@AE@%MAXCOL                80 %@NL@%
  10252. %@AI@%#define %@AE@%MONTH_SHIFT        5 %@NL@%
  10253. %@AI@%#define %@AE@%MONTH_MASK        0x0F %@NL@%
  10254. %@AI@%#define %@AE@%DAY_MASK        0x1F %@NL@%
  10255. %@AI@%#define %@AE@%YEAR_SHIFT        9 %@NL@%
  10256. %@AI@%#define %@AE@%DOS_EPOCH        80 %@NL@%
  10257. %@AI@%#define %@AE@%HOUR_SHIFT        11 %@NL@%
  10258. %@AI@%#define %@AE@%HOUR_MASK        0x1F %@NL@%
  10259. %@AI@%#define %@AE@%MINUTE_SHIFT        5 %@NL@%
  10260. %@AI@%#define %@AE@%MINUTE_MASK        0x3F %@NL@%
  10261. %@NL@%
  10262. extern int Filetype;%@NL@%
  10263. %@NL@%
  10264. %@AB@%/*%@NL@%
  10265. %@AB@% *        ls_long -- list items in "long" format (mode time size name)%@NL@%
  10266. %@AB@% */%@AE@%%@NL@%
  10267. %@NL@%
  10268. int%@NL@%
  10269. ls_long(buf, nelem)%@NL@%
  10270. struct OUTBUF *buf;%@NL@%
  10271. int nelem;%@NL@%
  10272. {%@NL@%
  10273.         int n = 0;%@NL@%
  10274.         char modebuf[5];%@NL@%
  10275.         static void modestr(unsigned short, char *);%@NL@%
  10276. %@NL@%
  10277.         while (nelem-- > 0) {%@NL@%
  10278.                 %@AB@%/* convert mode number to a string */%@AE@%%@NL@%
  10279.                 modestr(buf->o_mode, modebuf);%@NL@%
  10280.                 printf("%s ", modebuf);%@NL@%
  10281. %@NL@%
  10282.                 %@AB@%/* display file size in bytes */%@AE@%%@NL@%
  10283.                 if ((buf->o_mode & SUBDIR) == SUBDIR)%@NL@%
  10284.                         printf("        ");%@NL@%
  10285.                 else%@NL@%
  10286.                         printf("%7ld ", buf->o_size);%@NL@%
  10287. %@NL@%
  10288.                 %@AB@%/* convert date and time values to formatted presentation */%@AE@%%@NL@%
  10289.                 printf("%02d-%02d-%02d ", (buf->o_date >> MONTH_SHIFT) & MONTH_MASK,%@NL@%
  10290.                         buf->o_date & DAY_MASK, (buf->o_date >> YEAR_SHIFT) + DOS_EPOCH);%@NL@%
  10291.                 printf("%02d:%02d ", (buf->o_time >> HOUR_SHIFT) & HOUR_MASK,%@NL@%
  10292.                         (buf->o_time >> MINUTE_SHIFT) & MINUTE_MASK);%@NL@%
  10293. %@NL@%
  10294.                 %@AB@%/* display filenames as lowercase strings */%@AE@%%@NL@%
  10295.                 printf("%s\n", strlwr(buf->o_name));%@NL@%
  10296. %@NL@%
  10297.                 ++buf;%@NL@%
  10298.                 ++n;%@NL@%
  10299.         }%@NL@%
  10300. %@NL@%
  10301.         %@AB@%/* tell caller how many entries were printed */%@AE@%%@NL@%
  10302.         return (n);%@NL@%
  10303. } %@AB@%/* end ls_long() */%@AE@%%@NL@%
  10304. %@NL@%
  10305. %@NL@%
  10306. %@AB@%/*%@NL@%
  10307. %@AB@% *        ls_single -- list items in a single column%@NL@%
  10308. %@AB@% */%@AE@%%@NL@%
  10309. %@NL@%
  10310. int%@NL@%
  10311. ls_single(buf, nelem)%@NL@%
  10312. struct OUTBUF *buf;%@NL@%
  10313. int nelem;%@NL@%
  10314. {%@NL@%
  10315.         int n = 0;%@NL@%
  10316. %@NL@%
  10317.         while (nelem-- > 0) {%@NL@%
  10318.                 printf("%s", strlwr(buf->o_name));%@NL@%
  10319.                 if (Filetype && (buf->o_mode & SUBDIR) == SUBDIR)%@NL@%
  10320.                         putchar('\\');%@NL@%
  10321.                 putchar('\n');%@NL@%
  10322.                 ++buf;%@NL@%
  10323.                 ++n;%@NL@%
  10324.         }%@NL@%
  10325. %@NL@%
  10326.         %@AB@%/* tell caller how many entries were printed */%@AE@%%@NL@%
  10327.         return (n);%@NL@%
  10328. } %@AB@%/* end ls_single() */%@AE@%%@NL@%
  10329. %@NL@%
  10330. %@NL@%
  10331. %@AB@%/*%@NL@%
  10332. %@AB@% *        ls_multi -- list items in multiple columns that%@NL@%
  10333. %@AB@% *        vary in width and number based on longest item size%@NL@%
  10334. %@AB@% */%@AE@%%@NL@%
  10335. %@NL@%
  10336. int%@NL@%
  10337. ls_multi(buf, nelem)%@NL@%
  10338. struct OUTBUF *buf;%@NL@%
  10339. int nelem;%@NL@%
  10340. {%@NL@%
  10341.         int i, j;%@NL@%
  10342.         int errcount = 0;%@NL@%
  10343.         struct OUTBUF *tmp;        %@AB@%/* temporary buffer pointer */%@AE@%%@NL@%
  10344.         struct OUTBUF *base;        %@AB@%/* buffer pointer for multi-col output */%@AE@%%@NL@%
  10345.         int n;                        %@AB@%/* number of items in list */%@AE@%%@NL@%
  10346.         int len, maxlen;        %@AB@%/* pathname lengths */%@AE@%%@NL@%
  10347.         int ncols;                %@AB@%/* number of columns to output */%@AE@%%@NL@%
  10348.         int nlines;                %@AB@%/* number of lines to output */%@AE@%%@NL@%
  10349. %@NL@%
  10350.         %@AB@%/*%@NL@%
  10351. %@AB@%         *  get length of longest pathname and calculate number%@NL@%
  10352. %@AB@%         *  of columns and lines (col width = maxlen + 1)%@NL@%
  10353. %@AB@%         */%@AE@%%@NL@%
  10354.         tmp = buf;%@NL@%
  10355.         n = 0;%@NL@%
  10356.         maxlen = 0;%@NL@%
  10357.         for (tmp = buf, n = 0; n < nelem; ++tmp, ++n)%@NL@%
  10358.                 if ((len = strlen(tmp->o_name)) > maxlen)%@NL@%
  10359.                         maxlen = len;%@NL@%
  10360.         %@AB@%/*%@NL@%
  10361. %@AB@%         *  use width of screen - 1 to allow for newline at end of%@NL@%
  10362. %@AB@%         *  line and leave two spaces between entries (one for optional%@NL@%
  10363. %@AB@%         *  file type flag)%@NL@%
  10364. %@AB@%         */%@AE@%%@NL@%
  10365.         ncols = (MAXCOL - 1) / (maxlen + 2);%@NL@%
  10366.         nlines = n / ncols;%@NL@%
  10367.         if (n % ncols)%@NL@%
  10368.                 ++nlines;%@NL@%
  10369. %@NL@%
  10370.         %@AB@%/* output multi-column list */%@AE@%%@NL@%
  10371.         base = buf;%@NL@%
  10372.         for (i = 0; i < nlines; ++i) {%@NL@%
  10373.                 tmp = base;%@NL@%
  10374.                 for (j = 0; j < ncols; ++j) {%@NL@%
  10375.                         len = maxlen + 2;%@NL@%
  10376.                         len -= printf("%s", strlwr(tmp->o_name));%@NL@%
  10377.                         if (Filetype && (tmp->o_mode & SUBDIR) == SUBDIR) {%@NL@%
  10378.                                 putchar('\\');%@NL@%
  10379.                                 --len;%@NL@%
  10380.                         }%@NL@%
  10381.                         while (len-- > 0)%@NL@%
  10382.                                 putchar(' ');%@NL@%
  10383.                         tmp += nlines;%@NL@%
  10384.                         if (tmp - buf >= nelem)%@NL@%
  10385.                                 break;%@NL@%
  10386.                 }%@NL@%
  10387.                 putchar('\n');%@NL@%
  10388.                 ++base;%@NL@%
  10389.         }%@NL@%
  10390. %@NL@%
  10391.         return (errcount);%@NL@%
  10392. } %@AB@%/* end ls_multi() */%@AE@%%@NL@%
  10393. %@NL@%
  10394. %@NL@%
  10395. static void%@NL@%
  10396. modestr(mode, s)%@NL@%
  10397. unsigned short mode;        %@AB@%/* file mode number */%@AE@%%@NL@%
  10398. char s[];                %@AB@%/* mode string buffer */%@AE@%%@NL@%
  10399. {%@NL@%
  10400. %@NL@%
  10401.         %@AB@%/* fill in the mode string to show what's set */%@AE@%%@NL@%
  10402.         s[0] = (mode & SUBDIR) == SUBDIR ? 'd' : '-';%@NL@%
  10403.         s[1] = (mode & HIDDEN) == HIDDEN ? 'h' : '-';%@NL@%
  10404.         s[2] = (mode & SYSTEM) == SYSTEM ? 's' : '-';%@NL@%
  10405.         s[3] = (mode & READONLY) == READONLY ? 'r' : '-';%@NL@%
  10406.         s[4] = '\0';%@NL@%
  10407. } %@AB@%/* end modestr() */%@AE@%%@NL@%
  10408. %@NL@%
  10409. %@NL@%
  10410. %@2@%%@AH@%MEMCHK.C%@AE@%%@EH@%%@NL@%
  10411. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\MEMCHK.C%@AE@%%@NL@%
  10412. %@NL@%
  10413. %@AB@%/*%@NL@%
  10414. %@AB@% *        memchk -- look for random-access memory at%@NL@%
  10415. %@AB@% *        a specified location; return non-zero if found%@NL@%
  10416. %@AB@% */%@AE@%%@NL@%
  10417. %@NL@%
  10418. %@AI@%#include %@AE@%<dos.h> %@NL@%
  10419. %@AI@%#include %@AE@%<memory.h> %@NL@%
  10420. %@NL@%
  10421. int%@NL@%
  10422. memchk(seg, os)%@NL@%
  10423. unsigned int seg;%@NL@%
  10424. unsigned int os;%@NL@%
  10425. {%@NL@%
  10426.         unsigned char tstval, oldval, newval;%@NL@%
  10427.         unsigned int ds;%@NL@%
  10428.         struct SREGS segregs;%@NL@%
  10429. %@NL@%
  10430.         %@AB@%/* get value of current data segment */%@AE@%%@NL@%
  10431.         segread(&segregs);%@NL@%
  10432.         ds = segregs.ds;%@NL@%
  10433. %@NL@%
  10434.         %@AB@%/* save current contents of test location */%@AE@%%@NL@%
  10435.         movedata(seg, os, ds, (unsigned int)&oldval, 1);%@NL@%
  10436. %@NL@%
  10437.         %@AB@%/* copy a known value into test location */%@AE@%%@NL@%
  10438.         tstval = 0xFC;%@NL@%
  10439.         movedata(ds, (unsigned int)&tstval, seg, os, 1);%@NL@%
  10440. %@NL@%
  10441.         %@AB@%/* read test value back and comapre to value written */%@AE@%%@NL@%
  10442.         movedata(seg, os, ds, (unsigned int)&newval, 1);%@NL@%
  10443.         if (newval != tstval)%@NL@%
  10444.                 return (0);%@NL@%
  10445. %@NL@%
  10446.         %@AB@%/* restore original contents of test location */%@AE@%%@NL@%
  10447.         movedata(ds, (unsigned int)&oldval, seg, os, 1);%@NL@%
  10448. %@NL@%
  10449.         return (1);%@NL@%
  10450. }%@NL@%
  10451. %@NL@%
  10452. %@NL@%
  10453. %@2@%%@AH@%MEMSIZE.C%@AE@%%@EH@%%@NL@%
  10454. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\MEMSIZE.C%@AE@%%@NL@%
  10455. %@NL@%
  10456. %@AB@%/*%@NL@%
  10457. %@AB@% *        memsize -- get memory size%@NL@%
  10458. %@AB@% */%@AE@%%@NL@%
  10459. %@NL@%
  10460. %@AI@%#include %@AE@%<dos.h> %@NL@%
  10461. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  10462. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  10463. %@NL@%
  10464. int%@NL@%
  10465. memsize()%@NL@%
  10466. {%@NL@%
  10467.         union REGS inregs, outregs;%@NL@%
  10468. %@NL@%
  10469.         return (int86(MEM_SIZE, &inregs, &outregs));%@NL@%
  10470. }%@NL@%
  10471. %@NL@%
  10472. %@NL@%
  10473. %@2@%%@AH@%MENUMODE.C%@AE@%%@EH@%%@NL@%
  10474. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\13ANSI\MENUMODE.C%@AE@%%@NL@%
  10475. %@NL@%
  10476. %@AB@%/*%@NL@%
  10477. %@AB@% *  menumode -- process user commands interactively%@NL@%
  10478. %@AB@% */%@AE@%%@NL@%
  10479. %@NL@%
  10480. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  10481. %@AI@%#include %@AE@%<local\ansi.h> %@NL@%
  10482. %@AI@%#include %@AE@%<local\ibmcolor.h> %@NL@%
  10483. %@AI@%#include %@AE@%<local\keydefs.h> %@NL@%
  10484. %@NL@%
  10485. %@AB@%/* maximum color number */%@AE@%%@NL@%
  10486. %@AI@%#define %@AE@%MAX_CNUM        15 %@NL@%
  10487. %@NL@%
  10488. void%@NL@%
  10489. menumode()%@NL@%
  10490. {%@NL@%
  10491.         register int ch;%@NL@%
  10492.         int foreground, background, border;%@NL@%
  10493.         extern void setattr(POSITION, int);%@NL@%
  10494.         extern void sc_cmds(int, int, int);%@NL@%
  10495. %@NL@%
  10496.         %@AB@%/* default attributes */%@AE@%%@NL@%
  10497.         foreground = IBM_WHITE;%@NL@%
  10498.         background = IBM_BLACK;%@NL@%
  10499.         border = IBM_BLACK;%@NL@%
  10500. %@NL@%
  10501.         ANSI_SGR(ANSI_NORMAL);%@NL@%
  10502.         setattr(FGND, foreground);%@NL@%
  10503.         setattr(BKGND, background);%@NL@%
  10504.         ANSI_ED;%@NL@%
  10505.         palette(0, border);%@NL@%
  10506.         sc_cmds(foreground, background, border);%@NL@%
  10507.         while ((ch = getkey()) != K_RETURN) {%@NL@%
  10508.                 switch (ch) {%@NL@%
  10509.                 case K_F1:%@NL@%
  10510.                         %@AB@%/* decrement foreground color */%@AE@%%@NL@%
  10511.                         if (--foreground < 0)%@NL@%
  10512.                                 foreground = MAX_CNUM;%@NL@%
  10513.                         break;%@NL@%
  10514.                 case K_F2:%@NL@%
  10515.                         %@AB@%/* increment foreground color */%@AE@%%@NL@%
  10516.                         if (++foreground > MAX_CNUM)%@NL@%
  10517.                                 foreground = 0;%@NL@%
  10518.                         break;%@NL@%
  10519.                 case K_F3:%@NL@%
  10520.                         %@AB@%/* decrement background color */%@AE@%%@NL@%
  10521.                         if (--background < 0)%@NL@%
  10522.                                 background = MAX_CNUM;%@NL@%
  10523.                         break;%@NL@%
  10524.                 case K_F4:%@NL@%
  10525.                         %@AB@%/* increment background color */%@AE@%%@NL@%
  10526.                         if (++background > MAX_CNUM)%@NL@%
  10527.                                 background = 0;%@NL@%
  10528.                         break;%@NL@%
  10529.                 case K_F5:%@NL@%
  10530.                         %@AB@%/* decrement border color */%@AE@%%@NL@%
  10531.                         if (--border < 0)%@NL@%
  10532.                                 border = MAX_CNUM;%@NL@%
  10533.                         break;%@NL@%
  10534.                 case K_F6:%@NL@%
  10535.                         %@AB@%/* increment border color number */%@AE@%%@NL@%
  10536.                         if (++border > MAX_CNUM)%@NL@%
  10537.                                 border = 0;%@NL@%
  10538.                         break;%@NL@%
  10539.                 default:%@NL@%
  10540.                         continue;%@NL@%
  10541.                 }%@NL@%
  10542.                 ANSI_SGR(ANSI_NORMAL);%@NL@%
  10543.                 setattr(FGND, foreground);%@NL@%
  10544.                 setattr(BKGND, background);%@NL@%
  10545.                 palette(0, border);%@NL@%
  10546.                 ANSI_ED;%@NL@%
  10547.                 sc_cmds(foreground, background, border);%@NL@%
  10548.         }%@NL@%
  10549.         return;%@NL@%
  10550. }%@NL@%
  10551. %@NL@%
  10552. %@NL@%
  10553. %@2@%%@AH@%MESSAGE.C%@AE@%%@EH@%%@NL@%
  10554. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\14VIEW\MESSAGE.C%@AE@%%@NL@%
  10555. %@NL@%
  10556. %@AB@%/*%@NL@%
  10557. %@AB@% *        message -- routines used to display and clear%@NL@%
  10558. %@AB@% *        messages in a reserved message area%@NL@%
  10559. %@AB@% */%@AE@%%@NL@%
  10560. %@NL@%
  10561. %@AI@%#include %@AE@%"message.h" %@NL@%
  10562. %@NL@%
  10563. MESSAGE Ml;%@NL@%
  10564. extern int writec(char, int, int);%@NL@%
  10565. %@NL@%
  10566. %@AB@%/*%@NL@%
  10567. %@AB@% *        set up the message-line manager%@NL@%
  10568. %@AB@% */%@AE@%%@NL@%
  10569. %@NL@%
  10570. void%@NL@%
  10571. initmsg(r, c, w, a, pg)%@NL@%
  10572. int r;                        %@AB@%/* message row */%@AE@%%@NL@%
  10573. int c;                        %@AB@%/* message column */%@AE@%%@NL@%
  10574. int w;                        %@AB@%/* width of message field */%@AE@%%@NL@%
  10575. unsigned char a;        %@AB@%/* message field video attribute */%@AE@%%@NL@%
  10576. int pg;                        %@AB@%/* active page for messages */%@AE@%%@NL@%
  10577. {%@NL@%
  10578.         MESSAGE *mp;%@NL@%
  10579.         void clrmsg();%@NL@%
  10580. %@NL@%
  10581.         mp = &Ml;%@NL@%
  10582.         mp->m_row = r;%@NL@%
  10583.         mp->m_col = c;%@NL@%
  10584.         mp->m_wid = w;%@NL@%
  10585.         mp->m_attr = a;%@NL@%
  10586.         mp->m_pg = pg;%@NL@%
  10587.         mp->m_flag = 1;%@NL@%
  10588.         clrmsg();%@NL@%
  10589. }%@NL@%
  10590. %@NL@%
  10591. %@NL@%
  10592. %@AB@%/*%@NL@%
  10593. %@AB@% *        showmsg -- display a message and set the message flag%@NL@%
  10594. %@AB@% */%@AE@%%@NL@%
  10595. %@NL@%
  10596. void%@NL@%
  10597. showmsg(msg)%@NL@%
  10598. char *msg;%@NL@%
  10599. {%@NL@%
  10600.         MESSAGE *mp;%@NL@%
  10601. %@NL@%
  10602.         mp = &Ml;%@NL@%
  10603.         putcur(mp->m_row, mp->m_col, mp->m_pg);%@NL@%
  10604.         writec(' ', mp->m_wid, mp->m_pg);%@NL@%
  10605.         putstr(msg, mp->m_pg);%@NL@%
  10606.         mp->m_flag = 1;%@NL@%
  10607.         return;%@NL@%
  10608. }%@NL@%
  10609. %@NL@%
  10610. %@NL@%
  10611. %@AB@%/*%@NL@%
  10612. %@AB@% *        clrmsg -- erase the message area and reset the message flag%@NL@%
  10613. %@AB@% */%@AE@%%@NL@%
  10614. %@NL@%
  10615. void%@NL@%
  10616. clrmsg()%@NL@%
  10617. {%@NL@%
  10618.         MESSAGE *mp;%@NL@%
  10619. %@NL@%
  10620.         mp = &Ml;%@NL@%
  10621.         if (mp->m_flag != 0) {%@NL@%
  10622.                 putcur(mp->m_row, mp->m_col, mp->m_pg);%@NL@%
  10623.                 writec(' ', mp->m_wid, mp->m_pg);%@NL@%
  10624.                 mp->m_flag = 0;%@NL@%
  10625.         }%@NL@%
  10626.         return;%@NL@%
  10627. }%@NL@%
  10628. %@NL@%
  10629. %@NL@%
  10630. %@2@%%@AH@%MX.C%@AE@%%@EH@%%@NL@%
  10631. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\07CONFIG\MX.C%@AE@%%@NL@%
  10632. %@NL@%
  10633. %@AB@%/*%@NL@%
  10634. %@AB@% *        mx -- control Epson MX-series printer%@NL@%
  10635. %@AB@% */%@AE@%%@NL@%
  10636. %@NL@%
  10637. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  10638. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  10639. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  10640. %@AI@%#include %@AE@%<local\printer.h> %@NL@%
  10641. %@NL@%
  10642. extern PRINTER prt;        %@AB@%/* printer data */%@AE@%%@NL@%
  10643. %@NL@%
  10644. main(argc, argv)%@NL@%
  10645. int argc;%@NL@%
  10646. char **argv;%@NL@%
  10647. {%@NL@%
  10648.         int ch, font;%@NL@%
  10649.         BOOLEAN errflag;        %@AB@%/* option error */%@AE@%%@NL@%
  10650.         BOOLEAN clrflag;        %@AB@%/* clear special fonts */%@AE@%%@NL@%
  10651.         BOOLEAN rflag;                %@AB@%/* hardware reset */%@AE@%%@NL@%
  10652.         BOOLEAN tflag;                %@AB@%/* top-of-form */%@AE@%%@NL@%
  10653.         FILE *fout;%@NL@%
  10654.         static char pgm[MAXNAME + 1] = { "mx" };%@NL@%
  10655. %@NL@%
  10656.         extern void fatal(char *, char *, int);%@NL@%
  10657.         extern char *getpname(char *, char *);%@NL@%
  10658.         extern int getopt(int, char **, char *);%@NL@%
  10659.         extern char *optarg;%@NL@%
  10660.         extern int optind, opterr;%@NL@%
  10661.         extern int setprnt();%@NL@%
  10662.         extern int clrprnt(FILE *);%@NL@%
  10663.         extern int setfont(int, FILE *);%@NL@%
  10664. %@NL@%
  10665.         if (_osmajor >= 3)%@NL@%
  10666.                 getpname(*argv, pgm);%@NL@%
  10667. %@NL@%
  10668.         if (setprnt() == -1) {%@NL@%
  10669.                 fprintf(stderr, "%s: Bad printer configuration\n", pgm);%@NL@%
  10670.                 exit(1);%@NL@%
  10671.         }%@NL@%
  10672. %@NL@%
  10673.         %@AB@%/* interpret command line */%@AE@%%@NL@%
  10674.         errflag = clrflag = rflag = tflag = FALSE;%@NL@%
  10675.         font = 0;%@NL@%
  10676.         fout = stdprn;%@NL@%
  10677.         while ((ch = getopt(argc, argv, "bcdefino:prtu")) != EOF) {%@NL@%
  10678.                 switch (ch) {%@NL@%
  10679.                 case 'b':%@NL@%
  10680.                         %@AB@%/* set bold */%@AE@%%@NL@%
  10681.                         font |= EMPHASIZED;%@NL@%
  10682.                         break;%@NL@%
  10683.                 case 'c':%@NL@%
  10684.                         %@AB@%/* set compressed */%@AE@%%@NL@%
  10685.                         font |= CONDENSED;%@NL@%
  10686.                         break;%@NL@%
  10687.                 case 'd':%@NL@%
  10688.                         %@AB@%/* set double strike */%@AE@%%@NL@%
  10689.                         font |= DOUBLE;%@NL@%
  10690.                         break;%@NL@%
  10691.                 case 'e':%@NL@%
  10692.                         %@AB@%/* set double strike */%@AE@%%@NL@%
  10693.                         font |= EXPANDED;%@NL@%
  10694.                         break;%@NL@%
  10695.                 case 'i':%@NL@%
  10696.                         %@AB@%/* set italic */%@AE@%%@NL@%
  10697.                         font |= ITALICS;%@NL@%
  10698.                         break;%@NL@%
  10699.                 case 'n':%@NL@%
  10700.                         %@AB@%/* set normal (clear all special fonts) */%@AE@%%@NL@%
  10701.                         clrflag = TRUE;%@NL@%
  10702.                         break;%@NL@%
  10703.                 case 'o':%@NL@%
  10704.                         %@AB@%/* use specified output stream */%@AE@%%@NL@%
  10705.                         if ((fout = fopen(optarg, "w")) == NULL)%@NL@%
  10706.                                 fatal(pgm, "cannot open output stream", 1);%@NL@%
  10707.                         break;%@NL@%
  10708.                 case 'p':%@NL@%
  10709.                         %@AB@%/* preview control strings on stdout */%@AE@%%@NL@%
  10710.                         fout = stdout;%@NL@%
  10711.                         break;%@NL@%
  10712.                 case 'r':%@NL@%
  10713.                         %@AB@%/* hardware reset */%@AE@%%@NL@%
  10714.                         rflag = TRUE;%@NL@%
  10715.                         break;%@NL@%
  10716.                 case 't':%@NL@%
  10717.                         %@AB@%/* top of form */%@AE@%%@NL@%
  10718.                         tflag = TRUE;%@NL@%
  10719.                         break;%@NL@%
  10720.                 case 'u':%@NL@%
  10721.                         %@AB@%/* set underline */%@AE@%%@NL@%
  10722.                         font |= UNDERLINE;%@NL@%
  10723.                         break;%@NL@%
  10724.                 case '?':%@NL@%
  10725.                         %@AB@%/* unknown option */%@AE@%%@NL@%
  10726.                         errflag = TRUE;%@NL@%
  10727.                         break;%@NL@%
  10728.                 }%@NL@%
  10729.         }%@NL@%
  10730. %@NL@%
  10731.         %@AB@%/* report errors, if any */%@AE@%%@NL@%
  10732.         if (errflag == TRUE || argc == 1) {%@NL@%
  10733.                 fprintf(stderr, "Usage: %s -option\n", pgm);%@NL@%
  10734.                 fprintf(stderr,%@NL@%
  10735.                         "b=bold, c=compressed, d=double strike, e=expanded\n");%@NL@%
  10736.                 fprintf(stderr,%@NL@%
  10737.                         "i=italic, n=normal, o file=output to file\n");%@NL@%
  10738.                 fprintf(stderr,%@NL@%
  10739.                         "p=preview, r=reset, t=top-of-form, u=underline\n");%@NL@%
  10740.                 exit(2);%@NL@%
  10741.         }%@NL@%
  10742. %@NL@%
  10743.         %@AB@%/* do hardware reset and formfeed first */%@AE@%%@NL@%
  10744.         if (rflag == TRUE)%@NL@%
  10745.                 fputs(prt.p_init, fout);%@NL@%
  10746.         else if (tflag == TRUE)%@NL@%
  10747.                 fputc('\f', fout);%@NL@%
  10748. %@NL@%
  10749.         %@AB@%/* clear or set the aggregate font */%@AE@%%@NL@%
  10750.         if (clrflag == TRUE)%@NL@%
  10751.                 clrprnt(fout);%@NL@%
  10752.         else if (setfont(font, fout) == FAILURE) {%@NL@%
  10753.                 fprintf(stderr, "%s: Bad font spec\n", pgm);%@NL@%
  10754.                 exit(3);%@NL@%
  10755.         }%@NL@%
  10756. %@NL@%
  10757.         exit(0);%@NL@%
  10758. }%@NL@%
  10759. %@NL@%
  10760. %@NL@%
  10761. %@2@%%@AH@%NEXT_FM.C%@AE@%%@EH@%%@NL@%
  10762. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\DOS\NEXT_FM.C%@AE@%%@NL@%
  10763. %@NL@%
  10764. %@AB@%/*%@NL@%
  10765. %@AB@% *        next_fm - find next file match in work directory%@NL@%
  10766. %@AB@% */%@AE@%%@NL@%
  10767. %@NL@%
  10768. %@AI@%#include %@AE@%<dos.h> %@NL@%
  10769. %@AI@%#include %@AE@%<local\doslib.h> %@NL@%
  10770. %@NL@%
  10771. int%@NL@%
  10772. next_fm()%@NL@%
  10773. {%@NL@%
  10774.         union REGS inregs, outregs;%@NL@%
  10775. %@NL@%
  10776.         %@AB@%/* find next matching file */%@AE@%%@NL@%
  10777.         inregs.h.ah = FIND_NEXT;%@NL@%
  10778.         (void)intdos(&inregs, &outregs);%@NL@%
  10779. %@NL@%
  10780.         return (outregs.x.cflag);%@NL@%
  10781. }%@NL@%
  10782. %@NL@%
  10783. %@NL@%
  10784. %@2@%%@AH@%NLERASE.C%@AE@%%@EH@%%@NL@%
  10785. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\NLERASE.C%@AE@%%@NL@%
  10786. %@NL@%
  10787. %@AB@%/*%@NL@%
  10788. %@AB@% *        nlerase -- replace the first newline in a string%@NL@%
  10789. %@AB@% *        with a null character%@NL@%
  10790. %@AB@% */%@AE@%%@NL@%
  10791. %@NL@%
  10792. char *%@NL@%
  10793. nlerase(s)%@NL@%
  10794. char *s;%@NL@%
  10795. {%@NL@%
  10796.         register char *cp;%@NL@%
  10797. %@NL@%
  10798.         cp = s;%@NL@%
  10799.         while (*cp != '\n' && *cp != '\0')%@NL@%
  10800.                 ++cp;%@NL@%
  10801.         *cp = '\0';%@NL@%
  10802. %@NL@%
  10803.         return (s);%@NL@%
  10804. }%@NL@%
  10805. %@NL@%
  10806. %@NL@%
  10807. %@2@%%@AH@%NOTES1.C%@AE@%%@EH@%%@NL@%
  10808. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\04STDLIB\NOTES1.C%@AE@%%@NL@%
  10809. %@NL@%
  10810. %@AB@%/*%@NL@%
  10811. %@AB@% *        notes1 -- add an entry to a "notes" text file%@NL@%
  10812. %@AB@% *%@NL@%
  10813. %@AB@% *        version 1: appends new data to NOTES.TXT in the%@NL@%
  10814. %@AB@% *        current directory -- uses local date/time stamp%@NL@%
  10815. %@AB@% *        as a header for each new entry%@NL@%
  10816. %@AB@% */%@AE@%%@NL@%
  10817. %@NL@%
  10818. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  10819. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  10820. %@AI@%#include %@AE@%<time.h> %@NL@%
  10821. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  10822. %@NL@%
  10823. main()%@NL@%
  10824. {%@NL@%
  10825.         FILE *fp;%@NL@%
  10826.         static char notesfile[MAXPATH + 1] = { "notes.txt" };%@NL@%
  10827.         char ch;%@NL@%
  10828.         long ltime;%@NL@%
  10829.         static char pgm[MAXNAME + 1] = { "notes1" };%@NL@%
  10830. %@NL@%
  10831.         extern void fatal(char *, char *, int);%@NL@%
  10832. %@NL@%
  10833.         %@AB@%/* try to open notes file in current directory */%@AE@%%@NL@%
  10834.         if ((fp = fopen(notesfile, "a")) == NULL)%@NL@%
  10835.                 fatal(pgm, notesfile, 1);%@NL@%
  10836. %@NL@%
  10837.         %@AB@%/* append a header and date/time tag */%@AE@%%@NL@%
  10838.         ltime = time(NULL);%@NL@%
  10839.         fprintf(stderr, "Appending to %s: %s",%@NL@%
  10840.                  notesfile, ctime(<ime));%@NL@%
  10841.         fprintf(fp, "%s", ctime(<ime));%@NL@%
  10842. %@NL@%
  10843.         %@AB@%/* append new text */%@AE@%%@NL@%
  10844.         while ((ch = getchar()) != EOF)%@NL@%
  10845.                 putc(ch, fp);%@NL@%
  10846. %@NL@%
  10847.         %@AB@%/* clean up */%@AE@%%@NL@%
  10848.         if (fclose(fp))%@NL@%
  10849.                 fatal(pgm, notesfile, 2);%@NL@%
  10850.         exit(0);%@NL@%
  10851. }%@NL@%
  10852. %@NL@%
  10853. %@NL@%
  10854. %@2@%%@AH@%NOTES2.C%@AE@%%@EH@%%@NL@%
  10855. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\04STDLIB\NOTES2.C%@AE@%%@NL@%
  10856. %@NL@%
  10857. %@AB@%/*%@NL@%
  10858. %@AB@% *        notes2 -- add a date/time stamped entry to a%@NL@%
  10859. %@AB@% *        "notes" data file.  Allow user to optionally%@NL@%
  10860. %@AB@% *        edit the data file upon completion of the entry.%@NL@%
  10861. %@AB@% */%@AE@%%@NL@%
  10862. %@NL@%
  10863. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  10864. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  10865. %@AI@%#include %@AE@%<time.h> %@NL@%
  10866. %@AI@%#include %@AE@%<signal.h> %@NL@%
  10867. %@AI@%#include %@AE@%<process.h> %@NL@%
  10868. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  10869. %@NL@%
  10870. %@AB@%/* length of date/time string */%@AE@%%@NL@%
  10871. %@AI@%#define %@AE@%DT_STR        26 %@NL@%
  10872. %@NL@%
  10873. main(argc, argv)%@NL@%
  10874. int argc;%@NL@%
  10875. char **argv;%@NL@%
  10876. {%@NL@%
  10877.         int n;                %@AB@%/* number of lines added */%@AE@%%@NL@%
  10878.         int exitcode = 0;%@NL@%
  10879.         FILE *fp;%@NL@%
  10880.         static char notesfile[MAXPATH + 1] = { "notes.txt" };%@NL@%
  10881.         static char editname[MAXPATH + 1] = { "edlin" };%@NL@%
  10882.         char ch;%@NL@%
  10883.         char dt_stamp[DT_STR];%@NL@%
  10884.         char *s;%@NL@%
  10885.         long ltime;%@NL@%
  10886.         static char pgm[MAXNAME + 1] = { "notes3" };%@NL@%
  10887. %@NL@%
  10888.         extern void fatal(char *, char *, int);%@NL@%
  10889.         extern void getpname(char *, char *);%@NL@%
  10890.         static int addtxt(FILE *, FILE *);%@NL@%
  10891. %@NL@%
  10892.         if (_osmajor >= 3)%@NL@%
  10893.                 getpname(*argv, pgm);%@NL@%
  10894. %@NL@%
  10895.         %@AB@%/* locate the "notes" database file and open it */%@AE@%%@NL@%
  10896.         if (argc > 1)%@NL@%
  10897.                 strcpy(notesfile, *++argv);%@NL@%
  10898.         else if (s = getenv("NOTESFILE"))%@NL@%
  10899.                 strcpy(notesfile, s);%@NL@%
  10900.         if ((fp = fopen(notesfile, "a")) == NULL)%@NL@%
  10901.                 fatal(pgm, notesfile, 1);%@NL@%
  10902. %@NL@%
  10903.         %@AB@%/* disable Ctrl-Break interrupt */%@AE@%%@NL@%
  10904.         if (signal(SIGINT, SIG_IGN) == (int(*)())-1)%@NL@%
  10905.                 perror("Cannot set signal");%@NL@%
  10906. %@NL@%
  10907.         %@AB@%/* append a header and date/time tag */%@AE@%%@NL@%
  10908.         ltime = time(NULL);%@NL@%
  10909.         strcpy(dt_stamp, ctime(<ime));%@NL@%
  10910.         fprintf(stderr, "Appending to %s: %s", notesfile, dt_stamp);%@NL@%
  10911.         fprintf(fp, "\n%s", dt_stamp);%@NL@%
  10912. %@NL@%
  10913.         %@AB@%/* add text to notes file */%@AE@%%@NL@%
  10914.         if ((n = addtxt(stdin, fp)) == 0) {%@NL@%
  10915.                 fputs("No new text", stderr);%@NL@%
  10916.                 if (fclose(fp))%@NL@%
  10917.                         fatal(pgm, notesfile, 2);%@NL@%
  10918.                 exit(0);%@NL@%
  10919.         }%@NL@%
  10920.         else%@NL@%
  10921.                 fprintf(stderr, "%d line(s) added to %s\n", n, notesfile);%@NL@%
  10922.         if (fclose(fp))%@NL@%
  10923.                 fatal(pgm, notesfile, 2);%@NL@%
  10924. %@NL@%
  10925.         %@AB@%/* optionally edit text in the notes file */%@AE@%%@NL@%
  10926.         fprintf(stderr, "E + ENTER to edit; ENTER alone to quit: ");%@NL@%
  10927.         while ((ch = tolower(getchar())) != '\n')%@NL@%
  10928.                 if (ch = 'e') {%@NL@%
  10929.                         if (s = getenv("EDITOR"))%@NL@%
  10930.                                 strcpy(editname, s);%@NL@%
  10931.                         if ((exitcode = spawnlp(P_WAIT, editname, editname,%@NL@%
  10932.                                 notesfile, NULL)) == -1)%@NL@%
  10933.                                 fatal(pgm, editname, 3);%@NL@%
  10934.                 }%@NL@%
  10935.         exit(exitcode);%@NL@%
  10936. }%@NL@%
  10937. %@NL@%
  10938. %@AB@%/*%@NL@%
  10939. %@AB@% *        addtxt -- append new text to notes file%@NL@%
  10940. %@AB@% */%@AE@%%@NL@%
  10941. static int addtxt(fin, fout)%@NL@%
  10942. FILE *fin, *fout;%@NL@%
  10943. {%@NL@%
  10944.         int ch;%@NL@%
  10945.         int col = 0;        %@AB@%/* column */%@AE@%%@NL@%
  10946.         int n = 0;        %@AB@%/* number of lines added */%@AE@%%@NL@%
  10947. %@NL@%
  10948.         while ((ch = fgetc(fin)) != EOF) {%@NL@%
  10949.                 if (ch == '.' && col == 0) {%@NL@%
  10950.                         ch = fgetc(fin); %@AB@%/* trash the newline */%@AE@%%@NL@%
  10951.                         break;%@NL@%
  10952.                 }%@NL@%
  10953.                 fputc(ch, fout);%@NL@%
  10954.                 if (ch == '\n') {%@NL@%
  10955.                         col = 0;%@NL@%
  10956.                         ++n;%@NL@%
  10957.                 }%@NL@%
  10958.                 else%@NL@%
  10959.                         ++col;%@NL@%
  10960.         }%@NL@%
  10961.         return (n);%@NL@%
  10962. }%@NL@%
  10963. %@NL@%
  10964. %@NL@%
  10965. %@2@%%@AH@%PALETTE.C%@AE@%%@EH@%%@NL@%
  10966. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\PALETTE.C%@AE@%%@NL@%
  10967. %@NL@%
  10968. %@AB@%/*%@NL@%
  10969. %@AB@% *        palette        -- set graphics color values or border color%@NL@%
  10970. %@AB@% */%@AE@%%@NL@%
  10971. %@NL@%
  10972. %@AI@%#include %@AE@%<dos.h> %@NL@%
  10973. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  10974. %@NL@%
  10975. int%@NL@%
  10976. palette(id, color)%@NL@%
  10977. unsigned int id, color;%@NL@%
  10978. {%@NL@%
  10979.         union REGS inregs, outregs;%@NL@%
  10980. %@NL@%
  10981.         inregs.h.ah = PALETTE;%@NL@%
  10982.         inregs.h.bh = id;%@NL@%
  10983.         inregs.h.bl = color;%@NL@%
  10984.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  10985. %@NL@%
  10986.         return(outregs.x.cflag);%@NL@%
  10987. }%@NL@%
  10988. %@NL@%
  10989. %@NL@%
  10990. %@2@%%@AH@%PARSE.C%@AE@%%@EH@%%@NL@%
  10991. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\13ANSI\PARSE.C%@AE@%%@NL@%
  10992. %@NL@%
  10993. %@AB@%/*%@NL@%
  10994. %@AB@% *        parse -- process a list of attribute specifications%@NL@%
  10995. %@AB@% */%@AE@%%@NL@%
  10996. %@NL@%
  10997. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  10998. %@AI@%#include %@AE@%<local\ansi.h> %@NL@%
  10999. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11000. %@AI@%#include %@AE@%<local\ibmcolor.h> %@NL@%
  11001. %@NL@%
  11002. %@AB@%/* buffer length for string comparisons */%@AE@%%@NL@%
  11003. %@AI@%#define %@AE@%NCHARS        3 %@NL@%
  11004. %@AI@%#define %@AE@%C_MASK        0x7 %@NL@%
  11005. %@NL@%
  11006. void%@NL@%
  11007. parse(nargs, argvec)%@NL@%
  11008. int nargs;        %@AB@%/* number of argument vectors */%@AE@%%@NL@%
  11009. char *argvec[];        %@AB@%/* pointer to the argument vector array */%@AE@%%@NL@%
  11010. {%@NL@%
  11011.         int i, intensity;%@NL@%
  11012.         int attribute;%@NL@%
  11013.         POSITION pos;%@NL@%
  11014.         char str[NCHARS + 1];%@NL@%
  11015.         extern int colornum(char *);%@NL@%
  11016.         extern void setattr(POSITION, int);%@NL@%
  11017. %@NL@%
  11018.         %@AB@%/* clear all attributes */%@AE@%%@NL@%
  11019.         ANSI_SGR(ANSI_NORMAL);%@NL@%
  11020. %@NL@%
  11021.         %@AB@%/* look for a single attribute specification */%@AE@%%@NL@%
  11022.         if (nargs == 2) {%@NL@%
  11023.                 attribute = colornum(argvec[1]);%@NL@%
  11024.                 switch (attribute) {%@NL@%
  11025.                 case IBM_NORMAL:%@NL@%
  11026.                         palette(0, IBM_BLACK);%@NL@%
  11027.                         return;%@NL@%
  11028.                 case IBM_REVERSE:%@NL@%
  11029.                         ANSI_SGR(ANSI_REVERSE);%@NL@%
  11030.                         palette(0, IBM_WHITE);%@NL@%
  11031.                         return;%@NL@%
  11032.                 case IBM_INVISIBLE:%@NL@%
  11033.                         ANSI_SGR(ANSI_INVISIBLE);%@NL@%
  11034.                         return;%@NL@%
  11035.                 case IBM_BLINK:%@NL@%
  11036.                         ANSI_SGR(ANSI_BLINK);%@NL@%
  11037.                         return;%@NL@%
  11038.                 }%@NL@%
  11039.         }%@NL@%
  11040. %@NL@%
  11041.         %@AB@%/* must be separate attribute specifications */%@AE@%%@NL@%
  11042.         pos = FGND;%@NL@%
  11043.         intensity = 0;%@NL@%
  11044.         for (i = 1; i < nargs; ++i) {%@NL@%
  11045.                 attribute = colornum(argvec[i]);%@NL@%
  11046.                 if (attribute == -1) {%@NL@%
  11047.                         ANSI_ED;%@NL@%
  11048.                         fprintf(stderr, "\nIllegal parameter\n");%@NL@%
  11049.                         exit (2);%@NL@%
  11050.                 }%@NL@%
  11051.                 if (attribute == IBM_BRIGHT) {%@NL@%
  11052.                         intensity = IBM_BRIGHT;%@NL@%
  11053.                         continue;%@NL@%
  11054.                 }%@NL@%
  11055.                 setattr(pos, attribute | intensity);%@NL@%
  11056.                 if (pos == FGND)%@NL@%
  11057.                         pos = BKGND;%@NL@%
  11058.                 else if (pos == BKGND)%@NL@%
  11059.                         pos = BDR;%@NL@%
  11060.                 intensity = 0;%@NL@%
  11061.         }%@NL@%
  11062.         return;%@NL@%
  11063. }%@NL@%
  11064. %@NL@%
  11065. %@NL@%
  11066. %@2@%%@AH@%PR.C%@AE@%%@EH@%%@NL@%
  11067. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\09PRINT\PR.C%@AE@%%@NL@%
  11068. %@NL@%
  11069. %@AB@%/*%@NL@%
  11070. %@AB@% *        pr -- file printer%@NL@%
  11071. %@AB@% */%@AE@%%@NL@%
  11072. %@NL@%
  11073. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  11074. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  11075. %@AI@%#include %@AE@%<dos.h> %@NL@%
  11076. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11077. %@AI@%#include %@AE@%"print.h" %@NL@%
  11078. %@NL@%
  11079. char Pagelist[MAXLINE];%@NL@%
  11080. %@NL@%
  11081. main(argc, argv)%@NL@%
  11082. int argc;%@NL@%
  11083. char **argv;%@NL@%
  11084. {%@NL@%
  11085.         int ch;%@NL@%
  11086.         BOOLEAN errflag;%@NL@%
  11087.         extern PRINT pcnf;%@NL@%
  11088.         static char pgm[MAXNAME + 1] = { "pr" };%@NL@%
  11089. %@NL@%
  11090.         extern char *getpname(char *, char *);%@NL@%
  11091.         extern int getopt(int, char **, char *);%@NL@%
  11092.         extern char *optarg;%@NL@%
  11093.         extern int optind, opterr;%@NL@%
  11094.         extern int pr_gcnf(char *);%@NL@%
  11095.         extern pr_file(char *, int, char **);%@NL@%
  11096.         extern void pr_help(char *);%@NL@%
  11097.         extern void fixtabs(int);%@NL@%
  11098.         extern int setprnt();%@NL@%
  11099. %@NL@%
  11100.         if (_osmajor >= 3)%@NL@%
  11101.                 getpname(*argv, pgm);%@NL@%
  11102. %@NL@%
  11103.         %@AB@%/* do configuration */%@AE@%%@NL@%
  11104.         if (pr_gcnf(pgm) != 0) {%@NL@%
  11105.                 fprintf(stderr, "%s: Configuration error", pgm);%@NL@%
  11106.                 exit(2);%@NL@%
  11107.         }%@NL@%
  11108.         if (setprnt() == -1) {%@NL@%
  11109.                 fprintf(stderr, "%s: Bad printer configuration\n", pgm);%@NL@%
  11110.                 exit(1);%@NL@%
  11111.         }%@NL@%
  11112.         fixtabs(pcnf.p_tabint);%@NL@%
  11113. %@NL@%
  11114.         %@AB@%/* process command-line arguments */%@AE@%%@NL@%
  11115.         while ((ch = getopt(argc, argv, "efgh:l:no:ps:w:")) != EOF) {%@NL@%
  11116.                 switch (ch) {%@NL@%
  11117.                 case 'e':%@NL@%
  11118.                         %@AB@%/* force "Epson-compatible " printer mode */%@AE@%%@NL@%
  11119.                         pcnf.p_mode = 1;%@NL@%
  11120.                         break;%@NL@%
  11121.                 case 'f':%@NL@%
  11122.                         %@AB@%/* use formfeed to eject a page */%@AE@%%@NL@%
  11123.                         pcnf.p_ff = 1;%@NL@%
  11124.                         break;%@NL@%
  11125.                 case 'g':%@NL@%
  11126.                         %@AB@%/* force "generic" printer mode */%@AE@%%@NL@%
  11127.                         pcnf.p_mode = 0;%@NL@%
  11128.                         break;%@NL@%
  11129.                 case 'h':%@NL@%
  11130.                         %@AB@%/* use specified header */%@AE@%%@NL@%
  11131.                         strcpy(pcnf.p_hdr, optarg);%@NL@%
  11132.                         break;%@NL@%
  11133.                 case 'l':%@NL@%
  11134.                         %@AB@%/* set lines per page */%@AE@%%@NL@%
  11135.                         pcnf.p_len = atoi(optarg);%@NL@%
  11136.                         break;%@NL@%
  11137.                 case 'n':%@NL@%
  11138.                         %@AB@%/* enable line numbering */%@AE@%%@NL@%
  11139.                         pcnf.p_lnum = 1;%@NL@%
  11140.                         break;%@NL@%
  11141.                 case 'o':%@NL@%
  11142.                         %@AB@%/* set left margin */%@AE@%%@NL@%
  11143.                         pcnf.p_lmarg = atoi(optarg);%@NL@%
  11144.                         break;%@NL@%
  11145.                 case 'p':%@NL@%
  11146.                         %@AB@%/* preview output on screen */%@AE@%%@NL@%
  11147.                         strcpy(pcnf.p_dest, "");%@NL@%
  11148.                         break;%@NL@%
  11149.                 case 's':%@NL@%
  11150.                         %@AB@%/* output selected pages */%@AE@%%@NL@%
  11151.                         strcpy(Pagelist, optarg);%@NL@%
  11152.                         break;%@NL@%
  11153.                 case 'w':%@NL@%
  11154.                         %@AB@%/* set page width in columns */%@AE@%%@NL@%
  11155.                         pcnf.p_wid = atoi(optarg);%@NL@%
  11156.                         break;%@NL@%
  11157.                 case '?':%@NL@%
  11158.                         %@AB@%/* unknown option */%@AE@%%@NL@%
  11159.                         errflag = TRUE;%@NL@%
  11160.                         break;%@NL@%
  11161.                 }%@NL@%
  11162.         }%@NL@%
  11163.         if (errflag == TRUE) {%@NL@%
  11164.                 pr_help(pgm);%@NL@%
  11165.                 exit(3);%@NL@%
  11166.         }%@NL@%
  11167. %@NL@%
  11168.         %@AB@%/* print the files */%@AE@%%@NL@%
  11169.         pr_file(pgm, argc - optind, argv += optind);%@NL@%
  11170. %@NL@%
  11171.         exit(0);%@NL@%
  11172. }%@NL@%
  11173. %@NL@%
  11174. %@NL@%
  11175. %@2@%%@AH@%PRINTER.C%@AE@%%@EH@%%@NL@%
  11176. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\PRINTER.C%@AE@%%@NL@%
  11177. %@NL@%
  11178. %@AB@%/*%@NL@%
  11179. %@AB@% *        printer -- interface functions for printer%@NL@%
  11180. %@AB@% */%@AE@%%@NL@%
  11181. %@NL@%
  11182. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  11183. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  11184. %@AI@%#include %@AE@%<string.h> %@NL@%
  11185. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11186. %@AI@%#include %@AE@%<local\printer.h> %@NL@%
  11187. %@NL@%
  11188. PRINTER prt;        %@AB@%/* printer data */%@AE@%%@NL@%
  11189. %@NL@%
  11190. %@AB@%/*%@NL@%
  11191. %@AB@% *        setprnt -- install printer codes from configuration%@NL@%
  11192. %@AB@% *        file for printer (defaults to Epson MX/FX series)%@NL@%
  11193. %@AB@% */%@AE@%%@NL@%
  11194. %@NL@%
  11195. %@AI@%#define %@AE@%NSELEM        13 %@NL@%
  11196. %@NL@%
  11197. int%@NL@%
  11198. setprnt()%@NL@%
  11199. {%@NL@%
  11200.         int n;%@NL@%
  11201.         char *s, line[MAXLINE];%@NL@%
  11202.         FILE *fp, *fconfig(char *, char *);%@NL@%
  11203. %@NL@%
  11204.         %@AB@%/* use local or global config file, if any */%@AE@%%@NL@%
  11205.         if ((fp = fconfig("CONFIG", "printer.cnf")) != NULL) {%@NL@%
  11206.                 n = 0;%@NL@%
  11207.                 while (fgets(line, MAXLINE, fp) != NULL) {%@NL@%
  11208.                         if ((s = strtok(line, " \t\n")) == NULL)%@NL@%
  11209.                                 return (-1);%@NL@%
  11210.                         switch (n) {%@NL@%
  11211.                         case 0:%@NL@%
  11212.                                 strcpy(prt.p_init, s);%@NL@%
  11213.                                 break;%@NL@%
  11214.                         case 1:%@NL@%
  11215.                                 strcpy(prt.p_bold, s);%@NL@%
  11216.                                 break;%@NL@%
  11217.                         case 2:%@NL@%
  11218.                                 strcpy(prt.p_ds, s);%@NL@%
  11219.                                 break;%@NL@%
  11220.                         case 3:%@NL@%
  11221.                                 strcpy(prt.p_ital, s);%@NL@%
  11222.                                 break;%@NL@%
  11223.                         case 4:%@NL@%
  11224.                                 strcpy(prt.p_cmp, s);%@NL@%
  11225.                                 break;%@NL@%
  11226.                         case 5:%@NL@%
  11227.                                 strcpy(prt.p_exp, s);%@NL@%
  11228.                                 break;%@NL@%
  11229.                         case 6:%@NL@%
  11230.                                 strcpy(prt.p_ul, s);%@NL@%
  11231.                                 break;%@NL@%
  11232.                         case 7:%@NL@%
  11233.                                 strcpy(prt.p_xbold, s); %@NL@%
  11234.                                 break;%@NL@%
  11235.                         case 8:%@NL@%
  11236.                                 strcpy(prt.p_xds, s);%@NL@%
  11237.                                 break;%@NL@%
  11238.                         case 9:%@NL@%
  11239.                                 strcpy(prt.p_xital, s);%@NL@%
  11240.                                 break;%@NL@%
  11241.                         case 10:%@NL@%
  11242.                                 strcpy(prt.p_xcmp, s);%@NL@%
  11243.                                 break;%@NL@%
  11244.                         case 11:%@NL@%
  11245.                                 strcpy(prt.p_xexp, s);%@NL@%
  11246.                                 break;%@NL@%
  11247.                         case 12:%@NL@%
  11248.                                 strcpy(prt.p_xul, s);%@NL@%
  11249.                                 break;%@NL@%
  11250.                         default:%@NL@%
  11251.                                 %@AB@%/* too many lines */%@AE@%%@NL@%
  11252.                                 return (-1);%@NL@%
  11253.                         }%@NL@%
  11254.                         ++n;%@NL@%
  11255.                 }%@NL@%
  11256.                 if (n != NSELEM)%@NL@%
  11257.                         %@AB@%/* probably not enough lines */%@AE@%%@NL@%
  11258.                         return (-1);%@NL@%
  11259.         }%@NL@%
  11260.         %@NL@%
  11261.         %@AB@%/* or use Epson defaults */%@AE@%%@NL@%
  11262.         strcpy(prt.p_init, "\033@");        %@AB@%/* hardware reset */%@AE@%%@NL@%
  11263.         strcpy(prt.p_bold, "\033E");        %@AB@%/* emphasized mode */%@AE@%%@NL@%
  11264.         strcpy(prt.p_ds, "\033G");        %@AB@%/* double-strike mode */%@AE@%%@NL@%
  11265.         strcpy(prt.p_ital, "\0334");        %@AB@%/* italic mode */%@AE@%%@NL@%
  11266.         strcpy(prt.p_cmp, "\017");        %@AB@%/* condensed mode */%@AE@%%@NL@%
  11267.         strcpy(prt.p_exp, "\016");        %@AB@%/* expanded mode */%@AE@%%@NL@%
  11268.         strcpy(prt.p_ul, "\033-1");        %@AB@%/* underline mode */%@AE@%%@NL@%
  11269.         strcpy(prt.p_xbold, "\033F");        %@AB@%/* cancel emphasized mode */%@AE@%%@NL@%
  11270.         strcpy(prt.p_xds, "\033H");        %@AB@%/* cancel double-strike mode */%@AE@%%@NL@%
  11271.         strcpy(prt.p_xital, "\0335");        %@AB@%/* cancel italic mode */%@AE@%%@NL@%
  11272.         strcpy(prt.p_xcmp, "\022");        %@AB@%/* cancel condensed mode */%@AE@%%@NL@%
  11273.         strcpy(prt.p_xexp, "\024");        %@AB@%/* cancel expanded mode */%@AE@%%@NL@%
  11274.         strcpy(prt.p_xul, "\033-0");        %@AB@%/* cancel underline mode */%@AE@%%@NL@%
  11275. %@NL@%
  11276.         return (0);%@NL@%
  11277. }%@NL@%
  11278. %@NL@%
  11279. %@AB@%/*%@NL@%
  11280. %@AB@% *        clrprnt -- clear printer options to default values%@NL@%
  11281. %@AB@% *        (clears individual options to avoid the "paper creep"%@NL@%
  11282. %@AB@% *        that occurs with repeated printer resets and to avoid%@NL@%
  11283. %@AB@% *        changing the printer's notion of top-of-form position)%@NL@%
  11284. %@AB@% */%@AE@%%@NL@%
  11285. %@NL@%
  11286. int%@NL@%
  11287. clrprnt(fout)%@NL@%
  11288. FILE *fout;%@NL@%
  11289. {%@NL@%
  11290.         fputs(prt.p_xbold, fout);        %@AB@%/* cancel emphasized mode */%@AE@%%@NL@%
  11291.         fputs(prt.p_xds, fout);                %@AB@%/* cancel double-strike mode */%@AE@%%@NL@%
  11292.         fputs(prt.p_xital, fout);        %@AB@%/* cancel italic mode */%@AE@%%@NL@%
  11293.         fputs(prt.p_xcmp, fout);        %@AB@%/* cancel condensed mode */%@AE@%%@NL@%
  11294.         fputs(prt.p_xexp, fout);        %@AB@%/* cancel expanded mode */%@AE@%%@NL@%
  11295.         fputs(prt.p_xul, fout);                %@AB@%/* cancel underline mode */%@AE@%%@NL@%
  11296. } %@AB@%/* end clrprnt() */%@AE@%%@NL@%
  11297. %@NL@%
  11298. %@AB@%/*%@NL@%
  11299. %@AB@% *        setfont -- set the printing font to the type specified%@NL@%
  11300. %@AB@% *        by the argument (may be a compound font specification)%@NL@%
  11301. %@AB@% */%@AE@%%@NL@%
  11302. %@NL@%
  11303. int%@NL@%
  11304. setfont(ftype, fout)%@NL@%
  11305. int ftype;        %@AB@%/* font type specifier */%@AE@%%@NL@%
  11306. FILE *fout;        %@AB@%/* output stream */%@AE@%%@NL@%
  11307. {%@NL@%
  11308.         clrprnt(fout);%@NL@%
  11309.         if ((ftype & CONDENSED) == CONDENSED)%@NL@%
  11310.                 if ((ftype & DOUBLE) == DOUBLE ||%@NL@%
  11311.                         (ftype & EMPHASIZED) == EMPHASIZED)%@NL@%
  11312.                         return FAILURE;%@NL@%
  11313.                 else if (*prt.p_cmp)%@NL@%
  11314.                         fputs(prt.p_cmp, fout);%@NL@%
  11315.         if (*prt.p_ds && (ftype & DOUBLE) == DOUBLE)%@NL@%
  11316.                 fputs(prt.p_ds, fout);%@NL@%
  11317.         if (*prt.p_bold && (ftype & EMPHASIZED) == EMPHASIZED)%@NL@%
  11318.                 fputs(prt.p_bold, fout);%@NL@%
  11319.         if (*prt.p_exp && (ftype & EXPANDED) == EXPANDED)%@NL@%
  11320.                 fputs(prt.p_exp, fout);%@NL@%
  11321.         if (*prt.p_ital && (ftype & ITALICS) == ITALICS)%@NL@%
  11322.                 fputs(prt.p_ital, fout);%@NL@%
  11323.         if (*prt.p_ul && (ftype & UNDERLINE) == UNDERLINE)%@NL@%
  11324.                 fputs(prt.p_ul, fout);%@NL@%
  11325. %@NL@%
  11326.         return SUCCESS;%@NL@%
  11327. } %@AB@%/* end setfont() */%@AE@%%@NL@%
  11328. %@NL@%
  11329. %@NL@%
  11330. %@2@%%@AH@%PRTSTR.C%@AE@%%@EH@%%@NL@%
  11331. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\07CONFIG\PRTSTR.C%@AE@%%@NL@%
  11332. %@NL@%
  11333. %@AB@%/*%@NL@%
  11334. %@AB@% *        prtstr -- send text string(s) to standard printer%@NL@%
  11335. %@AB@% */%@AE@%%@NL@%
  11336. %@NL@%
  11337. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  11338. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  11339. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11340. %@NL@%
  11341. main(argc, argv)%@NL@%
  11342. int argc;%@NL@%
  11343. char **argv;%@NL@%
  11344. {%@NL@%
  11345.         int ch;%@NL@%
  11346.         BOOLEAN errflag, lineflag;%@NL@%
  11347.         static char pgm[MAXNAME + 1] = { "prtstr" };%@NL@%
  11348.         FILE *fout;%@NL@%
  11349. %@NL@%
  11350.         extern char *getpname(char *, char *);%@NL@%
  11351.         extern int getopt(int, char **, char *);%@NL@%
  11352.         extern int optind, opterr;%@NL@%
  11353.         extern char *optarg;%@NL@%
  11354. %@NL@%
  11355.         if (_osmajor >= 3)%@NL@%
  11356.                 getpname(*argv, pgm);%@NL@%
  11357. %@NL@%
  11358.         %@AB@%/* process options, if any */%@AE@%%@NL@%
  11359.         errflag = FALSE;%@NL@%
  11360.         lineflag = TRUE;%@NL@%
  11361.         fout = stdprn;%@NL@%
  11362.         while ((ch = getopt(argc, argv, "np")) != EOF)%@NL@%
  11363.                 switch (ch) {%@NL@%
  11364.                 case 'n':%@NL@%
  11365.                         %@AB@%/* don't emit the trailing newline */%@AE@%%@NL@%
  11366.                         lineflag = FALSE;%@NL@%
  11367.                         break;%@NL@%
  11368.                 case 'p':%@NL@%
  11369.                         %@AB@%/* preview on stdout */%@AE@%%@NL@%
  11370.                         fout = stdout;%@NL@%
  11371.                         break;%@NL@%
  11372.                 case '?':%@NL@%
  11373.                         %@AB@%/* bad option */%@AE@%%@NL@%
  11374.                         errflag = TRUE;%@NL@%
  11375.                         break;%@NL@%
  11376.                 }%@NL@%
  11377.         if (errflag == TRUE) {%@NL@%
  11378.                 fprintf(stderr, "Usage: %s [-np] [string...]\n");%@NL@%
  11379.                 exit(1);%@NL@%
  11380.         }%@NL@%
  11381. %@NL@%
  11382.         %@AB@%/* print the string(s) */%@AE@%%@NL@%
  11383.         argc -= optind;%@NL@%
  11384.         argv += optind;%@NL@%
  11385.         while (argc-- > 1 ) {%@NL@%
  11386.                 fputs(*argv++, fout);%@NL@%
  11387.                 fputc(' ', fout);%@NL@%
  11388.         }%@NL@%
  11389.         fputs(*argv++, fout);%@NL@%
  11390.         if (lineflag == TRUE)%@NL@%
  11391.                 fputc(' ', fout);%@NL@%
  11392.         if (lineflag == TRUE)%@NL@%
  11393.                 fputc('\n', fout);%@NL@%
  11394.         %@NL@%
  11395.         exit(0);%@NL@%
  11396. }%@NL@%
  11397. %@NL@%
  11398. %@NL@%
  11399. %@2@%%@AH@%PR_CPY.C%@AE@%%@EH@%%@NL@%
  11400. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\09PRINT\PR_CPY.C%@AE@%%@NL@%
  11401. %@NL@%
  11402. %@AB@%/*%@NL@%
  11403. %@AB@% *        pr_cpy -- copy input stream to output stream%@NL@%
  11404. %@AB@% */%@AE@%%@NL@%
  11405. %@NL@%
  11406. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  11407. %@AI@%#include %@AE@%<string.h> %@NL@%
  11408. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11409. %@AI@%#include %@AE@%<local\printer.h> %@NL@%
  11410. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  11411. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  11412. %@AI@%#include %@AE@%<time.h> %@NL@%
  11413. %@AI@%#include %@AE@%"print.h" %@NL@%
  11414. %@NL@%
  11415. extern PRINT pcnf;%@NL@%
  11416. extern char Pagelist[MAXLINE];%@NL@%
  11417. extern long Highest;%@NL@%
  11418. %@NL@%
  11419. int%@NL@%
  11420. pr_cpy(fin, fout, fname)%@NL@%
  11421. FILE *fin;%@NL@%
  11422. FILE *fout;%@NL@%
  11423. char *fname;%@NL@%
  11424. {%@NL@%
  11425.         int errcount = 0;%@NL@%
  11426.         unsigned int p_line;        %@AB@%/* page-relative line number */%@AE@%%@NL@%
  11427.         long f_line;                %@AB@%/* file-relative line number */%@AE@%%@NL@%
  11428.         long f_page;                %@AB@%/* file-relative page number */%@AE@%%@NL@%
  11429.         int lnlen;                %@AB@%/* line length */%@AE@%%@NL@%
  11430.         char line[MAXLINE];        %@AB@%/* input line buffer */%@AE@%%@NL@%
  11431.         struct stat tbuf;        %@AB@%/* file information */%@AE@%%@NL@%
  11432.         long ltime;                %@AB@%/* date and time */%@AE@%%@NL@%
  11433.         FILE *fnull, *fx;        %@AB@%/* additional output file pointers */%@AE@%%@NL@%
  11434. %@NL@%
  11435.         extern void mkslist(char *);        %@AB@%/* make a selection list */%@AE@%%@NL@%
  11436.         extern int selected(long);        %@AB@%/* is item in the list? */%@AE@%%@NL@%
  11437.         extern int spaces(int, FILE *);        %@AB@%/* emit string of spaces */%@AE@%%@NL@%
  11438.         extern int setfont(int, FILE *);%@AB@%/* set printer font type */%@AE@%%@NL@%
  11439.         extern int clrprnt(FILE *);        %@AB@%/* clear special fonts */%@AE@%%@NL@%
  11440.         extern int lines(int, FILE *);        %@AB@%/* emit string of blank lines */%@AE@%%@NL@%
  11441.         static int fit(int, int);        %@AB@%/* will line fit on page? */%@AE@%%@NL@%
  11442.         extern int pr_line(char *, FILE *, unsigned int);%@NL@%
  11443. %@NL@%
  11444.         %@AB@%/* install page selection list, if any */%@AE@%%@NL@%
  11445.         if (Pagelist[0] != '\0') {%@NL@%
  11446.                 %@AB@%/* open the NUL device for dumping output */%@AE@%%@NL@%
  11447.                 if ((fnull = fopen("NUL", "w")) == NULL) {%@NL@%
  11448.                         perror("Error opening NUL device");%@NL@%
  11449.                         exit(1);%@NL@%
  11450.                 }%@NL@%
  11451.                 mkslist(Pagelist);%@NL@%
  11452.         }%@NL@%
  11453.         else%@NL@%
  11454.                 Highest = BIGGEST;%@NL@%
  11455. %@NL@%
  11456.         %@AB@%/* get date and time stamp */%@AE@%%@NL@%
  11457.         if (*fname == '\0')%@NL@%
  11458.                 %@AB@%/* using stdin -- use today's date and time */%@AE@%%@NL@%
  11459.                 ltime = time(NULL);%@NL@%
  11460.         else {%@NL@%
  11461.                 if (stat(fname, &tbuf) == -1)%@NL@%
  11462.                         return (-1);%@NL@%
  11463.                 %@AB@%/* use file's modification time */%@AE@%%@NL@%
  11464.                 ltime = tbuf.st_mtime;%@NL@%
  11465.         }%@NL@%
  11466.         p_line = 0;%@NL@%
  11467.         f_line = 1;%@NL@%
  11468.         f_page = 1;%@NL@%
  11469.         while ((lnlen = pr_getln(line, MAXLINE, fin)) > 0 ) {%@NL@%
  11470.                 %@AB@%/* if formfeed or no room for line, eject page */%@AE@%%@NL@%
  11471.                 if (line[0] == '\f' || !fit(lnlen, p_line)) {%@NL@%
  11472.                         %@AB@%/* to top of next page */%@AE@%%@NL@%
  11473.                         if (pcnf.p_ff == 0)%@NL@%
  11474.                                 lines(pcnf.p_len - p_line, fx);%@NL@%
  11475.                         else%@NL@%
  11476.                                 fputc('\f', fx);%@NL@%
  11477.                         p_line = 0;%@NL@%
  11478.                 }%@NL@%
  11479. %@NL@%
  11480.                 %@AB@%/* if at top of page, print the header */%@AE@%%@NL@%
  11481.                 if (p_line == 0) {%@NL@%
  11482.                         if (f_page > Highest)%@NL@%
  11483.                                 break;%@NL@%
  11484.                         fx = selected(f_page) ? fout : fnull;%@NL@%
  11485.                         p_line += lines(pcnf.p_top1, fx);%@NL@%
  11486.                         if (pcnf.p_mode != 0)%@NL@%
  11487.                                 setfont(EMPHASIZED, fx);%@NL@%
  11488.                         spaces(pcnf.p_lmarg, fx);%@NL@%
  11489.                         if (*pcnf.p_hdr != '\0') %@NL@%
  11490.                                 fprintf(fx, "%s  ", pcnf.p_hdr);%@NL@%
  11491.                         else if (*fname != '\0')%@NL@%
  11492.                                 fprintf(fx, "%s  ", strupr(fname));%@NL@%
  11493.                         fprintf(fx, "Page %u  ", f_page++);%@NL@%
  11494.                         fputs(ctime(<ime), fx);%@NL@%
  11495.                         ++p_line;%@NL@%
  11496.                         if (pcnf.p_mode != 0)%@NL@%
  11497.                                 setfont(pcnf.p_font, fx);%@NL@%
  11498.                         p_line += lines(pcnf.p_top2, fx);%@NL@%
  11499.                 }%@NL@%
  11500. %@NL@%
  11501.                 %@AB@%/* OK to output the line */%@AE@%%@NL@%
  11502.                 if (line[0] != '\f')%@NL@%
  11503.                         p_line += pr_line(line, fx, f_line++);%@NL@%
  11504.         }%@NL@%
  11505.         if (ferror(fin) != 0)%@NL@%
  11506.                 ++errcount;%@NL@%
  11507.         if (p_line > 0 && p_line < pcnf.p_len)%@NL@%
  11508.                 if (pcnf.p_ff == 0)%@NL@%
  11509.                         lines(pcnf.p_len - p_line, fx);%@NL@%
  11510.                 else%@NL@%
  11511.                         fputc('\f', fx);%@NL@%
  11512. %@NL@%
  11513.         if (pcnf.p_mode != 0)%@NL@%
  11514.                 clrprnt(fx);%@NL@%
  11515.         return (errcount);%@NL@%
  11516. }%@NL@%
  11517. %@NL@%
  11518. %@AB@%/*%@NL@%
  11519. %@AB@% *        fit -- return non-zero value if enough physical%@NL@%
  11520. %@AB@% *        lines are available on the current page to take%@NL@%
  11521. %@AB@% *        the current logical line of text%@NL@%
  11522. %@AB@% */%@AE@%%@NL@%
  11523. %@NL@%
  11524. NFLDWIDTH  8        %@AB@%/* width of number field */%@AE@%%@NL@%
  11525. %@NL@%
  11526. static int%@NL@%
  11527. fit(len, ln)%@NL@%
  11528. int len, ln;%@NL@%
  11529. {%@NL@%
  11530.         int need, left;        %@AB@%/* physical lines */%@AE@%%@NL@%
  11531.         int cols;        %@AB@%/* columns of actual output */%@AE@%%@NL@%
  11532.         int lw;                %@AB@%/* displayable line width */%@AE@%%@NL@%
  11533. %@NL@%
  11534.         %@AB@%/* total need (columns -> physical lines) */%@AE@%%@NL@%
  11535.         cols = len + (pcnf.p_lnum > 0 ? NFLDWIDTH : 0);%@NL@%
  11536.         lw = pcnf.p_wid - pcnf.p_lmarg - pcnf.p_rmarg;%@NL@%
  11537.         need = 1 + cols / lw;%@NL@%
  11538. %@NL@%
  11539.         %@AB@%/* lines remaining on page */%@AE@%%@NL@%
  11540.         left = pcnf.p_len - ln - pcnf.p_btm;%@NL@%
  11541. %@NL@%
  11542.         return (need <= left ? 1 : 0);%@NL@%
  11543. }%@NL@%
  11544. %@NL@%
  11545. %@NL@%
  11546. %@2@%%@AH@%PR_FILE.C%@AE@%%@EH@%%@NL@%
  11547. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\09PRINT\PR_FILE.C%@AE@%%@NL@%
  11548. %@NL@%
  11549. %@AB@%/*%@NL@%
  11550. %@AB@% *        pr_file -- process each filename or standard input%@NL@%
  11551. %@AB@% */%@AE@%%@NL@%
  11552. %@NL@%
  11553. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  11554. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  11555. %@AI@%#include %@AE@%<string.h> %@NL@%
  11556. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11557. %@AI@%#include %@AE@%"print.h" %@NL@%
  11558. %@NL@%
  11559. int%@NL@%
  11560. pr_file(pname, ac, av)%@NL@%
  11561. char *pname;%@NL@%
  11562. int ac;%@NL@%
  11563. char **av;%@NL@%
  11564. {%@NL@%
  11565.         int ch, errcount = 0;%@NL@%
  11566.         FILE *fin, *fout;%@NL@%
  11567.         extern PRINT pcnf;%@NL@%
  11568. %@NL@%
  11569.         extern void fatal(char *, char *, int);%@NL@%
  11570.         extern int pr_cpy(FILE *, FILE *, char *);%@NL@%
  11571. %@NL@%
  11572.         %@AB@%/* open output stream only if not already open */%@AE@%%@NL@%
  11573.         if (*pcnf.p_dest == '\0' || strcmp(pcnf.p_dest, "CON") == 0)%@NL@%
  11574.                 fout = stdout;%@NL@%
  11575.         else if (strcmp(pcnf.p_dest, "PRN") == 0)%@NL@%
  11576.                 fout = stdprn;%@NL@%
  11577.         else if (strcmp(pcnf.p_dest, "AUX") == 0)%@NL@%
  11578.                 fout = stdaux;%@NL@%
  11579.         else%@NL@%
  11580.                 if ((fout = fopen(pcnf.p_dest, "w")) == NULL)%@NL@%
  11581.                         fatal(pname, "Error open destination stream", 1);%@NL@%
  11582. %@NL@%
  11583.         %@AB@%/* prepare input stream */%@AE@%%@NL@%
  11584.         if (ac == 0)%@NL@%
  11585.                 pr_cpy(stdin, fout, "");%@NL@%
  11586.         else {%@NL@%
  11587.                 for (; ac > 0; --ac, ++av) {%@NL@%
  11588.                         if ((fin = fopen(*av, "r")) == NULL) {%@NL@%
  11589.                                 fprintf(stderr, "%s: Error opening %s",%@NL@%
  11590.                                         pname, *av);%@NL@%
  11591.                                 continue;%@NL@%
  11592.                         }%@NL@%
  11593.                         if (pr_cpy(fin, fout, *av) == -1) {%@NL@%
  11594.                                 fprintf(stderr, "%s: Cannot stat %s",%@NL@%
  11595.                                         pname, *av);%@NL@%
  11596.                                 continue;%@NL@%
  11597.                         }%@NL@%
  11598.                         if (fclose(fin) == EOF)%@NL@%
  11599.                                 fatal(pname, "Error closing input file", 2);%@NL@%
  11600.                 }%@NL@%
  11601.         }%@NL@%
  11602. %@NL@%
  11603.         return (errcount);%@NL@%
  11604. }%@NL@%
  11605. %@NL@%
  11606. %@NL@%
  11607. %@2@%%@AH@%PR_GCNF.C%@AE@%%@EH@%%@NL@%
  11608. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\09PRINT\PR_GCNF.C%@AE@%%@NL@%
  11609. %@NL@%
  11610. %@AB@%/*%@NL@%
  11611. %@AB@% *        pr_gcnf -- get configuration for pr program%@NL@%
  11612. %@AB@% */%@AE@%%@NL@%
  11613. %@NL@%
  11614. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  11615. %@AI@%#include %@AE@%<string.h> %@NL@%
  11616. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11617. %@AI@%#include %@AE@%<local\printer.h> %@NL@%
  11618. %@AI@%#include %@AE@%"print.h" %@NL@%
  11619. %@NL@%
  11620. %@AB@%/* expected number of configuration items */%@AE@%%@NL@%
  11621. %@AI@%#define %@AE@%N_NBR        12 %@NL@%
  11622. %@NL@%
  11623. PRINT pcnf;%@NL@%
  11624. %@NL@%
  11625. int%@NL@%
  11626. pr_gcnf(pname)%@NL@%
  11627. char *pname;%@NL@%
  11628. {%@NL@%
  11629.         char line[MAXLINE];%@NL@%
  11630.         char *s;%@NL@%
  11631.         int cnf[N_NBR];%@NL@%
  11632.         int n, errcount, good;%@NL@%
  11633.         FILE *fp, *fconfig(char *, char *);%@NL@%
  11634. %@NL@%
  11635.         %@AB@%/* get configuration file values, if any */%@AE@%%@NL@%
  11636.         n = good = errcount = 0;%@NL@%
  11637.         if ((fp = fconfig("CONFIG", "pr.cnf")) != NULL) {%@NL@%
  11638.                 while (n < N_NBR && (s = fgets(line, MAXLINE, fp)) != NULL) {%@NL@%
  11639.                         cnf[n] = atoi(s);%@NL@%
  11640.                         ++n;%@NL@%
  11641.                 }%@NL@%
  11642.                 if ((s = fgets(line, MAXLINE, fp)) == NULL)%@NL@%
  11643.                         ++errcount;%@NL@%
  11644.                 else%@NL@%
  11645.                         strcpy(pcnf.p_dest, strtok(line, " \t\n"));%@NL@%
  11646.                 if (n != N_NBR)%@NL@%
  11647.                         ++errcount;%@NL@%
  11648.                 if (errcount == 0)%@NL@%
  11649.                         good = 1;%@NL@%
  11650.                 if (fclose(fp) == -1)%@NL@%
  11651.                         fatal(pname, "cannot close config file");%@NL@%
  11652.         }%@NL@%
  11653. %@NL@%
  11654.         %@AB@%/* use config data is good; use defaults otherwise */%@AE@%%@NL@%
  11655.         pcnf.p_top1 = good ? cnf[0]: TOP1;%@NL@%
  11656.         pcnf.p_top2 = good ? cnf[1] : TOP2;%@NL@%
  11657.         pcnf.p_btm = good ? cnf[2] : BOTTOM;%@NL@%
  11658.         pcnf.p_wid = good ? cnf[3] : MAXPCOL;%@NL@%
  11659.         pcnf.p_lmarg = good ? cnf[4] : MARGIN;%@NL@%
  11660.         pcnf.p_rmarg = good ? cnf[5] : MARGIN;%@NL@%
  11661.         pcnf.p_len = good ? cnf[6] : PAGELEN;%@NL@%
  11662.         pcnf.p_lpi = good ? cnf[7] : LPI;%@NL@%
  11663.         pcnf.p_mode = good ? cnf[8] : 0;%@NL@%
  11664.         pcnf.p_lnum = good ? cnf[9] : 0;%@NL@%
  11665.         pcnf.p_ff = good ? cnf[10] : 0;%@NL@%
  11666.         pcnf.p_tabint = good ? cnf[11] : TABSPEC;%@NL@%
  11667.         if (!good)%@NL@%
  11668.                 strcpy(pcnf.p_dest, "PRN");%@NL@%
  11669.         if (pcnf.p_mode == 1)%@NL@%
  11670.                 pcnf.p_font = CONDENSED;%@NL@%
  11671.         strcpy(pcnf.p_hdr, "");%@NL@%
  11672. %@NL@%
  11673.         return (errcount);%@NL@%
  11674. }%@NL@%
  11675. %@NL@%
  11676. %@NL@%
  11677. %@2@%%@AH@%PR_GETLN.C%@AE@%%@EH@%%@NL@%
  11678. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\09PRINT\PR_GETLN.C%@AE@%%@NL@%
  11679. %@NL@%
  11680. %@AB@%/*%@NL@%
  11681. %@AB@% *        pr_getln -- get a line of text while expanding tabs;%@NL@%
  11682. %@AB@% *        put text into an array and return the length of the line%@NL@%
  11683. %@AB@% *        including termination to the calling function.%@NL@%
  11684. %@AB@% */%@AE@%%@NL@%
  11685. %@NL@%
  11686. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  11687. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  11688. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11689. %@NL@%
  11690. int%@NL@%
  11691. pr_getln(s, lim, fin)%@NL@%
  11692. char *s;%@NL@%
  11693. int lim;%@NL@%
  11694. FILE *fin;%@NL@%
  11695. {%@NL@%
  11696.         int ch;%@NL@%
  11697.         register char *cp;%@NL@%
  11698. %@NL@%
  11699.         extern int tabstop();        %@AB@%/* query tabstop array */%@AE@%%@NL@%
  11700. %@NL@%
  11701.         cp = s;%@NL@%
  11702.         while (--lim > 0 && (ch = fgetc(fin)) != EOF && ch != '\n' && ch != '\f') {%@NL@%
  11703.                 if (ch == '\t')%@NL@%
  11704.                         %@AB@%/* loop and store spaces until next tabstop */%@AE@%%@NL@%
  11705.                         do%@NL@%
  11706.                                 *cp++ = ' ';%@NL@%
  11707.                         while (--lim > 0 && tabstop(cp - s) == 0);%@NL@%
  11708.                 else%@NL@%
  11709.                         *cp++ = ch;%@NL@%
  11710.         }%@NL@%
  11711.         if (ch == EOF && cp - s == 0)%@NL@%
  11712.                 ;%@NL@%
  11713.         else if (ch == EOF || ch == '\n')%@NL@%
  11714.                 *cp++ = '\n';        %@AB@%/* assure correct line termination */%@AE@%%@NL@%
  11715.         else if (ch == '\f' && cp - s == 0) {%@NL@%
  11716.                 *cp++ = '\f';%@NL@%
  11717.                 fgetc(fin);        %@AB@%/* toss the trailing newline */%@AE@%%@NL@%
  11718.         }%@NL@%
  11719.         *cp = '\0';%@NL@%
  11720. %@NL@%
  11721.         return (cp - s);%@NL@%
  11722. }%@NL@%
  11723. %@NL@%
  11724. %@NL@%
  11725. %@2@%%@AH@%PR_HELP.C%@AE@%%@EH@%%@NL@%
  11726. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\09PRINT\PR_HELP.C%@AE@%%@NL@%
  11727. %@NL@%
  11728. %@AB@%/*%@NL@%
  11729. %@AB@% *        pr_help -- display an abbreviated manual page%@NL@%
  11730. %@AB@% */%@AE@%%@NL@%
  11731. %@NL@%
  11732. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  11733. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11734. %@NL@%
  11735. void%@NL@%
  11736. pr_help(pname)%@NL@%
  11737. char *pname;%@NL@%
  11738. {%@NL@%
  11739.         static char *m_str[] = {%@NL@%
  11740.                 "The following options may be used singly or in combination:",%@NL@%
  11741.                 "-e\t set Epson-compatible mode",%@NL@%
  11742.                 "-f\t use formfeed to eject a page (default is newlines)",%@NL@%
  11743.                 "-g\t use generic printer mode",%@NL@%
  11744.                 "-h hdr\t use specified header instead of file name",%@NL@%
  11745.                 "-l len\t set page length in lines (default = 66)",%@NL@%
  11746.                 "-n\t enable line numbering (default = off)",%@NL@%
  11747.                 "-o cols\t offset from left edge in columns (default = 5)",%@NL@%
  11748.                 "-p\t preview output on screen (may be redirected)",%@NL@%
  11749.                 "-s list\t print only selected pages",%@NL@%
  11750.                 "-w cols\t line width in columns (default = 80)"%@NL@%
  11751.         };%@NL@%
  11752.         int i, n = sizeof (m_str)/ sizeof (char *);%@NL@%
  11753. %@NL@%
  11754.         fprintf(stderr, "Usage: %s [options] file...\n\n", pname);%@NL@%
  11755.         for (i = 0; i < n; ++i)%@NL@%
  11756.                 fprintf(stderr, "%s\n", m_str[i]);%@NL@%
  11757. %@NL@%
  11758.         return;%@NL@%
  11759. }%@NL@%
  11760. %@NL@%
  11761. %@NL@%
  11762. %@2@%%@AH@%PR_LINE.C%@AE@%%@EH@%%@NL@%
  11763. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\09PRINT\PR_LINE.C%@AE@%%@NL@%
  11764. %@NL@%
  11765. %@AB@%/*%@NL@%
  11766. %@AB@% *        pr_line -- ouput a buffered logical line and%@NL@%
  11767. %@AB@% *        return a count of physical lines produced%@NL@%
  11768. %@AB@% */%@AE@%%@NL@%
  11769. %@NL@%
  11770. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  11771. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  11772. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11773. %@AI@%#include %@AE@%"print.h" %@NL@%
  11774. %@NL@%
  11775. int%@NL@%
  11776. pr_line(s, fout, rline)%@NL@%
  11777. char *s;                %@AB@%/* buffered line of text */%@AE@%%@NL@%
  11778. FILE *fout;                %@AB@%/* output stream */%@AE@%%@NL@%
  11779. unsigned int rline;        %@AB@%/* file-relative line number */%@AE@%%@NL@%
  11780. {%@NL@%
  11781.         int c_cnt;        %@AB@%/* character position in output line */%@AE@%%@NL@%
  11782.         int nlines;        %@AB@%/* number of lines output */%@AE@%%@NL@%
  11783.         extern PRINT pcnf;%@NL@%
  11784. %@NL@%
  11785.         extern int spaces(int, FILE *);        %@AB@%/* emit string of spaces */%@AE@%%@NL@%
  11786. %@NL@%
  11787.         nlines = 1;%@NL@%
  11788.         c_cnt = 0;%@NL@%
  11789. %@NL@%
  11790.         %@AB@%/* output the left indentation, if any */%@AE@%%@NL@%
  11791.         c_cnt += spaces(pcnf.p_lmarg, fout);%@NL@%
  11792. %@NL@%
  11793.         %@AB@%/* output the line number if numbering enabled */%@AE@%%@NL@%
  11794.         if (pcnf.p_lnum != 0)%@NL@%
  11795.                 c_cnt += fprintf(fout, "%6u  ", rline);%@NL@%
  11796. %@NL@%
  11797.         %@AB@%/* output the text of the line */%@AE@%%@NL@%
  11798.         while (*s != '\0') {%@NL@%
  11799.                 if (c_cnt > (pcnf.p_wid - pcnf.p_rmarg)) {%@NL@%
  11800.                         fputc('\n', fout);%@NL@%
  11801.                         ++nlines;%@NL@%
  11802.                         c_cnt = 0;%@NL@%
  11803.                         c_cnt = spaces(pcnf.p_lmarg, fout);%@NL@%
  11804.                 }%@NL@%
  11805.                 fputc(*s, fout);%@NL@%
  11806.                 ++s;%@NL@%
  11807.                 ++c_cnt;%@NL@%
  11808.         }%@NL@%
  11809. %@NL@%
  11810.         return (nlines);%@NL@%
  11811. }%@NL@%
  11812. %@NL@%
  11813. %@NL@%
  11814. %@2@%%@AH@%PUTCUR.C%@AE@%%@EH@%%@NL@%
  11815. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\PUTCUR.C%@AE@%%@NL@%
  11816. %@NL@%
  11817. %@AB@%/*%@NL@%
  11818. %@AB@% *        putcur -- put cursor at specified position (row, col) %@NL@%
  11819. %@AB@% */%@AE@%%@NL@%
  11820. %@NL@%
  11821. %@AI@%#include %@AE@%<dos.h> %@NL@%
  11822. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11823. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  11824. %@NL@%
  11825. putcur(r, c, pg)%@NL@%
  11826. unsigned int%@NL@%
  11827.         r,        %@AB@%/* row */%@AE@%%@NL@%
  11828.         c,        %@AB@%/* column */%@AE@%%@NL@%
  11829.         pg;        %@AB@%/* screen page for writes */%@AE@%%@NL@%
  11830. {%@NL@%
  11831.         union REGS inregs, outregs;%@NL@%
  11832. %@NL@%
  11833.         inregs.h.ah = CUR_POS;%@NL@%
  11834.         inregs.h.bh = pg & 0x07;%@NL@%
  11835.         inregs.h.dh = r & 0xFF;%@NL@%
  11836.         inregs.h.dl = c & 0xFF;%@NL@%
  11837. %@NL@%
  11838.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  11839. %@NL@%
  11840.         return (outregs.x.cflag);%@NL@%
  11841. } %@AB@%/* end putcur() */%@AE@%%@NL@%
  11842. %@NL@%
  11843. %@NL@%
  11844. %@2@%%@AH@%PUTFLD.C%@AE@%%@EH@%%@NL@%
  11845. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\PUTFLD.C%@AE@%%@NL@%
  11846. %@NL@%
  11847. %@AB@%/*%@NL@%
  11848. %@AB@% *        putfld -- display a string in the prevailing%@NL@%
  11849. %@AB@% *        video attribute while compressing runs of a%@NL@%
  11850. %@AB@% *        single character, and pad the field to full width%@NL@%
  11851. %@AB@% *        with spaces if necessary%@NL@%
  11852. %@AB@% */%@AE@%%@NL@%
  11853. %@NL@%
  11854. int%@NL@%
  11855. putfld(s, w, pg)%@NL@%
  11856. register char *s;        %@AB@%/* string to write */%@AE@%%@NL@%
  11857. int w;                        %@AB@%/* field width */%@AE@%%@NL@%
  11858. int pg;                        %@AB@%/* screen page for writes */%@AE@%%@NL@%
  11859. {%@NL@%
  11860.         int r, c, cols;%@NL@%
  11861.         register int n;%@NL@%
  11862. %@NL@%
  11863.         extern int putcur(int, int, int);%@NL@%
  11864.         extern int readcur(int *, int *, int);%@NL@%
  11865.         extern int writec(unsigned char, int, int);%@NL@%
  11866. %@NL@%
  11867.         %@AB@%/* get starting (current) position */%@AE@%%@NL@%
  11868.         readcur(&r, &c, pg);%@NL@%
  11869. %@NL@%
  11870.         %@AB@%/* write the string */%@AE@%%@NL@%
  11871.         for (n = 0; *s != '\0' && n < w; s += cols, n += cols) {%@NL@%
  11872.                 putcur(r, c + n, pg);%@NL@%
  11873.                 %@AB@%/* compress runs to a single call on writec() */%@AE@%%@NL@%
  11874.                 cols = 1;%@NL@%
  11875.                 while (*(s + cols) == *s && n + cols < w)%@NL@%
  11876.                         ++cols;%@NL@%
  11877.                 writec(*s, cols, pg);%@NL@%
  11878.         }%@NL@%
  11879. %@NL@%
  11880.         %@AB@%/* pad the field, if necessary */%@AE@%%@NL@%
  11881.         if (n < w) {%@NL@%
  11882.                 putcur(r, c + n, pg);%@NL@%
  11883.                 writec(' ', w - n, pg);%@NL@%
  11884.         }%@NL@%
  11885. %@NL@%
  11886.         return (w - n);%@NL@%
  11887. }%@NL@%
  11888. %@NL@%
  11889. %@NL@%
  11890. %@2@%%@AH@%PUTSTR.C%@AE@%%@EH@%%@NL@%
  11891. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\PUTSTR.C%@AE@%%@NL@%
  11892. %@NL@%
  11893. %@AB@%/*%@NL@%
  11894. %@AB@% *        putstr -- display a character string in the%@NL@%
  11895. %@AB@% *        prevailing video attribute and return number%@NL@%
  11896. %@AB@% *        characters displayed%@NL@%
  11897. %@AB@% */%@AE@%%@NL@%
  11898. %@NL@%
  11899. int%@NL@%
  11900. putstr(s, pg)%@NL@%
  11901. register char *s;%@NL@%
  11902. unsigned int pg;%@NL@%
  11903. {%@NL@%
  11904.         unsigned int r, c, c0;%@NL@%
  11905. %@NL@%
  11906.         readcur(&r, &c, pg);%@NL@%
  11907.         for (c0 = c; *s != '\0'; ++s, ++c) {%@NL@%
  11908.                 putcur(r, c, pg);%@NL@%
  11909.                 writec(*s, 1, pg);%@NL@%
  11910.         }%@NL@%
  11911.         putcur(r, c, pg);%@NL@%
  11912.         return (c - c0);%@NL@%
  11913. }%@NL@%
  11914. %@NL@%
  11915. %@NL@%
  11916. %@2@%%@AH@%PUT_CH.C%@AE@%%@EH@%%@NL@%
  11917. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\PUT_CH.C%@AE@%%@NL@%
  11918. %@NL@%
  11919. %@AB@%/*%@NL@%
  11920. %@AB@% *        put_ch -- display a character in the prevailing video%@NL@%
  11921. %@AB@% *        attribute and advance the cursor position%@NL@%
  11922. %@AB@% */%@AE@%%@NL@%
  11923. %@NL@%
  11924. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  11925. %@NL@%
  11926. int%@NL@%
  11927. put_ch(ch, pg)%@NL@%
  11928. register char ch;%@NL@%
  11929. int pg;%@NL@%
  11930. {%@NL@%
  11931.         int r, c, c0;%@NL@%
  11932. %@NL@%
  11933.         readcur(&r, &c, pg);%@NL@%
  11934.         writec(ch, 1, pg);%@NL@%
  11935.         putcur(r, ++c, pg);%@NL@%
  11936.         return (1);%@NL@%
  11937. }%@NL@%
  11938. %@NL@%
  11939. %@NL@%
  11940. %@2@%%@AH@%PWD.C%@AE@%%@EH@%%@NL@%
  11941. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\08FILE\PWD.C%@AE@%%@NL@%
  11942. %@NL@%
  11943. %@AB@%/*%@NL@%
  11944. %@AB@% *        pwd -- print (display actually) the current directory pathname%@NL@%
  11945. %@AB@% */%@AE@%%@NL@%
  11946. %@NL@%
  11947. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  11948. %@AI@%#include %@AE@%<direct.h> %@NL@%
  11949. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11950. %@NL@%
  11951. main()%@NL@%
  11952. {%@NL@%
  11953.         char *path;%@NL@%
  11954. %@NL@%
  11955.         if ((path = getcwd(NULL, MAXPATH)) == NULL) {%@NL@%
  11956.                 perror("Error getting current directory");%@NL@%
  11957.                 exit(1);%@NL@%
  11958.         }%@NL@%
  11959.         printf("%s\n", path);%@NL@%
  11960.         exit(0);%@NL@%
  11961. }%@NL@%
  11962. %@NL@%
  11963. _setargv()%@NL@%
  11964. {%@NL@%
  11965. }%@NL@%
  11966. %@NL@%
  11967. _setenvp()%@NL@%
  11968. {%@NL@%
  11969. }%@NL@%
  11970. %@NL@%
  11971. _nullcheck()%@NL@%
  11972. {%@NL@%
  11973. }%@NL@%
  11974. %@NL@%
  11975. %@NL@%
  11976. %@2@%%@AH@%READCA.C%@AE@%%@EH@%%@NL@%
  11977. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\READCA.C%@AE@%%@NL@%
  11978. %@NL@%
  11979. %@AB@%/*%@NL@%
  11980. %@AB@% *        readca -- read character and attribute at current position%@NL@%
  11981. %@AB@% */%@AE@%%@NL@%
  11982. %@NL@%
  11983. %@AI@%#include %@AE@%<dos.h> %@NL@%
  11984. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  11985. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  11986. %@NL@%
  11987. int readca(ch, attr, pg)%@NL@%
  11988. unsigned char *ch;%@NL@%
  11989. unsigned char *attr;%@NL@%
  11990. unsigned int pg;        %@AB@%/* screen page for reads */%@AE@%%@NL@%
  11991. {%@NL@%
  11992.         union REGS inregs, outregs;%@NL@%
  11993. %@NL@%
  11994.         inregs.h.ah = READ_CHAR_ATTR;%@NL@%
  11995.         inregs.h.bh = pg;                %@AB@%/* display page */%@AE@%%@NL@%
  11996. %@NL@%
  11997.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  11998. %@NL@%
  11999.         *ch = outregs.h.al;                %@AB@%/* character */%@AE@%%@NL@%
  12000.         *attr = outregs.h.ah;                %@AB@%/* attribute */%@AE@%%@NL@%
  12001. %@NL@%
  12002.         %@AB@%/* return the value in AX register */%@AE@%%@NL@%
  12003.         return (outregs.x.cflag);%@NL@%
  12004. } %@AB@%/* end readca() */%@AE@%%@NL@%
  12005. %@NL@%
  12006. %@NL@%
  12007. %@2@%%@AH@%READCUR.C%@AE@%%@EH@%%@NL@%
  12008. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\READCUR.C%@AE@%%@NL@%
  12009. %@NL@%
  12010. %@AB@%/*%@NL@%
  12011. %@AB@% *        readcur        -- pass back the cursor position (row, col) %@NL@%
  12012. %@AB@% */%@AE@%%@NL@%
  12013. %@NL@%
  12014. %@AI@%#include %@AE@%<dos.h> %@NL@%
  12015. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  12016. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  12017. %@NL@%
  12018. unsigned int readcur(row, col, pg)%@NL@%
  12019. unsigned int *row;        %@AB@%/* current row */%@AE@%%@NL@%
  12020. unsigned int *col;        %@AB@%/* current column */%@AE@%%@NL@%
  12021. unsigned int pg;        %@AB@%/* screen page */%@AE@%%@NL@%
  12022. {%@NL@%
  12023.         union REGS inregs, outregs;%@NL@%
  12024. %@NL@%
  12025.         inregs.h.ah = GET_CUR;%@NL@%
  12026.         inregs.h.bh = pg;%@NL@%
  12027. %@NL@%
  12028.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  12029. %@NL@%
  12030.         *col = outregs.h.dl;                %@AB@%/* col */%@AE@%%@NL@%
  12031.         *row = outregs.h.dh;                %@AB@%/* row */%@AE@%%@NL@%
  12032. %@NL@%
  12033.         return (outregs.x.cflag);%@NL@%
  12034. } %@AB@%/* end readcur() */%@AE@%%@NL@%
  12035. %@NL@%
  12036. %@NL@%
  12037. %@2@%%@AH@%READDOT.C%@AE@%%@EH@%%@NL@%
  12038. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\READDOT.C%@AE@%%@NL@%
  12039. %@NL@%
  12040. %@AB@%/*%@NL@%
  12041. %@AB@% *        readdot -- read the value of a pixel%@NL@%
  12042. %@AB@% *        (in graphics mode only)%@NL@%
  12043. %@AB@% */%@AE@%%@NL@%
  12044. %@NL@%
  12045. %@AI@%#include %@AE@%<dos.h> %@NL@%
  12046. %@AI@%#include %@AE@%<local\std.h>  %@NL@%
  12047. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  12048. %@NL@%
  12049. int%@NL@%
  12050. readdot(row, col, dcolor)%@NL@%
  12051. int row, col;%@NL@%
  12052. int *dcolor;        %@AB@%/* pointer to dot color */%@AE@%%@NL@%
  12053. {%@NL@%
  12054.         union REGS inregs, outregs;%@NL@%
  12055. %@NL@%
  12056.         inregs.h.ah = READ_DOT;%@NL@%
  12057.         inregs.x.cx = col;%@NL@%
  12058.         inregs.x.dx = row;%@NL@%
  12059.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  12060.         *dcolor = outregs.h.al;%@NL@%
  12061. %@NL@%
  12062.         return (outregs.x.cflag);%@NL@%
  12063. }%@NL@%
  12064. %@NL@%
  12065. %@NL@%
  12066. %@2@%%@AH@%REPLAY.C%@AE@%%@EH@%%@NL@%
  12067. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\03DOS\REPLAY.C%@AE@%%@NL@%
  12068. %@NL@%
  12069. %@AB@%/*%@NL@%
  12070. %@AB@% *        replay -- echo the command-line arguments%@NL@%
  12071. %@AB@% *        to standard output%@NL@%
  12072. %@AB@% */%@AE@%%@NL@%
  12073. %@NL@%
  12074. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  12075. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  12076. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  12077. %@NL@%
  12078. main(argc, argv)%@NL@%
  12079. int argc;%@NL@%
  12080. char **argv;%@NL@%
  12081. {%@NL@%
  12082.         int i;%@NL@%
  12083.         char **p;%@NL@%
  12084.         static char pgm[MAXNAME + 1] = { "replay" };%@NL@%
  12085. %@NL@%
  12086.         void getpname(char *, char *);%@NL@%
  12087.         %@NL@%
  12088.         %@AB@%/* get program name from DOS (version 3.00 and later) */%@AE@%%@NL@%
  12089.         if (_osmajor >= 3)%@NL@%
  12090.                 getpname(*argv, pgm);%@NL@%
  12091. %@NL@%
  12092.         %@AB@%/* check for arguments */%@AE@%%@NL@%
  12093.         if (argc == 1)%@NL@%
  12094.                 exit(1);        %@AB@%/* none given */%@AE@%%@NL@%
  12095. %@NL@%
  12096.         %@AB@%/* echo the argument list, one per line */%@AE@%%@NL@%
  12097.         p = argv;%@NL@%
  12098.         printf("%s arguments:\n\n", pgm);%@NL@%
  12099.         for (--argc, ++argv; argc > 0; --argc, ++argv)%@NL@%
  12100.                 printf("argv[%d] -> %s\n", argv - p, *argv);%@NL@%
  12101.         exit(0);%@NL@%
  12102. } %@AB@%/* end main() */%@AE@%%@NL@%
  12103. %@NL@%
  12104. %@NL@%
  12105. %@2@%%@AH@%RM.C%@AE@%%@EH@%%@NL@%
  12106. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\08FILE\RM.C%@AE@%%@NL@%
  12107. %@NL@%
  12108. %@AB@%/*%@NL@%
  12109. %@AB@% *        rm -- remove file(s)%@NL@%
  12110. %@AB@% */%@AE@%%@NL@%
  12111. %@NL@%
  12112. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  12113. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  12114. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  12115. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  12116. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  12117. %@AI@%#include %@AE@%<io.h> %@NL@%
  12118. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  12119. %@NL@%
  12120. main(argc, argv)%@NL@%
  12121. int argc;%@NL@%
  12122. char *argv[];%@NL@%
  12123. {%@NL@%
  12124.         int ch;%@NL@%
  12125.         BOOLEAN errflag,%@NL@%
  12126.                 iflag;%@NL@%
  12127. %@NL@%
  12128.         static char pgm[MAXNAME + 1] = { "rm" };%@NL@%
  12129.         extern void getpname(char *, char *);%@NL@%
  12130.         static void do_rm(char *, char *, BOOLEAN);%@NL@%
  12131.         extern int getopt(int, char **, char *);%@NL@%
  12132.         extern int optind, opterr;%@NL@%
  12133.         extern char *optarg;%@NL@%
  12134.         %@NL@%
  12135.         %@AB@%/* get program name from DOS (version 3.00 and later) */%@AE@%%@NL@%
  12136.         if (_osmajor >= 3)%@NL@%
  12137.                 getpname(*argv, pgm);%@NL@%
  12138. %@NL@%
  12139.         %@AB@%/* process optional arguments first */%@AE@%%@NL@%
  12140.         errflag = iflag = FALSE;%@NL@%
  12141.         while ((ch = getopt(argc, argv, "i")) != EOF)%@NL@%
  12142.                 switch (ch) {%@NL@%
  12143.                 case 'i':%@NL@%
  12144.                         %@AB@%/* interactive -- requires confirmation */%@AE@%%@NL@%
  12145.                         iflag = TRUE;%@NL@%
  12146.                         break;%@NL@%
  12147.                 case '?':%@NL@%
  12148.                         %@AB@%/* say what? */%@AE@%%@NL@%
  12149.                         errflag = TRUE;%@NL@%
  12150.                         break;%@NL@%
  12151.                 }%@NL@%
  12152.         argc -= optind;%@NL@%
  12153.         argv += optind;%@NL@%
  12154. %@NL@%
  12155.         if (argc <= 0 || errflag == TRUE) {%@NL@%
  12156.                 fprintf(stderr, "%s [-i] file(s)\n", pgm);%@NL@%
  12157.                 exit(1);%@NL@%
  12158.         }%@NL@%
  12159. %@NL@%
  12160.         %@AB@%/* process remaining arguments */%@AE@%%@NL@%
  12161.         for (; argc-- > 0; ++argv)%@NL@%
  12162.                 do_rm(pgm, *argv, iflag);%@NL@%
  12163. %@NL@%
  12164.         exit(0);%@NL@%
  12165. } %@AB@%/* end main() */%@AE@%%@NL@%
  12166. %@NL@%
  12167. %@AB@%/*%@NL@%
  12168. %@AB@% *        do_rm -- remove a file%@NL@%
  12169. %@AB@% */%@AE@%%@NL@%
  12170. %@NL@%
  12171. static void%@NL@%
  12172. do_rm(pname, fname, iflag)%@NL@%
  12173. char *pname, *fname;%@NL@%
  12174. BOOLEAN iflag;%@NL@%
  12175. {%@NL@%
  12176.         int result = 0;%@NL@%
  12177.         struct stat statbuf;%@NL@%
  12178.         static BOOLEAN affirm();%@NL@%
  12179. %@NL@%
  12180.         if (iflag == TRUE) {%@NL@%
  12181.                 fprintf(stderr, "%s (y/n): ", fname);%@NL@%
  12182.                 if (affirm() == FALSE)%@NL@%
  12183.                         return;%@NL@%
  12184.         }%@NL@%
  12185.         if ((result = unlink(fname)) == -1) {%@NL@%
  12186.                 fprintf(stderr, "%s: ", pname);%@NL@%
  12187.                 perror(fname);%@NL@%
  12188.         }%@NL@%
  12189.         return;%@NL@%
  12190. }%@NL@%
  12191. %@NL@%
  12192. %@AB@%/*%@NL@%
  12193. %@AB@% *        affirm -- return TRUE if the first character of the%@NL@%
  12194. %@AB@% *        user's response is 'y' or FALSE otherwise%@NL@%
  12195. %@AB@% */%@AE@%%@NL@%
  12196. %@NL@%
  12197. %@AI@%#define %@AE@%MAXSTR        64 %@NL@%
  12198. %@NL@%
  12199. static BOOLEAN%@NL@%
  12200. affirm()%@NL@%
  12201. {%@NL@%
  12202.         char line[MAXSTR + 1];%@NL@%
  12203.         char *response;%@NL@%
  12204. %@NL@%
  12205.         response = fgets(line, MAXSTR, stdin); %@NL@%
  12206.         return (tolower(*response) == 'y' ? TRUE : FALSE);%@NL@%
  12207. }%@NL@%
  12208. %@NL@%
  12209. %@NL@%
  12210. %@2@%%@AH@%RUN_ONCE.C%@AE@%%@EH@%%@NL@%
  12211. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\04STDLIB\RUN_ONCE.C%@AE@%%@NL@%
  12212. %@NL@%
  12213. %@AB@%/*%@NL@%
  12214. %@AB@% *        run_once -- run a program one time and then%@NL@%
  12215. %@AB@% *        "hang" the system to prevent unwanted use%@NL@%
  12216. %@AB@% */%@AE@%%@NL@%
  12217. %@NL@%
  12218. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  12219. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  12220. %@AI@%#include %@AE@%<process.h> %@NL@%
  12221. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  12222. %@NL@%
  12223. main(argc, argv)%@NL@%
  12224. int argc;%@NL@%
  12225. char *argv[];%@NL@%
  12226. {%@NL@%
  12227.         extern void fatal(char *, char *, int);%@NL@%
  12228. %@NL@%
  12229.         %@AB@%/* skip over the program name */%@AE@%%@NL@%
  12230.         ++argv;%@NL@%
  12231. %@NL@%
  12232.         %@AB@%/* run the specified command line [pgmname arg(s)] */%@AE@%%@NL@%
  12233.         if (spawnvp(P_WAIT, *argv, argv) == -1)%@NL@%
  12234.                 fatal("run_once", "Error running specified program", 1);%@NL@%
  12235.         fprintf(stderr, "Please turn off the power to the computer.\n");%@NL@%
  12236. %@NL@%
  12237.         %@AB@%/* do nothing */%@AE@%%@NL@%
  12238.         FOREVER%@NL@%
  12239.                 ;%@NL@%
  12240. }%@NL@%
  12241. %@NL@%
  12242. %@NL@%
  12243. %@2@%%@AH@%SB_BOX.C%@AE@%%@EH@%%@NL@%
  12244. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_BOX.C%@AE@%%@NL@%
  12245. %@NL@%
  12246. %@AB@%/*%@NL@%
  12247. %@AB@% *        sb_box -- draw a box around the perimeter of a window%@NL@%
  12248. %@AB@% *        using the appropriate IBM graphics characters%@NL@%
  12249. %@AB@% */%@AE@%%@NL@%
  12250. %@NL@%
  12251. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  12252. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  12253. %@AI@%#include %@AE@%<local\box.h> %@NL@%
  12254. %@NL@%
  12255. int%@NL@%
  12256. sb_box(win, type, attr)%@NL@%
  12257. struct REGION *win;%@NL@%
  12258. short type;%@NL@%
  12259. unsigned char attr;%@NL@%
  12260. {%@NL@%
  12261.         register short r;        %@AB@%/* row index */%@AE@%%@NL@%
  12262.         short x;                %@AB@%/* interior horizontal line length */%@AE@%%@NL@%
  12263.         short maxr, maxc;        %@AB@%/* maximum row and col values */%@AE@%%@NL@%
  12264.         BOXTYPE *boxp;                %@AB@%/* pointer to box drawing character struct */%@AE@%%@NL@%
  12265.         static BOXTYPE box[] = {%@NL@%
  12266.                 '+',   '+',   '+',   '+',   '-',   '-',   '|',   '|',%@NL@%
  12267.                 ULC11, URC11, LLC11, LRC11, HBAR1, HBAR1, VBAR1, VBAR1,%@NL@%
  12268.                 ULC22, URC22, LLC22, LRC22, HBAR2, HBAR2, VBAR2, VBAR2,%@NL@%
  12269.                 ULC12, URC12, LLC12, LRC12, HBAR1, HBAR1, VBAR2, VBAR2,%@NL@%
  12270.                 ULC21, URC21, LLC21, LRC21, HBAR2, HBAR2, VBAR1, VBAR1,%@NL@%
  12271.                 BLOCK, BLOCK, BLOCK, BLOCK, HBART, HBARB, BLOCK, BLOCK%@NL@%
  12272.         };%@NL@%
  12273. %@NL@%
  12274.         boxp = &box[type];%@NL@%
  12275.         maxc = win->c1 - win->c0;%@NL@%
  12276.         maxr = win->r1 - win->r0;%@NL@%
  12277.         x = maxc - 1;%@NL@%
  12278. %@NL@%
  12279.         %@AB@%/* draw top row */%@AE@%%@NL@%
  12280.         sb_move(win, 0, 0);%@NL@%
  12281.         sb_wca(win, boxp->ul, attr, 1);%@NL@%
  12282.         sb_move(win, 0, 1);%@NL@%
  12283.         sb_wca(win, boxp->tbar, attr, x);%@NL@%
  12284.         sb_move(win, 0, maxc);%@NL@%
  12285.         sb_wca(win, boxp->ur, attr, 1);%@NL@%
  12286. %@NL@%
  12287.         %@AB@%/* draw left and right sides */%@AE@%%@NL@%
  12288.         for (r = 1; r < maxr; ++r) {%@NL@%
  12289.                 sb_move(win, r, 0);%@NL@%
  12290.                 sb_wca(win, boxp->lbar, attr, 1);%@NL@%
  12291.                 sb_move(win, r, maxc);%@NL@%
  12292.                 sb_wca(win, boxp->rbar, attr, 1);%@NL@%
  12293.         }%@NL@%
  12294. %@NL@%
  12295.         %@AB@%/* draw bottom row */%@AE@%%@NL@%
  12296.         sb_move(win, maxr, 0);%@NL@%
  12297.         sb_wca(win, boxp->ll, attr, 1);%@NL@%
  12298.         sb_move(win, maxr, 1);%@NL@%
  12299.         sb_wca(win, boxp->bbar, attr, x);%@NL@%
  12300.         sb_move(win, maxr, maxc);%@NL@%
  12301.         sb_wca(win, boxp->lr, attr, 1);%@NL@%
  12302.         %@NL@%
  12303.         return SB_OK;%@NL@%
  12304. }%@NL@%
  12305. %@NL@%
  12306. %@NL@%
  12307. %@2@%%@AH@%SB_FILL.C%@AE@%%@EH@%%@NL@%
  12308. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_FILL.C%@AE@%%@NL@%
  12309. %@NL@%
  12310. %@AB@%/*%@NL@%
  12311. %@AB@% *         sb_fill -- fill region routines%@NL@%
  12312. %@AB@% */%@AE@%%@NL@%
  12313. %@NL@%
  12314. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  12315. %@NL@%
  12316. extern struct BUFFER Sbuf;%@NL@%
  12317. extern union CELL Scrnbuf[SB_ROWS][SB_COLS];%@NL@%
  12318. %@NL@%
  12319. %@AB@%/*%@NL@%
  12320. %@AB@% *        sb_fill -- set all cells in a specified region%@NL@%
  12321. %@AB@% *        to the same character/attribute value%@NL@%
  12322. %@AB@% */%@AE@%%@NL@%
  12323. %@NL@%
  12324. int%@NL@%
  12325. sb_fill(win, ch, attr)%@NL@%
  12326. struct REGION *win;%@NL@%
  12327. unsigned char ch;        %@AB@%/* fill character */%@AE@%%@NL@%
  12328. unsigned char attr;        %@AB@%/* fill attribute */%@AE@%%@NL@%
  12329. {%@NL@%
  12330.         register int i, j;%@NL@%
  12331.         unsigned short ca;%@NL@%
  12332. %@NL@%
  12333.         ca = (attr << 8) | ch;%@NL@%
  12334.         for (i = win->sr0; i <= win->sr1; ++i) {%@NL@%
  12335.                 for (j = win->sc0; j <= win->sc1; ++j)%@NL@%
  12336.                         Scrnbuf[i][j].cap = ca;%@NL@%
  12337.                 if (win->sc0 < Sbuf.lcol[i])%@NL@%
  12338.                         Sbuf.lcol[i] = win->sc0;%@NL@%
  12339.                 if (win->sc1 > Sbuf.rcol[i])%@NL@%
  12340.                         Sbuf.rcol[i] = win->sc1;%@NL@%
  12341.         }%@NL@%
  12342.         Sbuf.flags |= SB_DELTA;%@NL@%
  12343. %@NL@%
  12344.         return SB_OK;%@NL@%
  12345. }%@NL@%
  12346. %@NL@%
  12347. %@NL@%
  12348. %@AB@%/*%@NL@%
  12349. %@AB@% *        sb_fillc -- set all cells in a specified region%@NL@%
  12350. %@AB@% *        to the same character value; leave attributes undisturbed%@NL@%
  12351. %@AB@% */%@AE@%%@NL@%
  12352. %@NL@%
  12353. int%@NL@%
  12354. sb_fillc(win, ch)%@NL@%
  12355. struct REGION *win;%@NL@%
  12356. unsigned char ch;        %@AB@%/* fill character */%@AE@%%@NL@%
  12357. {%@NL@%
  12358.         register int i, j;%@NL@%
  12359. %@NL@%
  12360.         for (i = win->sr0; i <= win->sr1; ++i) {%@NL@%
  12361.                 for (j = win->sc0; j <= win->sc1; ++j)%@NL@%
  12362.                         Scrnbuf[i][j].b.ch = ch;%@NL@%
  12363.                 if (win->sc0 < Sbuf.lcol[i])%@NL@%
  12364.                         Sbuf.lcol[i] = win->sc0;%@NL@%
  12365.                 if (win->sc1 > Sbuf.rcol[i])%@NL@%
  12366.                         Sbuf.rcol[i] = win->sc1;%@NL@%
  12367.         }%@NL@%
  12368.         Sbuf.flags |= SB_DELTA;%@NL@%
  12369. %@NL@%
  12370.         return SB_OK;%@NL@%
  12371. }%@NL@%
  12372. %@NL@%
  12373. %@NL@%
  12374. %@AB@%/*%@NL@%
  12375. %@AB@% *        sb_filla -- set all cells in a specified region%@NL@%
  12376. %@AB@% *        to the same attribute value; leave characters undisturbed%@NL@%
  12377. %@AB@% */%@AE@%%@NL@%
  12378. %@NL@%
  12379. int%@NL@%
  12380. sb_filla(win, attr)%@NL@%
  12381. struct REGION *win;%@NL@%
  12382. unsigned char attr;        %@AB@%/* fill attribute */%@AE@%%@NL@%
  12383. {%@NL@%
  12384.         register int i, j;%@NL@%
  12385. %@NL@%
  12386.         for (i = win->sr0; i <= win->sr1; ++i) {%@NL@%
  12387.                 for (j = win->sc0; j <= win->sc1; ++j)%@NL@%
  12388.                         Scrnbuf[i][j].b.attr = attr;%@NL@%
  12389.                 if (win->sc0 < Sbuf.lcol[i])%@NL@%
  12390.                         Sbuf.lcol[i] = win->sc0;%@NL@%
  12391.                 if (win->sc1 > Sbuf.rcol[i])%@NL@%
  12392.                         Sbuf.rcol[i] = win->sc1;%@NL@%
  12393.         }%@NL@%
  12394.         Sbuf.flags |= SB_DELTA;%@NL@%
  12395. %@NL@%
  12396.         return SB_OK;%@NL@%
  12397. }%@NL@%
  12398. %@NL@%
  12399. %@NL@%
  12400. %@2@%%@AH@%SB_INIT.C%@AE@%%@EH@%%@NL@%
  12401. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_INIT.C%@AE@%%@NL@%
  12402. %@NL@%
  12403. %@AB@%/*%@NL@%
  12404. %@AB@% *        sb_init -- initialize the buffered screen interface%@NL@%
  12405. %@AB@% */%@AE@%%@NL@%
  12406. %@NL@%
  12407. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  12408. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  12409. %@AI@%#include %@AE@%<string.h> %@NL@%
  12410. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  12411. %@NL@%
  12412. %@AB@%/* global data declarations */%@AE@%%@NL@%
  12413. struct BUFFER Sbuf;                        %@AB@%/* control information */%@AE@%%@NL@%
  12414. union CELL Scrnbuf[SB_ROWS][SB_COLS];        %@AB@%/* screen buffer array */%@AE@%%@NL@%
  12415. %@NL@%
  12416. int%@NL@%
  12417. sb_init()%@NL@%
  12418. {%@NL@%
  12419.         int i;%@NL@%
  12420.         char *um;        %@AB@%/* update mode */%@AE@%%@NL@%
  12421. %@NL@%
  12422.         %@AB@%/* set initial parameter values */%@AE@%%@NL@%
  12423.         Sbuf.bp = &Scrnbuf[0][0];%@NL@%
  12424.         Sbuf.row = Sbuf.col = 0;%@NL@%
  12425.         for (i = 0; i < SB_ROWS; ++i) {%@NL@%
  12426.                 Sbuf.lcol[i] = SB_COLS;%@NL@%
  12427.                 Sbuf.rcol[i] = 0;%@NL@%
  12428.         }%@NL@%
  12429.         Sbuf.flags = 0;%@NL@%
  12430. %@NL@%
  12431.         %@AB@%/* set screen update mode */%@AE@%%@NL@%
  12432.         um = strupr(getenv("UPDATEMODE"));%@NL@%
  12433.         if (um == NULL || strcmp(um, "BIOS") == 0)%@NL@%
  12434.                 Sbuf.flags &= ~SB_DIRECT;%@NL@%
  12435.         else if (strcmp(um, "DIRECT") == 0)%@NL@%
  12436.                 Sbuf.flags |= SB_DIRECT;%@NL@%
  12437.         else%@NL@%
  12438.                 return SB_ERR;%@NL@%
  12439. %@NL@%
  12440.         return SB_OK;%@NL@%
  12441. }%@NL@%
  12442. %@NL@%
  12443. %@NL@%
  12444. %@2@%%@AH@%SB_MOVE.C%@AE@%%@EH@%%@NL@%
  12445. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_MOVE.C%@AE@%%@NL@%
  12446. %@NL@%
  12447. %@AB@%/*%@NL@%
  12448. %@AB@% *        sb_move -- move the screen buffer "cursor"%@NL@%
  12449. %@AB@% */%@AE@%%@NL@%
  12450. %@NL@%
  12451. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  12452. %@NL@%
  12453. extern struct BUFFER Sbuf;%@NL@%
  12454. extern union CELL Scrnbuf[SB_ROWS][SB_COLS];%@NL@%
  12455. %@NL@%
  12456. int%@NL@%
  12457. sb_move(win, r, c)%@NL@%
  12458. struct REGION *win;        %@AB@%/* window pointer */%@AE@%%@NL@%
  12459. register short r, c;        %@AB@%/* buffer row and column */%@AE@%%@NL@%
  12460. {%@NL@%
  12461.         %@AB@%/* don't change anything if request is out of range */%@AE@%%@NL@%
  12462.         if (r < 0 || r > win->r1 - win->r0 || c < 0 || c > win->c1 - win->c0)%@NL@%
  12463.                 return SB_ERR;%@NL@%
  12464.         win->row = r;%@NL@%
  12465.         win->col = c;%@NL@%
  12466.         Sbuf.row = r + win->r0;%@NL@%
  12467.         Sbuf.col = c + win->c0;%@NL@%
  12468.         return SB_OK;%@NL@%
  12469. }%@NL@%
  12470. %@NL@%
  12471. %@NL@%
  12472. %@2@%%@AH@%SB_NEW.C%@AE@%%@EH@%%@NL@%
  12473. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_NEW.C%@AE@%%@NL@%
  12474. %@NL@%
  12475. %@AB@%/*%@NL@%
  12476. %@AB@% *        sb_new -- prepare a new window (rectangular region)%@NL@%
  12477. %@AB@% *        and return a pointer to it%@NL@%
  12478. %@AB@% */%@AE@%%@NL@%
  12479. %@NL@%
  12480. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  12481. %@AI@%#include %@AE@%<malloc.h> %@NL@%
  12482. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  12483. %@NL@%
  12484. struct REGION *%@NL@%
  12485. sb_new(top, left, height, width)%@NL@%
  12486. int top;        %@AB@%/* top row */%@AE@%%@NL@%
  12487. int left;        %@AB@%/* left column */%@AE@%%@NL@%
  12488. int height;        %@AB@%/* total rows */%@AE@%%@NL@%
  12489. int width;        %@AB@%/* total columns */%@AE@%%@NL@%
  12490. {%@NL@%
  12491.         struct REGION *new;%@NL@%
  12492. %@NL@%
  12493.         %@AB@%/* allocate the control data structure */%@AE@%%@NL@%
  12494.         new = (struct REGION *)malloc(sizeof (struct REGION));%@NL@%
  12495.         if (new != NULL) {%@NL@%
  12496.                 new->r0 = new->sr0 = top;%@NL@%
  12497.                 new->r1 = new->sr1 = top + height - 1;%@NL@%
  12498.                 new->c0 = new->sc0 = left;%@NL@%
  12499.                 new->c1 = new->sc1 = left + width - 1;%@NL@%
  12500.                 new->row = new->col = 0;%@NL@%
  12501.                 new->wflags = 0;%@NL@%
  12502.         }%@NL@%
  12503.         return (new);%@NL@%
  12504. }%@NL@%
  12505. %@NL@%
  12506. %@NL@%
  12507. %@2@%%@AH@%SB_PUT.C%@AE@%%@EH@%%@NL@%
  12508. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_PUT.C%@AE@%%@NL@%
  12509. %@NL@%
  12510. %@AB@%/*%@NL@%
  12511. %@AB@% *        sb_put -- routines to put characters and strings into the%@NL@%
  12512. %@AB@% *        screen buffer; the cursor location is altered%@NL@%
  12513. %@AB@% */%@AE@%%@NL@%
  12514. %@NL@%
  12515. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  12516. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  12517. %@NL@%
  12518. extern struct BUFFER Sbuf;%@NL@%
  12519. extern union CELL Scrnbuf[SB_ROWS][SB_COLS];%@NL@%
  12520. %@NL@%
  12521. %@AB@%/*%@NL@%
  12522. %@AB@% *        sb_putc -- put a character into a screen buffer window%@NL@%
  12523. %@AB@% */%@AE@%%@NL@%
  12524. %@NL@%
  12525. int%@NL@%
  12526. sb_putc(win, ch)%@NL@%
  12527. struct REGION *win;%@NL@%
  12528. unsigned char ch;%@NL@%
  12529. {%@NL@%
  12530.         short cmax, rmax;%@NL@%
  12531.         short lim;%@NL@%
  12532.         short noscroll = 0, puterr = 0;%@NL@%
  12533. %@NL@%
  12534.         %@AB@%/* calculate screen buffer position and limits */%@AE@%%@NL@%
  12535.         cmax = win->c1 - win->c0;%@NL@%
  12536.         rmax = win->r1 - win->r0;%@NL@%
  12537.         Sbuf.row = win->r0 + win->row;%@NL@%
  12538.         Sbuf.col = win->c0 + win->col;%@NL@%
  12539. %@NL@%
  12540.         %@AB@%/* process the character */%@AE@%%@NL@%
  12541.         switch (ch) {%@NL@%
  12542.         case '\b':%@NL@%
  12543.                 %@AB@%/* non-destructive backspace */%@AE@%%@NL@%
  12544.                 if (win->col > 0) {%@NL@%
  12545.                         --win->col;%@NL@%
  12546.                         Sbuf.col = win->c0 + win->col;%@NL@%
  12547.                         return SB_OK;%@NL@%
  12548.                 }%@NL@%
  12549.                 else%@NL@%
  12550.                         return SB_ERR;%@NL@%
  12551.         case '\n':%@NL@%
  12552.                 %@AB@%/* clear trailing line segment */%@AE@%%@NL@%
  12553.                 while (win->col < cmax)%@NL@%
  12554.                         if (sb_putc(win, ' ') == SB_ERR)%@NL@%
  12555.                                 ++puterr;%@NL@%
  12556.                 break;%@NL@%
  12557.         case '\t':%@NL@%
  12558.                 %@AB@%/* convert tab to required number of spaces */%@AE@%%@NL@%
  12559.                 lim = win->col + 8 - (win->col & 0x7);%@NL@%
  12560.                 while (win->col < lim)%@NL@%
  12561.                         if (sb_putc(win, ' ') == SB_ERR)%@NL@%
  12562.                                 ++puterr;%@NL@%
  12563.                 break;%@NL@%
  12564.         default:%@NL@%
  12565.                 %@AB@%/* if printable ASCII, place the character in the buffer */%@AE@%%@NL@%
  12566.                 if (isascii(ch) && isprint(ch))%@NL@%
  12567.                         Scrnbuf[Sbuf.row][Sbuf.col].b.ch = ch;%@NL@%
  12568.                 if (Sbuf.col < Sbuf.lcol[Sbuf.row])%@NL@%
  12569.                         Sbuf.lcol[Sbuf.row] = Sbuf.col;%@NL@%
  12570.                 if (Sbuf.col > Sbuf.rcol[Sbuf.row])%@NL@%
  12571.                         Sbuf.rcol[Sbuf.row] = Sbuf.col;%@NL@%
  12572.                 break;%@NL@%
  12573.         }%@NL@%
  12574. %@NL@%
  12575.         %@AB@%/* update the cursor position */%@AE@%%@NL@%
  12576.         if (win->col < cmax)%@NL@%
  12577.                 ++win->col;%@NL@%
  12578.         else if (win->row < rmax) {%@NL@%
  12579.                 win->col = 0;%@NL@%
  12580.                 ++win->row;%@NL@%
  12581.         }%@NL@%
  12582.         else if ((win->wflags & SB_SCROLL) == SB_SCROLL) {%@NL@%
  12583.                 sb_scrl(win, 1);%@NL@%
  12584.                 win->col = 0;%@NL@%
  12585.                 win->row = rmax;%@NL@%
  12586.         }%@NL@%
  12587.         else%@NL@%
  12588.                 ++noscroll;%@NL@%
  12589. %@NL@%
  12590.         %@AB@%/* update screen buffer position */%@AE@%%@NL@%
  12591.         Sbuf.row = win->r0 + win->row;%@NL@%
  12592.         Sbuf.col = win->c0 + win->col;%@NL@%
  12593.         Sbuf.flags |= SB_DELTA;%@NL@%
  12594. %@NL@%
  12595.         return ((noscroll || puterr) ? SB_ERR : SB_OK);%@NL@%
  12596. } %@AB@%/* end sb_putc() */%@AE@%%@NL@%
  12597. %@NL@%
  12598. %@NL@%
  12599. %@AB@%/*%@NL@%
  12600. %@AB@% *        sb_puts -- put a string into the screen buffer%@NL@%
  12601. %@AB@% */%@AE@%%@NL@%
  12602. %@NL@%
  12603. int%@NL@%
  12604. sb_puts(win, s)%@NL@%
  12605. struct REGION *win;%@NL@%
  12606. unsigned char *s;%@NL@%
  12607. {%@NL@%
  12608.         while (*s)%@NL@%
  12609.                 if (sb_putc(win, *s++) == SB_ERR)%@NL@%
  12610.                         return SB_ERR;%@NL@%
  12611.         return SB_OK;%@NL@%
  12612. } %@AB@%/* end sb_puts() */%@AE@%%@NL@%
  12613. %@NL@%
  12614. %@NL@%
  12615. %@2@%%@AH@%SB_READ.C%@AE@%%@EH@%%@NL@%
  12616. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_READ.C%@AE@%%@NL@%
  12617. %@NL@%
  12618. %@AB@%/*%@NL@%
  12619. %@AB@% *        sb_read -- read character/attribute data%@NL@%
  12620. %@AB@% */%@AE@%%@NL@%
  12621. %@NL@%
  12622. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  12623. %@NL@%
  12624. extern struct BUFFER Sbuf;%@NL@%
  12625. extern union CELL Scrnbuf[SB_ROWS][SB_COLS];%@NL@%
  12626. %@NL@%
  12627. unsigned char%@NL@%
  12628. sb_ra(win)%@NL@%
  12629. struct REGION *win;        %@AB@%/* window pointer */%@AE@%%@NL@%
  12630. {%@NL@%
  12631.         return (Scrnbuf[win->r0 + win->row][win->c0 + win->col].b.attr);%@NL@%
  12632. } %@AB@%/* end sb_ra() */%@AE@%%@NL@%
  12633. %@NL@%
  12634. %@NL@%
  12635. %@AB@%/*%@NL@%
  12636. %@AB@% *        sb_rc -- read character from current location in screen buffer%@NL@%
  12637. %@AB@% */%@AE@%%@NL@%
  12638. %@NL@%
  12639. unsigned char%@NL@%
  12640. sb_rc(win)%@NL@%
  12641. struct REGION *win;        %@AB@%/* window pointer */%@AE@%%@NL@%
  12642. {%@NL@%
  12643.         return (Scrnbuf[win->r0 + win->row][win->c0 + win->col].b.ch);%@NL@%
  12644. } %@AB@%/* end sb_rc() */%@AE@%%@NL@%
  12645. %@NL@%
  12646. %@NL@%
  12647. %@AB@%/*%@NL@%
  12648. %@AB@% *        sb_rca -- read character/attribute pair from current%@NL@%
  12649. %@AB@% *        location in screen buffer%@NL@%
  12650. %@AB@% */%@AE@%%@NL@%
  12651. %@NL@%
  12652. unsigned short%@NL@%
  12653. sb_rca(win)%@NL@%
  12654. struct REGION *win;        %@AB@%/* window pointer */%@AE@%%@NL@%
  12655. {%@NL@%
  12656.         return (Scrnbuf[win->r0 + win->row][win->c0 + win->col].cap);%@NL@%
  12657. } %@AB@%/* end sb_rca() */%@AE@%%@NL@%
  12658. %@NL@%
  12659. %@NL@%
  12660. %@2@%%@AH@%SB_SCRL.C%@AE@%%@EH@%%@NL@%
  12661. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_SCRL.C%@AE@%%@NL@%
  12662. %@NL@%
  12663. %@AB@%/*%@NL@%
  12664. %@AB@% *        sb_scrl -- scrolling routines%@NL@%
  12665. %@AB@% */%@AE@%%@NL@%
  12666. %@NL@%
  12667. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  12668. %@NL@%
  12669. extern struct BUFFER Sbuf;%@NL@%
  12670. extern union CELL Scrnbuf[SB_ROWS][SB_COLS];%@NL@%
  12671. %@NL@%
  12672. %@NL@%
  12673. %@AB@%/*%@NL@%
  12674. %@AB@% *        sb_scrl -- scroll the specified window%@NL@%
  12675. %@AB@% *        n lines (direction indicated by sign)%@NL@%
  12676. %@AB@% */%@AE@%%@NL@%
  12677. %@NL@%
  12678. int%@NL@%
  12679. sb_scrl(win, n)%@NL@%
  12680. struct REGION *win;%@NL@%
  12681. short n;                %@AB@%/* number of rows to scroll */%@AE@%%@NL@%
  12682. {%@NL@%
  12683.         register short r, c;%@NL@%
  12684. %@NL@%
  12685.         if (n == 0)%@NL@%
  12686.                 %@AB@%/* clear the entire region to spaces */%@AE@%%@NL@%
  12687.                 sb_fillc(win, ' ');%@NL@%
  12688.         else if (n > 0) {%@NL@%
  12689.                 %@AB@%/* scroll n rows up */%@AE@%%@NL@%
  12690.                 for (r = win->sr0; r <= win->sr1 - n; ++r) {%@NL@%
  12691.                         for (c = win->sc0; c <= win->sc1; ++c)%@NL@%
  12692.                                 Scrnbuf[r][c] = Scrnbuf[r + n][c];%@NL@%
  12693.                         if (win->sc0 < Sbuf.lcol[r])%@NL@%
  12694.                                 Sbuf.lcol[r] = win->sc0;%@NL@%
  12695.                         if (win->sc1 > Sbuf.rcol[r])%@NL@%
  12696.                                 Sbuf.rcol[r] = win->sc1;%@NL@%
  12697.                 }%@NL@%
  12698.                 for ( ; r <= win->sr1; ++r) {%@NL@%
  12699.                         for (c = win->sc0; c <= win->sc1; ++c)%@NL@%
  12700.                                 Scrnbuf[r][c].b.ch = ' ';%@NL@%
  12701.                         if (win->sc0 < Sbuf.lcol[r])%@NL@%
  12702.                                 Sbuf.lcol[r] = win->sc0;%@NL@%
  12703.                         if (win->sc1 > Sbuf.rcol[r])%@NL@%
  12704.                                 Sbuf.rcol[r] = win->sc1;%@NL@%
  12705.                 }%@NL@%
  12706.         }%@NL@%
  12707.         else {%@NL@%
  12708.                 %@AB@%/* scroll n rows down */%@AE@%%@NL@%
  12709.                 n = -n;%@NL@%
  12710.                 for (r = win->sr1; r >= win->sr0 + n; --r) {%@NL@%
  12711.                         for (c = win->sc0; c <= win->sc1; ++c)%@NL@%
  12712.                                 Scrnbuf[r][c] = Scrnbuf[r - n][c];%@NL@%
  12713.                         if (win->sc0 < Sbuf.lcol[r])%@NL@%
  12714.                                 Sbuf.lcol[r] = win->sc0;%@NL@%
  12715.                         if (win->sc1 > Sbuf.rcol[r])%@NL@%
  12716.                                 Sbuf.rcol[r] = win->sc1;%@NL@%
  12717.                 }%@NL@%
  12718.                 for ( ; r >= win->sr0; --r) {%@NL@%
  12719.                         for (c = win->sc0; c <= win->sc1; ++c)%@NL@%
  12720.                                 Scrnbuf[r][c].b.ch = ' ';%@NL@%
  12721.                         if (win->sc0 < Sbuf.lcol[r])%@NL@%
  12722.                                 Sbuf.lcol[r] = win->sc0;%@NL@%
  12723.                         if (win->sc1 > Sbuf.rcol[r])%@NL@%
  12724.                                 Sbuf.rcol[r] = win->sc1;%@NL@%
  12725.                 }%@NL@%
  12726.         }%@NL@%
  12727.         Sbuf.flags |= SB_DELTA;%@NL@%
  12728.         return SB_OK;%@NL@%
  12729. } %@AB@%/* end sb_scrl() */%@AE@%%@NL@%
  12730. %@NL@%
  12731. %@NL@%
  12732. %@AB@%/*%@NL@%
  12733. %@AB@% *        sb_set_scrl -- set the scroll region boundaries%@NL@%
  12734. %@AB@% */%@AE@%%@NL@%
  12735. %@NL@%
  12736. int%@NL@%
  12737. sb_set_scrl(win, top, left, bottom, right)%@NL@%
  12738. struct REGION *win;        %@AB@%/* window pointer */%@AE@%%@NL@%
  12739. short top, left;        %@AB@%/* upper-left corner */%@AE@%%@NL@%
  12740. short bottom, right;        %@AB@%/* lower-left corner */%@AE@%%@NL@%
  12741. {%@NL@%
  12742.         if (top < 0 || left < 0 ||%@NL@%
  12743.                 bottom > win->r1 - win->r0 || right > win->c1 - win->c0)%@NL@%
  12744.                 return SB_ERR;%@NL@%
  12745.         win->sr0 = win->r0 + top;%@NL@%
  12746.         win->sc0 = win->c0 + left;%@NL@%
  12747.         win->sr1 = win->r0 + bottom - 1;%@NL@%
  12748.         win->sc1 = win->c0 + right - 1;%@NL@%
  12749.         return SB_OK;%@NL@%
  12750. } %@AB@%/* end sb_set_scrl() */%@AE@%%@NL@%
  12751. %@NL@%
  12752. %@NL@%
  12753. %@2@%%@AH@%SB_SHOW.C%@AE@%%@EH@%%@NL@%
  12754. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_SHOW.C%@AE@%%@NL@%
  12755. %@NL@%
  12756. %@AB@%/*%@NL@%
  12757. %@AB@% *        sb_show -- copy the screen buffer to display memory%@NL@%
  12758. %@AB@% */%@AE@%%@NL@%
  12759. %@NL@%
  12760. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  12761. %@AI@%#include %@AE@%<dos.h> %@NL@%
  12762. %@AI@%#include %@AE@%<memory.h> %@NL@%
  12763. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  12764. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  12765. %@NL@%
  12766. %@AI@%#define %@AE@%       MDA_SEG        0xB000 %@NL@%
  12767. %@AI@%#define %@AE@%       CGA_SEG        0xB800 %@NL@%
  12768. %@AI@%#define %@AE@%NBYTES        (2 * SB_COLS) %@NL@%
  12769. %@NL@%
  12770. %@AB@%/* macro to synchronize with vertical retrace period */%@AE@%%@NL@%
  12771. %@AI@%#define %@AE@%VSTAT        0x3DA %@NL@%
  12772. %@AI@%#define %@AE@%VRBIT        8 %@NL@%
  12773. %@AI@%#define %@AE@%VSYNC        while ((inp(VSTAT) & VRBIT) == VRBIT); \ %@NL@%
  12774.                 while ((inp(VSTAT) & VRBIT) != VRBIT)%@NL@%
  12775. %@NL@%
  12776. extern struct BUFFER Sbuf;%@NL@%
  12777. extern union CELL Scrnbuf[SB_ROWS][SB_COLS];%@NL@%
  12778. %@NL@%
  12779. int%@NL@%
  12780. sb_show(pg)%@NL@%
  12781. short pg;%@NL@%
  12782. {%@NL@%
  12783.         register short r, c;%@NL@%
  12784.         short n;%@NL@%
  12785.         short count, ncols;%@NL@%
  12786.         unsigned int src_os, dest_os;%@NL@%
  12787.         struct SREGS segregs;%@NL@%
  12788. %@NL@%
  12789.         if ((Sbuf.flags & SB_DIRECT) == SB_DIRECT) {%@NL@%
  12790.                 %@AB@%/* use the direct-screen interface */%@AE@%%@NL@%
  12791.                 segread(&segregs);%@NL@%
  12792. %@NL@%
  12793.                 %@AB@%/* determine extent of changes */%@AE@%%@NL@%
  12794.                 n = 0;%@NL@%
  12795.                 for (r = 0; r < SB_ROWS; ++r)%@NL@%
  12796.                         if (Sbuf.lcol[r] <= Sbuf.rcol[r])%@NL@%
  12797.                                 ++n;%@NL@%
  12798.                 src_os = dest_os = 0;%@NL@%
  12799.                 if (n <= 2)%@NL@%
  12800.                         %@AB@%/* copy only rows that contain changes */%@AE@%%@NL@%
  12801.                         for (r = 0; r < SB_ROWS; ++r) {%@NL@%
  12802.                                 if (Sbuf.lcol[r] <= Sbuf.rcol[r]) {%@NL@%
  12803.                                         %@AB@%/* copy blocks during vertical retrace */%@AE@%%@NL@%
  12804.                                         VSYNC;%@NL@%
  12805.                                         movedata(segregs.ds,%@NL@%
  12806.                                                 (unsigned)&Scrnbuf[0][0] + src_os,%@NL@%
  12807.                                                 CGA_SEG, dest_os, NBYTES);%@NL@%
  12808.                                         Sbuf.lcol[r] = SB_COLS;%@NL@%
  12809.                                         Sbuf.rcol[r] = 0;%@NL@%
  12810.                                 }%@NL@%
  12811.                                 src_os += SB_COLS;%@NL@%
  12812.                                 dest_os += NBYTES;%@NL@%
  12813.                         }%@NL@%
  12814.                 else {%@NL@%
  12815.                         %@AB@%/* copy the entire buffer */%@AE@%%@NL@%
  12816.                         count = 3 * NBYTES;%@NL@%
  12817.                         ncols = 3 * SB_COLS;%@NL@%
  12818.                         for (r = 0; r < SB_ROWS - 1; r += 3) {%@NL@%
  12819.                                 VSYNC;%@NL@%
  12820.                                 movedata(segregs.ds, (unsigned)&Scrnbuf[0][0]%@NL@%
  12821.                                         + src_os, CGA_SEG, dest_os, count);%@NL@%
  12822.                                 src_os += ncols;%@NL@%
  12823.                                 dest_os += count;%@NL@%
  12824.                         }%@NL@%
  12825.                         VSYNC;%@NL@%
  12826.                         movedata(segregs.ds, (unsigned)&Scrnbuf[0][0] + src_os,%@NL@%
  12827.                                 CGA_SEG, dest_os, NBYTES);%@NL@%
  12828.                         for (r = 0; r < SB_ROWS; ++r) {%@NL@%
  12829.                                 Sbuf.lcol[r] = SB_COLS;%@NL@%
  12830.                                 Sbuf.rcol[r] = 0;%@NL@%
  12831.                         }%@NL@%
  12832.                 }%@NL@%
  12833.         }%@NL@%
  12834.         else%@NL@%
  12835.                 %@AB@%/* use the BIOS video interface */%@AE@%%@NL@%
  12836.                 for (r = 0; r < SB_ROWS; ++r)%@NL@%
  12837.                         %@AB@%/* copy only changed portions of lines */%@AE@%%@NL@%
  12838.                         if (Sbuf.lcol[r] < SB_COLS && Sbuf.rcol[r] > 0) {%@NL@%
  12839.                                 for (c = Sbuf.lcol[r]; c <= Sbuf.rcol[r]; ++c) {%@NL@%
  12840.                                         putcur(r, c, pg);%@NL@%
  12841.                                         writeca(Scrnbuf[r][c].b.ch,%@NL@%
  12842.                                                 Scrnbuf[r][c].b.attr, 1, pg);%@NL@%
  12843.                                 }%@NL@%
  12844.                                 Sbuf.lcol[r] = SB_COLS;%@NL@%
  12845.                                 Sbuf.rcol[r] = 0;%@NL@%
  12846.                         }%@NL@%
  12847. %@NL@%
  12848.         %@AB@%/* the display now matches the buffer -- clear flag bit */%@AE@%%@NL@%
  12849.         Sbuf.flags &= ~SB_DELTA;%@NL@%
  12850. %@NL@%
  12851.         return SB_OK;%@NL@%
  12852. }%@NL@%
  12853. %@NL@%
  12854. %@NL@%
  12855. %@2@%%@AH@%SB_TEST.C%@AE@%%@EH@%%@NL@%
  12856. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_TEST.C%@AE@%%@NL@%
  12857. %@NL@%
  12858. %@AB@%/*%@NL@%
  12859. %@AB@% *        sb_test -- driver for screen-buffer interface functions%@NL@%
  12860. %@AB@% */%@AE@%%@NL@%
  12861. %@NL@%
  12862. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  12863. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  12864. %@AI@%#include %@AE@%<conio.h> %@NL@%
  12865. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  12866. %@AI@%#include %@AE@%<local\keydefs.h> %@NL@%
  12867. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  12868. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  12869. %@AI@%#include %@AE@%<local\box.h> %@NL@%
  12870. %@AI@%#include %@AE@%"sb_test.h" %@NL@%
  12871. %@NL@%
  12872. %@AI@%#define %@AE@%BEL 7 %@NL@%
  12873. %@NL@%
  12874. extern struct BUFFER Sbuf;%@NL@%
  12875. %@NL@%
  12876. main(argc, argv)%@NL@%
  12877. int argc;%@NL@%
  12878. char *argv[];%@NL@%
  12879. {%@NL@%
  12880.         char *s, line[MAXLINE];%@NL@%
  12881.         int k;%@NL@%
  12882.         short i;%@NL@%
  12883.         FILE *fp;%@NL@%
  12884.         char fname[MAXPATH];%@NL@%
  12885.         struct REGION *cmnd, *stat, *text, *help, *curwin;%@NL@%
  12886.         unsigned char cmndattr, statattr, textattr, helpattr, curattr;%@NL@%
  12887.         unsigned char ch, userattr;%@NL@%
  12888. %@NL@%
  12889.         %@AB@%/* function prototypes */%@AE@%%@NL@%
  12890.         int sb_init();%@NL@%
  12891.         int sb_move(struct REGION *, short, short);%@NL@%
  12892.         struct REGION *sb_new(short, short, short, short);%@NL@%
  12893.         int sb_putc(struct REGION *, unsigned char);%@NL@%
  12894.         int sb_puts(struct REGION *, char *);%@NL@%
  12895.         int sb_show(short);%@NL@%
  12896.         int sb_fill(struct REGION *, unsigned char, unsigned char);%@NL@%
  12897.         char *get_fname(struct REGION *, char *, short);%@NL@%
  12898. %@NL@%
  12899.         getstate();%@NL@%
  12900.         readca(&ch, &userattr, Vpage);%@NL@%
  12901. %@NL@%
  12902.         %@AB@%/* set up the screen buffer */%@AE@%%@NL@%
  12903.         if (sb_init() == SB_ERR) {%@NL@%
  12904.                 fprintf(stderr, "Bad UPDATEMODE value in environment\n");%@NL@%
  12905.                 exit(1);%@NL@%
  12906.         }%@NL@%
  12907. %@NL@%
  12908.         %@AB@%/* set up windows and scrolling regions */%@AE@%%@NL@%
  12909.         cmnd = sb_new(CMND_ROW, CMND_COL, CMND_HT, CMND_WID);%@NL@%
  12910.         stat = sb_new(STAT_ROW, STAT_COL, STAT_HT, STAT_WID);%@NL@%
  12911.         text = sb_new(TEXT_ROW, TEXT_COL, TEXT_HT, TEXT_WID);%@NL@%
  12912.         help = sb_new(HELP_ROW, HELP_COL, HELP_HT, HELP_WID);%@NL@%
  12913.         text->wflags |= SB_SCROLL;%@NL@%
  12914.         sb_set_scrl(help, 1, 1, HELP_HT - 1, HELP_WID - 1);%@NL@%
  12915. %@NL@%
  12916.         %@AB@%/* display each primary window in its own attribute */%@AE@%%@NL@%
  12917.         cmndattr = GRN;%@NL@%
  12918.         statattr = (WHT << 4) | BLK;%@NL@%
  12919.         textattr = (BLU << 4) | CYAN;%@NL@%
  12920.         helpattr = (GRN << 4) | YEL;%@NL@%
  12921.         sb_fill(cmnd, ' ', cmndattr);%@NL@%
  12922.         if (sb_move(cmnd, 0, 0) == SB_OK)%@NL@%
  12923.                 sb_puts(cmnd, "SB_TEST (Version 1.0)");%@NL@%
  12924.         sb_fill(stat, ' ', statattr);%@NL@%
  12925.         if (sb_move(stat, 0, 0) == SB_OK)%@NL@%
  12926.                 sb_puts(stat, "*** STATUS AREA ***");%@NL@%
  12927.         for (i = 0; i <= text->r1 - text->r0; ++i) {%@NL@%
  12928.                 sb_move(text, i, 0);%@NL@%
  12929.                 sb_wca(text, i + 'a', textattr,%@NL@%
  12930.                         text->c1 - text->c0 + 1);%@NL@%
  12931.         }%@NL@%
  12932.         if (sb_move(text, 10, 25) == SB_OK)%@NL@%
  12933.                 sb_puts(text, " *** TEXT DISPLAY AREA *** ");%@NL@%
  12934.         sb_show(Vpage);%@NL@%
  12935.         curwin = text;%@NL@%
  12936.         curattr = textattr;%@NL@%
  12937. %@NL@%
  12938.         %@AB@%/* respond to user commands */%@AE@%%@NL@%
  12939.         while ((k = getkey()) != K_ESC) {%@NL@%
  12940.                 switch (k) {%@NL@%
  12941.                 case K_UP:%@NL@%
  12942.                         sb_scrl(curwin, 1);%@NL@%
  12943.                         break;%@NL@%
  12944.                 case K_DOWN:%@NL@%
  12945.                         sb_scrl(curwin, -1);%@NL@%
  12946.                         break;%@NL@%
  12947.                 case K_PGUP:%@NL@%
  12948.                         sb_scrl(curwin, curwin->sr1 - curwin->sr0);%@NL@%
  12949.                         break;%@NL@%
  12950.                 case K_PGDN:%@NL@%
  12951.                         sb_scrl(curwin, -(curwin->sr1 - curwin->sr0));%@NL@%
  12952.                         break;%@NL@%
  12953.                 case K_ALTC:%@NL@%
  12954.                         %@AB@%/* clear the current window */%@AE@%%@NL@%
  12955.                         sb_fill(curwin, ' ', curattr);%@NL@%
  12956.                         break;%@NL@%
  12957.                 case K_ALTH:%@NL@%
  12958.                         %@AB@%/* display help */%@AE@%%@NL@%
  12959.                         curwin = help;%@NL@%
  12960.                         curattr = helpattr;%@NL@%
  12961.                         for (i = 0; i < help->r1 - help->r0; ++i) {%@NL@%
  12962.                                 sb_move(help, i, 0);%@NL@%
  12963.                                 sb_wca(help, i + 'a', helpattr,%@NL@%
  12964.                                         help->c1 - help->c0 + 1);%@NL@%
  12965.                         }%@NL@%
  12966.                         sb_box(help, BOXBLK, helpattr);%@NL@%
  12967.                         break;%@NL@%
  12968.                 case K_ALTS:%@NL@%
  12969.                         %@AB@%/* fill the command area with letters */%@AE@%%@NL@%
  12970.                         curwin = stat;%@NL@%
  12971.                         curattr = statattr;%@NL@%
  12972.                         sb_fill(stat, 's', statattr);%@NL@%
  12973.                         break;%@NL@%
  12974.                 case K_ALTT:%@NL@%
  12975.                         %@AB@%/* fill the text area */%@AE@%%@NL@%
  12976.                         curwin = text;%@NL@%
  12977.                         curattr = textattr;%@NL@%
  12978.                         for (i = 0; i <= text->r1 - text->r0; ++i) {%@NL@%
  12979.                                 sb_move(text, i, 0);%@NL@%
  12980.                                 sb_wca(text, i + 'a', textattr,%@NL@%
  12981.                                         text->c1 - text->c0 + 1);%@NL@%
  12982.                         }%@NL@%
  12983.                         break;%@NL@%
  12984.                 case K_ALTR:%@NL@%
  12985.                         %@AB@%/* read a file into the current window */%@AE@%%@NL@%
  12986.                         sb_fill(stat, ' ', statattr);%@NL@%
  12987.                         sb_move(stat, 0, 0);%@NL@%
  12988.                         sb_puts(stat, "File to read: ");%@NL@%
  12989.                         sb_show(Vpage);%@NL@%
  12990.                         (void)get_fname(stat, fname, MAXPATH);%@NL@%
  12991.                         if ((fp = fopen(fname, "r")) == NULL) {%@NL@%
  12992.                                 sb_fill(stat, ' ', statattr);%@NL@%
  12993.                                 sb_move(stat, 0, 0);%@NL@%
  12994.                                 sb_puts(stat, "Cannot open ");%@NL@%
  12995.                                 sb_puts(stat, fname);%@NL@%
  12996.                         }%@NL@%
  12997.                         else {%@NL@%
  12998.                                 sb_fill(stat, ' ', statattr);%@NL@%
  12999.                                 sb_move(stat, 0, 0);%@NL@%
  13000.                                 sb_puts(stat, "File: ");%@NL@%
  13001.                                 sb_puts(stat, fname);%@NL@%
  13002.                                 sb_show(Vpage);%@NL@%
  13003.                                 sb_fill(text, ' ', textattr);%@NL@%
  13004.                                 sb_move(text, 0, 0);%@NL@%
  13005.                                 putcur(text->r0, text->c0, Vpage);%@NL@%
  13006.                                 while ((s = fgets(line, MAXLINE, fp)) != NULL) {%@NL@%
  13007.                                         if (sb_puts(text, s) == SB_ERR) {%@NL@%
  13008.                                                 clrscrn(userattr);%@NL@%
  13009.                                                 putcur(0, 0, Vpage);%@NL@%
  13010.                                                 fprintf(stderr, "puts error\n");%@NL@%
  13011.                                                 exit(1);%@NL@%
  13012.                                         }%@NL@%
  13013.                                         sb_show(Vpage);%@NL@%
  13014.                                 }%@NL@%
  13015.                                 if (ferror(fp)) {%@NL@%
  13016.                                         putcur(text->r0, text->c0, Vpage);%@NL@%
  13017.                                         fprintf(stderr, "Error reading file\n");%@NL@%
  13018.                                 }%@NL@%
  13019.                                 fclose(fp);%@NL@%
  13020.                         }%@NL@%
  13021.                         break;%@NL@%
  13022.                 default:%@NL@%
  13023.                         %@AB@%/* say what? */%@AE@%%@NL@%
  13024.                         fputc(BEL, stderr);%@NL@%
  13025.                         continue;%@NL@%
  13026.                 }%@NL@%
  13027.                 if ((Sbuf.flags & SB_DELTA) == SB_DELTA)%@NL@%
  13028.                         sb_show(Vpage);%@NL@%
  13029.         }%@NL@%
  13030. %@NL@%
  13031.         clrscrn(userattr);%@NL@%
  13032.         putcur(0, 0, Vpage);%@NL@%
  13033.         exit(0);%@NL@%
  13034. }%@NL@%
  13035. %@NL@%
  13036. %@AB@%/*%@NL@%
  13037. %@AB@% *        get_fname -- get a filename from the user%@NL@%
  13038. %@AB@% */%@AE@%%@NL@%
  13039. %@NL@%
  13040. char *%@NL@%
  13041. get_fname(win, path, lim)%@NL@%
  13042. struct REGION *win;%@NL@%
  13043. char *path;%@NL@%
  13044. short lim;%@NL@%
  13045. {%@NL@%
  13046.         int ch;%@NL@%
  13047.         char *s;%@NL@%
  13048. %@NL@%
  13049.         s = path;%@NL@%
  13050.         sb_show(Vpage);%@NL@%
  13051.         while ((ch = getch()) != K_RETURN) {%@NL@%
  13052.                 if (ch == '\b')%@NL@%
  13053.                         --s;%@NL@%
  13054.                 else {%@NL@%
  13055.                         sb_putc(win, ch);%@NL@%
  13056.                         *s++ = ch;%@NL@%
  13057.                 }%@NL@%
  13058.                 sb_show(Vpage);%@NL@%
  13059.         }%@NL@%
  13060.         *s = '\0';%@NL@%
  13061.         return (path);%@NL@%
  13062. }%@NL@%
  13063. %@NL@%
  13064. %@NL@%
  13065. %@2@%%@AH@%SB_WRITE.C%@AE@%%@EH@%%@NL@%
  13066. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\12SBUF\SB_WRITE.C%@AE@%%@NL@%
  13067. %@NL@%
  13068. %@AB@%/*%@NL@%
  13069. %@AB@% *        sb_write -- screen buffer write routines%@NL@%
  13070. %@AB@% */%@AE@%%@NL@%
  13071. %@NL@%
  13072. %@AI@%#include %@AE@%<local\sbuf.h> %@NL@%
  13073. %@NL@%
  13074. extern struct BUFFER Sbuf;%@NL@%
  13075. extern union CELL Scrnbuf[SB_ROWS][SB_COLS];%@NL@%
  13076. %@NL@%
  13077. %@AB@%/*%@NL@%
  13078. %@AB@% *        sb_wa -- write an attribute to a region of the screen buffer%@NL@%
  13079. %@AB@% */%@AE@%%@NL@%
  13080. %@NL@%
  13081. int%@NL@%
  13082. sb_wa(win, attr, n)%@NL@%
  13083. struct REGION *win;        %@AB@%/* window pointer */%@AE@%%@NL@%
  13084. unsigned char attr;        %@AB@%/* attribute */%@AE@%%@NL@%
  13085. short n;                %@AB@%/* repetition count */%@AE@%%@NL@%
  13086. {%@NL@%
  13087.         short i;%@NL@%
  13088.         short row;%@NL@%
  13089.         short col;%@NL@%
  13090. %@NL@%
  13091.         i = n;%@NL@%
  13092.         row = win->r0 + win->row;%@NL@%
  13093.         col = win->c0 + win->col;%@NL@%
  13094.         while (i--)%@NL@%
  13095.                 Scrnbuf[row][col + i].b.attr = attr;%@NL@%
  13096. %@NL@%
  13097.         %@AB@%/* marked the changed region */%@AE@%%@NL@%
  13098.         if (col < Sbuf.lcol[row])%@NL@%
  13099.                 Sbuf.lcol[row] = col;%@NL@%
  13100.         if (col + n > Sbuf.rcol[row])%@NL@%
  13101.                 Sbuf.rcol[row] = col + n;%@NL@%
  13102.         Sbuf.flags |= SB_DELTA;%@NL@%
  13103. %@NL@%
  13104.         return (i == 0) ? SB_OK : SB_ERR;%@NL@%
  13105. } %@AB@%/* end sb_wa() */%@AE@%%@NL@%
  13106. %@NL@%
  13107. %@NL@%
  13108. %@AB@%/*%@NL@%
  13109. %@AB@% *        sb_wc -- write a character to a region of the screen buffer%@NL@%
  13110. %@AB@% */%@AE@%%@NL@%
  13111. %@NL@%
  13112. int%@NL@%
  13113. sb_wc(win, ch, n)%@NL@%
  13114. struct REGION *win;        %@AB@%/* window pointer */%@AE@%%@NL@%
  13115. unsigned char ch;        %@AB@%/* character */%@AE@%%@NL@%
  13116. short n;                %@AB@%/* repetition count */%@AE@%%@NL@%
  13117. {%@NL@%
  13118.         short i;%@NL@%
  13119.         short row;%@NL@%
  13120.         short col;%@NL@%
  13121. %@NL@%
  13122.         i = n;%@NL@%
  13123.         row = win->r0 + win->row;%@NL@%
  13124.         col = win->c0 + win->col;%@NL@%
  13125.         while (i--)%@NL@%
  13126.                 Scrnbuf[row][col + i].b.ch = ch;%@NL@%
  13127. %@NL@%
  13128.         %@AB@%/* marked the changed region */%@AE@%%@NL@%
  13129.         if (col < Sbuf.lcol[row])%@NL@%
  13130.                 Sbuf.lcol[row] = col;%@NL@%
  13131.         if (col + n > Sbuf.rcol[row])%@NL@%
  13132.                 Sbuf.rcol[row] = col + n;%@NL@%
  13133.         Sbuf.flags |= SB_DELTA;%@NL@%
  13134. %@NL@%
  13135.         return (i == 0 ? SB_OK : SB_ERR);%@NL@%
  13136. } %@AB@%/* end sb_wc() */%@AE@%%@NL@%
  13137. %@NL@%
  13138. %@NL@%
  13139. %@AB@%/*%@NL@%
  13140. %@AB@% *        sb_wca -- write a character/attribute pair to a region%@NL@%
  13141. %@AB@% *        of the screen buffer%@NL@%
  13142. %@AB@% */%@AE@%%@NL@%
  13143. %@NL@%
  13144. int%@NL@%
  13145. sb_wca(win, ch, attr, n)%@NL@%
  13146. struct REGION *win;        %@AB@%/* window pointer */%@AE@%%@NL@%
  13147. unsigned char ch;        %@AB@%/* character */%@AE@%%@NL@%
  13148. unsigned char attr;        %@AB@%/* attribute */%@AE@%%@NL@%
  13149. short n;                %@AB@%/* repetition count */%@AE@%%@NL@%
  13150. {%@NL@%
  13151.         int i;%@NL@%
  13152.         short row;%@NL@%
  13153.         short col;%@NL@%
  13154. %@NL@%
  13155.         i = n;%@NL@%
  13156.         row = win->r0 + win->row;%@NL@%
  13157.         col = win->c0 + win->col;%@NL@%
  13158.         while (i--)%@NL@%
  13159.                 Scrnbuf[row][col + i].cap = (attr << 8) | ch;%@NL@%
  13160. %@NL@%
  13161.         %@AB@%/* marked the changed region */%@AE@%%@NL@%
  13162.         if (col < Sbuf.lcol[row])%@NL@%
  13163.                 Sbuf.lcol[row] = col;%@NL@%
  13164.         if (col + n > Sbuf.rcol[row])%@NL@%
  13165.                 Sbuf.rcol[row] = col + n;%@NL@%
  13166.         Sbuf.flags |= SB_DELTA;%@NL@%
  13167. %@NL@%
  13168.         return (i == 0 ? SB_OK : SB_ERR);%@NL@%
  13169. } %@AB@%/* end sb_wca() */%@AE@%%@NL@%
  13170. %@NL@%
  13171. %@NL@%
  13172. %@2@%%@AH@%SC.C%@AE@%%@EH@%%@NL@%
  13173. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\13ANSI\SC.C%@AE@%%@NL@%
  13174. %@NL@%
  13175. %@AB@%/*%@NL@%
  13176. %@AB@% *        SetColor (sc) -- set foreground, background, and border%@NL@%
  13177. %@AB@% *        attributes on systems equipped with color display systems%@NL@%
  13178. %@AB@% *%@NL@%
  13179. %@AB@% *        Usage:        sc [foreground [background [border]]]%@NL@%
  13180. %@AB@% *                sc [attribute]%@NL@%
  13181. %@AB@% */%@AE@%%@NL@%
  13182. %@NL@%
  13183. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  13184. %@AI@%#include %@AE@%<dos.h> %@NL@%
  13185. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  13186. %@AI@%#include %@AE@%<local\ansi.h> %@NL@%
  13187. %@AI@%#include %@AE@%<local\ibmcolor.h> %@NL@%
  13188. %@NL@%
  13189. main(argc, argv)%@NL@%
  13190. int argc;%@NL@%
  13191. char **argv;%@NL@%
  13192. {%@NL@%
  13193.         extern void ansi_tst();%@NL@%
  13194.         extern BOOLEAN iscolor();%@NL@%
  13195.         extern void setattr(POSITION, int);%@NL@%
  13196.         extern void menumode();%@NL@%
  13197.         extern parse(int, char **);%@NL@%
  13198. %@NL@%
  13199.         ansi_tst();%@NL@%
  13200.         if (iscolor() == FALSE) {%@NL@%
  13201.                 fprintf(stderr, "\n\nSystem not in a color text mode.\n");%@NL@%
  13202.                 fprintf(stderr, "Use the MODE command to set the mode.\n");%@NL@%
  13203.                 exit(2);%@NL@%
  13204.         }%@NL@%
  13205. %@NL@%
  13206.         %@AB@%/* process either batch or interactive commands */%@AE@%%@NL@%
  13207.         if (argc > 1)%@NL@%
  13208.                 %@AB@%/* batch mode processing */%@AE@%%@NL@%
  13209.                 parse(argc, argv);%@NL@%
  13210.         else%@NL@%
  13211.                 %@AB@%/* no command-line args -- interactive mode */%@AE@%%@NL@%
  13212.                 menumode();%@NL@%
  13213. %@NL@%
  13214.         ANSI_ED;%@NL@%
  13215.         exit (0);%@NL@%
  13216. }%@NL@%
  13217. %@NL@%
  13218. %@NL@%
  13219. %@2@%%@AH@%SCROLL.C%@AE@%%@EH@%%@NL@%
  13220. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\SCROLL.C%@AE@%%@NL@%
  13221. %@NL@%
  13222. %@AB@%/*%@NL@%
  13223. %@AB@% *        scroll -- scroll a region of the "visual" screen%@NL@%
  13224. %@AB@% *        page up or down by n rows (0 = initialize region)%@NL@%
  13225. %@AB@% */%@AE@%%@NL@%
  13226. %@NL@%
  13227. %@AI@%#include %@AE@%<dos.h> %@NL@%
  13228. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  13229. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  13230. %@NL@%
  13231. int%@NL@%
  13232. scroll(t, l, b, r, n, a)%@NL@%
  13233. int t;        %@AB@%/* top row of scroll region */%@AE@%%@NL@%
  13234. int l;        %@AB@%/* left column */%@AE@%%@NL@%
  13235. int b;        %@AB@%/* bottom row */%@AE@%%@NL@%
  13236. int r;        %@AB@%/* right column */%@AE@%%@NL@%
  13237. int n;        %@AB@%/* number of lines to scroll */%@AE@%%@NL@%
  13238.         %@AB@%/* sign indicates direction to scroll */%@AE@%%@NL@%
  13239.         %@AB@%/* 0 means scroll all lines in the region (initialize) */%@AE@%%@NL@%
  13240. unsigned char a;%@AB@%/* attribute for new lines */%@AE@%%@NL@%
  13241. {%@NL@%
  13242.         union REGS inregs, outregs;%@NL@%
  13243. %@NL@%
  13244.         if (n < 0) {%@NL@%
  13245.                 %@AB@%/* scroll visual page down n lines */%@AE@%%@NL@%
  13246.                 inregs.h.ah = SCROLL_DN;%@NL@%
  13247.                 inregs.h.al = -n;%@NL@%
  13248.         }%@NL@%
  13249.         else {%@NL@%
  13250.                 %@AB@%/* scroll visual page up n lines */%@AE@%%@NL@%
  13251.                 inregs.h.ah = SCROLL_UP;%@NL@%
  13252.                 inregs.h.al = n;%@NL@%
  13253.         }%@NL@%
  13254.         inregs.h.bh = a;        %@AB@%/* attribute of blank lines */%@AE@%%@NL@%
  13255.         inregs.h.bl = 0;%@NL@%
  13256.         inregs.h.ch = t;        %@AB@%/* upper-left of scroll region */%@AE@%%@NL@%
  13257.         inregs.h.cl = l;%@NL@%
  13258.         inregs.h.dh = b;        %@AB@%/* lower-right of scroll region */%@AE@%%@NL@%
  13259.         inregs.h.dl = r;%@NL@%
  13260.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  13261. %@NL@%
  13262.         return (outregs.x.cflag);%@NL@%
  13263. }%@NL@%
  13264. %@NL@%
  13265. %@NL@%
  13266. %@2@%%@AH@%SC_CMDS.C%@AE@%%@EH@%%@NL@%
  13267. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\13ANSI\SC_CMDS.C%@AE@%%@NL@%
  13268. %@NL@%
  13269. %@AB@%/*%@NL@%
  13270. %@AB@% *        sc_cmds -- display command summary%@NL@%
  13271. %@AB@% */%@AE@%%@NL@%
  13272. %@NL@%
  13273. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  13274. %@AI@%#include %@AE@%<local\ansi.h> %@NL@%
  13275. %@NL@%
  13276. void%@NL@%
  13277. sc_cmds(fg, bkg, bdr)%@NL@%
  13278. int fg, bkg, bdr;%@NL@%
  13279. {%@NL@%
  13280.         static char *color_xlat[] = {%@NL@%
  13281.                 "Black (0)", "Blue (1)", "Green (2)", "Cyan (3)",%@NL@%
  13282.                 "Red (4)", "Magenta (5)", "Brown (6)", "White (7)",%@NL@%
  13283.                 "Grey (8)", "Light blue (9)", "Light green (10)",%@NL@%
  13284.                 "Light cyan (11)", "Light red (12)", "Light magenta (13)",%@NL@%
  13285.                 "Yellow (14)", "Bright white (15)"%@NL@%
  13286.         };%@NL@%
  13287. %@NL@%
  13288.         ANSI_CUP(2, 29);%@NL@%
  13289.         fputs("*** SetColor (SC) ***", stdout);%@NL@%
  13290.         ANSI_CUP(4, 17);%@NL@%
  13291.         fputs("Attribute  Decrement  Increment  Current Value", stdout);%@NL@%
  13292.         ANSI_CUP(5, 17);%@NL@%
  13293.         fputs("---------  ---------  ---------  -------------", stdout);%@NL@%
  13294.         ANSI_CUP(6, 17);%@NL@%
  13295.         fputs("Foreground    F1         F2", stdout);%@NL@%
  13296.         ANSI_CUP(7, 17);%@NL@%
  13297.         fputs("Background    F3         F4", stdout);%@NL@%
  13298.         ANSI_CUP(8, 17);%@NL@%
  13299.         fputs("Border        F5         F6", stdout);%@NL@%
  13300.         ANSI_CUP(6, 50);%@NL@%
  13301.         fputs(color_xlat[fg], stdout);%@NL@%
  13302.         ANSI_CUP(7, 50);%@NL@%
  13303.         fputs(color_xlat[bkg], stdout);%@NL@%
  13304.         ANSI_CUP(8, 50);%@NL@%
  13305.         fputs(color_xlat[bdr], stdout);%@NL@%
  13306.         ANSI_CUP(10, 17);%@NL@%
  13307.         fputs("Type RETURN to exit. SetColor/Version 2.2", stdout);%@NL@%
  13308.         return;%@NL@%
  13309. }%@NL@%
  13310. %@NL@%
  13311. %@NL@%
  13312. %@2@%%@AH@%SELECT.C%@AE@%%@EH@%%@NL@%
  13313. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\SELECT.C%@AE@%%@NL@%
  13314. %@NL@%
  13315. %@AB@%/*%@NL@%
  13316. %@AB@% *        select -- functions to create a selection table and%@NL@%
  13317. %@AB@% *        to determine whether an item is an entry in the table%@NL@%
  13318. %@AB@% */%@AE@%%@NL@%
  13319. %@NL@%
  13320. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  13321. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  13322. %@AI@%#include %@AE@%<string.h> %@NL@%
  13323. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  13324. %@NL@%
  13325. %@AI@%#define %@AE@%NRANGES        10 %@NL@%
  13326. %@AI@%#define %@AE@%NDIGITS        5 %@NL@%
  13327. %@NL@%
  13328. struct slist_st {%@NL@%
  13329.         long int s_min;%@NL@%
  13330.         long int s_max;%@NL@%
  13331. } Slist[NRANGES + 1];%@NL@%
  13332. %@NL@%
  13333. long Highest = 0;%@NL@%
  13334. %@NL@%
  13335. %@AB@%/*%@NL@%
  13336. %@AB@% *        mkslist -- create the selection lookup table%@NL@%
  13337. %@AB@% */%@AE@%%@NL@%
  13338. %@NL@%
  13339. int%@NL@%
  13340. mkslist(list)%@NL@%
  13341. char *list;%@NL@%
  13342. {%@NL@%
  13343.         int i;%@NL@%
  13344.         char *listp, *s;%@NL@%
  13345.         long tmp;%@NL@%
  13346. %@NL@%
  13347.         static long save_range();%@NL@%
  13348. %@NL@%
  13349.         %@AB@%/* fill in table of selected items */%@AE@%%@NL@%
  13350.         if (*list == '\0') {%@NL@%
  13351.                 %@AB@%/* if no list, select all */%@AE@%%@NL@%
  13352.                 Slist[0].s_min = 0;%@NL@%
  13353.                 Slist[0].s_max = Highest = BIGGEST;%@NL@%
  13354.                 Slist[1].s_min = -1;%@NL@%
  13355.         }%@NL@%
  13356.         else {%@NL@%
  13357.                 listp = list;%@NL@%
  13358.                 for (i = 0; i < NRANGES; ++i) {%@NL@%
  13359.                         if ((s = strtok(listp, ", \t")) == NULL)%@NL@%
  13360.                                 break;%@NL@%
  13361.                         if ((tmp = save_range(i, s)) > Highest)%@NL@%
  13362.                                 Highest = tmp;%@NL@%
  13363.                         listp = NULL;%@NL@%
  13364.                 }%@NL@%
  13365.                 Slist[i].s_min = -1;%@NL@%
  13366.         }%@NL@%
  13367.         return (0);%@NL@%
  13368. } %@AB@%/* end mkslist() */%@AE@%%@NL@%
  13369. %@NL@%
  13370. %@AB@%/*%@NL@%
  13371. %@AB@% *        selected -- return non-zero value if the number%@NL@%
  13372. %@AB@% *        argument is a member of the selection list%@NL@%
  13373. %@AB@% */%@AE@%%@NL@%
  13374. %@NL@%
  13375. int%@NL@%
  13376. selected(n)%@NL@%
  13377. unsigned int n;%@NL@%
  13378. {%@NL@%
  13379.         int i;%@NL@%
  13380. %@NL@%
  13381.         %@AB@%/* look for converted number in selection list */%@AE@%%@NL@%
  13382.         for (i = 0; Slist[i].s_min != -1; ++i)%@NL@%
  13383.                 if (n >= Slist[i].s_min && n <= Slist[i].s_max)%@NL@%
  13384.                         return (1);%@NL@%
  13385.         return (0);%@NL@%
  13386. } %@AB@%/* end selected() */%@AE@%%@NL@%
  13387. %@NL@%
  13388. %@AB@%/*%@NL@%
  13389. %@AB@% *        save_range -- convert a string number spec to a%@NL@%
  13390. %@AB@% *        numeric range in the selection table and return%@NL@%
  13391. %@AB@% *        the highest number in the range%@NL@%
  13392. %@AB@% */%@AE@%%@NL@%
  13393. %@NL@%
  13394. static long%@NL@%
  13395. save_range(n, s)%@NL@%
  13396. int n;%@NL@%
  13397. char *s;%@NL@%
  13398. {%@NL@%
  13399.         int radix = 10;%@NL@%
  13400.         char *cp, num[NDIGITS + 1];%@NL@%
  13401. %@NL@%
  13402.         %@AB@%/* get the first (and possibly only) number */%@AE@%%@NL@%
  13403.         cp = num;%@NL@%
  13404.         while (*s != '\0' && *s != '-')%@NL@%
  13405.                 *cp++ = *s++;%@NL@%
  13406.         *cp = '\0';%@NL@%
  13407.         Slist[n].s_min = atol(num);%@NL@%
  13408.         if (*s == '\0')%@NL@%
  13409.                 %@AB@%/* pretty narrow range, huh? */%@AE@%%@NL@%
  13410.                 return (Slist[n].s_max = Slist[n].s_min);%@NL@%
  13411. %@NL@%
  13412.         %@AB@%/* get the second number */%@AE@%%@NL@%
  13413.         if (*++s == '\0')%@NL@%
  13414.                 %@AB@%/* unspecified top end of range */%@AE@%%@NL@%
  13415.                 Slist[n].s_max = BIGGEST;%@NL@%
  13416.         else {%@NL@%
  13417.                 cp = num;%@NL@%
  13418.                 while (*s != '\0' && *s != '-')%@NL@%
  13419.                         *cp++ = *s++;%@NL@%
  13420.                 *cp = '\0';%@NL@%
  13421.                 Slist[n].s_max = atol(num);%@NL@%
  13422.         }%@NL@%
  13423.         return (Slist[n].s_max);%@NL@%
  13424. } %@AB@%/* end save_range() */%@AE@%%@NL@%
  13425. %@NL@%
  13426. %@NL@%
  13427. %@2@%%@AH@%SETATTR.C%@AE@%%@EH@%%@NL@%
  13428. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\13ANSI\SETATTR.C%@AE@%%@NL@%
  13429. %@NL@%
  13430. %@AB@%/*%@NL@%
  13431. %@AB@% *        setattr -- execute an attribute update%@NL@%
  13432. %@AB@% */%@AE@%%@NL@%
  13433. %@NL@%
  13434. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  13435. %@AI@%#include %@AE@%<local\ansi.h> %@NL@%
  13436. %@AI@%#include %@AE@%<local\ibmcolor.h> %@NL@%
  13437. %@NL@%
  13438. %@AI@%#define %@AE@%C_MASK        0x7 %@NL@%
  13439. %@NL@%
  13440. void%@NL@%
  13441. setattr(pos, attr)%@NL@%
  13442. POSITION pos;        %@AB@%/* attribute position */%@AE@%%@NL@%
  13443. int attr;        %@AB@%/* composite attribute number (base attr | intensity) */%@AE@%%@NL@%
  13444. {%@NL@%
  13445.         static int ibm2ansi[] = {%@NL@%
  13446.                 ANSI_BLACK, ANSI_BLUE, ANSI_GREEN, ANSI_CYAN,%@NL@%
  13447.                 ANSI_RED, ANSI_MAGENTA, ANSI_BROWN, ANSI_WHITE%@NL@%
  13448.         };%@NL@%
  13449. %@NL@%
  13450.         switch (pos) {%@NL@%
  13451.         case FGND:%@NL@%
  13452.                 if (attr & IBM_BRIGHT)%@NL@%
  13453.                         ANSI_SGR(ANSI_BOLD);%@NL@%
  13454.                 ANSI_SGR(ibm2ansi[attr & C_MASK] + ANSI_FOREGROUND);%@NL@%
  13455.                 break;%@NL@%
  13456.         case BKGND:%@NL@%
  13457.                 if (attr & IBM_BRIGHT)%@NL@%
  13458.                         ANSI_SGR(ANSI_BLINK);%@NL@%
  13459.                 ANSI_SGR(ibm2ansi[attr & C_MASK] + ANSI_BACKGROUND);%@NL@%
  13460.                 break;%@NL@%
  13461.         case BDR:%@NL@%
  13462.                 palette(0, attr);%@NL@%
  13463.                 break;%@NL@%
  13464.         }%@NL@%
  13465.         return;%@NL@%
  13466. }%@NL@%
  13467. %@NL@%
  13468. %@NL@%
  13469. %@2@%%@AH@%SETCTYPE.C%@AE@%%@EH@%%@NL@%
  13470. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\SETCTYPE.C%@AE@%%@NL@%
  13471. %@NL@%
  13472. %@AB@%/*%@NL@%
  13473. %@AB@% *        setctype -- set the cursor start and end raster scan lines%@NL@%
  13474. %@AB@% */%@AE@%%@NL@%
  13475. %@NL@%
  13476. %@AI@%#include %@AE@%<dos.h> %@NL@%
  13477. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  13478. %@NL@%
  13479. %@AI@%#define %@AE@%LO_NIBBLE        0x0F %@NL@%
  13480. %@AI@%#define %@AE@%CURSOR_OFF        0x2 %@NL@%
  13481. %@AI@%#define %@AE@%MAXSCANLN        15 %@NL@%
  13482. %@NL@%
  13483. int%@NL@%
  13484. setctype(start, end)%@NL@%
  13485. int start;        %@AB@%/* starting raster scan line */%@AE@%%@NL@%
  13486. int end;        %@AB@%/* ending raster scan line */%@AE@%%@NL@%
  13487. {%@NL@%
  13488.         union REGS inregs, outregs;%@NL@%
  13489. %@NL@%
  13490.         inregs.h.ah = CUR_TYPE;%@NL@%
  13491.         inregs.h.ch = start & LO_NIBBLE;%@NL@%
  13492.         inregs.h.cl = end & LO_NIBBLE;%@NL@%
  13493.         if (start >= MAXSCANLN) {%@NL@%
  13494.                 inregs.h.ah |= CURSOR_OFF;%@NL@%
  13495.                 inregs.h.al = MAXSCANLN;%@NL@%
  13496.         }%@NL@%
  13497.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  13498. %@NL@%
  13499.         return (outregs.x.cflag);%@NL@%
  13500. }%@NL@%
  13501. %@NL@%
  13502. %@NL@%
  13503. %@2@%%@AH@%SETDTA.C%@AE@%%@EH@%%@NL@%
  13504. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\DOS\SETDTA.C%@AE@%%@NL@%
  13505. %@NL@%
  13506. %@AB@%/*%@NL@%
  13507. %@AB@% *        setdta -- tell DOS where to do disk transfers%@NL@%
  13508. %@AB@% */%@AE@%%@NL@%
  13509. %@NL@%
  13510. %@AI@%#include %@AE@%<dos.h> %@NL@%
  13511. %@AI@%#include %@AE@%<local\doslib.h> %@NL@%
  13512. %@NL@%
  13513. void%@NL@%
  13514. setdta(bp)%@NL@%
  13515. char *bp;        %@AB@%/* pointer to byte-aligned disk transfer area */%@AE@%%@NL@%
  13516. {%@NL@%
  13517.         union REGS inregs, outregs;%@NL@%
  13518. %@NL@%
  13519.         inregs.h.ah = SET_DTA;%@NL@%
  13520.         inregs.x.dx = (unsigned int)bp;%@NL@%
  13521.         (void)intdos(&inregs, &outregs);%@NL@%
  13522. }%@NL@%
  13523. %@NL@%
  13524. %@NL@%
  13525. %@2@%%@AH@%SETFREQ.C%@AE@%%@EH@%%@NL@%
  13526. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\SETFREQ.C%@AE@%%@NL@%
  13527. %@NL@%
  13528. %@AB@%/*%@NL@%
  13529. %@AB@% *        setfreq -- sets PC's tone generator to run%@NL@%
  13530. %@AB@% *        continuously at the specified frequency%@NL@%
  13531. %@AB@% */%@AE@%%@NL@%
  13532. %@NL@%
  13533. %@AI@%#include %@AE@%<conio.h> %@NL@%
  13534. %@AI@%#include %@AE@%<local\timer.h> %@NL@%
  13535. %@NL@%
  13536. void%@NL@%
  13537. setfreq(f)%@NL@%
  13538. unsigned f;        %@AB@%/* frequency in Hertz (approximate) */%@AE@%%@NL@%
  13539. {%@NL@%
  13540.         unsigned divisor = TIMER_CLK / f;%@NL@%
  13541. %@NL@%
  13542.         outp(TIMER_CTRL, TIMER_PREP);                %@AB@%/* prepare timer */%@AE@%%@NL@%
  13543.         outp(TIMER_COUNT, (divisor & 0xFF));        %@AB@%/* low byte of divisor */%@AE@%%@NL@%
  13544.         outp(TIMER_COUNT, (divisor >> 8));        %@AB@%/* high byte of divisor */%@AE@%%@NL@%
  13545. }%@NL@%
  13546. %@NL@%
  13547. %@NL@%
  13548. %@2@%%@AH@%SETMYDIR.C%@AE@%%@EH@%%@NL@%
  13549. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\03DOS\SETMYDIR.C%@AE@%%@NL@%
  13550. %@NL@%
  13551. %@AB@%/*%@NL@%
  13552. %@AB@% *        setmydir -- try to change the DOS environment%@NL@%
  13553. %@AB@% */%@AE@%%@NL@%
  13554. %@NL@%
  13555. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  13556. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  13557. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  13558. %@NL@%
  13559. main(argc, argv)%@NL@%
  13560. int argc;%@NL@%
  13561. char **argv;%@NL@%
  13562. {%@NL@%
  13563.         register char **p;%@NL@%
  13564.         static char var[] = { "MYDIR" };%@NL@%
  13565.         static char pgm[MAXNAME + 1] = { "setmydir" };%@NL@%
  13566.         extern void fatal(char *, char *, int);%@NL@%
  13567.         extern void getpname(char *, char *);%@NL@%
  13568. %@NL@%
  13569.         %@AB@%/* use an alias if one is given to this program */%@AE@%%@NL@%
  13570.         if (_osmajor >= 3)%@NL@%
  13571.                 getpname(*argv, pgm);%@NL@%
  13572. %@NL@%
  13573.         %@AB@%/* try to add the MYDIR variable to the environment */%@AE@%%@NL@%
  13574.         if (putenv("MYDIR=c:\\mydir") == -1)%@NL@%
  13575.                 fatal(pgm, "Error changing environment", 1);%@NL@%
  13576. %@NL@%
  13577.         %@AB@%/* display the environment for this process */%@AE@%%@NL@%
  13578.         for (p = environ; *p; p++) {%@NL@%
  13579.                 printf("%s\n", *p);%@NL@%
  13580.         }%@NL@%
  13581.         %@NL@%
  13582.         exit(0);%@NL@%
  13583. }%@NL@%
  13584. %@NL@%
  13585. %@NL@%
  13586. %@2@%%@AH@%SETPAGE.C%@AE@%%@EH@%%@NL@%
  13587. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\SETPAGE.C%@AE@%%@NL@%
  13588. %@NL@%
  13589. %@AB@%/*%@NL@%
  13590. %@AB@% *        setpage -- select "visual" screen page for viewing%@NL@%
  13591. %@AB@% *        (the "active" page is the one being written to)%@NL@%
  13592. %@AB@% */%@AE@%%@NL@%
  13593. %@NL@%
  13594. %@AI@%#include %@AE@%<dos.h> %@NL@%
  13595. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  13596. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  13597. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  13598. %@NL@%
  13599. int%@NL@%
  13600. setpage(pg)%@NL@%
  13601. int pg;        %@AB@%/* visual screen page number */%@AE@%%@NL@%
  13602. {%@NL@%
  13603.         union REGS inregs, outregs;%@NL@%
  13604. %@NL@%
  13605.         %@AB@%/* check page number against table */%@AE@%%@NL@%
  13606.         if (Maxpage[Vmode] > 0 && (pg < 0 || pg >= Maxpage[Vmode]))%@NL@%
  13607.                 return (-1);%@NL@%
  13608. %@NL@%
  13609.         %@AB@%/* change the visual page */%@AE@%%@NL@%
  13610.         inregs.h.ah = SET_PAGE;%@NL@%
  13611.         inregs.h.al = pg;%@NL@%
  13612.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  13613. %@NL@%
  13614.         return (outregs.x.cflag);%@NL@%
  13615. }%@NL@%
  13616. %@NL@%
  13617. %@NL@%
  13618. %@2@%%@AH@%SETVMODE.C%@AE@%%@EH@%%@NL@%
  13619. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\SETVMODE.C%@AE@%%@NL@%
  13620. %@NL@%
  13621. %@AB@%/*%@NL@%
  13622. %@AB@% *        setvmode -- set the video mode%@NL@%
  13623. %@AB@% *        (color/graphics systems only)%@NL@%
  13624. %@AB@% */%@AE@%%@NL@%
  13625. %@NL@%
  13626. %@AI@%#include %@AE@%<dos.h> %@NL@%
  13627. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  13628. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  13629. %@NL@%
  13630. %@AB@%/***********************************************************%@NL@%
  13631. %@AB@%*%@NL@%
  13632. %@AB@%* mode # (m)        description%@NL@%
  13633. %@AB@%* ------------- ---------------------------------%@NL@%
  13634. %@AB@%* PC MODES:%@NL@%
  13635. %@AB@%*         0        40x25 Mono text        (c/g default)%@NL@%
  13636. %@AB@%*         1        40x25 Color text%@NL@%
  13637. %@AB@%*        2        80x25 Mono text%@NL@%
  13638. %@AB@%*        3        80x25 Color text%@NL@%
  13639. %@AB@%*        4        320x200 4-color graphics (med res)%@NL@%
  13640. %@AB@%*        5        320x200 Mono graphics (med res)%@NL@%
  13641. %@AB@%*        6        640x200 2-color graphics (hi res)%@NL@%
  13642. %@AB@%*        7        80x25 on monochrome adapter %@NL@%
  13643. %@AB@%*%@NL@%
  13644. %@AB@%* PCjr MODES:%@NL@%
  13645. %@AB@%*        8        160x200 16-color graphics%@NL@%
  13646. %@AB@%*        9        320x200 16-color graphics%@NL@%
  13647. %@AB@%*        10        640x200 4-color fraphics%@NL@%
  13648. %@AB@%*%@NL@%
  13649. %@AB@%* EGA MODES:%@NL@%
  13650. %@AB@%*        13        320x200 16-color graphics%@NL@%
  13651. %@AB@%*        14        620x200 16-color graphics%@NL@%
  13652. %@AB@%*        15        640x350 mono graphics%@NL@%
  13653. %@AB@%*        16        640x350 color graphics (4- or 16-color)%@NL@%
  13654. %@AB@%***********************************************************/%@AE@%%@NL@%
  13655. %@NL@%
  13656. int%@NL@%
  13657. setvmode(vmode)%@NL@%
  13658. unsigned int vmode;        %@AB@%/* user-specified mode number */%@AE@%%@NL@%
  13659. {%@NL@%
  13660.         union REGS inregs, outregs;%@NL@%
  13661. %@NL@%
  13662.         inregs.h.ah = SET_MODE;%@NL@%
  13663.         inregs.h.al = vmode;        %@AB@%/* value not checked */%@AE@%%@NL@%
  13664.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  13665. %@NL@%
  13666.         %@AB@%/* update video structure */%@AE@%%@NL@%
  13667.         getstate();%@NL@%
  13668. %@NL@%
  13669.         return (outregs.x.cflag);%@NL@%
  13670. }%@NL@%
  13671. %@NL@%
  13672. %@NL@%
  13673. %@2@%%@AH@%SHOW.C%@AE@%%@EH@%%@NL@%
  13674. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\10DUMP\SHOW.C%@AE@%%@NL@%
  13675. %@NL@%
  13676. %@AB@%/*%@NL@%
  13677. %@AB@% *        show -- a filter that displays the contents of a file%@NL@%
  13678. %@AB@% *        in a way that is guaranteed to be displayable%@NL@%
  13679. %@AB@% */%@AE@%%@NL@%
  13680. %@NL@%
  13681. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  13682. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  13683. %@AI@%#include %@AE@%<fcntl.h> %@NL@%
  13684. %@AI@%#include %@AE@%<io.h> %@NL@%
  13685. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  13686. %@NL@%
  13687. main(argc, argv)%@NL@%
  13688. int argc;%@NL@%
  13689. char **argv;%@NL@%
  13690. {%@NL@%
  13691.         int ch;%@NL@%
  13692.         FILE *fp;%@NL@%
  13693.         BOOLEAN sflag = FALSE;        %@AB@%/* strip non-ASCII characters */%@AE@%%@NL@%
  13694.         BOOLEAN vflag = FALSE;        %@AB@%/* verbose mode */%@AE@%%@NL@%
  13695.         BOOLEAN wflag = FALSE;        %@AB@%/* filter typical word processing codes */%@AE@%%@NL@%
  13696.         BOOLEAN errflag = FALSE;%@NL@%
  13697.         static char pgm[] = { "show" };%@NL@%
  13698. %@NL@%
  13699.         extern int getopt(int, char **, char *);%@NL@%
  13700.         extern char *optarg;%@NL@%
  13701.         extern int optind, opterr;%@NL@%
  13702.         extern int showit(FILE *, BOOLEAN, BOOLEAN);%@NL@%
  13703.         extern void fatal(char *, char *, int);%@NL@%
  13704. %@NL@%
  13705.         if (_osmajor >= 3)%@NL@%
  13706.                 getpname(*argv, pgm);%@NL@%
  13707. %@NL@%
  13708.         while ((ch = getopt(argc, argv, "svw")) != EOF) {%@NL@%
  13709.                 switch (ch) {%@NL@%
  13710.                 case 's': %@AB@%/* strip non-ASCII characters */%@AE@%%@NL@%
  13711.                         sflag = TRUE;%@NL@%
  13712.                         break;%@NL@%
  13713.                 case 'v': %@AB@%/* verbose */%@AE@%%@NL@%
  13714.                         vflag = TRUE;%@NL@%
  13715.                         break;%@NL@%
  13716.                 case 'w': %@AB@%/* use word-processing conventions */%@AE@%%@NL@%
  13717.                         wflag = sflag = TRUE;%@NL@%
  13718.                         break;%@NL@%
  13719.                 case '?':%@NL@%
  13720.                         errflag = TRUE;%@NL@%
  13721.                         break;%@NL@%
  13722.                 }%@NL@%
  13723.         }%@NL@%
  13724. %@NL@%
  13725.         %@AB@%/* check for errors */%@AE@%%@NL@%
  13726.         if (errflag == TRUE) {%@NL@%
  13727.                 fprintf(stderr, "Usage: %s [-sv] [file...]\n", pgm);%@NL@%
  13728.                 exit(1);%@NL@%
  13729.         }%@NL@%
  13730. %@NL@%
  13731.         %@AB@%/* if no file names, use standard input */%@AE@%%@NL@%
  13732.         if (optind == argc) {%@NL@%
  13733.                 if (setmode(fileno(stdin), O_BINARY) == -1)%@NL@%
  13734.                         fatal(pgm, "Cannot set binary mode on stdin", 2);%@NL@%
  13735.                 showit(stdin, sflag, wflag);%@NL@%
  13736.                 exit(0);%@NL@%
  13737.         }%@NL@%
  13738. %@NL@%
  13739.         %@AB@%/* otherwise, process remainder of command line */%@AE@%%@NL@%
  13740.         for ( ; optind < argc; ++optind) {%@NL@%
  13741.                 if ((fp = fopen(argv[optind], "rb")) == NULL) {%@NL@%
  13742.                         fprintf(stderr,%@NL@%
  13743.                                 "%s: Error opening %s\n", pgm, argv[optind]);%@NL@%
  13744.                         perror("");%@NL@%
  13745.                         continue;%@NL@%
  13746.                 }%@NL@%
  13747.                 if (vflag == TRUE)%@NL@%
  13748.                         fprintf(stdout, "\n%s:\n", argv[optind]);%@NL@%
  13749.                 if (showit(fp, sflag, wflag) != 0) {%@NL@%
  13750.                         fprintf(stderr,%@NL@%
  13751.                                 "%s: Error reading %s\n", pgm, argv[optind]);%@NL@%
  13752.                         perror("");%@NL@%
  13753.                 }%@NL@%
  13754.                 if (fclose(fp) == EOF)%@NL@%
  13755.                         fatal(pgm, "Error closing input file", 2);%@NL@%
  13756.         }%@NL@%
  13757.         %@NL@%
  13758.         exit(0);%@NL@%
  13759. }%@NL@%
  13760. %@NL@%
  13761. %@NL@%
  13762. %@2@%%@AH@%SHOWENV.C%@AE@%%@EH@%%@NL@%
  13763. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\03DOS\SHOWENV.C%@AE@%%@NL@%
  13764. %@NL@%
  13765. %@AB@%/*%@NL@%
  13766. %@AB@% *        showenv        -- display the values of any DOS variables%@NL@%
  13767. %@AB@% *        named on the invocation command line%@NL@%
  13768. %@AB@% */%@AE@%%@NL@%
  13769. %@NL@%
  13770. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  13771. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  13772. %@AI@%#include %@AE@%<string.h> %@NL@%
  13773. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  13774. %@NL@%
  13775. main(argc, argv, envp)%@NL@%
  13776. int argc;%@NL@%
  13777. char **argv;%@NL@%
  13778. char **envp;%@NL@%
  13779. {%@NL@%
  13780.         register char *ep;%@NL@%
  13781.         static char pgm[MAXNAME + 1] = { "showenv" };%@NL@%
  13782.         extern void getpname(char *, char *);%@NL@%
  13783. %@NL@%
  13784.         %@AB@%/* use an alias if one is given to this program */%@AE@%%@NL@%
  13785.         if (_osmajor >= 3)%@NL@%
  13786.                 getpname(*argv, pgm);%@NL@%
  13787. %@NL@%
  13788.         %@AB@%/* if no arguments, show full environment list */%@AE@%%@NL@%
  13789.         if (argc == 1)%@NL@%
  13790.                 for (; *envp; ++envp)%@NL@%
  13791.                         printf("%s\n", *envp);%@NL@%
  13792.         else {%@NL@%
  13793.                 %@AB@%/*%@NL@%
  13794. %@AB@%                 *  treat all args as DOS variable names and%@NL@%
  13795. %@AB@%                 *  display values of only specified variables%@NL@%
  13796. %@AB@%                 */%@AE@%%@NL@%
  13797.                 ++argv;        %@AB@%/* skip past command name */%@AE@%%@NL@%
  13798.                 --argc;%@NL@%
  13799.                 while (argc > 0) {%@NL@%
  13800.                         if ((ep = getenv(strupr(*argv))) == NULL)%@NL@%
  13801.                                 fprintf(stderr, "%s not defined\n", *argv);%@NL@%
  13802.                         else%@NL@%
  13803.                                 printf("%s=%s\n", *argv, ep);%@NL@%
  13804.                         ++argv;%@NL@%
  13805.                         --argc;%@NL@%
  13806.                 }%@NL@%
  13807.         }%@NL@%
  13808.         exit(0);%@NL@%
  13809. }%@NL@%
  13810. %@NL@%
  13811. %@NL@%
  13812. %@2@%%@AH@%SHOWIT.C%@AE@%%@EH@%%@NL@%
  13813. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\10DUMP\SHOWIT.C%@AE@%%@NL@%
  13814. %@NL@%
  13815. %@AB@%/*%@NL@%
  13816. %@AB@% *        showit -- make non-printable characters in%@NL@%
  13817. %@AB@% *        the stream fp visible (or optionally strip them)%@NL@%
  13818. %@AB@% */%@AE@%%@NL@%
  13819. %@NL@%
  13820. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  13821. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  13822. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  13823. %@NL@%
  13824. int%@NL@%
  13825. showit(fp, strip, wp)%@NL@%
  13826. FILE *fp;%@NL@%
  13827. BOOLEAN strip;        %@AB@%/* strip non-ASCII codes */%@AE@%%@NL@%
  13828. BOOLEAN wp;        %@AB@%/* filter typical word processing codes */%@AE@%%@NL@%
  13829. {%@NL@%
  13830.         int ch;%@NL@%
  13831.         %@NL@%
  13832.         clearerr(fp);%@NL@%
  13833.         while ((ch = getc(fp)) != EOF)%@NL@%
  13834.                 if (isascii(ch) && (isprint(ch) || isspace(ch)))%@NL@%
  13835.                         switch (ch) {%@NL@%
  13836.                         case '\r':%@NL@%
  13837.                                 if (wp == TRUE) {%@NL@%
  13838.                                         if ((ch = getc(fp)) != '\n')%@NL@%
  13839.                                                 ungetc(ch, fp);%@NL@%
  13840.                                         putchar('\r');%@NL@%
  13841.                                         putchar('\n');%@NL@%
  13842.                                 }%@NL@%
  13843.                                 else%@NL@%
  13844.                                         putchar(ch);%@NL@%
  13845.                                 break;%@NL@%
  13846.                         default:%@NL@%
  13847.                                 putchar(ch);%@NL@%
  13848.                                 break;%@NL@%
  13849.                         }%@NL@%
  13850.                 else if (strip == FALSE)%@NL@%
  13851.                         printf("\\%02X", ch);%@NL@%
  13852.                 else if (wp == TRUE && isprint(ch & ASCII))%@NL@%
  13853.                         putchar(ch & ASCII);%@NL@%
  13854.         return (ferror(fp));%@NL@%
  13855. }%@NL@%
  13856. %@NL@%
  13857. %@NL@%
  13858. %@2@%%@AH@%SHOWTABS.C%@AE@%%@EH@%%@NL@%
  13859. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\07CONFIG\SHOWTABS.C%@AE@%%@NL@%
  13860. %@NL@%
  13861. %@AB@%/*%@NL@%
  13862. %@AB@% *        showtabs -- graphically display tabstop settings%@NL@%
  13863. %@AB@% */%@AE@%%@NL@%
  13864. %@NL@%
  13865. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  13866. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  13867. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  13868. %@NL@%
  13869. %@AI@%#define %@AE@%MAXCOL          80 %@NL@%
  13870. %@AI@%#define %@AE@%TABWIDTH  8 %@NL@%
  13871. %@NL@%
  13872. extern long Highest;%@NL@%
  13873. %@NL@%
  13874. main(argc, argv)%@NL@%
  13875. int argc;%@NL@%
  13876. char *argv[];%@NL@%
  13877. {%@NL@%
  13878.         int ch, i;%@NL@%
  13879.         int interval, tablist[MAXLINE + 1], *p;%@NL@%
  13880.         char *tabstr;%@NL@%
  13881.         BOOLEAN errflag, fflag, vflag;%@NL@%
  13882.         static char pgm[MAXNAME + 1] = { "showtabs" };%@NL@%
  13883. %@NL@%
  13884.         extern char *getpname(char *, char *);%@NL@%
  13885.         extern int getopt(int, char **, char *);%@NL@%
  13886.         extern char *optarg;%@NL@%
  13887.         extern int optind, opterr;%@NL@%
  13888.         extern int mkslist(char *);%@NL@%
  13889.         extern int selected(long);%@NL@%
  13890.         extern void fixtabs(int);%@NL@%
  13891.         extern void vartabs(int *);%@NL@%
  13892.         extern int tabstop(int);%@NL@%
  13893. %@NL@%
  13894.         if (_osmajor >= 3)%@NL@%
  13895.                 getpname(*argv, pgm);%@NL@%
  13896. %@NL@%
  13897.         %@AB@%/* process command-line options */%@AE@%%@NL@%
  13898.         errflag = fflag = vflag = FALSE;%@NL@%
  13899.         interval = 0;%@NL@%
  13900.         while ((ch = getopt(argc, argv, "f:v:")) != EOF) {%@NL@%
  13901.                 switch (ch) {%@NL@%
  13902.                 case 'f':%@NL@%
  13903.                         %@AB@%/* used fixed tabbing interval */%@AE@%%@NL@%
  13904.                         if (vflag == FALSE) {%@NL@%
  13905.                                 fflag = TRUE;%@NL@%
  13906.                                 interval = atoi(optarg);%@NL@%
  13907.                         }%@NL@%
  13908.                         break;%@NL@%
  13909.                 case 'v':%@NL@%
  13910.                         %@AB@%/* use list of tabs */%@AE@%%@NL@%
  13911.                         if (fflag == FALSE) {%@NL@%
  13912.                                 vflag = TRUE;%@NL@%
  13913.                                 strcpy(tabstr, optarg);%@NL@%
  13914.                         }%@NL@%
  13915.                         break;%@NL@%
  13916.                 case '?':%@NL@%
  13917.                         errflag = TRUE;%@NL@%
  13918.                         break;%@NL@%
  13919.                 }%@NL@%
  13920.         }%@NL@%
  13921.         if (errflag == TRUE) {%@NL@%
  13922.                 fprintf(stderr, "Usage: %s [-f interval | -v tablist]\n", pgm);%@NL@%
  13923.                 exit(2);%@NL@%
  13924.         }%@NL@%
  13925. %@NL@%
  13926.         %@AB@%/* set the tabstops */%@AE@%%@NL@%
  13927.         if (vflag == TRUE) {%@NL@%
  13928.                 %@AB@%/* user-supplied variable tab list */%@AE@%%@NL@%
  13929.                 mkslist(tabstr);%@NL@%
  13930.                 p = tablist;%@NL@%
  13931.                 for (i = 0; i < MAXLINE && i < Highest; ++i)%@NL@%
  13932.                         *p++ = selected((long)i + 1) ? i : 0;%@NL@%
  13933.                 *p = -1;        %@AB@%/* terminate the list */%@AE@%%@NL@%
  13934.                 vartabs(tablist);%@NL@%
  13935.         }%@NL@%
  13936.         else if (fflag == TRUE)%@NL@%
  13937.                 %@AB@%/* user-supplied fixed tabbing interval */%@AE@%%@NL@%
  13938.                 fixtabs(interval);%@NL@%
  13939.         else%@NL@%
  13940.                 %@AB@%/* hardware default tabbing interval*/%@AE@%%@NL@%
  13941.                 fixtabs(TABWIDTH);%@NL@%
  13942. %@NL@%
  13943.         %@AB@%/* display current tabs settings */%@AE@%%@NL@%
  13944.         for (i = 0; i < MAXCOL; ++i)%@NL@%
  13945.                 if (tabstop(i))%@NL@%
  13946.                         fputc('T', stdout);%@NL@%
  13947.                 else if ((i + 1) % 10 == 0)%@NL@%
  13948.                         fputc('+', stdout);%@NL@%
  13949.                 else%@NL@%
  13950.                         fputc('-', stdout);%@NL@%
  13951.         fputc('\n', stdout);%@NL@%
  13952. %@NL@%
  13953.         exit(0);%@NL@%
  13954. }%@NL@%
  13955. %@NL@%
  13956. %@NL@%
  13957. %@2@%%@AH@%SOUND.C%@AE@%%@EH@%%@NL@%
  13958. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\06USER\SOUND.C%@AE@%%@NL@%
  13959. %@NL@%
  13960. %@AB@%/*%@NL@%
  13961. %@AB@% *        sound -- produce a constant tone for a specified duration%@NL@%
  13962. %@AB@% */%@AE@%%@NL@%
  13963. %@NL@%
  13964. %@AI@%#include %@AE@%<local\sound.h> %@NL@%
  13965. %@NL@%
  13966. void%@NL@%
  13967. sound(f, dur)%@NL@%
  13968. unsigned int f;        %@AB@%/* frequency of pitch in hertz */%@AE@%%@NL@%
  13969. float dur;        %@AB@%/* in seconds and tenths of seconds */%@AE@%%@NL@%
  13970. {%@NL@%
  13971.         extern void setfreq(unsigned int);%@NL@%
  13972.         extern void delay(float);%@NL@%
  13973.         %@NL@%
  13974.         %@AB@%/* set frequency in hertz */%@AE@%%@NL@%
  13975.         setfreq(f);%@NL@%
  13976.         %@NL@%
  13977.         %@AB@%/* turn the speaker on for specified duration */%@AE@%%@NL@%
  13978.         SPKR_ON;%@NL@%
  13979.         delay(dur);%@NL@%
  13980.         SPKR_OFF;%@NL@%
  13981. }%@NL@%
  13982. %@NL@%
  13983. %@NL@%
  13984. %@2@%%@AH@%SOUNDS.C%@AE@%%@EH@%%@NL@%
  13985. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\06USER\SOUNDS.C%@AE@%%@NL@%
  13986. %@NL@%
  13987. %@AB@%/*%@NL@%
  13988. %@AB@% *        sounds -- make various sounds on demand%@NL@%
  13989. %@AB@% */%@AE@%%@NL@%
  13990. %@NL@%
  13991. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  13992. %@AI@%#include %@AE@%<conio.h> %@NL@%
  13993. %@AI@%#include %@AE@%<math.h> %@NL@%
  13994. %@NL@%
  13995. %@AI@%#define %@AE@%ESC        27 %@NL@%
  13996. %@NL@%
  13997. extern void sound(unsigned int, float);%@NL@%
  13998. %@NL@%
  13999. main()%@NL@%
  14000. {%@NL@%
  14001.         int ch;%@NL@%
  14002. %@NL@%
  14003.         fprintf(stderr, "1=warble 2=error 3=confirm 4=warn\n");%@NL@%
  14004.         fprintf(stderr, "Esc=quit\n");%@NL@%
  14005.         while ((ch = getch()) != ESC)%@NL@%
  14006.                 switch (ch) {%@NL@%
  14007.                 case '1':%@NL@%
  14008.                         warble();%@NL@%
  14009.                         break;%@NL@%
  14010.                 case '2':%@NL@%
  14011.                         error();%@NL@%
  14012.                         break;%@NL@%
  14013.                 case '3':%@NL@%
  14014.                         confirm();%@NL@%
  14015.                         break;%@NL@%
  14016.                 case '4':%@NL@%
  14017.                         warn();%@NL@%
  14018.                         break;%@NL@%
  14019.                 }%@NL@%
  14020.         exit(0);%@NL@%
  14021. }%@NL@%
  14022. %@NL@%
  14023. %@AI@%#define %@AE@%CYCLES        3 %@NL@%
  14024. %@AI@%#define %@AE@%LOTONE        600 %@NL@%
  14025. %@AI@%#define %@AE@%HITONE        1200 %@NL@%
  14026. %@AI@%#define %@AE@%PERIOD        0.1 %@NL@%
  14027. %@NL@%
  14028. warble()%@NL@%
  14029. {%@NL@%
  14030.         int i;%@NL@%
  14031. %@NL@%
  14032.         for (i = 0; i < 2 * CYCLES; ++i)%@NL@%
  14033.                 if (i % 2)%@NL@%
  14034.                         sound(LOTONE, PERIOD);%@NL@%
  14035.                 else%@NL@%
  14036.                         sound(HITONE, PERIOD);%@NL@%
  14037. }%@NL@%
  14038. %@NL@%
  14039. error()%@NL@%
  14040. {%@NL@%
  14041.         float d = 0.15;%@NL@%
  14042. %@NL@%
  14043.         sound(440, d);%@NL@%
  14044.         sound(220, d);%@NL@%
  14045. }%@NL@%
  14046. %@NL@%
  14047. confirm()%@NL@%
  14048. {%@NL@%
  14049.         float d = 0.15;%@NL@%
  14050. %@NL@%
  14051.         sound(440, d);%@NL@%
  14052.         sound(880, d);%@NL@%
  14053. }%@NL@%
  14054. %@NL@%
  14055. warn()%@NL@%
  14056. {%@NL@%
  14057.         float d = 0.2;%@NL@%
  14058. %@NL@%
  14059.         sound(100, d);%@NL@%
  14060. }%@NL@%
  14061. %@NL@%
  14062. %@NL@%
  14063. %@2@%%@AH@%SPACES.C%@AE@%%@EH@%%@NL@%
  14064. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\SPACES.C%@AE@%%@NL@%
  14065. %@NL@%
  14066. %@AB@%/*%@NL@%
  14067. %@AB@% *        spaces -- send spaces (blanks) to the output stream%@NL@%
  14068. %@AB@% */%@AE@%%@NL@%
  14069. %@NL@%
  14070. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14071. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  14072. %@NL@%
  14073. int%@NL@%
  14074. spaces(n, fp)%@NL@%
  14075. int n;%@NL@%
  14076. FILE *fp;%@NL@%
  14077. {%@NL@%
  14078.         register int i;%@NL@%
  14079. %@NL@%
  14080.         for (i = 0; i < n; ++i)%@NL@%
  14081.                 if (putc(' ', fp) == EOF && ferror(fp))%@NL@%
  14082.                         break;%@NL@%
  14083. %@NL@%
  14084.         %@AB@%/* return number of spaces emitted */%@AE@%%@NL@%
  14085.         return (i);%@NL@%
  14086. }%@NL@%
  14087. %@NL@%
  14088. %@NL@%
  14089. %@2@%%@AH@%SPKR.C%@AE@%%@EH@%%@NL@%
  14090. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\06USER\SPKR.C%@AE@%%@NL@%
  14091. %@NL@%
  14092. %@AB@%/*%@NL@%
  14093. %@AB@% *        spkr -- turn speaker ON/OFF%@NL@%
  14094. %@AB@% *%@NL@%
  14095. %@AB@% *                no args => OFF%@NL@%
  14096. %@AB@% *                any arg(s) => ON%@NL@%
  14097. %@AB@% */%@AE@%%@NL@%
  14098. %@NL@%
  14099. %@AI@%#include %@AE@%<local\sound.h> %@NL@%
  14100. %@NL@%
  14101. main(argc, argv)%@NL@%
  14102. int argc;%@NL@%
  14103. char **argv;%@NL@%
  14104. {%@NL@%
  14105.         %@AB@%/* turn speaker on or off */%@AE@%%@NL@%
  14106.         if (argc == 1)%@NL@%
  14107.                 SPKR_OFF;%@NL@%
  14108.         else%@NL@%
  14109.                 SPKR_ON;%@NL@%
  14110.         exit(0);%@NL@%
  14111. }%@NL@%
  14112. %@NL@%
  14113. %@NL@%
  14114. %@2@%%@AH@%ST.C%@AE@%%@EH@%%@NL@%
  14115. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\11SCREEN\ST.C%@AE@%%@NL@%
  14116. %@NL@%
  14117. %@AB@%/*%@NL@%
  14118. %@AB@% *        st -- screen test using cpblk function%@NL@%
  14119. %@AB@% */%@AE@%%@NL@%
  14120. %@NL@%
  14121. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14122. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  14123. %@AI@%#include %@AE@%<conio.h> %@NL@%
  14124. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  14125. %@AI@%#include %@AE@%<dos.h> %@NL@%
  14126. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  14127. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  14128. %@NL@%
  14129. %@AI@%#define %@AE@%CG_SEG                0xB800 %@NL@%
  14130. %@AI@%#define %@AE@%MONO_SEG        0xB000 %@NL@%
  14131. %@AI@%#define %@AE@%NBYTES                0x1000 %@NL@%
  14132. %@AI@%#define %@AE@%PAGESIZ                (NBYTES / 2) %@NL@%
  14133. %@AI@%#define %@AE@%PG0_OS                0 %@NL@%
  14134. %@AI@%#define %@AE@%PG1_OS                PG0_OS + NBYTES %@NL@%
  14135. %@AI@%#define %@AE@%ESC                27 %@NL@%
  14136. %@AI@%#define %@AE@%MAXSCAN                14 %@NL@%
  14137. %@NL@%
  14138. main(argc, argv)%@NL@%
  14139. int argc;%@NL@%
  14140. char *argv[];%@NL@%
  14141. {%@NL@%
  14142.         int i;%@NL@%
  14143.         int k;                                %@AB@%/* user command character */%@AE@%%@NL@%
  14144.         int ca;                                %@AB@%/* character/attribute word */%@AE@%%@NL@%
  14145.         int ch;                                %@AB@%/* character value read */%@AE@%%@NL@%
  14146.         int row, col;                        %@AB@%/* cursor position upon entry */%@AE@%%@NL@%
  14147.         int c_start, c_end;                %@AB@%/* cursor scan lines */%@AE@%%@NL@%
  14148.         unsigned char attr;                %@AB@%/* saved video attribute */%@AE@%%@NL@%
  14149.         unsigned dseg;                        %@AB@%/* destination buffer segment */%@AE@%%@NL@%
  14150.         unsigned os;                        %@AB@%/* page offset in bytes */%@AE@%%@NL@%
  14151.         static unsigned sbuf[NBYTES];        %@AB@%/* screen buffer */%@AE@%%@NL@%
  14152.         unsigned *bp;                        %@AB@%/* screen element pointer */%@AE@%%@NL@%
  14153.         unsigned sseg;                        %@AB@%/* source segment */%@AE@%%@NL@%
  14154.         int special;                        %@AB@%/* use special copy routine */%@AE@%%@NL@%
  14155.         int apg, vpg;                        %@AB@%/* active and visual display pages */%@AE@%%@NL@%
  14156. %@NL@%
  14157.         %@AB@%/* segment register values */%@AE@%%@NL@%
  14158.         struct SREGS segregs;%@NL@%
  14159. %@NL@%
  14160.         extern void swap_int(int *, int *);%@NL@%
  14161. %@NL@%
  14162.         static char pgm[] = { "st" };        %@AB@%/* program name */%@AE@%%@NL@%
  14163. %@NL@%
  14164.         %@AB@%/* save user's current video state */%@AE@%%@NL@%
  14165.         getstate();%@NL@%
  14166.         readcur(&row, &col, Vpage);%@NL@%
  14167.         putcur(row - 1, 0, Vpage);%@NL@%
  14168.         readca(&ch, &attr, Vpage);%@NL@%
  14169.         getctype(&c_start, &c_end, Vpage);%@NL@%
  14170.         setctype(MAXSCAN, c_end);%@NL@%
  14171.         clrscrn(attr);%@NL@%
  14172.         putcur(1, 1, Vpage);%@NL@%
  14173. %@NL@%
  14174.         %@AB@%/* initialize destination segment */%@AE@%%@NL@%
  14175.         special = 1;%@NL@%
  14176.         fputs("ScreenTest (ST): ", stderr);%@NL@%
  14177.         if (Vmode == CGA_C80 || Vmode == CGA_M80) {%@NL@%
  14178.                 dseg = CG_SEG;%@NL@%
  14179.                 fprintf(stderr, "Using CGA mode %d", Vmode);%@NL@%
  14180.         }%@NL@%
  14181.         else if (Vmode == MDA_M80) {%@NL@%
  14182.                 dseg = MONO_SEG;%@NL@%
  14183.                 fprintf(stderr, "Using MDA (mode %d)", Vmode);%@NL@%
  14184.                 special = 0;%@NL@%
  14185.         } else%@NL@%
  14186.                 fprintf(stderr, "%s: Requires 80-column text mode\n", pgm);%@NL@%
  14187. %@NL@%
  14188.         %@AB@%/* process command-line arguments */%@AE@%%@NL@%
  14189.         if (argc > 2) {%@NL@%
  14190.                 fprintf(stderr, "Usage: %s [x]\n", pgm);%@NL@%
  14191.                 exit(2);%@NL@%
  14192.         }%@NL@%
  14193.         else if (argc == 2)%@NL@%
  14194.                 special = 0;        %@AB@%/* bypass special block move */%@AE@%%@NL@%
  14195. %@NL@%
  14196.         %@AB@%/* get data segment value */%@AE@%%@NL@%
  14197.         segread(&segregs);%@NL@%
  14198.         sseg = segregs.ds;%@NL@%
  14199. %@NL@%
  14200.         %@AB@%/* set up "active" and "visual" display pages */%@AE@%%@NL@%
  14201.         apg = 1;        %@AB@%/* page being written to */%@AE@%%@NL@%
  14202.         vpg = 0;        %@AB@%/* page being viewed */%@AE@%%@NL@%
  14203. %@NL@%
  14204.         %@AB@%/* display buffers on the user's command */%@AE@%%@NL@%
  14205.         fprintf(stderr, " -- Type printable text; Esc=exit");%@NL@%
  14206.         while ((k = getkey()) != ESC) {%@NL@%
  14207.                 if (isascii(k) && isprint(k)) {%@NL@%
  14208.                         %@AB@%/* fill the buffer */%@AE@%%@NL@%
  14209.                         ca = ((k % 0xEF) << 8) | k;%@NL@%
  14210.                         for (bp = sbuf; bp - sbuf < PAGESIZ; ++bp)%@NL@%
  14211.                                 *bp = ca;%@NL@%
  14212.                         if (Vmode == MDA_M80)%@NL@%
  14213.                                 os = 0;%@NL@%
  14214.                         else%@NL@%
  14215.                                 os = (apg == 0) ? PG0_OS : PG1_OS;%@NL@%
  14216.                         if (special)%@NL@%
  14217.                                 cpblk(sbuf, sseg, os, dseg);%@NL@%
  14218.                         else%@NL@%
  14219.                                 movedata(sseg, sbuf, dseg, os, NBYTES);%@NL@%
  14220.                         if (Vmode != MDA_M80) {%@NL@%
  14221.                                 swap_int(&apg, &vpg);%@NL@%
  14222.                                 setpage(vpg);%@NL@%
  14223.                         }%@NL@%
  14224.                 }%@NL@%
  14225.                 else {%@NL@%
  14226.                         clrscrn(attr);%@NL@%
  14227.                         putcur(0, 0, Vpage);%@NL@%
  14228.                         writestr(" Type printable text; Esc = exit ", vpg);%@NL@%
  14229.                 }%@NL@%
  14230.         }%@NL@%
  14231. %@NL@%
  14232.         %@AB@%/* restore user's video conditions and return to DOS */%@AE@%%@NL@%
  14233.         setpage(Vpage);%@NL@%
  14234.         clrscrn(attr);%@NL@%
  14235.         putcur(0, 0, Vpage);%@NL@%
  14236.         setctype(c_start, c_end);%@NL@%
  14237.         exit(0);%@NL@%
  14238. }%@NL@%
  14239. %@NL@%
  14240. %@NL@%
  14241. %@2@%%@AH@%STRDUP.C%@AE@%%@EH@%%@NL@%
  14242. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\14VIEW\STRDUP.C%@AE@%%@NL@%
  14243. %@NL@%
  14244. %@AB@%/*%@NL@%
  14245. %@AB@% *        strdup -- duplicate a string%@NL@%
  14246. %@AB@% */%@AE@%%@NL@%
  14247. %@NL@%
  14248. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14249. %@AI@%#include %@AE@%<malloc.h> %@NL@%
  14250. %@AI@%#include %@AE@%<string.h> %@NL@%
  14251. %@NL@%
  14252. char *%@NL@%
  14253. strdup(str)%@NL@%
  14254. char *str;%@NL@%
  14255. {%@NL@%
  14256.         char *buf;%@NL@%
  14257. %@NL@%
  14258.         buf = malloc(strlen(str) + 1);%@NL@%
  14259.         return (buf == NULL ? buf : strcpy(buf, str));%@NL@%
  14260. }%@NL@%
  14261. %@NL@%
  14262. %@NL@%
  14263. %@2@%%@AH@%STRING.C%@AE@%%@EH@%%@NL@%
  14264. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\STRING.C%@AE@%%@NL@%
  14265. %@NL@%
  14266. %@AB@%/*%@NL@%
  14267. %@AB@% *        string -- emit a sequence of n repetitions of the%@NL@%
  14268. %@AB@% *        character ch %@NL@%
  14269. %@AB@% */%@AE@%%@NL@%
  14270. %@NL@%
  14271. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14272. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  14273. %@NL@%
  14274. int%@NL@%
  14275. string(n, ch, fp)%@NL@%
  14276. int n;%@NL@%
  14277. char ch;%@NL@%
  14278. FILE *fp;%@NL@%
  14279. {%@NL@%
  14280.         register int i;%@NL@%
  14281. %@NL@%
  14282.         for (i = 0; i < n; ++i)%@NL@%
  14283.                 if (fputc(ch, fp) == EOF && ferror(fp))%@NL@%
  14284.                         break;%@NL@%
  14285. %@NL@%
  14286.         %@AB@%/* return number of actual repetitions */%@AE@%%@NL@%
  14287.         return (i);%@NL@%
  14288. }%@NL@%
  14289. %@NL@%
  14290. %@NL@%
  14291. %@2@%%@AH@%SWAP_INT.C%@AE@%%@EH@%%@NL@%
  14292. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\11SCREEN\SWAP_INT.C%@AE@%%@NL@%
  14293. %@NL@%
  14294. %@AB@%/*%@NL@%
  14295. %@AB@% *        swap_int -- exchange the values of the two integers%@NL@%
  14296. %@AB@% */%@AE@%%@NL@%
  14297. %@NL@%
  14298. void%@NL@%
  14299. swap_int(p1, p2)%@NL@%
  14300. register int *p1;%@NL@%
  14301. register int *p2;%@NL@%
  14302. {%@NL@%
  14303.         int tmp;%@NL@%
  14304. %@NL@%
  14305.         %@AB@%/* exchange the values */%@AE@%%@NL@%
  14306.         tmp = *p1;%@NL@%
  14307.         *p1 = *p2;%@NL@%
  14308.         *p2 = tmp;%@NL@%
  14309. }%@NL@%
  14310. %@NL@%
  14311. %@NL@%
  14312. %@2@%%@AH@%SWEEP.C%@AE@%%@EH@%%@NL@%
  14313. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\06USER\SWEEP.C%@AE@%%@NL@%
  14314. %@NL@%
  14315. %@AB@%/*%@NL@%
  14316. %@AB@% *        sweep -- produce a sound that sweeps from%@NL@%
  14317. %@AB@% *        a low to a high frequency repeatedly until a%@NL@%
  14318. %@AB@% *        key is pressed%@NL@%
  14319. %@AB@% */%@AE@%%@NL@%
  14320. %@NL@%
  14321. %@AI@%#include %@AE@%<conio.h> %@NL@%
  14322. %@AI@%#include %@AE@%<local\sound.h> %@NL@%
  14323. %@NL@%
  14324. main()%@NL@%
  14325. {%@NL@%
  14326.         unsigned int f;%@NL@%
  14327.         int d, n;%@NL@%
  14328.         extern void setfreq(unsigned int);%@NL@%
  14329. %@NL@%
  14330.         SPKR_ON;%@NL@%
  14331.         while (1) {%@NL@%
  14332.                 %@AB@%/* give user a way out */%@AE@%%@NL@%
  14333.                 if (kbhit())%@NL@%
  14334.                         break;%@NL@%
  14335.                 n = 10;%@NL@%
  14336.                 for (f = 100; f <= 5000; f += n) {%@NL@%
  14337.                         setfreq(f);%@NL@%
  14338.                         d = 1000;%@NL@%
  14339.                         %@AB@%/* fake a short delay (machine dependednt) */%@AE@%%@NL@%
  14340.                         while (d-- > 0)%@NL@%
  14341.                                 ;%@NL@%
  14342.                         n += 10;%@NL@%
  14343.                 }%@NL@%
  14344.         }%@NL@%
  14345.         SPKR_OFF;%@NL@%
  14346.         exit(0);%@NL@%
  14347. }%@NL@%
  14348. %@NL@%
  14349. %@NL@%
  14350. %@2@%%@AH@%TABS.C%@AE@%%@EH@%%@NL@%
  14351. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\TABS.C%@AE@%%@NL@%
  14352. %@NL@%
  14353. %@AB@%/*%@NL@%
  14354. %@AB@% *        tabs -- a group of cooperating functions that set%@NL@%
  14355. %@AB@% *        and report the settings of "tabstops"%@NL@%
  14356. %@AB@% */%@AE@%%@NL@%
  14357. %@NL@%
  14358. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  14359. %@NL@%
  14360. static char Tabstops[MAXLINE];%@NL@%
  14361. %@NL@%
  14362. %@AB@%/*%@NL@%
  14363. %@AB@% *        fixtabs -- set up fixed-interval tabstops%@NL@%
  14364. %@AB@% */%@AE@%%@NL@%
  14365. void%@NL@%
  14366. fixtabs(interval)%@NL@%
  14367. register int interval;%@NL@%
  14368. {%@NL@%
  14369.         register int i;%@NL@%
  14370. %@NL@%
  14371.         for (i = 0; i < MAXLINE; i++)%@NL@%
  14372.                 Tabstops[i] = (i % interval == 0) ? 1 : 0;%@NL@%
  14373. } %@AB@%/* end fixtabs() */%@AE@%%@NL@%
  14374. %@NL@%
  14375. %@AB@%/*%@NL@%
  14376. %@AB@% *        vartabs -- set up variable tabstops from an array%@NL@%
  14377. %@AB@% *        integers terminated by a -1 entry%@NL@%
  14378. %@AB@% */%@AE@%%@NL@%
  14379. void%@NL@%
  14380. vartabs(list)%@NL@%
  14381. int *list;%@NL@%
  14382. {%@NL@%
  14383.         register int i;%@NL@%
  14384. %@NL@%
  14385.         %@AB@%/* initialize the tabstop array */%@AE@%%@NL@%
  14386.         for (i = 0; i < MAXLINE; ++i)%@NL@%
  14387.                 Tabstops[i] = 0;%@NL@%
  14388. %@NL@%
  14389.         %@AB@%/* set user-sprcified tabstops */%@AE@%%@NL@%
  14390.         while (*list != -1)%@NL@%
  14391.                 Tabstops[*++list] = 1;%@NL@%
  14392. } %@AB@%/* end vartabs() */%@AE@%%@NL@%
  14393. %@NL@%
  14394. %@AB@%/*%@NL@%
  14395. %@AB@% *        tabstop -- return non-zero if col is a tabstop%@NL@%
  14396. %@AB@% */%@AE@%%@NL@%
  14397. int%@NL@%
  14398. tabstop(col)%@NL@%
  14399. register int col;%@NL@%
  14400. {%@NL@%
  14401.         return (col >= MAXLINE ? 1 : Tabstops[col]);%@NL@%
  14402. } %@AB@%/* end tabstop() */%@AE@%%@NL@%
  14403. %@NL@%
  14404. %@NL@%
  14405. %@2@%%@AH@%TEE.C%@AE@%%@EH@%%@NL@%
  14406. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\08FILE\TEE.C%@AE@%%@NL@%
  14407. %@NL@%
  14408. %@AB@%/*%@NL@%
  14409. %@AB@% *        tee -- a "pipe fitter" for DOS%@NL@%
  14410. %@AB@% */%@AE@%%@NL@%
  14411. %@NL@%
  14412. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14413. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  14414. %@AI@%#include %@AE@%<string.h> %@NL@%
  14415. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  14416. %@NL@%
  14417. main(argc, argv)%@NL@%
  14418. int argc;%@NL@%
  14419. char *argv[];%@NL@%
  14420. {%@NL@%
  14421.         register int ch, n;%@NL@%
  14422.         static char openmode[] = { "w" };%@NL@%
  14423.         static char pgm[MAXPATH + 1] = { "tee" };%@NL@%
  14424.         FILE *fp[_NFILE];        %@AB@%/* array of file pointers */%@AE@%%@NL@%
  14425. %@NL@%
  14426.         extern int getopt(int, char **, char *);%@NL@%
  14427.         extern int optind, opterr;%@NL@%
  14428.         extern char *optarg;%@NL@%
  14429.         extern void getpname(char *, char *);%@NL@%
  14430. %@NL@%
  14431.         %@AB@%/* check for an alias */%@AE@%%@NL@%
  14432.         if (_osmajor >= 3)%@NL@%
  14433.                 getpname(argv[0], pgm);%@NL@%
  14434. %@NL@%
  14435.         %@AB@%/* process command-line options, if any */%@AE@%%@NL@%
  14436.         while ((ch = getopt(argc, argv, "a")) != EOF)%@NL@%
  14437.                 switch (ch) {%@NL@%
  14438.                 case 'a':%@NL@%
  14439.                         strcpy(openmode, "a");%@NL@%
  14440.                         break;%@NL@%
  14441.                 case '?':%@NL@%
  14442.                         break;%@NL@%
  14443.                 }%@NL@%
  14444.         n = argc -= optind;%@NL@%
  14445.         argv += optind;%@NL@%
  14446. %@NL@%
  14447.         %@AB@%/* check for errors */%@AE@%%@NL@%
  14448.         if (argc > _NFILE) {%@NL@%
  14449.                 fprintf(stderr, "Too many files (max = %d)\n", _NFILE);%@NL@%
  14450.                 exit(1);%@NL@%
  14451.         }%@NL@%
  14452. %@NL@%
  14453.         %@AB@%/* open the output file(s) */%@AE@%%@NL@%
  14454.         for (n = 0; n < argc; ++n) {%@NL@%
  14455.                 if ((fp[n] = fopen(argv[n], openmode)) == NULL) {%@NL@%
  14456.                         fprintf(stderr, "Cannot open %s\n", argv[n]);%@NL@%
  14457.                         continue;%@NL@%
  14458.                 }%@NL@%
  14459.         }%@NL@%
  14460. %@NL@%
  14461.         %@AB@%/* copy input to stdout plus opened file(s) */%@AE@%%@NL@%
  14462.         while ((ch = getchar()) != EOF) {%@NL@%
  14463.                 putchar(ch);%@NL@%
  14464.                 for (n = 0; n < argc; ++n)%@NL@%
  14465.                         if (fp[n] != NULL)%@NL@%
  14466.                                 fputc(ch, fp[n]);%@NL@%
  14467.         }%@NL@%
  14468. %@NL@%
  14469.         %@AB@%/* close file(s) */%@AE@%%@NL@%
  14470.         if (fcloseall() == -1) {%@NL@%
  14471.                 fprintf(stderr, "Error closing a file\n");%@NL@%
  14472.                 exit(2);%@NL@%
  14473.         }%@NL@%
  14474. %@NL@%
  14475.         exit(0);%@NL@%
  14476. }%@NL@%
  14477. %@NL@%
  14478. %@NL@%
  14479. %@2@%%@AH@%TIMEDATA.C%@AE@%%@EH@%%@NL@%
  14480. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\04STDLIB\TIMEDATA.C%@AE@%%@NL@%
  14481. %@NL@%
  14482. %@AB@%/*%@NL@%
  14483. %@AB@% *        timedata -- time zone and time value tests%@NL@%
  14484. %@AB@% */%@AE@%%@NL@%
  14485. %@NL@%
  14486. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14487. %@AI@%#include %@AE@%<time.h> %@NL@%
  14488. %@NL@%
  14489. main()%@NL@%
  14490. {%@NL@%
  14491.         long now;%@NL@%
  14492.         struct tm *tbuf;%@NL@%
  14493.         %@NL@%
  14494.         %@AB@%/* get TZ data into global variables */%@AE@%%@NL@%
  14495.         tzset();%@NL@%
  14496. %@NL@%
  14497.         %@AB@%/* display the global time values */%@AE@%%@NL@%
  14498.         printf("daylight savings time flag = %d\n", daylight);%@NL@%
  14499.         printf("difference (in seconds) from GMT = %ld\n", timezone);%@NL@%
  14500.         printf("standard time zone string is %s\n", tzname[0]);%@NL@%
  14501.         printf("daylight time zone string is %s\n", tzname[1]);%@NL@%
  14502. %@NL@%
  14503.         %@AB@%/*%@NL@%
  14504. %@AB@%         *  display the current date and time values for%@NL@%
  14505. %@AB@%         *  local and universal time%@NL@%
  14506. %@AB@%         */%@AE@%%@NL@%
  14507.         now = time(NULL);%@NL@%
  14508.         printf("\nctime():\t%s\n", ctime(&now));%@NL@%
  14509.         tbuf = localtime(&now);%@NL@%
  14510.         printf("local time:\t%s\n", asctime(tbuf));%@NL@%
  14511.         tbuf = gmtime(&now);%@NL@%
  14512.         printf("universal time:\t%s\n", asctime(tbuf));%@NL@%
  14513. %@NL@%
  14514.         exit(0);%@NL@%
  14515. }%@NL@%
  14516. %@NL@%
  14517. %@NL@%
  14518. %@2@%%@AH@%TIMER.C%@AE@%%@EH@%%@NL@%
  14519. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\06USER\TIMER.C%@AE@%%@NL@%
  14520. %@NL@%
  14521. %@AB@%/*%@NL@%
  14522. %@AB@% *        timer -- general purpose timer program; uses the%@NL@%
  14523. %@AB@% *        PC's intra-application communication area (ICA) as%@NL@%
  14524. %@AB@% *        a time and date buffer%@NL@%
  14525. %@AB@% */%@AE@%%@NL@%
  14526. %@NL@%
  14527. %@AI@%#include %@AE@%<dos.h> %@NL@%
  14528. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14529. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  14530. %@AI@%#include %@AE@%<string.h> %@NL@%
  14531. %@AI@%#include %@AE@%<time.h> %@NL@%
  14532. %@AI@%#include %@AE@%<memory.h> %@NL@%
  14533. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  14534. %@NL@%
  14535. %@AI@%#define %@AE@%NBYTES                16 %@NL@%
  14536. %@AI@%#define %@AE@%ICA_SEG        0x4F %@NL@%
  14537. %@AI@%#define %@AE@%MAXTNUM                3 %@NL@%
  14538. %@AI@%#define %@AE@%TIMEMASK        0x3FFFFFFF %@NL@%
  14539. %@AI@%#define %@AE@%FLAGBIT                0x80000000 %@NL@%
  14540. %@AI@%#define %@AE@%IDBIT                0x40000000 %@NL@%
  14541. %@NL@%
  14542. main(argc, argv)%@NL@%
  14543. int argc;%@NL@%
  14544. char *argv[];%@NL@%
  14545. {%@NL@%
  14546.         int ch;%@NL@%
  14547.         char *cp;        %@NL@%
  14548.         int tn;                        %@AB@%/* timer number */%@AE@%%@NL@%
  14549.         int errflag;                %@AB@%/* error flag */%@AE@%%@NL@%
  14550.         int eflag;                %@AB@%/* elapsed time flag */%@AE@%%@NL@%
  14551.         int sflag;                %@AB@%/* start timer flag */%@AE@%%@NL@%
  14552.         char dest[MAXPATH + 1];        %@AB@%/* destination file name */%@AE@%%@NL@%
  14553.         char timestr[MAXLINE];        %@AB@%/* buffer for elapsed time string */%@AE@%%@NL@%
  14554.         long now;                %@AB@%/* current time */%@AE@%%@NL@%
  14555.         long then;                %@AB@%/* previously recorded time */%@AE@%%@NL@%
  14556.         FILE *fout;%@NL@%
  14557.         unsigned long tdata[MAXTNUM];%@NL@%
  14558. %@NL@%
  14559.         struct SREGS segregs;%@NL@%
  14560. %@NL@%
  14561.         static char pgm[MAXNAME + 1] = { "timer" };%@NL@%
  14562. %@NL@%
  14563.         static void usage(char *, char *);%@NL@%
  14564.         extern char interval(long, char *);%@NL@%
  14565.         extern char *getpname(char *, char *);%@NL@%
  14566.         extern int getopt(int, char **, char *);%@NL@%
  14567.         extern int optind, opterr;%@NL@%
  14568.         extern char *optarg;%@NL@%
  14569. %@NL@%
  14570.         if (_osmajor >= 3)%@NL@%
  14571.                 getpname(*argv, pgm);%@NL@%
  14572. %@NL@%
  14573.         %@AB@%/* process optional arguments */%@AE@%%@NL@%
  14574.         fout = stdout;%@NL@%
  14575.         tn = 0;%@NL@%
  14576.         errflag = eflag = sflag = 0;%@NL@%
  14577.         while ((ch = getopt(argc, argv, "0123ef:s")) != EOF) {%@NL@%
  14578.                 switch (ch) {%@NL@%
  14579.                 case 'e':%@NL@%
  14580.                         %@AB@%/* report elapsed timing */%@AE@%%@NL@%
  14581.                         ++eflag;%@NL@%
  14582.                         break;%@NL@%
  14583.                 case 'f':%@NL@%
  14584.                         %@AB@%/* use specified log file or stream */%@AE@%%@NL@%
  14585.                         strcpy(dest, optarg);%@NL@%
  14586.                         if ((fout = fopen(dest, "a")) == NULL) {%@NL@%
  14587.                                 fprintf(stderr, "%s: Cannot open %s\n",%@NL@%
  14588.                                         pgm, dest);%@NL@%
  14589.                                 exit(1);%@NL@%
  14590.                         }%@NL@%
  14591.                         break;%@NL@%
  14592.                 case 's':%@NL@%
  14593.                         %@AB@%/* start (or restart) timing an interval */%@AE@%%@NL@%
  14594.                         ++sflag;%@NL@%
  14595.                         break;%@NL@%
  14596.                 case '0':%@NL@%
  14597.                 case '1':%@NL@%
  14598.                 case '2':%@NL@%
  14599.                 case '3':%@NL@%
  14600.                         %@AB@%/* use specified timer */%@AE@%%@NL@%
  14601.                         tn = ch - 0x30;%@NL@%
  14602.                         break;%@NL@%
  14603.                 case '?':%@NL@%
  14604.                         %@AB@%/* bad option flag */%@AE@%%@NL@%
  14605.                         ++errflag;%@NL@%
  14606.                         break;%@NL@%
  14607.                 }%@NL@%
  14608.         }%@NL@%
  14609.         argc -= optind;%@NL@%
  14610.         argv += optind;%@NL@%
  14611. %@NL@%
  14612.         %@AB@%/* check for errors */%@AE@%%@NL@%
  14613.         if (errflag > 0 || argc > 0) %@NL@%
  14614.                 usage(pgm, "Bad command line option(s)");%@NL@%
  14615. %@NL@%
  14616.         segread(&segregs);%@NL@%
  14617. %@NL@%
  14618.         %@AB@%/* report current date and time */%@AE@%%@NL@%
  14619.         now = time(NULL);%@NL@%
  14620.         fprintf(fout, "%s", ctime(&now));%@NL@%
  14621. %@NL@%
  14622.         %@AB@%/* control and report timer data */%@AE@%%@NL@%
  14623.         if (eflag) {%@NL@%
  14624.                 %@AB@%/* report elapsed time for specified timer */%@AE@%%@NL@%
  14625.                 movedata(ICA_SEG, 0, segregs.ds, tdata, NBYTES);%@NL@%
  14626.                 then = tdata[tn];%@NL@%
  14627.                 if ((then & FLAGBIT) != FLAGBIT || (then & IDBIT) != IDBIT) {%@NL@%
  14628.                         fprintf(stderr, "Timer database corrupted or not set\n");%@NL@%
  14629.                         exit(1);%@NL@%
  14630.                 }%@NL@%
  14631.                 interval(now - (then & TIMEMASK), timestr);%@NL@%
  14632.                 fprintf(stdout, "Elapsed time = %s\n", timestr);%@NL@%
  14633.         }%@NL@%
  14634.         if (sflag) {%@NL@%
  14635.                 %@AB@%/* start (or restart) specified timer */%@AE@%%@NL@%
  14636.                 movedata(ICA_SEG, 0, segregs.ds, tdata, NBYTES);%@NL@%
  14637.                 tdata[tn] = (now & TIMEMASK) | FLAGBIT | IDBIT;%@NL@%
  14638.                 movedata(segregs.ds, tdata, ICA_SEG, 0, NBYTES);%@NL@%
  14639.         }%@NL@%
  14640.         fputc('\n', fout);%@NL@%
  14641. %@NL@%
  14642.         exit(0);%@NL@%
  14643. }%@NL@%
  14644. %@NL@%
  14645. %@AB@%/*%@NL@%
  14646. %@AB@% *        usage -- display a usage message and exit%@NL@%
  14647. %@AB@% *        with an error indication%@NL@%
  14648. %@AB@% */%@AE@%%@NL@%
  14649. %@NL@%
  14650. static void%@NL@%
  14651. usage(pname, mesg)%@NL@%
  14652. char *pname;%@NL@%
  14653. char *mesg;%@NL@%
  14654. {%@NL@%
  14655.         fprintf(stderr, "%s\n", mesg);%@NL@%
  14656.         fprintf(stderr, "Usage: %s [-efs#]\n", pname);%@NL@%
  14657.         fprintf(stderr, "\t-e \tshow an elapsed time (must use start first)\n");%@NL@%
  14658.         fprintf(stderr, "\t-f file\tappend output to specified file\n");%@NL@%
  14659.         fprintf(stderr, "\t-s \tstart (or restart) an interval timer\n");%@NL@%
  14660.         fprintf(stderr, "\t-# \tselect a timer (0 to 3; default is 0)\n");%@NL@%
  14661.         exit(2);%@NL@%
  14662. }%@NL@%
  14663. %@NL@%
  14664. %@NL@%
  14665. %@2@%%@AH@%TONE.C%@AE@%%@EH@%%@NL@%
  14666. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\06USER\TONE.C%@AE@%%@NL@%
  14667. %@NL@%
  14668. %@AB@%/*%@NL@%
  14669. %@AB@% *        tone -- set the frequency of the sound generator%@NL@%
  14670. %@AB@% */%@AE@%%@NL@%
  14671. %@NL@%
  14672. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14673. %@NL@%
  14674. main(argc, argv)%@NL@%
  14675. int argc;%@NL@%
  14676. char **argv;%@NL@%
  14677. {%@NL@%
  14678.         extern void setfreq(unsigned int);%@NL@%
  14679. %@NL@%
  14680.         if (argc != 2) {%@NL@%
  14681.                 fprintf(stderr, "Usage: tone hertz\n");%@NL@%
  14682.                 exit(1);%@NL@%
  14683.         }%@NL@%
  14684. %@NL@%
  14685.         %@AB@%/* set the frequency in Hertz */%@AE@%%@NL@%
  14686.         setfreq(atoi(*++argv));%@NL@%
  14687.         exit(0);%@NL@%
  14688. }%@NL@%
  14689. %@NL@%
  14690. %@NL@%
  14691. %@2@%%@AH@%TOUCH.C%@AE@%%@EH@%%@NL@%
  14692. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\08FILE\TOUCH.C%@AE@%%@NL@%
  14693. %@NL@%
  14694. %@AB@%/*%@NL@%
  14695. %@AB@% *        touch -- update modification time of file(s)%@NL@%
  14696. %@AB@% */%@AE@%%@NL@%
  14697. %@NL@%
  14698. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14699. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  14700. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  14701. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  14702. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  14703. %@AI@%#include %@AE@%<sys\utime.h> %@NL@%
  14704. %@AI@%#include %@AE@%<io.h> %@NL@%
  14705. %@AI@%#include %@AE@%<errno.h> %@NL@%
  14706. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  14707. %@NL@%
  14708. %@AB@%/* error return -- big enough not to be mistaken for a bad file count */%@AE@%%@NL@%
  14709. %@AI@%#define %@AE@%ERR        0x7FFF %@NL@%
  14710. %@NL@%
  14711. main(argc, argv)%@NL@%
  14712. int argc;%@NL@%
  14713. char *argv[];%@NL@%
  14714. {%@NL@%
  14715.         int ch;%@NL@%
  14716.         int i;%@NL@%
  14717.         int badcount;                %@AB@%/* # of files that can't be updated */%@AE@%%@NL@%
  14718.         struct stat statbuf;        %@AB@%/* buffer for stat results */%@AE@%%@NL@%
  14719.         BOOLEAN errflag,        %@AB@%/* error flag */%@AE@%%@NL@%
  14720.                 cflag,                %@AB@%/* creation flag */%@AE@%%@NL@%
  14721.                 vflag;                %@AB@%/* verbose flag */%@AE@%%@NL@%
  14722.         FILE *fp;%@NL@%
  14723. %@NL@%
  14724.         static char pgm[MAXNAME + 1] = { "touch" };%@NL@%
  14725.         extern int getopt(int, char **, char *);%@NL@%
  14726.         extern int optind, opterr;%@NL@%
  14727.         extern char *optarg;%@NL@%
  14728.         extern void getpname(char *, char *);%@NL@%
  14729.         static void usage(char *);%@NL@%
  14730. %@NL@%
  14731.         %@AB@%/* get program name from DOS (version 3.00 and later) */%@AE@%%@NL@%
  14732.         if (_osmajor >= 3)%@NL@%
  14733.                 getpname(argv[0], pgm);%@NL@%
  14734. %@NL@%
  14735.         %@AB@%/* process optional arguments first */%@AE@%%@NL@%
  14736.         errflag = cflag = vflag = FALSE;%@NL@%
  14737.         badcount = 0;%@NL@%
  14738.         while ((ch = getopt(argc, argv, "cv")) != EOF)%@NL@%
  14739.                 switch (ch) {%@NL@%
  14740.                 case 'c':%@NL@%
  14741.                         %@AB@%/* don't create files */%@AE@%%@NL@%
  14742.                         cflag = TRUE;%@NL@%
  14743.                         break;%@NL@%
  14744.                 case 'v':%@NL@%
  14745.                         %@AB@%/* verbose -- report activity */%@AE@%%@NL@%
  14746.                         vflag = TRUE;%@NL@%
  14747.                         break;%@NL@%
  14748.                 case '?':%@NL@%
  14749.                         errflag = TRUE;%@NL@%
  14750.                         break;%@NL@%
  14751.                 }%@NL@%
  14752.         argc -= optind;%@NL@%
  14753.         argv += optind;%@NL@%
  14754. %@NL@%
  14755.         %@AB@%/* check for errors including no file names */%@AE@%%@NL@%
  14756.         if (errflag == TRUE || argc <= 0) {%@NL@%
  14757.                 usage(pgm);%@NL@%
  14758.                 exit(ERR);%@NL@%
  14759.         }%@NL@%
  14760. %@NL@%
  14761.         %@AB@%/* update modification times of files */%@AE@%%@NL@%
  14762.         for (; argc-- > 0; ++argv) {%@NL@%
  14763.                 if (stat(*argv, &statbuf) == -1) {%@NL@%
  14764.                         %@AB@%/* file doesn't exist */%@AE@%%@NL@%
  14765.                         if (cflag == TRUE) {%@NL@%
  14766.                                 %@AB@%/* don't create it */%@AE@%%@NL@%
  14767.                                 ++badcount;%@NL@%
  14768.                                 continue;%@NL@%
  14769.                         }%@NL@%
  14770.                         else if ((fp = fopen(*argv, "w")) == NULL) {%@NL@%
  14771.                                 fprintf(stderr, "%s: Cannot create %s\n",%@NL@%
  14772.                                         pgm, *argv);%@NL@%
  14773.                                 ++badcount;%@NL@%
  14774.                                 continue;%@NL@%
  14775.                         }%@NL@%
  14776.                         else {%@NL@%
  14777.                                 if (fclose(fp) == EOF) {%@NL@%
  14778.                                         perror("Error closing file");%@NL@%
  14779.                                         exit(ERR);%@NL@%
  14780.                                 }%@NL@%
  14781.                                 if (stat(*argv, &statbuf) == -1) {%@NL@%
  14782.                                         fprintf(stderr, "%s: Cannot stat %s\n", pgm, *argv);%@NL@%
  14783.                                         ++badcount;%@NL@%
  14784.                                         continue;%@NL@%
  14785.                                 }%@NL@%
  14786.                         }%@NL@%
  14787.                 }                        %@NL@%
  14788.                 if (utime(*argv, NULL) == -1) {%@NL@%
  14789.                         ++badcount;%@NL@%
  14790.                         perror("Error updating date/time stamp");%@NL@%
  14791.                         continue;%@NL@%
  14792.                 }%@NL@%
  14793.                 if (vflag == TRUE)%@NL@%
  14794.                         fprintf(stderr, "Touched file %s\n", *argv);%@NL@%
  14795.         }%@NL@%
  14796. %@NL@%
  14797.         exit(badcount);%@NL@%
  14798. } %@AB@%/* end main() */%@AE@%%@NL@%
  14799. %@NL@%
  14800. %@AB@%/*%@NL@%
  14801. %@AB@% *        usage -- display an informative usage message%@NL@%
  14802. %@AB@% */%@AE@%%@NL@%
  14803. %@NL@%
  14804. static void%@NL@%
  14805. usage(pname)%@NL@%
  14806. char *pname;%@NL@%
  14807. {%@NL@%
  14808.         fprintf(stderr, "Usage: %s [-cv] file ...\n", pname);%@NL@%
  14809.         fprintf(stderr, "\t-c  Do not create any files\n");%@NL@%
  14810.         fprintf(stderr, "\t-v  Verbose mode -- report activities\n");%@NL@%
  14811. } %@AB@%/* end usage() */%@AE@%%@NL@%
  14812. %@NL@%
  14813. %@AB@%/*%@NL@%
  14814. %@AB@% *        dummy functions to show how to save a little space%@NL@%
  14815. %@AB@% */%@AE@%%@NL@%
  14816. %@NL@%
  14817. _setenvp()%@NL@%
  14818. {%@NL@%
  14819. }%@NL@%
  14820. %@NL@%
  14821. %@AI@%#ifndef %@AE@%DEBUG %@NL@%
  14822. _nullcheck()%@NL@%
  14823. {%@NL@%
  14824. }%@NL@%
  14825. %@AI@%#endif %@AE@%%@NL@%
  14826. %@NL@%
  14827. %@NL@%
  14828. %@2@%%@AH@%TSTREPLY.C%@AE@%%@EH@%%@NL@%
  14829. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\06USER\TSTREPLY.C%@AE@%%@NL@%
  14830. %@NL@%
  14831. %@AB@%/*%@NL@%
  14832. %@AB@% *        tstreply -- test the getreply function%@NL@%
  14833. %@AB@% */%@AE@%%@NL@%
  14834. %@NL@%
  14835. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14836. %@AI@%#include %@AE@%<string.h> %@NL@%
  14837. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  14838. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  14839. %@AI@%#include %@AE@%"linebuf.h" %@NL@%
  14840. %@NL@%
  14841. %@AI@%#define %@AE@%INPUT_ROW        0 %@NL@%
  14842. %@AI@%#define %@AE@%INPUT_COL        40 %@NL@%
  14843. %@AI@%#define %@AE@%WIDTH                40 %@NL@%
  14844. %@NL@%
  14845. int Apage = 0;%@NL@%
  14846. BOOLEAN Silent = FALSE;%@NL@%
  14847. %@NL@%
  14848. main(argc, argv)%@NL@%
  14849. int argc;%@NL@%
  14850. char *argv[];%@NL@%
  14851. {%@NL@%
  14852.         unsigned int r, c, ch, attr, revattr;%@NL@%
  14853.         char reply[MAXPATH + 1];%@NL@%
  14854.         LINEBUF buf;%@NL@%
  14855. %@NL@%
  14856.         extern char *getreply(short, short, short,%@NL@%
  14857.                 char *, LINEBUF *, short, short, short);%@NL@%
  14858. %@NL@%
  14859.         %@AB@%/* process command line */%@AE@%%@NL@%
  14860.         if (argc == 2 && strcmp(argv[1], "-s") == 0)%@NL@%
  14861.                 Silent = TRUE;%@NL@%
  14862.         else if (argc > 2) {%@NL@%
  14863.                 fprintf(stderr, "Usage: tstreply [-s]\n");%@NL@%
  14864.                 exit(1);%@NL@%
  14865.         }%@NL@%
  14866. %@NL@%
  14867.         %@AB@%/* initial setup */%@AE@%%@NL@%
  14868.         getstate();%@NL@%
  14869.         readca(&ch, &attr, Apage);%@NL@%
  14870.         revattr = ((attr << 4) | (attr >> 4)) & 0x77;%@NL@%
  14871.         clrscrn(attr);%@NL@%
  14872.         putcur(0, 0, Apage);%@NL@%
  14873.         writestr("TSTREPLY", Apage);%@NL@%
  14874.         putcur(1, 0, Apage);%@NL@%
  14875.         writec(HLINE, Maxcol[Vmode] - 1, Apage);%@NL@%
  14876.         buf.l_buf = reply;%@NL@%
  14877.         buf.l_next = buf.l_prev = (LINEBUF *)NULL;%@NL@%
  14878. %@NL@%
  14879.         %@AB@%/* demo getreply() */%@AE@%%@NL@%
  14880.         %@NL@%
  14881.         if (getreply(INPUT_ROW, INPUT_COL, WIDTH, "File: ", &buf,%@NL@%
  14882.                 MAXPATH, revattr, 0) == NULL) {%@NL@%
  14883.                 putcur(INPUT_ROW, INPUT_COL, Apage);%@NL@%
  14884.                 writeca(' ', attr, WIDTH, Apage);%@NL@%
  14885.                 putcur(2, 0, Apage);%@NL@%
  14886.                 fprintf(stderr, "input aborted\n");%@NL@%
  14887.                 exit(1);%@NL@%
  14888.         }%@NL@%
  14889.         putcur(INPUT_ROW, INPUT_COL, Apage);%@NL@%
  14890.         writeca(' ', attr, WIDTH, Apage);%@NL@%
  14891.         putcur(2, 0, Apage);%@NL@%
  14892.         fprintf(stderr, "reply = %s\n", reply);%@NL@%
  14893.         exit(0);%@NL@%
  14894. }%@NL@%
  14895. %@NL@%
  14896. %@AI@%#define %@AE@%MSG_ROW        24 %@NL@%
  14897. %@AI@%#define %@AE@%MSG_COL        0 %@NL@%
  14898. %@NL@%
  14899. int%@NL@%
  14900. errmsg(mesg)%@NL@%
  14901. char *mesg;%@NL@%
  14902. {%@NL@%
  14903.         int n;%@NL@%
  14904.         extern void sound(unsigned int, float);%@NL@%
  14905. %@NL@%
  14906.         putcur(MSG_ROW, MSG_COL, Apage);%@NL@%
  14907.         if ((n = strlen(mesg)) > 0) {%@NL@%
  14908.                 writestr(mesg, Apage);%@NL@%
  14909.                 if (Silent == FALSE)%@NL@%
  14910.                         sound(100, 0.2);%@NL@%
  14911.         }%@NL@%
  14912.         else%@NL@%
  14913.                 writec(' ', Maxcol[Vmode] - 1 - MSG_COL, Apage);%@NL@%
  14914.         return (n);%@NL@%
  14915. }%@NL@%
  14916. %@NL@%
  14917. %@NL@%
  14918. %@2@%%@AH@%TSTSEL.C%@AE@%%@EH@%%@NL@%
  14919. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\07CONFIG\TSTSEL.C%@AE@%%@NL@%
  14920. %@NL@%
  14921. %@AB@%/*%@NL@%
  14922. %@AB@% *        tstsel -- test driver for the "select" functions%@NL@%
  14923. %@AB@% */%@AE@%%@NL@%
  14924. %@NL@%
  14925. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14926. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  14927. %@NL@%
  14928. %@AI@%#define %@AE@%MAXTEST        20 %@NL@%
  14929. %@AI@%#define %@AE@%NRANGES        10 %@NL@%
  14930. %@AI@%#define %@AE@%NDIGITS        5 %@NL@%
  14931. %@NL@%
  14932. extern struct slist_st {%@NL@%
  14933.         long int s_min;%@NL@%
  14934.         long int s_max;%@NL@%
  14935. } Slist[NRANGES + 1];%@NL@%
  14936. %@NL@%
  14937. main (argc, argv)%@NL@%
  14938. int argc;%@NL@%
  14939. char **argv;%@NL@%
  14940. {%@NL@%
  14941.         int i;%@NL@%
  14942.         extern int mkslist(char *);%@NL@%
  14943.         extern int selected(unsigned int);%@NL@%
  14944.         static void showlist();%@NL@%
  14945. %@NL@%
  14946.         if (argc != 2) {%@NL@%
  14947.                 fprintf(stderr, "Usage: tstsel list\n");%@NL@%
  14948.                 exit(1);%@NL@%
  14949.         }%@NL@%
  14950.         printf("argv[1] = %s\n", argv[1]);%@NL@%
  14951.         mkslist(argv[1]);%@NL@%
  14952.         showlist();%@NL@%
  14953.         for (i = 0; i < MAXTEST; ++i)%@NL@%
  14954.                 printf("%2d -> %s\n", i, selected(i) ? "YES" : "NO");%@NL@%
  14955.         exit(0);%@NL@%
  14956. }%@NL@%
  14957. %@NL@%
  14958. %@AB@%/*%@NL@%
  14959. %@AB@% *        showlist -- display the contents of the select list%@NL@%
  14960. %@AB@% */%@AE@%%@NL@%
  14961. %@NL@%
  14962. static void%@NL@%
  14963. showlist()%@NL@%
  14964. {%@NL@%
  14965.         int i;%@NL@%
  14966. %@NL@%
  14967.         %@AB@%/* scan the selection list and display values */%@AE@%%@NL@%
  14968.         for (i = 0; i <= NRANGES; ++i)%@NL@%
  14969.                 printf("%2d %5ld %5ld\n", i, Slist[i].s_min, Slist[i].s_max);%@NL@%
  14970. }%@NL@%
  14971. %@NL@%
  14972. %@NL@%
  14973. %@2@%%@AH@%USERATTR.C%@AE@%%@EH@%%@NL@%
  14974. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\13ANSI\USERATTR.C%@AE@%%@NL@%
  14975. %@NL@%
  14976. %@AB@%/*%@NL@%
  14977. %@AB@% *        userattr -- set video attributes to user-specified values%@NL@%
  14978. %@AB@% *        (DOS environment parameters) or to reasonable defaults and%@NL@%
  14979. %@AB@% *        return success or a failure indication for bad attributes%@NL@%
  14980. %@AB@% */%@AE@%%@NL@%
  14981. %@NL@%
  14982. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  14983. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  14984. %@AI@%#include %@AE@%<string.h> %@NL@%
  14985. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  14986. %@AI@%#include %@AE@%<local\ansi.h> %@NL@%
  14987. %@AI@%#include %@AE@%<local\ibmcolor.h> %@NL@%
  14988. %@NL@%
  14989. int%@NL@%
  14990. userattr(foreground, background, border)%@NL@%
  14991. char *foreground, *background, *border;%@NL@%
  14992. {%@NL@%
  14993.         register char *s;%@NL@%
  14994.         static int attrset(POSITION, char *);%@NL@%
  14995.         %@NL@%
  14996.         if ((s = getenv("FGND")) == NULL)%@NL@%
  14997.                 s = foreground;%@NL@%
  14998.         if (attrset(FGND, s) == -1)%@NL@%
  14999.                 return FAILURE;%@NL@%
  15000. %@NL@%
  15001.         if ((s = getenv("BKGND")) == NULL)%@NL@%
  15002.                 s = background;%@NL@%
  15003.         if (attrset(BKGND, s) == -1)%@NL@%
  15004.                 return FAILURE;%@NL@%
  15005. %@NL@%
  15006.         if ((s = getenv("BORDER")) == NULL)%@NL@%
  15007.                 s = border;%@NL@%
  15008.         if (attrset(BDR, s) == -1)%@NL@%
  15009.                 return FAILURE;%@NL@%
  15010. %@NL@%
  15011.         return SUCCESS;%@NL@%
  15012. }%@NL@%
  15013. %@NL@%
  15014. %@AB@%/*%@NL@%
  15015. %@AB@% *        attrset -- parse the color spec and try to set it.%@NL@%
  15016. %@AB@% *        return 0 if OK and -1 upon error (bad color number)%@NL@%
  15017. %@AB@% */%@AE@%%@NL@%
  15018. static int%@NL@%
  15019. attrset(apos, str)%@NL@%
  15020. POSITION apos;%@NL@%
  15021. register char *str;%@NL@%
  15022. {%@NL@%
  15023.         register int attr;%@NL@%
  15024.         extern int colornum(char *);%@NL@%
  15025.         extern void setattr(POSITION, int);%@NL@%
  15026. %@NL@%
  15027.         if ((attr = colornum(strtok(str, " \t"))) == IBM_BRIGHT)%@NL@%
  15028.                 attr |= colornum(strtok(NULL, " \t"));%@NL@%
  15029.         if (attr >= 0)%@NL@%
  15030.                 setattr(apos, attr);%@NL@%
  15031.         else%@NL@%
  15032.                 return (-1);%@NL@%
  15033.         return (0);%@NL@%
  15034. }%@NL@%
  15035. %@NL@%
  15036. %@NL@%
  15037. %@2@%%@AH@%VER.C%@AE@%%@EH@%%@NL@%
  15038. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\DOS\VER.C%@AE@%%@NL@%
  15039. %@NL@%
  15040. %@AB@%/*%@NL@%
  15041. %@AB@% *        ver -- get the MS-DOS (or PC-DOS) version number%@NL@%
  15042. %@AB@% */%@AE@%%@NL@%
  15043. %@NL@%
  15044. %@AI@%#include %@AE@%<local\doslib.h> %@NL@%
  15045. %@NL@%
  15046. %@AB@%/************************************************************%@NL@%
  15047. %@AB@%*  For MS-DOS versions prior to 2.00, the low byte (AL) of%@NL@%
  15048. %@AB@%*  the return value is zero.  For versions 2.00 and beyond,%@NL@%
  15049. %@AB@%*  the low byte is the major version number and the high%@NL@%
  15050. %@AB@%*  byte (AH) is the minor version number.%@NL@%
  15051. %@AB@%************************************************************/%@AE@%%@NL@%
  15052. %@NL@%
  15053. int ver()%@NL@%
  15054. {%@NL@%
  15055.         return(bdos(DOS_VERSION, 0, 0));%@NL@%
  15056. }%@NL@%
  15057. %@NL@%
  15058. %@NL@%
  15059. %@2@%%@AH@%VF.C%@AE@%%@EH@%%@NL@%
  15060. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\14VIEW\VF.C%@AE@%%@NL@%
  15061. %@NL@%
  15062. %@AB@%/*%@NL@%
  15063. %@AB@% *        vf -- view a file using a full-screen window onto%@NL@%
  15064. %@AB@% *        an in-memory text file buffer%@NL@%
  15065. %@AB@% */%@AE@%%@NL@%
  15066. %@NL@%
  15067. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  15068. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  15069. %@AI@%#include %@AE@%<string.h> %@NL@%
  15070. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  15071. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  15072. %@AI@%#include %@AE@%"vf.h" %@NL@%
  15073. %@NL@%
  15074. extern int setctype(int, int);%@NL@%
  15075. int Startscan, Endscan;        %@AB@%/* cursor scan line range */%@AE@%%@NL@%
  15076. unsigned char Attr;        %@AB@%/* primary display attribute */%@AE@%%@NL@%
  15077. unsigned char Revattr;        %@AB@%/* reverse video for highlighting */%@AE@%%@NL@%
  15078. unsigned char Usrattr;        %@AB@%/* user's original attribute */%@AE@%%@NL@%
  15079. %@NL@%
  15080. main(argc, argv)%@NL@%
  15081. int argc;%@NL@%
  15082. char **argv;%@NL@%
  15083. {%@NL@%
  15084.         int ch;%@NL@%
  15085.         unsigned char chr;%@NL@%
  15086.         BOOLEAN errflag;%@NL@%
  15087.         BOOLEAN numbers;%@NL@%
  15088.         int errcode;%@NL@%
  15089.         FILE *fp;%@NL@%
  15090.         extern char *optarg;%@NL@%
  15091.         extern int optind;%@NL@%
  15092.         static char pgm[MAXNAME + 1] = { "vf" };%@NL@%
  15093. %@NL@%
  15094.         %@AB@%/* function prototypes */%@AE@%%@NL@%
  15095.         void clean();%@NL@%
  15096.         extern clrscrn(unsigned char);%@NL@%
  15097.         extern void getpname(char *, char *);%@NL@%
  15098.         extern void fixtabs(int);%@NL@%
  15099.         extern void initmsg(int, int, int, unsigned char, int);%@NL@%
  15100.         extern int getopt(int, char **, char *);%@NL@%
  15101.         extern int vf_cmd(FILE *, char *, BOOLEAN);%@NL@%
  15102.         extern int readca(unsigned char *, unsigned char *, unsigned int);%@NL@%
  15103.         extern int getctype(int *, int *, int);%@NL@%
  15104. %@NL@%
  15105.         errcode = 0;%@NL@%
  15106.         fixtabs(TABWIDTH);%@NL@%
  15107. %@NL@%
  15108.         %@AB@%/* get program name from DOS (version 3.00 and later) */%@AE@%%@NL@%
  15109.         if (_osmajor >= 3)%@NL@%
  15110.                 getpname(*argv, pgm);%@NL@%
  15111. %@NL@%
  15112.         %@AB@%/* be sure we have needed DOS support */%@AE@%%@NL@%
  15113.         if (_osmajor < 2) {%@NL@%
  15114.                 fprintf(stderr, "%s requires DOS 2.00 or later\n", pgm);%@NL@%
  15115.                 exit(1);%@NL@%
  15116.         }%@NL@%
  15117. %@NL@%
  15118.         %@AB@%/* process optional arguments first */%@AE@%%@NL@%
  15119.         errflag = numbers = FALSE;%@NL@%
  15120.         while ((ch = getopt(argc, argv, "n")) != EOF)%@NL@%
  15121.                 switch (ch) {%@NL@%
  15122.                 case 'n':%@NL@%
  15123.                         %@AB@%/* turn on line numbering */%@AE@%%@NL@%
  15124.                         numbers = TRUE;%@NL@%
  15125.                         break;%@NL@%
  15126.                 case '?':%@NL@%
  15127.                         %@AB@%/* bad option */%@AE@%%@NL@%
  15128.                         errflag = TRUE;%@NL@%
  15129.                         break;%@NL@%
  15130.                 }%@NL@%
  15131. %@NL@%
  15132.         %@AB@%/* check for command-line errors */%@AE@%%@NL@%
  15133.         argc -= optind;%@NL@%
  15134.         argv += optind;%@NL@%
  15135.         if (errflag == TRUE || argc == 0) {%@NL@%
  15136.                 fprintf(stderr, "Usage: %s [-n] file...\n", pgm);%@NL@%
  15137.                 exit(1);%@NL@%
  15138.         }%@NL@%
  15139. %@NL@%
  15140.         %@AB@%/* get current video attribute and set VF attributes */%@AE@%%@NL@%
  15141.         getstate();%@NL@%
  15142.         readca(&chr, &Usrattr, Vpage);        %@AB@%/* save user's attribute settings */%@AE@%%@NL@%
  15143.         %@AB@%/*%@NL@%
  15144. %@AB@%         * These colors are modified from those presented in the%@NL@%
  15145. %@AB@%         * book so that VF will work on all monochrome and%@NL@%
  15146. %@AB@%         * color systems the same way -- change them to suit%@NL@%
  15147. %@AB@%         * your own shade of reality and operating conditions%@NL@%
  15148. %@AB@%         */%@AE@%%@NL@%
  15149.         Attr = Usrattr;                        %@AB@%/* basic text attributes */%@AE@%%@NL@%
  15150.         %@AB@%/* reverse video for highlighting */%@AE@%%@NL@%
  15151.         Revattr = ((Attr & 0x7) << 4) | (Attr >> 4);%@NL@%
  15152.         clrscrn(Attr);%@NL@%
  15153. %@NL@%
  15154.         %@AB@%/* save user's cursor shape and turn cursor off */%@AE@%%@NL@%
  15155.         getctype(&Startscan, &Endscan, Vpage);%@NL@%
  15156.         setctype(MAXSCAN, MAXSCAN);%@NL@%
  15157. %@NL@%
  15158.         %@AB@%/* set up the message line manager */%@AE@%%@NL@%
  15159.         initmsg(MSGROW, MSGCOL, Maxcol[Vmode] - MSGCOL, Attr, Vpage);%@NL@%
  15160. %@NL@%
  15161.         %@AB@%/* display first screen page */%@AE@%%@NL@%
  15162.         putcur(0, 0, Vpage);%@NL@%
  15163.         putstr("ViewFile/1.0  H=Help Q=Quit Esc=Next", Vpage);%@NL@%
  15164.         putcur(1, 0, Vpage);%@NL@%
  15165.         writea(Revattr, Maxcol[Vmode], Vpage);%@NL@%
  15166. %@NL@%
  15167.         for (; argc-- > 0; ++argv) {%@NL@%
  15168.                 if ((fp = fopen(*argv, "r")) == NULL) {%@NL@%
  15169.                         fprintf(stderr, "%s: cannot open %s -- ", pgm, *argv);%@NL@%
  15170.                         perror("");%@NL@%
  15171.                         ++errcode;%@NL@%
  15172.                         continue;%@NL@%
  15173.                 }%@NL@%
  15174.                 if (vf_cmd(fp, *argv, numbers) != 0)%@NL@%
  15175.                         break;%@NL@%
  15176.                 fclose(fp);%@NL@%
  15177.         }%@NL@%
  15178.         clean();%@NL@%
  15179.         exit(errcode);%@NL@%
  15180. }%@NL@%
  15181. %@NL@%
  15182. %@NL@%
  15183. %@AB@%/*%@NL@%
  15184. %@AB@% *        clean -- restore the user's original conditions%@NL@%
  15185. %@AB@% */%@AE@%%@NL@%
  15186. %@NL@%
  15187. void%@NL@%
  15188. clean()%@NL@%
  15189. {%@NL@%
  15190.         %@AB@%/* set screen to user's attribute */%@AE@%%@NL@%
  15191.         clrscrn(Usrattr);%@NL@%
  15192.         putcur(0, 0, Vpage);%@NL@%
  15193. %@NL@%
  15194.         %@AB@%/* restore user's cursor shape */%@AE@%%@NL@%
  15195.         setctype(Startscan, Endscan);%@NL@%
  15196. }%@NL@%
  15197. %@NL@%
  15198. %@NL@%
  15199. %@2@%%@AH@%VF_CMD.C%@AE@%%@EH@%%@NL@%
  15200. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\14VIEW\VF_CMD.C%@AE@%%@NL@%
  15201. %@NL@%
  15202. %@AB@%/*%@NL@%
  15203. %@AB@% *        vf_cmd -- ViewFile command processor%@NL@%
  15204. %@AB@% */%@AE@%%@NL@%
  15205. %@NL@%
  15206. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  15207. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  15208. %@AI@%#include %@AE@%<string.h> %@NL@%
  15209. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  15210. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  15211. %@AI@%#include %@AE@%<local\keydefs.h> %@NL@%
  15212. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  15213. %@AI@%#include %@AE@%"vf.h" %@NL@%
  15214. %@AI@%#include %@AE@%"message.h" %@NL@%
  15215. %@NL@%
  15216. extern unsigned char Attr;%@NL@%
  15217. %@NL@%
  15218. int%@NL@%
  15219. vf_cmd(fp, fname, numbers)%@NL@%
  15220. FILE *fp;%@NL@%
  15221. char *fname;%@NL@%
  15222. BOOLEAN numbers;%@NL@%
  15223. {%@NL@%
  15224.         register int i;                %@AB@%/* general index */%@AE@%%@NL@%
  15225.         unsigned int offset;        %@AB@%/* horizontal scroll offset */%@AE@%%@NL@%
  15226.         unsigned int n;                %@AB@%/* relative line number */%@AE@%%@NL@%
  15227.         int memerr;                %@AB@%/* flag for memory allocation errors */%@AE@%%@NL@%
  15228.         char *s, lbuf[MAXLINE];        %@AB@%/* input line buffer and pointer */%@AE@%%@NL@%
  15229.         int k;                        %@AB@%/* key code (see keydefs.h) */%@AE@%%@NL@%
  15230.         int radix = 10;                %@AB@%/* base for number-to-character conversions */%@AE@%%@NL@%
  15231.         char number[17];        %@AB@%/* buffer for conversions */%@AE@%%@NL@%
  15232.         int errcount = 0;        %@AB@%/* error counter */%@AE@%%@NL@%
  15233.         DNODE *tmp;                %@AB@%/* pointer to buffer control nodes */%@AE@%%@NL@%
  15234.         DIRECTION srchdir;        %@AB@%/* search direction */%@AE@%%@NL@%
  15235.         char *ss;                %@AB@%/* pointer to search string */%@AE@%%@NL@%
  15236.         static char srchstr[MAXSTR] = { "" };        %@AB@%/* search string buffer */%@AE@%%@NL@%
  15237.         DNODE *head;                %@AB@%/* pointer to starting node of text buffer list */%@AE@%%@NL@%
  15238.         DNODE *current;                %@AB@%/* pointer to the current node (text line) */%@AE@%%@NL@%
  15239.         static DNODE *freelist;        %@AB@%/* pointer to starting node of "free" list */%@AE@%%@NL@%
  15240.                                 %@AB@%/* initialized to 0 at runtime; retains value */%@AE@%%@NL@%
  15241. %@NL@%
  15242.         %@AB@%/* function prototypes */%@AE@%%@NL@%
  15243.         static void prtshift(int, int);%@NL@%
  15244.         extern DNODE *vf_mklst();%@NL@%
  15245.         extern DNODE *vf_alloc(int);%@NL@%
  15246.         extern DNODE *vf_ins(DNODE *, DNODE *);%@NL@%
  15247.         extern DNODE *vf_del(DNODE *, DNODE *);%@NL@%
  15248.         extern DNODE *search(DNODE *, DIRECTION, char *);%@NL@%
  15249.         extern DNODE *gotoln(DNODE *);%@NL@%
  15250.         extern char *getxline(char *, int, FILE *);%@NL@%
  15251.         extern char *getsstr(char *);%@NL@%
  15252.         extern int clrscrn(unsigned char);%@NL@%
  15253.         extern void showhelp(unsigned char);%@NL@%
  15254.         extern void clrmsg();%@NL@%
  15255.         extern void vf_dspy(DNODE *, DNODE *, int, BOOLEAN);%@NL@%
  15256.         extern int putstr(char *, int);%@NL@%
  15257.         extern int writec(char, int, int);%@NL@%
  15258.         extern char *nlerase(char *);%@NL@%
  15259. %@NL@%
  15260.         %@AB@%/* display the file name */%@AE@%%@NL@%
  15261.         offset = 0;%@NL@%
  15262.         putcur(HEADROW, 0, Vpage);%@NL@%
  15263.         writec(' ', Maxcol[Vmode], Vpage);%@NL@%
  15264.         putstr("File: ", Vpage);%@NL@%
  15265.         putstr(fname, Vpage);%@NL@%
  15266. %@NL@%
  15267.         %@AB@%/* establish the text buffer */%@AE@%%@NL@%
  15268.         memerr = 0;%@NL@%
  15269.         if ((head = vf_mklst()) == NULL)%@NL@%
  15270.                 ++memerr;%@NL@%
  15271.         if (freelist == NULL && (freelist = vf_alloc(N_NODES)) == NULL)%@NL@%
  15272.                 ++memerr;%@NL@%
  15273.         if (memerr) {%@NL@%
  15274.                 clean();%@NL@%
  15275.                 fprintf(stderr, "Memory allocation error\n");%@NL@%
  15276.                 exit(1);%@NL@%
  15277.         }%@NL@%
  15278. %@NL@%
  15279.         %@AB@%/* read the file into the buffer */%@AE@%%@NL@%
  15280.         current = head;%@NL@%
  15281.         n = 0;%@NL@%
  15282.         while ((s = getxline(lbuf, MAXLINE, fp)) != NULL) {%@NL@%
  15283.                 %@AB@%/* add a node to the list */%@AE@%%@NL@%
  15284.                 if ((freelist = vf_ins(current, freelist)) == NULL)%@NL@%
  15285.                         ++memerr;%@NL@%
  15286.                 current = current->d_next;%@NL@%
  15287. %@NL@%
  15288.                 %@AB@%/* save the received text in a line buffer */%@AE@%%@NL@%
  15289.                 if ((current->d_line = strdup(nlerase(s))) == NULL)%@NL@%
  15290.                         ++memerr;%@NL@%
  15291.                 if (memerr) {%@NL@%
  15292.                         clean();%@NL@%
  15293.                         fprintf(stderr, "File too big to load\n");%@NL@%
  15294.                         exit(1);%@NL@%
  15295.                 }%@NL@%
  15296.                 current->d_lnum = ++n;%@NL@%
  15297.                 current->d_flags = 0;%@NL@%
  15298.         }%@NL@%
  15299. %@NL@%
  15300.         %@AB@%/* show the file size as a count of lines */%@AE@%%@NL@%
  15301.         putstr(" (", Vpage);%@NL@%
  15302.         putstr(itoa(current->d_lnum, number, radix), Vpage);%@NL@%
  15303.         putstr(" lines)", Vpage);%@NL@%
  15304.         prtshift(offset, Vpage);%@NL@%
  15305.         current = head->d_next;%@NL@%
  15306.         vf_dspy(head, current, offset, numbers);%@NL@%
  15307. %@NL@%
  15308.         %@AB@%/* process user commands */%@AE@%%@NL@%
  15309.         while ((k = getkey()) != K_ESC) {%@NL@%
  15310.                 clrmsg();%@NL@%
  15311.                 switch (k) {%@NL@%
  15312.                 case 'b':%@NL@%
  15313.                 case 'B':%@NL@%
  15314.                 case K_HOME:%@NL@%
  15315.                         current = head->d_next;%@NL@%
  15316.                         break;%@NL@%
  15317.                 case 'e':%@NL@%
  15318.                 case 'E':%@NL@%
  15319.                 case K_END:%@NL@%
  15320.                         current = head->d_prev;%@NL@%
  15321.                         i = NROWS - 1;%@NL@%
  15322.                         while (i-- > 0)%@NL@%
  15323.                                 if (current->d_prev != head->d_next)%@NL@%
  15324.                                         current = current->d_prev;%@NL@%
  15325.                         break;%@NL@%
  15326.                 case K_PGUP:%@NL@%
  15327.                 case 'u':%@NL@%
  15328.                 case 'U':%@NL@%
  15329.                         i = NROWS - OVERLAP;%@NL@%
  15330.                         while (i-- > 0)%@NL@%
  15331.                                 if (current != head->d_next)%@NL@%
  15332.                                         current = current->d_prev;%@NL@%
  15333.                         break;%@NL@%
  15334.                 case K_PGDN:%@NL@%
  15335.                 case 'd':%@NL@%
  15336.                 case 'D':%@NL@%
  15337.                         i = NROWS - OVERLAP;%@NL@%
  15338.                         while (i-- > 0)%@NL@%
  15339.                                 if (current != head->d_prev)%@NL@%
  15340.                                         current = current->d_next;%@NL@%
  15341.                         break;%@NL@%
  15342.                 case K_UP:%@NL@%
  15343.                 case '-':%@NL@%
  15344.                         if (current == head->d_next)%@NL@%
  15345.                                 continue;%@NL@%
  15346.                         current = current->d_prev;%@NL@%
  15347.                         break;%@NL@%
  15348.                 case K_DOWN:%@NL@%
  15349.                 case '+':%@NL@%
  15350.                         if (current == head->d_prev)%@NL@%
  15351.                                 continue;%@NL@%
  15352.                         current = current->d_next;%@NL@%
  15353.                         break;%@NL@%
  15354.                 case K_RIGHT:%@NL@%
  15355.                 case '>':%@NL@%
  15356.                 case '.':%@NL@%
  15357.                         if (offset < MAXLINE - SHIFTWIDTH)%@NL@%
  15358.                                 offset += SHIFTWIDTH;%@NL@%
  15359.                         prtshift(offset, Vpage);%@NL@%
  15360.                         break;%@NL@%
  15361.                 case K_LEFT:%@NL@%
  15362.                 case '<':%@NL@%
  15363.                 case ',':%@NL@%
  15364.                         if ((offset -= SHIFTWIDTH) < 0)%@NL@%
  15365.                                 offset = 0;%@NL@%
  15366.                         prtshift(offset, Vpage);%@NL@%
  15367.                         break;%@NL@%
  15368.                 case K_ALTG:%@NL@%
  15369.                 case 'g':%@NL@%
  15370.                 case 'G':%@NL@%
  15371.                         if ((tmp = gotoln(head)) == NULL)%@NL@%
  15372.                                 continue;%@NL@%
  15373.                         current = tmp;%@NL@%
  15374.                         break;%@NL@%
  15375.                 case K_ALTH:%@NL@%
  15376.                 case 'h':%@NL@%
  15377.                 case 'H':%@NL@%
  15378.                 case '?':%@NL@%
  15379.                         showhelp(Attr);%@NL@%
  15380.                         break;%@NL@%
  15381.                 case K_ALTN:%@NL@%
  15382.                 case 'n':%@NL@%
  15383.                 case 'N':%@NL@%
  15384.                         numbers = (numbers == TRUE) ? FALSE : TRUE;%@NL@%
  15385.                         break;%@NL@%
  15386.                 case K_ALTQ:%@NL@%
  15387.                 case 'q':%@NL@%
  15388.                 case 'Q':%@NL@%
  15389.                         clrscrn(Attr);%@NL@%
  15390.                         putcur(0, 0, Vpage);%@NL@%
  15391.                         return (-1);%@NL@%
  15392.                 case 'r':%@NL@%
  15393.                 case 'R':%@NL@%
  15394.                 case '\\':%@NL@%
  15395.                         srchdir = BACKWARD;%@NL@%
  15396.                         ss = getsstr(srchstr);%@NL@%
  15397.                         if (ss == NULL)%@NL@%
  15398.                                 %@AB@%/* cancel search */%@AE@%%@NL@%
  15399.                                 break;%@NL@%
  15400.                         if (strlen(ss) > 0)%@NL@%
  15401.                                 strcpy(srchstr, ss);%@NL@%
  15402.                         if ((tmp = search(current, srchdir, srchstr)) == NULL)%@NL@%
  15403.                                 continue;%@NL@%
  15404.                         current = tmp;%@NL@%
  15405.                         break;%@NL@%
  15406.                 case 's':%@NL@%
  15407.                 case 'S':%@NL@%
  15408.                 case '/':%@NL@%
  15409.                         srchdir = FORWARD;%@NL@%
  15410.                         ss = getsstr(srchstr);%@NL@%
  15411.                         if (ss == NULL)%@NL@%
  15412.                                 %@AB@%/* cancel search */%@AE@%%@NL@%
  15413.                                 break;%@NL@%
  15414.                         if (strlen(ss) > 0)%@NL@%
  15415.                                 strcpy(srchstr, ss);%@NL@%
  15416.                         if ((tmp = search(current, srchdir, srchstr)) == NULL)%@NL@%
  15417.                                 continue;%@NL@%
  15418.                         current = tmp;%@NL@%
  15419.                         break;%@NL@%
  15420.                 default:%@NL@%
  15421.                         %@AB@%/* ignore all other keys */%@AE@%%@NL@%
  15422.                         continue;%@NL@%
  15423.                 }%@NL@%
  15424.                 vf_dspy(head, current, offset, numbers);%@NL@%
  15425.         }%@NL@%
  15426.         clrmsg();%@NL@%
  15427. %@NL@%
  15428.         %@AB@%/* release the allocated text buffer memory */%@AE@%%@NL@%
  15429.         while (head->d_next != head) {%@NL@%
  15430.                 %@AB@%/* release text buffer */%@AE@%%@NL@%
  15431.                 free(head->d_next->d_line);%@NL@%
  15432. %@NL@%
  15433.                 %@AB@%/* put node back on the freelist */%@AE@%%@NL@%
  15434.                 freelist = vf_del(head->d_next, freelist);%@NL@%
  15435.         }%@NL@%
  15436.         %@AB@%/* release the list header node */%@AE@%%@NL@%
  15437.         free((char *)head);%@NL@%
  15438. %@NL@%
  15439.         return (errcount);%@NL@%
  15440. }%@NL@%
  15441. %@NL@%
  15442. %@AB@%/*%@NL@%
  15443. %@AB@% *        prtshift -- display the number of columns of horizontal shift%@NL@%
  15444. %@AB@% */%@AE@%%@NL@%
  15445. %@NL@%
  15446. %@AI@%#define %@AE@%SHFTDSP        5 %@NL@%
  15447. %@NL@%
  15448. static void%@NL@%
  15449. prtshift(amt, pg)%@NL@%
  15450. int amt, pg;%@NL@%
  15451. {%@NL@%
  15452.         char number[17];%@NL@%
  15453.         int radix = 10;%@NL@%
  15454. %@NL@%
  15455.         %@AB@%/* clear the shift display area */%@AE@%%@NL@%
  15456.         putcur(1, Maxcol[Vmode] - 1 - SHFTDSP, pg);%@NL@%
  15457.         writec(' ', SHFTDSP, pg);%@NL@%
  15458. %@NL@%
  15459.         %@AB@%/* display the new shift amount, if any */%@AE@%%@NL@%
  15460.         if (amt > 0) {%@NL@%
  15461.                 putstr(itoa(amt, number, radix), pg);%@NL@%
  15462.                 putstr("->", pg);%@NL@%
  15463.         }%@NL@%
  15464. }%@NL@%
  15465. %@NL@%
  15466. %@NL@%
  15467. %@2@%%@AH@%VF_DSPY.C%@AE@%%@EH@%%@NL@%
  15468. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\14VIEW\VF_DSPY.C%@AE@%%@NL@%
  15469. %@NL@%
  15470. %@AB@%/*%@NL@%
  15471. %@AB@% *        vf_dspy -- display a screen page%@NL@%
  15472. %@AB@% */%@AE@%%@NL@%
  15473. %@NL@%
  15474. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  15475. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  15476. %@AI@%#include %@AE@%<string.h> %@NL@%
  15477. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  15478. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  15479. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  15480. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  15481. %@AI@%#include %@AE@%"vf.h" %@NL@%
  15482. %@NL@%
  15483. %@AB@%/* number field width */%@AE@%%@NL@%
  15484. %@AI@%#define %@AE@%NFW        8 %@NL@%
  15485. %@NL@%
  15486. void%@NL@%
  15487. vf_dspy(buf, lp, os, numbers)%@NL@%
  15488. DNODE *buf;%@NL@%
  15489. register DNODE *lp;%@NL@%
  15490. int os;%@NL@%
  15491. BOOLEAN numbers;%@NL@%
  15492. {%@NL@%
  15493.         register int i;%@NL@%
  15494.         int j;%@NL@%
  15495.         int textwidth;%@NL@%
  15496.         char *cp;%@NL@%
  15497.         char nbuf[NFW + 1];%@NL@%
  15498. %@NL@%
  15499.         textwidth = Maxcol[Vmode];%@NL@%
  15500.         if (numbers == TRUE)%@NL@%
  15501.                 textwidth -= NFW;%@NL@%
  15502.                 %@NL@%
  15503.         for (i = 0; i < NROWS; ++i) {%@NL@%
  15504.                 putcur(TOPROW + i, 0, Vpage);%@NL@%
  15505.                 cp = lp->d_line;%@NL@%
  15506.                 if (numbers == TRUE) {%@NL@%
  15507.                         sprintf(nbuf, "%6u", lp->d_lnum);%@NL@%
  15508.                         putfld(nbuf, NFW, Vpage);%@NL@%
  15509.                         putcur(TOPROW + i, NFW, Vpage);%@NL@%
  15510.                 }%@NL@%
  15511.                 if (os < strlen(cp))%@NL@%
  15512.                         putfld(cp + os, textwidth, Vpage);%@NL@%
  15513.                 else%@NL@%
  15514.                         writec(' ', textwidth, Vpage);%@NL@%
  15515.                 if (lp == buf->d_prev) {%@NL@%
  15516.                         ++i;%@NL@%
  15517.                         break;        %@AB@%/* no more displayable lines */%@AE@%%@NL@%
  15518.                 }%@NL@%
  15519.                 else%@NL@%
  15520.                         lp = lp->d_next;%@NL@%
  15521.         }%@NL@%
  15522. %@NL@%
  15523.         %@AB@%/* clear and mark any unused lines */%@AE@%%@NL@%
  15524.         for ( ; i < NROWS; ++i) {%@NL@%
  15525.                 putcur(i + TOPROW, 0, Vpage);%@NL@%
  15526.                 writec(' ', Maxcol[Vmode], Vpage);%@NL@%
  15527.                 writec('~', 1, Vpage);%@NL@%
  15528.         }%@NL@%
  15529.         return;%@NL@%
  15530. }%@NL@%
  15531. %@NL@%
  15532. %@NL@%
  15533. %@2@%%@AH@%VF_LIST.C%@AE@%%@EH@%%@NL@%
  15534. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\14VIEW\VF_LIST.C%@AE@%%@NL@%
  15535. %@NL@%
  15536. %@AB@%/*%@NL@%
  15537. %@AB@% *        vf_list -- linked list management functions%@NL@%
  15538. %@AB@% */%@AE@%%@NL@%
  15539. %@NL@%
  15540. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  15541. %@AI@%#include %@AE@%<malloc.h> %@NL@%
  15542. %@AI@%#include %@AE@%"vf.h" %@NL@%
  15543. %@NL@%
  15544. %@AB@%/*%@NL@%
  15545. %@AB@% *        vf_mklst -- create a new list by allocating a node,%@NL@%
  15546. %@AB@% *        making it point to itself, and setting its values%@NL@%
  15547. %@AB@% *        to zero (appropriately cast)%@NL@%
  15548. %@AB@% */%@AE@%%@NL@%
  15549. %@NL@%
  15550. DNODE *%@NL@%
  15551. vf_mklst()%@NL@%
  15552. {%@NL@%
  15553.         DNODE *new;%@NL@%
  15554. %@NL@%
  15555.         new = (DNODE *)malloc(sizeof (DNODE));%@NL@%
  15556.         if (new != NULL) {%@NL@%
  15557.                 new->d_next = new;%@NL@%
  15558.                 new->d_prev = new;%@NL@%
  15559.                 new->d_lnum = new->d_flags = 0;%@NL@%
  15560.                 new->d_line = (char *)NULL;%@NL@%
  15561.         }%@NL@%
  15562.         return (new);%@NL@%
  15563. } %@AB@%/* end vf_mklst() */%@AE@%%@NL@%
  15564. %@NL@%
  15565. %@NL@%
  15566. %@AB@%/*%@NL@%
  15567. %@AB@% *        vf_alloc -- create a pool of available nodes%@NL@%
  15568. %@AB@% */%@AE@%%@NL@%
  15569. %@NL@%
  15570. DNODE *%@NL@%
  15571. vf_alloc(n)%@NL@%
  15572. int n;%@NL@%
  15573. {%@NL@%
  15574.         register DNODE *new;%@NL@%
  15575.         register DNODE *tmp;%@NL@%
  15576. %@NL@%
  15577.         %@AB@%/* allocate a block of n nodes */%@AE@%%@NL@%
  15578.         new = (DNODE *)malloc(n * sizeof (DNODE));%@NL@%
  15579. %@NL@%
  15580.         %@AB@%/* if allocation OK, string the nodes in one direction */%@AE@%%@NL@%
  15581.         if (new != NULL) {%@NL@%
  15582.                 for (tmp = new; 1 + tmp - new < n; tmp = tmp->d_next)%@NL@%
  15583.                         tmp->d_next = tmp + 1;%@NL@%
  15584.                 tmp->d_next = (DNODE *)NULL;%@NL@%
  15585.         }%@NL@%
  15586. %@NL@%
  15587.         return (new);        %@AB@%/* pointer to free list */%@AE@%%@NL@%
  15588. } %@AB@%/* end vf_alloc() */%@AE@%%@NL@%
  15589. %@NL@%
  15590. %@NL@%
  15591. %@AB@%/*%@NL@%
  15592. %@AB@% *        vf_ins -- insert a node into a list after the specified node%@NL@%
  15593. %@AB@% */%@AE@%%@NL@%
  15594. %@NL@%
  15595. DNODE *%@NL@%
  15596. vf_ins(node, avail)%@NL@%
  15597. DNODE *node, *avail;%@NL@%
  15598. {%@NL@%
  15599.         DNODE *tmp;%@NL@%
  15600.         DNODE *vf_alloc(int);%@NL@%
  15601. %@NL@%
  15602.         %@AB@%/*%@NL@%
  15603. %@AB@%         *  check freelist -- get another block of nodes%@NL@%
  15604. %@AB@%         *  if the list is almost empty%@NL@%
  15605. %@AB@%         */%@AE@%%@NL@%
  15606.         if (avail->d_next == NULL)%@NL@%
  15607.                 if ((avail->d_next = vf_alloc(N_NODES)) == NULL)%@NL@%
  15608.                         %@AB@%/* not enough memory */%@AE@%%@NL@%
  15609.                         return (DNODE *)NULL;%@NL@%
  15610. %@NL@%
  15611.         %@AB@%/* get a node from the freelist */%@AE@%%@NL@%
  15612.         tmp = avail;%@NL@%
  15613.         avail = avail->d_next;%@NL@%
  15614. %@NL@%
  15615.         %@AB@%/* insert the node into the list after node */%@AE@%%@NL@%
  15616.         tmp->d_prev = node;%@NL@%
  15617.         tmp->d_next = node->d_next;%@NL@%
  15618.         node->d_next->d_prev = tmp;%@NL@%
  15619.         node->d_next = tmp;%@NL@%
  15620. %@NL@%
  15621.         %@AB@%/* point to next node in the freelist */%@AE@%%@NL@%
  15622.         return (avail);%@NL@%
  15623. } %@AB@%/* end vf_ins() */%@AE@%%@NL@%
  15624. %@NL@%
  15625. %@NL@%
  15626. %@AB@%/*%@NL@%
  15627. %@AB@% *        vf_del -- delete a node from a list%@NL@%
  15628. %@AB@% */%@AE@%%@NL@%
  15629. %@NL@%
  15630. DNODE *%@NL@%
  15631. vf_del(node, avail)%@NL@%
  15632. DNODE *node, *avail;%@NL@%
  15633. {%@NL@%
  15634.         %@AB@%/* unlink the node from the list */%@AE@%%@NL@%
  15635.         node->d_prev->d_next = node->d_next;%@NL@%
  15636.         node->d_next->d_prev = node->d_prev;%@NL@%
  15637. %@NL@%
  15638.         %@AB@%/* return the deleted node to the freelist */%@AE@%%@NL@%
  15639.         node->d_next = avail;%@NL@%
  15640.         avail = node;%@NL@%
  15641. %@NL@%
  15642.         %@AB@%/* point to the new freelist node */%@AE@%%@NL@%
  15643.         return (avail);%@NL@%
  15644. } %@AB@%/* end vf_del() */%@AE@%%@NL@%
  15645. %@NL@%
  15646. %@NL@%
  15647. %@2@%%@AH@%VF_SRCH.C%@AE@%%@EH@%%@NL@%
  15648. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\14VIEW\VF_SRCH.C%@AE@%%@NL@%
  15649. %@NL@%
  15650. %@AB@%/*%@NL@%
  15651. %@AB@% *        vf_srch -- search functions%@NL@%
  15652. %@AB@% */%@AE@%%@NL@%
  15653. %@NL@%
  15654. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  15655. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  15656. %@AI@%#include %@AE@%<string.h> %@NL@%
  15657. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  15658. %@AI@%#include %@AE@%"vf.h" %@NL@%
  15659. %@NL@%
  15660. %@AB@%/*%@NL@%
  15661. %@AB@% *        search -- search for a literal string in the buffer%@NL@%
  15662. %@AB@% */%@AE@%%@NL@%
  15663. %@NL@%
  15664. DNODE *%@NL@%
  15665. search(buf, dir, str)%@NL@%
  15666. DNODE *buf;%@NL@%
  15667. DIRECTION dir;%@NL@%
  15668. char *str;%@NL@%
  15669. {%@NL@%
  15670.         int n;%@NL@%
  15671.         register DNODE *lp;%@NL@%
  15672.         register char *cp;%@NL@%
  15673. %@NL@%
  15674.         extern void showmsg(char *);%@NL@%
  15675. %@NL@%
  15676.         %@AB@%/* try to find a match -- wraps around buffer boundaries */%@AE@%%@NL@%
  15677.         n = strlen(str);%@NL@%
  15678.         lp = (dir == FORWARD) ? buf->d_next : buf->d_prev;%@NL@%
  15679.         while (lp != buf) {%@NL@%
  15680.                 if ((cp = lp->d_line) != NULL)        %@AB@%/* skip over header node */%@AE@%%@NL@%
  15681.                         while (*cp != '\n' && *cp != '\0') {%@NL@%
  15682.                                 if (strncmp(cp, str, n) == 0)%@NL@%
  15683.                                         return (lp);%@NL@%
  15684.                                 ++cp;%@NL@%
  15685.                         }%@NL@%
  15686.                 lp = (dir == FORWARD) ? lp->d_next : lp->d_prev;%@NL@%
  15687.         }%@NL@%
  15688.         showmsg("Not found");%@NL@%
  15689.         return ((DNODE *)NULL);%@NL@%
  15690. }%@NL@%
  15691. %@NL@%
  15692. %@NL@%
  15693. %@AB@%/*%@NL@%
  15694. %@AB@% *        getsstr -- prompt the user for a search string%@NL@%
  15695. %@AB@% */%@AE@%%@NL@%
  15696. %@NL@%
  15697. extern int Startscan, Endscan;%@NL@%
  15698. %@NL@%
  15699. char *%@NL@%
  15700. getsstr(str)%@NL@%
  15701. char *str;%@NL@%
  15702. {%@NL@%
  15703.         char line[MAXSTR];%@NL@%
  15704.         char *resp;%@NL@%
  15705. %@NL@%
  15706.         extern int putstr(char *, int);%@NL@%
  15707.         extern char *getstr(char *, int);%@NL@%
  15708.         extern int put_ch(char, int);%@NL@%
  15709.         extern void showmsg(char *);%@NL@%
  15710.         extern void clrmsg();%@NL@%
  15711. %@NL@%
  15712.         static char prompt[] = { "Search for: " };%@NL@%
  15713. %@NL@%
  15714.         %@AB@%/* get search string */%@AE@%%@NL@%
  15715.         showmsg(prompt);%@NL@%
  15716.         setctype(Startscan, Endscan);                %@AB@%/* cursor on */%@AE@%%@NL@%
  15717.         resp = getstr(line, MAXSTR - strlen(prompt));%@NL@%
  15718.         setctype(MAXSCAN, MAXSCAN);                %@AB@%/* cursor off */%@AE@%%@NL@%
  15719.         if (resp == NULL)%@NL@%
  15720.                 return (char *)NULL;%@NL@%
  15721.         if (strlen(resp) == 0)%@NL@%
  15722.                 return (str);%@NL@%
  15723.         showmsg(resp);%@NL@%
  15724.         return (resp);%@NL@%
  15725. }%@NL@%
  15726. %@NL@%
  15727. %@NL@%
  15728. %@2@%%@AH@%VF_UTIL.C%@AE@%%@EH@%%@NL@%
  15729. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\14VIEW\VF_UTIL.C%@AE@%%@NL@%
  15730. %@NL@%
  15731. %@AB@%/*%@NL@%
  15732. %@AB@% *        vf_util -- utility functions for ViewFile%@NL@%
  15733. %@AB@% */%@AE@%%@NL@%
  15734. %@NL@%
  15735. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  15736. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  15737. %@AI@%#include %@AE@%<string.h> %@NL@%
  15738. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  15739. %@AI@%#include %@AE@%<local\video.h> %@NL@%
  15740. %@AI@%#include %@AE@%<local\keydefs.h> %@NL@%
  15741. %@AI@%#include %@AE@%"vf.h" %@NL@%
  15742. %@NL@%
  15743. extern int Startscan, Endscan;%@NL@%
  15744. %@NL@%
  15745. %@AI@%#define %@AE@%NDIGITS        6 %@NL@%
  15746. %@NL@%
  15747. %@AB@%/*%@NL@%
  15748. %@AB@% *        gotoln -- jump to an absolute line number%@NL@%
  15749. %@AB@% */%@AE@%%@NL@%
  15750. %@NL@%
  15751. DNODE *%@NL@%
  15752. gotoln(buf)%@NL@%
  15753. DNODE *buf;%@NL@%
  15754. {%@NL@%
  15755.         register int ln;%@NL@%
  15756.         register DNODE *lp;%@NL@%
  15757.         char line[NDIGITS + 1];%@NL@%
  15758. %@NL@%
  15759.         extern void showmsg(char *);%@NL@%
  15760.         extern char *getstr(char *, int);%@NL@%
  15761. %@NL@%
  15762.         %@AB@%/* get line number from user */%@AE@%%@NL@%
  15763.         showmsg("Line number: ");%@NL@%
  15764.         setctype(Startscan, Endscan);                        %@AB@%/* cursor on */%@AE@%%@NL@%
  15765.         ln = atoi(getstr(line, NDIGITS + 1));%@NL@%
  15766.         setctype(MAXSCAN, MAXSCAN);                        %@AB@%/* cursor off */%@AE@%%@NL@%
  15767. %@NL@%
  15768.         %@AB@%/* check boundary conditions */%@AE@%%@NL@%
  15769.         if (ln > buf->d_prev->d_lnum || ln <= 0) {%@NL@%
  15770.                 showmsg("Line out of range");%@NL@%
  15771.                 return ((DNODE *)NULL);%@NL@%
  15772.         }%@NL@%
  15773. %@NL@%
  15774.         %@AB@%/* find the line */%@AE@%%@NL@%
  15775.         for (lp = buf->d_next; ln != lp->d_lnum; lp = lp->d_next)%@NL@%
  15776.                 ;%@NL@%
  15777.         return (lp);%@NL@%
  15778. }%@NL@%
  15779. %@NL@%
  15780. %@NL@%
  15781. %@AB@%/*%@NL@%
  15782. %@AB@% *        showhelp -- display a help frame%@NL@%
  15783. %@AB@% */%@AE@%%@NL@%
  15784. %@NL@%
  15785. %@AI@%#define %@AE@%HELPROW        TOPROW + 3 %@NL@%
  15786. %@AI@%#define %@AE@%HELPCOL        10 %@NL@%
  15787. %@AI@%#define %@AE@%VBORDER        1 %@NL@%
  15788. %@AI@%#define %@AE@%HBORDER        2 %@NL@%
  15789. %@NL@%
  15790. void%@NL@%
  15791. showhelp(textattr)%@NL@%
  15792. unsigned char textattr;        %@AB@%/* attribute of text area */%@AE@%%@NL@%
  15793. {%@NL@%
  15794.         register int i, n;%@NL@%
  15795.         int nlines, ncols;%@NL@%
  15796.         unsigned char helpattr;%@NL@%
  15797.         static char *help[] = {%@NL@%
  15798.                 "PgUp (U)        Scroll up the file one screen page",%@NL@%
  15799.                 "PgDn (D)        Scroll down the file one screen page",%@NL@%
  15800.                 "Up arrow (-)    Scroll up in the file one line",%@NL@%
  15801.                 "Down arrow (+)  Scroll down in the file one line",%@NL@%
  15802.                 "Right arrow (>) Scroll right by 20 columns",%@NL@%
  15803.                 "Left arrow (<)  Scroll left by 20 columns",%@NL@%
  15804.                 "Home (B)        Go to beginning of file buffer",%@NL@%
  15805.                 "End (E)         Go to end of file buffer",%@NL@%
  15806.                 "Alt-g (G)       Go to a specified line in the buffer",%@NL@%
  15807.                 "Alt-h (H or ?)  Display this help frame",%@NL@%
  15808.                 "Alt-n (N)       Toggle line-numbering feature",%@NL@%
  15809.                 "\\ (R)           Reverse search for a literal text string",%@NL@%
  15810.                 "/ (S)           Search forward for a literal text string",%@NL@%
  15811.                 "Esc             Next file from list (quits if none)",%@NL@%
  15812.                 "Alt-q (Q)       Quit",%@NL@%
  15813.                 "--------------------------------------------------------",%@NL@%
  15814.                 "            << Press a key to continue >>",%@NL@%
  15815.                 (char *)NULL%@NL@%
  15816.         };%@NL@%
  15817. %@NL@%
  15818.         %@AB@%/* prepare help window */%@AE@%%@NL@%
  15819.         ncols = 0;%@NL@%
  15820.         for (i = 0; help[i] != (char *)NULL; ++i)%@NL@%
  15821.                 if ((n = strlen(help[i])) > ncols)%@NL@%
  15822.                         ncols = n;%@NL@%
  15823.         nlines = i - 1;%@NL@%
  15824.         --ncols;%@NL@%
  15825.         helpattr = (RED << 4) | BWHT;%@NL@%
  15826.         clrw(HELPROW - VBORDER, HELPCOL - HBORDER,%@NL@%
  15827.                 HELPROW + nlines + VBORDER, HELPCOL + ncols + HBORDER,%@NL@%
  15828.                 helpattr);%@NL@%
  15829.         drawbox(HELPROW - VBORDER, HELPCOL - HBORDER,%@NL@%
  15830.                 HELPROW + nlines + VBORDER, HELPCOL + ncols + HBORDER,%@NL@%
  15831.                 Vpage);%@NL@%
  15832. %@NL@%
  15833.         %@AB@%/* display the help text */%@AE@%%@NL@%
  15834.         for (i = 0; help[i] != (char *)NULL; ++i) {%@NL@%
  15835.                 putcur(HELPROW + i, HELPCOL, Vpage);%@NL@%
  15836.                 putstr(help[i], Vpage);%@NL@%
  15837.         }%@NL@%
  15838. %@NL@%
  15839.         %@AB@%/* pause until told by a keypress to proceed */%@AE@%%@NL@%
  15840.         getkey();%@NL@%
  15841. %@NL@%
  15842.         %@AB@%/* restore help display area to the text attribute */%@AE@%%@NL@%
  15843.         clrw(HELPROW - VBORDER, HELPCOL - HBORDER,%@NL@%
  15844.                 HELPROW + nlines + VBORDER, HELPCOL + ncols + HBORDER,%@NL@%
  15845.                 textattr);%@NL@%
  15846. }%@NL@%
  15847. %@NL@%
  15848. %@NL@%
  15849. %@2@%%@AH@%WARN.C%@AE@%%@EH@%%@NL@%
  15850. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\UTIL\WARN.C%@AE@%%@NL@%
  15851. %@NL@%
  15852. %@AB@%/*%@NL@%
  15853. %@AB@% *        warn -- display a warning message in a highly%@NL@%
  15854. %@AB@% *        visible place; return number of characters written%@NL@%
  15855. %@AB@% */%@AE@%%@NL@%
  15856. %@NL@%
  15857. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  15858.  %@NL@%
  15859. int%@NL@%
  15860. warn(pname, mesg)%@NL@%
  15861. char *pname, *mesg;%@NL@%
  15862. {%@NL@%
  15863.         return (fprintf(stderr, "%s: %s\n", pname, mesg));%@NL@%
  15864. }%@NL@%
  15865. %@NL@%
  15866. %@NL@%
  15867. %@2@%%@AH@%WRITEA.C%@AE@%%@EH@%%@NL@%
  15868. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\WRITEA.C%@AE@%%@NL@%
  15869. %@NL@%
  15870. %@AB@%/*%@NL@%
  15871. %@AB@% *        writea -- write attribute only to screen memory (faked by%@NL@%
  15872. %@AB@% *        reading char and attr and writing back the original%@NL@%
  15873. %@AB@% *        character and the new attribute at each position)%@NL@%
  15874. %@AB@% */%@AE@%%@NL@%
  15875. %@NL@%
  15876. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  15877. %@NL@%
  15878. int writea(a, n, pg)%@NL@%
  15879. unsigned char a;%@AB@%/* video attribute */%@AE@%%@NL@%
  15880. int n;                %@AB@%/* number of positions to write */%@AE@%%@NL@%
  15881. int pg;                %@AB@%/* screen page */%@AE@%%@NL@%
  15882. {%@NL@%
  15883.         int i;%@NL@%
  15884.         int status;%@NL@%
  15885.         unsigned short chx, attrx;%@NL@%
  15886.         unsigned short r, c;%@NL@%
  15887. %@NL@%
  15888.         %@AB@%/* get starting (current) position */%@AE@%%@NL@%
  15889.         status = 0;%@NL@%
  15890.         status = readcur(&r, &c, pg);%@NL@%
  15891.         for (i = 0; i < n; ++i) {%@NL@%
  15892.                 status += putcur(r, c + i, pg);%@NL@%
  15893.                 status += readca(&chx, &attrx, pg);%@NL@%
  15894.                 status += writeca(chx, a, 1, pg);%@NL@%
  15895.         }%@NL@%
  15896. %@NL@%
  15897.         %@AB@%/* restore cursor position */%@AE@%%@NL@%
  15898.         status += putcur(r, c, pg);%@NL@%
  15899. %@NL@%
  15900.         return (status);%@NL@%
  15901. }%@NL@%
  15902. %@NL@%
  15903. %@NL@%
  15904. %@2@%%@AH@%WRITEC.C%@AE@%%@EH@%%@NL@%
  15905. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\WRITEC.C%@AE@%%@NL@%
  15906. %@NL@%
  15907. %@AB@%/*%@NL@%
  15908. %@AB@% *        writec -- write a character only%@NL@%
  15909. %@AB@% *        (leave attribute undisturbed)%@NL@%
  15910. %@AB@% */%@AE@%%@NL@%
  15911. %@NL@%
  15912. %@AI@%#include %@AE@%<dos.h> %@NL@%
  15913. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  15914. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  15915. %@NL@%
  15916. int%@NL@%
  15917. writec(ch, count, pg)%@NL@%
  15918. unsigned char ch;        %@AB@%/* character */%@AE@%%@NL@%
  15919. int count;                %@AB@%/* repetitions */%@AE@%%@NL@%
  15920. int pg;                        %@AB@%/* screen page for writes */%@AE@%%@NL@%
  15921. {%@NL@%
  15922.         union REGS inregs, outregs;%@NL@%
  15923. %@NL@%
  15924.         inregs.h.ah = WRITE_CHAR;%@NL@%
  15925.         inregs.h.al = ch;%@NL@%
  15926.         inregs.h.bh = pg;%@NL@%
  15927.         inregs.x.cx = count;%@NL@%
  15928.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  15929. %@NL@%
  15930.         return (outregs.x.cflag);%@NL@%
  15931. }%@NL@%
  15932. %@NL@%
  15933. %@NL@%
  15934. %@2@%%@AH@%WRITECA.C%@AE@%%@EH@%%@NL@%
  15935. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\WRITECA.C%@AE@%%@NL@%
  15936. %@NL@%
  15937. %@AB@%/*%@NL@%
  15938. %@AB@% *        writeca -- write character and attribute to the screen%@NL@%
  15939. %@AB@% */%@AE@%%@NL@%
  15940. %@NL@%
  15941. %@AI@%#include %@AE@%<dos.h> %@NL@%
  15942. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  15943. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  15944. %@NL@%
  15945. int%@NL@%
  15946. writeca(ch, attr, count, pg)%@NL@%
  15947. unsigned char ch;        %@AB@%/* character */%@AE@%%@NL@%
  15948. unsigned char attr;        %@AB@%/* attribute */%@AE@%%@NL@%
  15949. int count;                %@AB@%/* number of repetitions */%@AE@%%@NL@%
  15950. int pg;                        %@AB@%/* screen page for writes */%@AE@%%@NL@%
  15951. {%@NL@%
  15952.         union REGS inregs, outregs;%@NL@%
  15953. %@NL@%
  15954.         inregs.h.ah = WRITE_CHAR_ATTR;%@NL@%
  15955.         inregs.h.al = ch;%@NL@%
  15956.         inregs.h.bh = pg;%@NL@%
  15957.         inregs.h.bl = attr;%@NL@%
  15958.         inregs.x.cx = count;%@NL@%
  15959.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  15960. %@NL@%
  15961.         return (outregs.x.cflag);%@NL@%
  15962. }%@NL@%
  15963. %@NL@%
  15964. %@NL@%
  15965. %@2@%%@AH@%WRITEDOT.C%@AE@%%@EH@%%@NL@%
  15966. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\WRITEDOT.C%@AE@%%@NL@%
  15967. %@NL@%
  15968. %@AB@%/*%@NL@%
  15969. %@AB@% *        writedot -- display a dot at the specified position%@NL@%
  15970. %@AB@% */%@AE@%%@NL@%
  15971. %@NL@%
  15972. %@AI@%#include %@AE@%<dos.h> %@NL@%
  15973. %@AI@%#include %@AE@%<local\std.h>  %@NL@%
  15974. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  15975. %@NL@%
  15976. int%@NL@%
  15977. writedot(r, c, color)%@NL@%
  15978. int r, c;        %@AB@%/* row and column cordinate */%@AE@%%@NL@%
  15979. int color;        %@AB@%/* dot (pixel) color */%@AE@%%@NL@%
  15980. {%@NL@%
  15981.         union REGS inregs, outregs;%@NL@%
  15982. %@NL@%
  15983.         inregs.h.ah = WRITE_DOT;%@NL@%
  15984.         inregs.h.al = color;%@NL@%
  15985.         inregs.x.cx = c;%@NL@%
  15986.         inregs.x.dx = r;%@NL@%
  15987.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  15988. %@NL@%
  15989.         return (outregs.x.cflag);%@NL@%
  15990. }%@NL@%
  15991. %@NL@%
  15992. %@NL@%
  15993. %@2@%%@AH@%WRITEMSG.C%@AE@%%@EH@%%@NL@%
  15994. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\WRITEMSG.C%@AE@%%@NL@%
  15995. %@NL@%
  15996. %@AB@%/*%@NL@%
  15997. %@AB@% *        writemsg -- displays a message in a field of the prevailing%@NL@%
  15998. %@AB@% *        video attribute and returns the number of displayable message%@NL@%
  15999. %@AB@% *        characters written; truncates the message if its too long%@NL@%
  16000. %@AB@% *        to fit in the field%@NL@%
  16001. %@AB@% */%@AE@%%@NL@%
  16002. %@NL@%
  16003. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  16004. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  16005. %@NL@%
  16006. int writemsg(r, c, w, s1, s2, pg)%@NL@%
  16007. int r, c, w ;%@NL@%
  16008. char *s1, *s2;%@NL@%
  16009. int pg;%@NL@%
  16010. {%@NL@%
  16011.         int n = 0;%@NL@%
  16012.         char *cp;%@NL@%
  16013. %@NL@%
  16014.         %@AB@%/* display first part of the message */%@AE@%%@NL@%
  16015.         if (s1 != NULL)%@NL@%
  16016.                 for (cp = s1; *cp != '\0' && n < w; ++n, ++cp) {%@NL@%
  16017.                         putcur(r, c + n, pg);%@NL@%
  16018.                         writec(*cp, 1, pg);%@NL@%
  16019.                 }%@NL@%
  16020. %@NL@%
  16021.         %@AB@%/* display second part of the message */%@AE@%%@NL@%
  16022.         if (s2 != NULL)%@NL@%
  16023.                 for (cp = s2; *cp != '\0' && n < w; ++n, ++cp) {%@NL@%
  16024.                         putcur(r, c + n, pg);%@NL@%
  16025.                         writec(*cp, 1, pg);%@NL@%
  16026.                 }%@NL@%
  16027. %@NL@%
  16028.         %@AB@%/* pad the remainder of the field, if any, with spaces */%@AE@%%@NL@%
  16029.         if (n < w) {%@NL@%
  16030.                 putcur(r, c + n, pg);%@NL@%
  16031.                 writec(' ', w - n, pg);%@NL@%
  16032.         }%@NL@%
  16033. %@NL@%
  16034.         return (n);%@NL@%
  16035. }%@NL@%
  16036. %@NL@%
  16037. %@NL@%
  16038. %@2@%%@AH@%WRITESTR.C%@AE@%%@EH@%%@NL@%
  16039. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\WRITESTR.C%@AE@%%@NL@%
  16040. %@NL@%
  16041. %@AB@%/*%@NL@%
  16042. %@AB@% *        writestr -- write a string in%@NL@%
  16043. %@AB@% *        the prevailing video attribute%@NL@%
  16044. %@AB@% */%@AE@%%@NL@%
  16045. %@NL@%
  16046. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  16047. %@NL@%
  16048. int%@NL@%
  16049. writestr(s, pg)%@NL@%
  16050. register char *s;        %@AB@%/* string to write */%@AE@%%@NL@%
  16051. unsigned int pg;        %@AB@%/* screen page for writes */%@AE@%%@NL@%
  16052. {%@NL@%
  16053.         unsigned int r, c, c0;%@NL@%
  16054. %@NL@%
  16055.         readcur(&r, &c, pg);%@NL@%
  16056.         for (c0 = c; *s != '\0'; ++s, ++c) {%@NL@%
  16057.                 putcur(r, c, pg);%@NL@%
  16058.                 writec(*s, 1, pg);%@NL@%
  16059.         }%@NL@%
  16060. %@NL@%
  16061.         %@AB@%/* restore cursor position and return # of characters displayed */%@AE@%%@NL@%
  16062.         putcur(r, c0, pg);%@NL@%
  16063.         return (c - c0);%@NL@%
  16064. }%@NL@%
  16065. %@NL@%
  16066. %@NL@%
  16067. %@2@%%@AH@%WRITETTY.C%@AE@%%@EH@%%@NL@%
  16068. %@AS@%CD-ROM Disc Path:   \SAMPCODE\PROF_C\05OSLIB\BIOS\WRITETTY.C%@AE@%%@NL@%
  16069. %@NL@%
  16070. %@AB@%/*%@NL@%
  16071. %@AB@% *        writetty -- write to screen using TTY interface%@NL@%
  16072. %@AB@% */%@AE@%%@NL@%
  16073. %@NL@%
  16074. %@AI@%#include %@AE@%<dos.h> %@NL@%
  16075. %@AI@%#include %@AE@%<local\std.h> %@NL@%
  16076. %@AI@%#include %@AE@%<local\bioslib.h> %@NL@%
  16077. %@NL@%
  16078. int%@NL@%
  16079. writetty(ch, attr, pg)%@NL@%
  16080. unsigned char ch;        %@AB@%/* character */%@AE@%%@NL@%
  16081. unsigned char attr;        %@AB@%/* video attribute */%@AE@%%@NL@%
  16082. int pg;                        %@AB@%/* screen page for writes */%@AE@%%@NL@%
  16083. {%@NL@%
  16084.         union REGS inregs, outregs;%@NL@%
  16085. %@NL@%
  16086.         inregs.h.ah = WRITE_TTY;%@NL@%
  16087.         inregs.h.al = ch;%@NL@%
  16088.         inregs.h.bl = attr;%@NL@%
  16089.         inregs.h.bh = pg;%@NL@%
  16090.         int86(VIDEO_IO, &inregs, &outregs);%@NL@%
  16091. %@NL@%
  16092.         return (outregs.x.cflag);%@NL@%
  16093. }%@NL@%
  16094. %@1@%%@AH@%Quick-C Programming - Sample Code%@EH@%%@AE@%
  16095. %@NL@%
  16096. %@NL@%
  16097. %@2@%%@AH@%12_10A.C%@AE@%%@EH@%%@NL@%
  16098. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\12_10A.C%@AE@%%@NL@%
  16099. %@NL@%
  16100. %@AI@%#include %@AE@%"texed.h" %@NL@%
  16101. menu()%@NL@%
  16102. {%@NL@%
  16103.     struct key_struct *kp, *Read_kbd();%@NL@%
  16104.     int cur_key, cur_move;%@NL@%
  16105.     %@NL@%
  16106.     kp = Read_kbd();%@NL@%
  16107.     cur_key = kp->key;%@NL@%
  16108.     cur_move = kp->move;%@NL@%
  16109.     if (cur_key == ERROR)%@NL@%
  16110.         return (cur_move);%@NL@%
  16111.     return (cur_key);%@NL@%
  16112. }%@NL@%
  16113. %@NL@%
  16114. %@NL@%
  16115. %@2@%%@AH@%12_10B.C%@AE@%%@EH@%%@NL@%
  16116. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\12_10B.C%@AE@%%@NL@%
  16117. %@NL@%
  16118. %@AI@%#include %@AE@%"texed.h" %@NL@%
  16119. struct key_struct *Read_key()%@NL@%
  16120. {%@NL@%
  16121.     struct key_struct k;%@NL@%
  16122. %@NL@%
  16123.     k.key = getch();%@NL@%
  16124.     if (k.key == ERROR)%@NL@%
  16125.     k.move = getch();%@NL@%
  16126.         return (&k);%@NL@%
  16127. }%@NL@%
  16128. %@NL@%
  16129. %@NL@%
  16130. %@2@%%@AH@%12_8A.C%@AE@%%@EH@%%@NL@%
  16131. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\12_8A.C%@AE@%%@NL@%
  16132. %@NL@%
  16133. %@AI@%#define %@AE@%OK 1 %@NL@%
  16134. %@AI@%#define %@AE@%ERROR 0 %@NL@%
  16135. menu()%@NL@%
  16136. {%@NL@%
  16137.     struct key_struct {%@NL@%
  16138.         char key;%@NL@%
  16139.         unsigned char move;%@NL@%
  16140.     } *kp, *Read_kbd();%@NL@%
  16141.     int cur_key, cur_move;%@NL@%
  16142. %@NL@%
  16143.     kp = Read_kbd();%@NL@%
  16144.     cur_key = kp->key;%@NL@%
  16145.     cur_move = kp->move;%@NL@%
  16146.     if (cur_key == ERROR)%@NL@%
  16147.         return (cur_move);%@NL@%
  16148.     return (cur_key);%@NL@%
  16149. }%@NL@%
  16150. %@NL@%
  16151. %@NL@%
  16152. %@2@%%@AH@%12_8B.C%@AE@%%@EH@%%@NL@%
  16153. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\12_8B.C%@AE@%%@NL@%
  16154. %@NL@%
  16155. %@AI@%#define %@AE@%OK 1 %@NL@%
  16156. %@AI@%#define %@AE@%ERROR 0 %@NL@%
  16157. struct key_struct {%@NL@%
  16158.     char key;%@NL@%
  16159.     unsigned char move;%@NL@%
  16160. };%@NL@%
  16161. %@NL@%
  16162. struct key_struct *Read_key()%@NL@%
  16163. {%@NL@%
  16164.     struct key_struct k;%@NL@%
  16165. %@NL@%
  16166.     k.key = getch();%@NL@%
  16167.     if (k.key == ERROR)%@NL@%
  16168.         k.move = getch();%@NL@%
  16169.     return (&k);%@NL@%
  16170. }%@NL@%
  16171. %@NL@%
  16172. %@NL@%
  16173. %@2@%%@AH@%ACME.C%@AE@%%@EH@%%@NL@%
  16174. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\ACME.C%@AE@%%@NL@%
  16175. %@NL@%
  16176. %@AB@%/* acme.c  --     illustrate an assortment of the     */%@AE@%%@NL@%
  16177. %@AB@%/*                C library string-handling routines  */%@AE@%%@NL@%
  16178. %@NL@%
  16179. <stdio.h>         %@AB@%/* for NULL */%@AE@%%@NL@%
  16180. <string.h>        %@AB@%/* for strchr(), et al */%@AE@%%@NL@%
  16181. %@NL@%
  16182. %@AI@%#define %@AE@%NAME_PATTERN \ %@NL@%
  16183. "first<space>last  or\n\%@NL@%
  16184. first<space>middle<space>last"%@NL@%
  16185. %@NL@%
  16186. %@AI@%#define %@AE@%ADDRESS_PATTERN \ %@NL@%
  16187. "number<space>street<comma><space>city<comma>"%@NL@%
  16188. %@NL@%
  16189. char Buf[BUFSIZ];        %@AB@%/* global I/O buffer */%@AE@%%@NL@%
  16190. %@NL@%
  16191. main()%@NL@%
  16192. {%@NL@%
  16193.     char *ocp, *cp, *first, *last, *street, *city;%@NL@%
  16194.     void Prompt(), Cant();%@NL@%
  16195. %@NL@%
  16196.     printf("Acme Employment Questionaire\n");%@NL@%
  16197. %@NL@%
  16198.     %@AB@%/*%@NL@%
  16199. %@AB@%     * Expect first<space>last or%@NL@%
  16200. %@AB@%     *        first<space>middle<space>last%@NL@%
  16201. %@AB@%     */%@AE@%%@NL@%
  16202.     Prompt("Full Name");%@NL@%
  16203.      %@AB@%/* search forward for a space */%@AE@%%@NL@%
  16204.     if ((cp = strchr(Buf,' ')) == NULL)%@NL@%
  16205.         Cant("First Name", NAME_PATTERN);%@NL@%
  16206.     *cp = '\0';%@NL@%
  16207.     first = strdup(Buf);%@NL@%
  16208.     *cp = ' ';%@NL@%
  16209. %@NL@%
  16210.      %@AB@%/* Search back from end for a space */%@AE@%%@NL@%
  16211.     if ((cp = strrchr(Buf,' ')) == NULL)%@NL@%
  16212.         Cant("Last Name", NAME_PATTERN);%@NL@%
  16213.     last = strdup(++cp);%@NL@%
  16214. %@NL@%
  16215.     %@AB@%/*%@NL@%
  16216. %@AB@%     * Expect number<space>street<comma><space>city<comma>%@NL@%
  16217. %@AB@%     */%@AE@%%@NL@%
  16218.     Prompt("Full Address");%@NL@%
  16219.      %@AB@%/* search forward for a comma */%@AE@%%@NL@%
  16220.     if ((cp = strchr(Buf,',')) == NULL)%@NL@%
  16221.         Cant("Street", ADDRESS_PATTERN);%@NL@%
  16222.     *cp = '\0';%@NL@%
  16223.     street = strdup(Buf);%@NL@%
  16224. %@NL@%
  16225.      %@AB@%/* Search forward from last comma for next comma */%@AE@%%@NL@%
  16226.     if ((ocp = strchr(++cp,',')) == NULL)%@NL@%
  16227.         Cant("City", ADDRESS_PATTERN);%@NL@%
  16228.     *ocp = '\0';%@NL@%
  16229.     city = strdup(++cp);%@NL@%
  16230.     %@NL@%
  16231.     printf("\n\nYou Entered:\n");%@NL@%
  16232.     printf("\tFirst Name: \"%s\"\n", first);%@NL@%
  16233.     printf("\tLast Name:  \"%s\"\n", last);%@NL@%
  16234.     printf("\tStreet:     \"%s\"\n", street);%@NL@%
  16235.     printf("\tCity:       \"%s\"\n", city);%@NL@%
  16236. %@NL@%
  16237. }%@NL@%
  16238. %@NL@%
  16239. void Cant(char *what, char *pattern)%@NL@%
  16240. {%@NL@%
  16241.     printf("\n\n\bFormat Error!!!\n");%@NL@%
  16242.     printf("Can't parse your %s.\n", what );%@NL@%
  16243.     printf("Expected an entry of the form:\n\n");%@NL@%
  16244.     printf("%s\n\nAborted\n", pattern);%@NL@%
  16245.     exit(1);%@NL@%
  16246. }%@NL@%
  16247. %@NL@%
  16248. void Prompt(char *str)%@NL@%
  16249. {%@NL@%
  16250.     while (1)%@NL@%
  16251.         {%@NL@%
  16252.         printf("\n%s: ", str );%@NL@%
  16253.         if (gets(Buf) == NULL || *Buf == '\0')%@NL@%
  16254.             {%@NL@%
  16255.             printf("Do you wish to quit? ");%@NL@%
  16256.             if (gets(Buf) == NULL || *Buf == 'y')%@NL@%
  16257.                 exit (0);%@NL@%
  16258.             continue;%@NL@%
  16259.             }%@NL@%
  16260.         break;%@NL@%
  16261.         }%@NL@%
  16262. }%@NL@%
  16263. %@NL@%
  16264. %@NL@%
  16265. %@2@%%@AH@%ALERT.C%@AE@%%@EH@%%@NL@%
  16266. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\ALERT.C%@AE@%%@NL@%
  16267. %@NL@%
  16268. %@AB@%/* alert.c -- sounds alarm by calling a        */%@AE@%%@NL@%
  16269. %@AB@%/*            beep() function with a parameter */%@AE@%%@NL@%
  16270. %@NL@%
  16271. main()%@NL@%
  16272. {%@NL@%
  16273.     void beep(times);  %@AB@%/* function declaration */%@AE@%%@NL@%
  16274.     printf("*** Alert! Alert! ***\n");%@NL@%
  16275.     beep(3);     %@AB@%/* call beep() with parameter */%@AE@%%@NL@%
  16276. }%@NL@%
  16277. %@NL@%
  16278. void beep(times)%@NL@%
  16279.     int times;   %@AB@%/* declare function parameter */%@AE@%%@NL@%
  16280. {%@NL@%
  16281.     int count;%@NL@%
  16282. %@NL@%
  16283.     %@AB@%/* check that parameter is between 1 and 4 */%@AE@%%@NL@%
  16284.     if ((times < 1) || (times > 4))%@NL@%
  16285.         {%@NL@%
  16286.         printf("Error in beep(): %d beeps specified.\n",%@NL@%
  16287.                 times);%@NL@%
  16288.         printf("Specify one to four beeps");%@NL@%
  16289.         }%@NL@%
  16290.     else %@AB@%/* sound the beeps */%@AE@%%@NL@%
  16291.         for (count = 1; count <= times; count++)%@NL@%
  16292.             printf("\a");  %@AB@%/* "alert" escape sequence */%@AE@%%@NL@%
  16293. }%@NL@%
  16294. %@NL@%
  16295. %@NL@%
  16296. %@2@%%@AH@%ALLCOLOR.C%@AE@%%@EH@%%@NL@%
  16297. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\ALLCOLOR.C%@AE@%%@NL@%
  16298. %@NL@%
  16299. %@AB@%/* allcolor.c -- shows _ERESCOLOR 64-color palette     */%@AE@%%@NL@%
  16300. %@AB@%/* If you load graphics.qlb, no program list is needed.*/%@AE@%%@NL@%
  16301. %@NL@%
  16302. %@AB@%/*  Hit <g> to advance left palette, <G> to go back.   */%@AE@%%@NL@%
  16303. %@AB@%/*  Hit <h> to advance right palette, <H> to go back.  */%@AE@%%@NL@%
  16304. %@AB@%/*  Hit <Esc> to quit                                  */%@AE@%%@NL@%
  16305. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  16306. %@AI@%#include %@AE@%<graph.h> %@NL@%
  16307. %@AI@%#include %@AE@%<conio.h> %@NL@%
  16308. %@AI@%#define %@AE@%MAXCOLORS 64 %@NL@%
  16309. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  16310. long Ega_to_vga(int);   %@AB@%/* color value conversion */%@AE@%%@NL@%
  16311. %@NL@%
  16312. main(argc, argv)%@NL@%
  16313. int argc;%@NL@%
  16314. char *argv[];%@NL@%
  16315. {%@NL@%
  16316.     struct videoconfig vc;%@NL@%
  16317.     int mode = _ERESCOLOR;%@NL@%
  16318.     int xmax, ymax;%@NL@%
  16319.     int c1 = 1;%@NL@%
  16320.     int c2 = 4;%@NL@%
  16321.     char left[11];%@NL@%
  16322.     char right[11];%@NL@%
  16323.     int lpos, rpos;%@NL@%
  16324.     char ch;%@NL@%
  16325. %@NL@%
  16326.     if (argc > 1)%@NL@%
  16327.         mode = atoi(argv[1]);%@NL@%
  16328.     if (_setvideomode(mode) == 0)%@NL@%
  16329.     {%@NL@%
  16330.         fprintf(stderr,"%d mode not supported\n", mode);%@NL@%
  16331.         exit(1);%@NL@%
  16332.     }%@NL@%
  16333.     _getvideoconfig(&vc);%@NL@%
  16334.     _setlogorg(vc.numxpixels/2, vc.numypixels/2);%@NL@%
  16335. %@NL@%
  16336.     xmax = vc.numxpixels / 2 - 1;%@NL@%
  16337.     ymax = vc.numypixels / 2 - 1;%@NL@%
  16338.     lpos = vc.numxpixels/32 - 5;%@NL@%
  16339.     rpos = lpos + vc.numxpixels / 16;%@NL@%
  16340.     _setcolor(1);%@NL@%
  16341.     _rectangle(_GFILLINTERIOR, -xmax, -ymax, 0, ymax);%@NL@%
  16342.     _setcolor(4);%@NL@%
  16343.     _rectangle(_GFILLINTERIOR, 1, -ymax, xmax, ymax);%@NL@%
  16344.     sprintf(left,"<-G %2d g->", c1);%@NL@%
  16345.     sprintf(right,"<-H %2d h->", c2);%@NL@%
  16346.     _settextcolor(6);%@NL@%
  16347.     _settextposition(0, 0);%@NL@%
  16348.     _outtext("Press Esc to quit");%@NL@%
  16349.     _settextposition(24, lpos);%@NL@%
  16350.     _outtext(left);%@NL@%
  16351.     _settextposition(24, rpos);%@NL@%
  16352.     _outtext(right);%@NL@%
  16353.     while ((ch = getch()) != ESC)%@NL@%
  16354.     {%@NL@%
  16355.         switch (ch)%@NL@%
  16356.         {%@NL@%
  16357.             case 'g': c1 = (c1 + 1) % MAXCOLORS;%@NL@%
  16358.                       _remappalette(1, Ega_to_vga(c1));%@NL@%
  16359.                       break;%@NL@%
  16360.             case 'G': c1 = (c1 - 1) % MAXCOLORS;%@NL@%
  16361.                       _remappalette(1, Ega_to_vga(c1));%@NL@%
  16362.                       break;%@NL@%
  16363.             case 'h': c2 = (c2 + 1) % MAXCOLORS;%@NL@%
  16364.                       _remappalette(4, Ega_to_vga(c2));%@NL@%
  16365.                       break;%@NL@%
  16366.             case 'H': c2 = (c2 - 1) % MAXCOLORS;%@NL@%
  16367.                       _remappalette(4, Ega_to_vga(c2));%@NL@%
  16368.                       break;%@NL@%
  16369.         }%@NL@%
  16370.         sprintf(left,"<-G %2d ->g", c1);%@NL@%
  16371.         sprintf(right,"<-H %2d ->h", c2);%@NL@%
  16372.         _settextposition(0, 0);%@NL@%
  16373.         _outtext("Press Esc to quit");%@NL@%
  16374.         _settextposition(24, lpos);%@NL@%
  16375.         _outtext(left);%@NL@%
  16376.         _settextposition(24, rpos);%@NL@%
  16377.         _outtext(right);%@NL@%
  16378.     }%@NL@%
  16379.     _setvideomode(_DEFAULTMODE);%@NL@%
  16380. }%@NL@%
  16381. long Ega_to_vga(egacolor)%@NL@%
  16382. int egacolor;       %@AB@%/* ega color value */%@AE@%%@NL@%
  16383. {%@NL@%
  16384.     static long vgavals[6] = {0x2A0000L, 0x002A00L, 0x00002AL,%@NL@%
  16385.                               0x150000L, 0x001500L, 0x000015L};%@NL@%
  16386.     long vgacolor = 0L; %@AB@%/* vga color value */%@AE@%%@NL@%
  16387.     int bit;%@NL@%
  16388. %@NL@%
  16389.     for (bit = 0; bit < 6; bit++)%@NL@%
  16390.         vgacolor += ((egacolor >> bit) &1) * vgavals[bit];%@NL@%
  16391.     return (vgacolor);%@NL@%
  16392. }%@NL@%
  16393. %@NL@%
  16394. %@NL@%
  16395. %@2@%%@AH@%ALLVGA.C%@AE@%%@EH@%%@NL@%
  16396. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\ALLVGA.C%@AE@%%@NL@%
  16397. %@NL@%
  16398. %@AB@%/* allvga.c -- shows _MRES256COLOR 256K colors         */%@AE@%%@NL@%
  16399. %@AB@%/* If you load graphics.qlb, no program list is needed.*/%@AE@%%@NL@%
  16400. %@NL@%
  16401. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  16402. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  16403. %@AI@%#include %@AE@%<graph.h> %@NL@%
  16404. %@AI@%#include %@AE@%<conio.h> %@NL@%
  16405. %@AI@%#define %@AE@%FULLBRIGHT 64 %@NL@%
  16406. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  16407. char label[2][7] = {"ACTIVE", "      "};%@NL@%
  16408. %@NL@%
  16409. main(argc, argv)%@NL@%
  16410. int argc;%@NL@%
  16411. char *argv[];%@NL@%
  16412. {%@NL@%
  16413.     struct videoconfig vc;%@NL@%
  16414.     int mode = _MRES256COLOR;%@NL@%
  16415.     int xmax, ymax;%@NL@%
  16416.     static long colors[2] = {_BLUE, _RED};%@NL@%
  16417.     char left[11];%@NL@%
  16418.     char right[11];%@NL@%
  16419.     int lpos, rpos;%@NL@%
  16420.     char ch;%@NL@%
  16421.     unsigned long blue = _BLUE >> 16;%@NL@%
  16422.     unsigned long green = 0L;%@NL@%
  16423.     unsigned long red = 0L;%@NL@%
  16424.     long color;%@NL@%
  16425.     short palnum = 0;%@NL@%
  16426. %@NL@%
  16427.     if (argc > 1)%@NL@%
  16428.         mode = atoi(argv[1]);%@NL@%
  16429.     if (_setvideomode(mode) == 0)%@NL@%
  16430.         {%@NL@%
  16431.         fprintf(stderr,"%d mode not supported\n", mode);%@NL@%
  16432.         exit(1);%@NL@%
  16433.         }%@NL@%
  16434.     _getvideoconfig(&vc);%@NL@%
  16435.     _setlogorg(vc.numxpixels/2, vc.numypixels/2);%@NL@%
  16436. %@NL@%
  16437.     xmax = vc.numxpixels / 2 - 1;%@NL@%
  16438.     ymax = vc.numypixels / 2 - 1;%@NL@%
  16439.     lpos = vc.numxpixels / 32 - 5;%@NL@%
  16440.     rpos = lpos + vc.numxpixels/16;%@NL@%
  16441.     _remappalette(2, _RED);%@NL@%
  16442.     _setcolor(1);%@NL@%
  16443.     _rectangle(_GFILLINTERIOR, -xmax, -ymax, 0, ymax);%@NL@%
  16444.     _setcolor(2);%@NL@%
  16445.     _rectangle(_GFILLINTERIOR, 1, -ymax, xmax, ymax);%@NL@%
  16446.     sprintf(left," %6lxH ", colors[0]);%@NL@%
  16447.     sprintf(right," %6lxH ", colors[1]);%@NL@%
  16448.     _settextcolor(6);%@NL@%
  16449.     _settextposition(1, 1);%@NL@%
  16450.     _outtext("Press Tab to toggle panels, Esc to quit.");%@NL@%
  16451.     _settextposition(2, 1);%@NL@%
  16452.     _outtext("B increases blue level, b decreases it. ");%@NL@%
  16453.     _settextposition(3, 1);%@NL@%
  16454.     _outtext("G and g control green, R and r red.     ");%@NL@%
  16455.     _settextposition(24, lpos);%@NL@%
  16456.     _outtext(left);%@NL@%
  16457.     _settextposition(24, rpos);%@NL@%
  16458.     _outtext(right);%@NL@%
  16459.     _settextposition(5, 7);%@NL@%
  16460.     _outtext(label[0]);%@NL@%
  16461.     _settextposition(5, 27);%@NL@%
  16462.     _outtext(label[1]);%@NL@%
  16463.     while ((ch = getch()) != ESC)%@NL@%
  16464.         {%@NL@%
  16465.         switch (ch)%@NL@%
  16466.             {%@NL@%
  16467.             case '\t': _settextposition(5, 27);%@NL@%
  16468.                        _outtext(label[palnum]);%@NL@%
  16469.                        palnum ^= 1;%@NL@%
  16470.                        blue = (colors[palnum] << 16) & 0x3F;%@NL@%
  16471.                        green = (colors[palnum] << 8) & 0x3F;%@NL@%
  16472.                        red = colors[palnum] & 0x3F;%@NL@%
  16473.                        _settextposition(5, 7);%@NL@%
  16474.                        _outtext(label[palnum]);%@NL@%
  16475.                        break;%@NL@%
  16476.             case 'B':  blue = (blue + 1) % FULLBRIGHT;%@NL@%
  16477.                        colors[palnum] = blue << 16 |%@NL@%
  16478.                                green << 8 | red;%@NL@%
  16479.                        _remappalette(palnum + 1, colors[palnum]);%@NL@%
  16480. %@NL@%
  16481.                        break;%@NL@%
  16482.             case 'b':  blue = (blue - 1) % FULLBRIGHT;%@NL@%
  16483.                        colors[palnum] = blue << 16 |%@NL@%
  16484.                                green << 8 | red;%@NL@%
  16485.                        _remappalette(palnum + 1, colors[palnum]);%@NL@%
  16486. %@NL@%
  16487.                        break;%@NL@%
  16488.             case 'G':  green = (green + 1) % FULLBRIGHT;%@NL@%
  16489.                        colors[palnum] = blue << 16 |%@NL@%
  16490.                                green << 8 | red;%@NL@%
  16491.                        _remappalette(palnum + 1, colors[palnum]);%@NL@%
  16492. %@NL@%
  16493.                        break;%@NL@%
  16494.             case 'g':  green = (green - 1) % FULLBRIGHT;%@NL@%
  16495.                        colors[palnum] = blue << 16 |%@NL@%
  16496.                                green << 8 | red;%@NL@%
  16497.                        _remappalette(palnum + 1, colors[palnum]);%@NL@%
  16498. %@NL@%
  16499.                        break;%@NL@%
  16500.             case 'R':  red = (red + 1) % FULLBRIGHT;%@NL@%
  16501.                        colors[palnum] = blue << 16 |%@NL@%
  16502.                                green << 8 | red;%@NL@%
  16503.                        _remappalette(palnum + 1, colors[palnum]);%@NL@%
  16504. %@NL@%
  16505.                        break;%@NL@%
  16506.             case 'r':  red = (red - 1) % FULLBRIGHT;%@NL@%
  16507.                        colors[palnum] = blue << 16 |%@NL@%
  16508.                                green << 8 | red;%@NL@%
  16509.                        _remappalette(palnum + 1, colors[palnum]);%@NL@%
  16510. %@NL@%
  16511.                        break;%@NL@%
  16512. %@NL@%
  16513.             }%@NL@%
  16514.         sprintf(left," %6lxH ", colors[0]);%@NL@%
  16515.         sprintf(right," %6lxH ", colors[1]);%@NL@%
  16516.         _settextposition(24, lpos);%@NL@%
  16517.         _outtext(left);%@NL@%
  16518.         _settextposition(24, rpos);%@NL@%
  16519.         _outtext(right);%@NL@%
  16520.         }%@NL@%
  16521.     _setvideomode(_DEFAULTMODE);%@NL@%
  16522. }%@NL@%
  16523. %@NL@%
  16524. %@NL@%
  16525. %@2@%%@AH@%ALPHABET.C%@AE@%%@EH@%%@NL@%
  16526. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\ALPHABET.C%@AE@%%@NL@%
  16527. %@NL@%
  16528. %@NL@%
  16529. %@AB@%/* ALPHABET.C -- uses for loop to         */%@AE@%%@NL@%
  16530. %@AB@%/*               print lowercase alphabet */%@AE@%%@NL@%
  16531. %@NL@%
  16532. main()%@NL@%
  16533. {%@NL@%
  16534.     int i;%@NL@%
  16535.     for (i = 'a'; i <= 'z'; i++)%@NL@%
  16536.         {    %@NL@%
  16537.         printf("%c", i);%@NL@%
  16538.         }%@NL@%
  16539. }%@NL@%
  16540. %@NL@%
  16541. %@NL@%
  16542. %@NL@%
  16543. %@2@%%@AH@%ANIMATE.C%@AE@%%@EH@%%@NL@%
  16544. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\ANIMATE.C%@AE@%%@NL@%
  16545. %@NL@%
  16546. %@NL@%
  16547. %@AB@%/* ANIMATE.C -- animate a graphics character */%@AE@%%@NL@%
  16548. %@AB@%/*              until a key is pressed       */%@AE@%%@NL@%
  16549. %@NL@%
  16550. %@AB@%/* Special characters */%@AE@%%@NL@%
  16551. %@AI@%#define %@AE@%RTARROW 175 %@NL@%
  16552. %@AI@%#define %@AE@%LFTARROW 174 %@NL@%
  16553. %@AI@%#define %@AE@%BLANK 32 %@NL@%
  16554. %@AI@%#define %@AE@%BACKSPACE 8 %@NL@%
  16555. %@NL@%
  16556. main()%@NL@%
  16557. {%@NL@%
  16558.     int pos, i, j = 1;%@NL@%
  16559.     while ( !kbhit() )%@NL@%
  16560.         {%@NL@%
  16561.         pos = 1;%@NL@%
  16562.         while (pos < 79)%@NL@%
  16563.             {%@NL@%
  16564.             putch(RTARROW);%@NL@%
  16565.             i = 1;%@NL@%
  16566.             while (i < 1000)%@NL@%
  16567.                 {%@NL@%
  16568.                 j = i + 10;%@NL@%
  16569.                 i++;%@NL@%
  16570.                 }%@NL@%
  16571.             putch(BACKSPACE);%@NL@%
  16572.             putch(BLANK);%@NL@%
  16573.             pos++;%@NL@%
  16574.             }%@NL@%
  16575.         while (pos > 1)%@NL@%
  16576.             {%@NL@%
  16577.             putch(LFTARROW);%@NL@%
  16578.             i = 1;%@NL@%
  16579.             while (i < 1000)%@NL@%
  16580.                 {%@NL@%
  16581.                 j = i + 10;%@NL@%
  16582.                 i++;%@NL@%
  16583.                 }%@NL@%
  16584.             putch(BACKSPACE);%@NL@%
  16585.             putch(BLANK);%@NL@%
  16586.             putch(BACKSPACE);%@NL@%
  16587.             putch(BACKSPACE);%@NL@%
  16588.             pos--;%@NL@%
  16589.             }%@NL@%
  16590.         }%@NL@%
  16591. }%@NL@%
  16592. %@NL@%
  16593. %@NL@%
  16594. %@NL@%
  16595. %@2@%%@AH@%ARRAY1.C%@AE@%%@EH@%%@NL@%
  16596. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\ARRAY1.C%@AE@%%@NL@%
  16597. %@NL@%
  16598. %@AB@%/* array1.c -- how to declare arrays legally         */%@AE@%%@NL@%
  16599. %@NL@%
  16600. %@AI@%#define %@AE@%SIZEOARRAY 26 %@NL@%
  16601. %@NL@%
  16602. main()%@NL@%
  16603. {%@NL@%
  16604.     char  initials[26];%@NL@%
  16605.     int   num_men[26], num_women[SIZEOARRAY];%@NL@%
  16606.     float ages[SIZEOARRAY * 2];%@NL@%
  16607. }%@NL@%
  16608. %@NL@%
  16609. %@NL@%
  16610. %@2@%%@AH@%ARROW.C%@AE@%%@EH@%%@NL@%
  16611. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\ARROW.C%@AE@%%@NL@%
  16612. %@NL@%
  16613. %@AB@%/* arrow.c -- fill inside and outside of a line       */%@AE@%%@NL@%
  16614. %@AB@%/*            drawing                                 */%@AE@%%@NL@%
  16615. %@AB@%/* If you load graphics.qlb, no program list is needed*/%@AE@%%@NL@%
  16616. %@NL@%
  16617. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  16618. %@AI@%#include %@AE@%<graph.h> %@NL@%
  16619. %@AI@%#include %@AE@%<conio.h> %@NL@%
  16620. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  16621. BKCOLS 16   %@AB@%/* use 16 background colors */%@AE@%%@NL@%
  16622. long Bkcolors[BKCOLS] = {_BLACK, _BLUE, _GREEN, _CYAN,%@NL@%
  16623.                  _RED, _MAGENTA, _BROWN, _WHITE,%@NL@%
  16624.                  _GRAY, _LIGHTBLUE, _LIGHTGREEN,%@NL@%
  16625.                  _LIGHTCYAN, _LIGHTRED, _LIGHTMAGENTA,%@NL@%
  16626.                  _LIGHTYELLOW,_BRIGHTWHITE };%@NL@%
  16627. char Mask[8] = {0x90, 0x68, 0x34, 0x19, 0x19, 0x34, 0x68,%@NL@%
  16628.                 0x90};%@NL@%
  16629. char Outmask[8] = {0xff, 0x80, 0x80, 0x80, 0xff, 0x08, 0x08,%@NL@%
  16630.                    0x08};%@NL@%
  16631. main(argc, argv)%@NL@%
  16632. int argc;%@NL@%
  16633. char *argv[];%@NL@%
  16634. {%@NL@%
  16635.     struct videoconfig vc;%@NL@%
  16636.     int mode = _MRES4COLOR;%@NL@%
  16637.     float x1, y1, x2, y2, x3, y3, y4, x5, y5;%@NL@%
  16638.     long bk = _BLUE;%@NL@%
  16639. %@NL@%
  16640.     if (argc > 1)%@NL@%
  16641.         mode = atoi(argv[1]);%@NL@%
  16642.     if (_setvideomode(mode) == 0)%@NL@%
  16643.         {%@NL@%
  16644.         printf("Can't set mode %d.\n", mode);%@NL@%
  16645.         exit(1);%@NL@%
  16646.         }%@NL@%
  16647.     _getvideoconfig(&vc);%@NL@%
  16648. %@NL@%
  16649.     x1 = 0.1 * vc.numxpixels;%@NL@%
  16650.     x2 = 0.7 * vc.numxpixels;%@NL@%
  16651.     x3 = 0.6 * vc.numxpixels;%@NL@%
  16652.     x5 = 0.9 * vc.numxpixels;%@NL@%
  16653.     y1 = 0.45 * vc.numypixels;%@NL@%
  16654.     y2 = 0.55 * vc.numypixels;%@NL@%
  16655.     y3 = 0.3 * vc.numypixels;%@NL@%
  16656.     y4 = 0.7 * vc.numypixels;%@NL@%
  16657.     y5 = 0.5 * vc.numypixels;%@NL@%
  16658.     _selectpalette(0);%@NL@%
  16659.     _setcolor(1);%@NL@%
  16660.     _moveto(x1, y1);%@NL@%
  16661.     _lineto(x2, y1);%@NL@%
  16662.     _lineto(x3, y3);%@NL@%
  16663.     _lineto(x5, y5);%@NL@%
  16664.     _lineto(x3, y4);%@NL@%
  16665.     _lineto(x2, y2);%@NL@%
  16666.     _lineto(x1, y2);%@NL@%
  16667.     _lineto(x1, y1);%@NL@%
  16668.     _setcolor(2);%@NL@%
  16669.     _setfillmask(Mask);%@NL@%
  16670.     _floodfill(x2, y5, 1) ;%@NL@%
  16671.     _setcolor(3);%@NL@%
  16672.     _setfillmask(Outmask);     %@AB@%/* restores default mask */%@AE@%%@NL@%
  16673.     _floodfill(5, 5, 1) ;%@NL@%
  16674.     _settextcolor(1);%@NL@%
  16675.     _settextposition(23, 0);%@NL@%
  16676.     _outtext("Press <enter> to change background.");%@NL@%
  16677.     _settextposition(24, 0);%@NL@%
  16678.     _outtext("Press <esc> to end.");%@NL@%
  16679.     while (getch() != ESC)%@NL@%
  16680.         _setbkcolor(Bkcolors[++bk % BKCOLS]);%@NL@%
  16681.     _setvideomode(_DEFAULTMODE);%@NL@%
  16682. }%@NL@%
  16683. %@NL@%
  16684. %@NL@%
  16685. %@2@%%@AH@%ASGNKEY.C%@AE@%%@EH@%%@NL@%
  16686. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\ASGNKEY.C%@AE@%%@NL@%
  16687. %@NL@%
  16688. %@AB@%/*   asgnkey.c -- uses ansi.sys to assign meanings   */%@AE@%%@NL@%
  16689. %@AB@%/*                to several function keys.          */%@AE@%%@NL@%
  16690. %@AB@%/*  Note: This requires ANSI.SYS to be installed.    */%@AE@%%@NL@%
  16691. %@NL@%
  16692. %@AI@%#define %@AE@%KASSIGN(K, S) printf("\033[0;%d;\"%s\";13p", K, S) %@NL@%
  16693. %@AB@%/* This macro assigns string S to key K */%@AE@%%@NL@%
  16694. %@AI@%#define %@AE@%F5 63 %@NL@%
  16695. %@AI@%#define %@AE@%F6 64 %@NL@%
  16696. %@AI@%#define %@AE@%F7 65 %@NL@%
  16697. %@AI@%#define %@AE@%F8 66 %@NL@%
  16698. %@AI@%#define %@AE@%F9 67 %@NL@%
  16699. %@AI@%#define %@AE@%F10 68 %@NL@%
  16700. main()%@NL@%
  16701. {%@NL@%
  16702.      KASSIGN(F5, "DIR *.C");%@NL@%
  16703.      KASSIGN(F6, "DIR *.H");%@NL@%
  16704.      KASSIGN(F7, "DIR *.OBJ");%@NL@%
  16705.      KASSIGN(F8, "DIR *.EXE");%@NL@%
  16706.      KASSIGN(F9, "DIR /W");%@NL@%
  16707.      KASSIGN(F10, "CD \\");%@NL@%
  16708. }%@NL@%
  16709. %@NL@%
  16710. %@NL@%
  16711. %@2@%%@AH@%ASIMOV.C%@AE@%%@EH@%%@NL@%
  16712. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\ASIMOV.C%@AE@%%@NL@%
  16713. %@NL@%
  16714. %@AB@%/* asimov.c -- illustrates how to initialize an      */%@AE@%%@NL@%
  16715. %@AB@%/*             array with starting values            */%@AE@%%@NL@%
  16716. %@NL@%
  16717. %@AI@%#define %@AE@%MAXL 16 %@NL@%
  16718. char Letters[MAXL] = { %@NL@%
  16719.     'e', 'I', 'a', 'N', 'o', 'R', 'O', 'o',%@NL@%
  16720.     'u', 't', 'o', 'R', 'l', 'o', 'B', 'b', %@NL@%
  16721. };%@NL@%
  16722. %@NL@%
  16723. main()%@NL@%
  16724. {%@NL@%
  16725.     int num, i;%@NL@%
  16726. %@NL@%
  16727.     printf("Guess my identity with numbers.\n");%@NL@%
  16728.     printf("(any non number quits)\n\n");%@NL@%
  16729. %@NL@%
  16730.     while (scanf("%d", &num) == 1)%@NL@%
  16731.         {%@NL@%
  16732.         if (num <= 0)%@NL@%
  16733.             {%@NL@%
  16734.             printf("Guesses must be above zero\n");%@NL@%
  16735.             continue;%@NL@%
  16736.             }%@NL@%
  16737.         for (i = 1; i <= num; ++i)%@NL@%
  16738.             {%@NL@%
  16739.             printf("%c", Letters[(i * num) % MAXL]);%@NL@%
  16740.             }%@NL@%
  16741.         printf("\n");%@NL@%
  16742.         }%@NL@%
  16743. }%@NL@%
  16744. %@NL@%
  16745. %@NL@%
  16746. %@2@%%@AH@%ATTRIB.C%@AE@%%@EH@%%@NL@%
  16747. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\ATTRIB.C%@AE@%%@NL@%
  16748. %@NL@%
  16749. %@AB@%/* attrib.c -- this program illustrates attributes */%@AE@%%@NL@%
  16750. %@AB@%/* program list: attrib.c, scrfun.c                */%@AE@%%@NL@%
  16751. %@AB@%/* user include files: scrn.h                      */%@AE@%%@NL@%
  16752. %@AB@%/* Note: activate Screen Swapping On in Debug menu */%@AE@%%@NL@%
  16753. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  16754. %@AI@%#include %@AE@%<conio.h> %@NL@%
  16755. %@AI@%#include %@AE@%"scrn.h" %@NL@%
  16756. %@AI@%#define %@AE@%PAGE 0 %@NL@%
  16757. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  16758. char *Format = "This message is displayed using an "%@NL@%
  16759.                "attribute value of %2X hex (%s).";%@NL@%
  16760. int Get_attrib(char *);%@NL@%
  16761. void Print_attr(char *, unsigned char, unsigned char);%@NL@%
  16762. %@NL@%
  16763. main()%@NL@%
  16764. {%@NL@%
  16765. %@NL@%
  16766.     int attribute;       %@AB@%/* value of attribute */%@AE@%%@NL@%
  16767.     char attr_str[9];    %@AB@%/* attr. in string form */%@AE@%%@NL@%
  16768.     char mesg[80];%@NL@%
  16769. %@NL@%
  16770.     Clearscr();%@NL@%
  16771.     Home();%@NL@%
  16772.     printf("Enter an attribute as an 8-digit binary "%@NL@%
  16773.            "number, such as 00000111, and see a\n"%@NL@%
  16774.            "message displayed using that attribute."%@NL@%
  16775.            "Hit <Esc> to quit.\n"%@NL@%
  16776.            "Attribute = ");%@NL@%
  16777.     while ((attribute = Get_attrib(attr_str)) != -1)%@NL@%
  16778.         {%@NL@%
  16779.         Setcurs(10,0,PAGE);%@NL@%
  16780.         sprintf(mesg, Format, attribute, attr_str);%@NL@%
  16781.         Print_attr(mesg, attribute, PAGE);%@NL@%
  16782.         Setcurs(2, 12, PAGE);%@NL@%
  16783.         printf("         ");  %@AB@%/* clear old display */%@AE@%%@NL@%
  16784.         Setcurs(2, 12, PAGE);%@NL@%
  16785.         }%@NL@%
  16786.     Clearscr();%@NL@%
  16787. }%@NL@%
  16788. %@NL@%
  16789. %@AB@%/* The following function reads in a binary number    */%@AE@%%@NL@%
  16790. %@AB@%/* as a sequence of 1s and 0s. It places the 1 and 0  */%@AE@%%@NL@%
  16791. %@AB@%/* characters in a string whose address is passed as  */%@AE@%%@NL@%
  16792. %@AB@%/* an argument. It returns the numeric value of the   */%@AE@%%@NL@%
  16793. %@AB@%/* binary number. Bad input is summarily rejected.    */%@AE@%%@NL@%
  16794. %@AB@%/* The function returns -1 when you press Esc.        */%@AE@%%@NL@%
  16795. %@NL@%
  16796. int Get_attrib(a_str)%@NL@%
  16797. char a_str[];     %@AB@%/* attribute as binary string */%@AE@%%@NL@%
  16798. {%@NL@%
  16799.     int attrib[8];%@NL@%
  16800.     int index = 7;%@NL@%
  16801.     int ch;%@NL@%
  16802.     int attribute = 0; %@AB@%/* attrib. as numeric value */%@AE@%%@NL@%
  16803.     int pow;%@NL@%
  16804. %@NL@%
  16805.     a_str[8] = '\0';  %@AB@%/* terminate string */%@AE@%%@NL@%
  16806.     while ((index >= 0) &&  (ch = getch()) != ESC)%@NL@%
  16807.         {%@NL@%
  16808.         if (ch != '0' && ch != '1')  %@AB@%/* bad input */%@AE@%%@NL@%
  16809.             putch('\a');%@NL@%
  16810.         else%@NL@%
  16811.             {%@NL@%
  16812.             putch(ch);%@NL@%
  16813.             a_str[index] = ch;      %@AB@%/* string form */%@AE@%%@NL@%
  16814.             attrib[index--] = ch - '0'; %@AB@%/* numeric */%@AE@%%@NL@%
  16815.             }%@NL@%
  16816.         }%@NL@%
  16817.     if (ch == ESC)%@NL@%
  16818.         return (-1);%@NL@%
  16819.     else            %@AB@%/* convert numeric array to a number */%@AE@%%@NL@%
  16820.         {%@NL@%
  16821.         for(index = 0, pow = 1; index < 8;%@NL@%
  16822.                                   index++, pow *= 2)%@NL@%
  16823.             attribute += attrib[index] * pow;%@NL@%
  16824.         return attribute;%@NL@%
  16825.         }%@NL@%
  16826. }%@NL@%
  16827. %@NL@%
  16828. %@AB@%/* The following function prints the string str using */%@AE@%%@NL@%
  16829. %@AB@%/* attribute attr on the indicated page.              */%@AE@%%@NL@%
  16830. %@AB@%/* It uses functions from the scrfun.c file.          */%@AE@%%@NL@%
  16831. %@NL@%
  16832. void Print_attr(str, attr, page)%@NL@%
  16833. char *str;%@NL@%
  16834. unsigned char attr, page;%@NL@%
  16835. {%@NL@%
  16836.     while (*str != '\0')%@NL@%
  16837.         {%@NL@%
  16838.         Write_ch_atr(*str++, attr , page, 1);%@NL@%
  16839.         Cursrt();%@NL@%
  16840.         }%@NL@%
  16841. }%@NL@%
  16842. %@NL@%
  16843. %@NL@%
  16844. %@2@%%@AH@%AVGTEMP.C%@AE@%%@EH@%%@NL@%
  16845. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\AVGTEMP.C%@AE@%%@NL@%
  16846. %@NL@%
  16847. %@AB@%/* avgtemp.c -- finds average temperature */%@AE@%%@NL@%
  16848. %@AB@%/*              for the week              */%@AE@%%@NL@%
  16849. %@NL@%
  16850. main()%@NL@%
  16851. {%@NL@%
  16852.     int t1, t2, t3, t4, t5, t6, t7;%@NL@%
  16853.     float avg;%@NL@%
  16854. %@NL@%
  16855.     printf("Enter the high temperature for:\n");%@NL@%
  16856.     printf("Monday: ");%@NL@%
  16857.     scanf("%d", &t1);%@NL@%
  16858.     printf("Tuesday: ");%@NL@%
  16859.     scanf("%d", &t2);%@NL@%
  16860.     printf("Wednesday: ");%@NL@%
  16861.     scanf("%d", &t3);%@NL@%
  16862.     printf("Thursday: ");%@NL@%
  16863.     scanf("%d", &t4);%@NL@%
  16864.     printf("Friday: ");%@NL@%
  16865.     scanf("%d", &t5);%@NL@%
  16866.     printf("Saturday: ");%@NL@%
  16867.     scanf("%d", &t6);%@NL@%
  16868.     printf("Sunday: ");%@NL@%
  16869.     scanf("%d", &t7);%@NL@%
  16870. %@NL@%
  16871.     %@AB@%/* calculate and display average */%@AE@%%@NL@%
  16872.     avg = (t1 + t2 + t3 + t4 + t5 + t6 + t7) / 7.0;%@NL@%
  16873.           %@AB@%/* divide by 7.0 to ensure float result */%@AE@%%@NL@%
  16874.     printf("The average high temperature for");%@NL@%
  16875.     printf(" this week was %5.2f degrees.\n", avg);%@NL@%
  16876. }%@NL@%
  16877. %@NL@%
  16878. %@NL@%
  16879. %@2@%%@AH@%BACKWARD.C%@AE@%%@EH@%%@NL@%
  16880. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\BACKWARD.C%@AE@%%@NL@%
  16881. %@NL@%
  16882. %@AB@%/* backward.c -- the backwards word displayer         */%@AE@%%@NL@%
  16883. %@NL@%
  16884. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  16885. %@AI@%#define %@AE@%SIZE 5 %@NL@%
  16886. char word[SIZE] = "trap";%@NL@%
  16887. main()%@NL@%
  16888. {%@NL@%
  16889.     unsigned int index;%@NL@%
  16890. %@NL@%
  16891.     printf("%s backwards is ", word);%@NL@%
  16892.     for (index = SIZE - 2; index >= 0; index--)%@NL@%
  16893.         putchar(word[index]);%@NL@%
  16894.     putchar('\n');%@NL@%
  16895. }%@NL@%
  16896. %@NL@%
  16897. %@NL@%
  16898. %@NL@%
  16899. %@2@%%@AH@%BADPUTC.C%@AE@%%@EH@%%@NL@%
  16900. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\BADPUTC.C%@AE@%%@NL@%
  16901. %@NL@%
  16902. %@AB@%/* badputc.c -- misusing putc()                  */%@AE@%%@NL@%
  16903. %@NL@%
  16904. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  16905. main()%@NL@%
  16906. {%@NL@%
  16907.     FILE *fp;%@NL@%
  16908.     int ch;%@NL@%
  16909. %@NL@%
  16910.     if ((fp = fopen("junk", "w")) == NULL)%@NL@%
  16911.         exit(1);%@NL@%
  16912. %@NL@%
  16913.     while ((ch = getchar()) != EOF)%@NL@%
  16914.         putc(fp, ch);%@NL@%
  16915.     fclose(fp);%@NL@%
  16916. }%@NL@%
  16917. %@NL@%
  16918. %@NL@%
  16919. %@NL@%
  16920. %@2@%%@AH@%BADREF.C%@AE@%%@EH@%%@NL@%
  16921. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\BADREF.C%@AE@%%@NL@%
  16922. %@NL@%
  16923. %@AB@%/* badref.c -- misusing a pointer                     */%@AE@%%@NL@%
  16924. main()%@NL@%
  16925. {%@NL@%
  16926.     char name[81];%@NL@%
  16927.     char *pt_ch;%@NL@%
  16928. %@NL@%
  16929.     printf("Enter your first name: -> ");%@NL@%
  16930.     scanf("%s", name);%@NL@%
  16931.     *pt_ch = name[1];%@NL@%
  16932.     printf("The second letter of your name is %c\n",%@NL@%
  16933.             *pt_ch );%@NL@%
  16934. }%@NL@%
  16935. %@NL@%
  16936. %@NL@%
  16937. %@NL@%
  16938. %@2@%%@AH@%BADSIGN.C%@AE@%%@EH@%%@NL@%
  16939. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\BADSIGN.C%@AE@%%@NL@%
  16940. %@NL@%
  16941. %@AB@%/* badsign.c -- uncaught typo                 */%@AE@%%@NL@%
  16942. %@NL@%
  16943. main()%@NL@%
  16944. { %@NL@%
  16945.     int i; %@NL@%
  16946.     int j = 1; %@NL@%
  16947.  %@NL@%
  16948.     for (i = 0; i < 10; i++)%@NL@%
  16949.         {%@NL@%
  16950.         j =+ 10;         %@AB@%/* transposed += */%@AE@%%@NL@%
  16951.         printf("%4d ", j); %@NL@%
  16952.         }%@NL@%
  16953.     printf("\n"); %@NL@%
  16954. } %@NL@%
  16955. %@NL@%
  16956. %@NL@%
  16957. %@NL@%
  16958. %@2@%%@AH@%BIFFRED.C%@AE@%%@EH@%%@NL@%
  16959. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\BIFFRED.C%@AE@%%@NL@%
  16960. %@NL@%
  16961. %@AB@%/* biffred.c  --  strings in the string pool can be */%@AE@%%@NL@%
  16962. %@AB@%/*                manipulated via pointers          */%@AE@%%@NL@%
  16963. %@NL@%
  16964. char Start[] = "start";%@NL@%
  16965. %@NL@%
  16966. main()%@NL@%
  16967. {%@NL@%
  16968.     char *cp;%@NL@%
  16969.     int pass;%@NL@%
  16970. %@NL@%
  16971.     for (pass = 0; pass < 2; ++pass)%@NL@%
  16972.         {%@NL@%
  16973.         printf("My name is FRED\n");%@NL@%
  16974. %@NL@%
  16975.         cp = Start;        %@NL@%
  16976. %@NL@%
  16977.         while (*cp != 'F')%@NL@%
  16978.             ++cp;%@NL@%
  16979. %@NL@%
  16980.         *cp   = 'B';%@NL@%
  16981.         *++cp = 'I';%@NL@%
  16982.         *++cp = 'F';%@NL@%
  16983.         *++cp = 'F';%@NL@%
  16984.         }%@NL@%
  16985. }%@NL@%
  16986. %@NL@%
  16987. %@NL@%
  16988. %@2@%%@AH@%BITOUT.C%@AE@%%@EH@%%@NL@%
  16989. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\BITOUT.C%@AE@%%@NL@%
  16990. %@NL@%
  16991. %@AB@%/* bitout.c  -- compiles one way on an IBM-PC and      */%@AE@%%@NL@%
  16992. %@AB@%/*              another on a 68000 chip-based machine  */%@AE@%%@NL@%
  16993. %@NL@%
  16994. CHIP_80286    %@AB@%/* don't define on a 68000 machine */%@AE@%%@NL@%
  16995. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  16996. %@NL@%
  16997. main()%@NL@%
  16998. {%@NL@%
  16999.     int num;%@NL@%
  17000. %@NL@%
  17001.     printf("Enter an integer number and I will print"%@NL@%
  17002.            " it out in binary\nNumber: ");%@NL@%
  17003.     %@NL@%
  17004.     if (scanf("%d", &num) != 1)%@NL@%
  17005.         {%@NL@%
  17006.         fprintf(stderr, "Not an integer\n");%@NL@%
  17007.         exit(1);%@NL@%
  17008.         }%@NL@%
  17009.     Bitout(num);%@NL@%
  17010. }%@NL@%
  17011. %@NL@%
  17012. Bitout(unsigned int num)%@NL@%
  17013. {%@NL@%
  17014.     int i, j;%@NL@%
  17015.     unsigned char *cp;%@NL@%
  17016. %@NL@%
  17017.     cp = (char *)#%@NL@%
  17018. %@NL@%
  17019. defined(CHIP_80286)    %@AB@%/* IBM-PC */%@AE@%%@NL@%
  17020.     for (i = 1; i >= 0; --i)%@NL@%
  17021. %@AI@%#endif %@AE@%%@NL@%
  17022. !defined(CHIP_80286)   %@AB@%/* otherwise 68000 machine */%@AE@%%@NL@%
  17023.     for (i = 0; i < 4; ++i)%@NL@%
  17024. %@AI@%#endif %@AE@%%@NL@%
  17025.         {%@NL@%
  17026.         for (j = 7; j >= 0; --j)%@NL@%
  17027.             putchar((cp[i] & (1 << j)) ? '1' : '0');%@NL@%
  17028.         }%@NL@%
  17029.     putchar('\n');%@NL@%
  17030. }%@NL@%
  17031. %@NL@%
  17032. %@NL@%
  17033. %@2@%%@AH@%BITWISE.C%@AE@%%@EH@%%@NL@%
  17034. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\BITWISE.C%@AE@%%@NL@%
  17035. %@NL@%
  17036. %@AB@%/* bitwise.c -- demonstrate the bitwise operators */%@AE@%%@NL@%
  17037. %@NL@%
  17038. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  17039. %@NL@%
  17040. main()%@NL@%
  17041. {%@NL@%
  17042.     unsigned int val1, val2, result;%@NL@%
  17043.     int ch;%@NL@%
  17044.     extern void show();%@NL@%
  17045. %@NL@%
  17046.     while(1)%@NL@%
  17047.         {%@NL@%
  17048.         printf("\nval1: ");%@NL@%
  17049.         if (scanf("%d", &val1) != 1)%@NL@%
  17050.             break;%@NL@%
  17051. %@NL@%
  17052.         printf("val2: ");%@NL@%
  17053.         if (scanf("%d", &val2) != 1)%@NL@%
  17054.             break;%@NL@%
  17055. %@NL@%
  17056.         printf("\tval1   = ");%@NL@%
  17057.         show(val1);%@NL@%
  17058.         printf("\tval2   = ");%@NL@%
  17059.         show(val2);%@NL@%
  17060. %@NL@%
  17061.         printf("Bitwise Operator: ");%@NL@%
  17062.         while ((ch = getchar()) == '\n')%@NL@%
  17063.             {%@NL@%
  17064.             continue;%@NL@%
  17065.             }%@NL@%
  17066.         if (ch == EOF)%@NL@%
  17067.             break;%@NL@%
  17068.         switch (ch)%@NL@%
  17069.             {%@NL@%
  17070.             case '&':%@NL@%
  17071.                 result = val1 & val2;%@NL@%
  17072.                 printf("Executing: result = val1 & val2;\n");%@NL@%
  17073.                 break;%@NL@%
  17074.             case '|':%@NL@%
  17075.                 result = val1 |= val2;%@NL@%
  17076.                 printf("Executing: result = val1 | val2;\n");%@NL@%
  17077.                 break;%@NL@%
  17078.             case '^':%@NL@%
  17079.                 result = val1 ^= val2;%@NL@%
  17080.                 printf("Executing: result = val1 ^ val2;\n");%@NL@%
  17081.                 break;%@NL@%
  17082.             case '~':%@NL@%
  17083.                 result = ~val1;%@NL@%
  17084.                 printf("Executing: result = ~val1;\n");%@NL@%
  17085.                 printf("\tresult = ");%@NL@%
  17086.                 show(result);%@NL@%
  17087.                 result = ~val2;%@NL@%
  17088.                 printf("Executing: result = ~val2;\n");%@NL@%
  17089.                 break;%@NL@%
  17090.             case '<':%@NL@%
  17091.                 result = val1 <<= val2;%@NL@%
  17092.                 printf("Executing: result = val1 <<val2;\n");%@NL@%
  17093.                 break;%@NL@%
  17094.             case '>':%@NL@%
  17095.                 result = val1 >>= val2;%@NL@%
  17096.                 printf("Executing: result = val1 >>val2;\n");%@NL@%
  17097.                 break;%@NL@%
  17098.             case 'q':%@NL@%
  17099.             case 'Q':%@NL@%
  17100.                 return(0);%@NL@%
  17101.               default:%@NL@%
  17102.                 continue;%@NL@%
  17103.             }%@NL@%
  17104.         printf("\tresult = ");%@NL@%
  17105.         show(result);%@NL@%
  17106.         }%@NL@%
  17107. }%@NL@%
  17108. %@NL@%
  17109. void bitout(unsigned char num[])%@NL@%
  17110. {%@NL@%
  17111.     int bytes = 2, i, j;%@NL@%
  17112. %@NL@%
  17113.     %@AB@%/* IBM PC stores ints low/hi. */%@AE@%%@NL@%
  17114.     for (i = bytes-1; i >= 0; --i)%@NL@%
  17115.         {%@NL@%
  17116.         for (j = 7; j >= 0; --j)%@NL@%
  17117.             {%@NL@%
  17118.             putchar((num[i]&(1<<j))?'1':'0');%@NL@%
  17119.             }%@NL@%
  17120.         }%@NL@%
  17121. }%@NL@%
  17122. %@NL@%
  17123. void show(unsigned int val)%@NL@%
  17124. {%@NL@%
  17125.     extern void bitout();%@NL@%
  17126. %@NL@%
  17127.     printf("(%05u decimal)", val);%@NL@%
  17128.     bitout(&val);%@NL@%
  17129.     printf(" binary\n");%@NL@%
  17130. }%@NL@%
  17131. %@NL@%
  17132. %@NL@%
  17133. %@2@%%@AH@%BLANK.C%@AE@%%@EH@%%@NL@%
  17134. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\BLANK.C%@AE@%%@NL@%
  17135. %@NL@%
  17136. %@AB@%/* blank.c -- blanks MDA screen                        */%@AE@%%@NL@%
  17137. %@AB@%/* program list -- blank.c (outp() not in core lib)    */%@AE@%%@NL@%
  17138. %@NL@%
  17139. %@AI@%#include %@AE@%<conio.h> %@NL@%
  17140. CONTROLREG 0x3B8 %@AB@%/* control register MDA */%@AE@%%@NL@%
  17141. %@AI@%#define %@AE@%DEFAULTSET 0x29 %@NL@%
  17142. %@AI@%#define %@AE@%VIDEOOFF 0x21 %@NL@%
  17143. main()%@NL@%
  17144. {%@NL@%
  17145.     outp(CONTROLREG, VIDEOOFF);%@NL@%
  17146.     getch();%@NL@%
  17147.     outp(CONTROLREG, DEFAULTSET);%@NL@%
  17148. }%@NL@%
  17149. %@NL@%
  17150. %@NL@%
  17151. %@2@%%@AH@%BOX.C%@AE@%%@EH@%%@NL@%
  17152. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\BOX.C%@AE@%%@NL@%
  17153. %@NL@%
  17154. %@AB@%/* box.c  --  demonstrate the result of initializing  */%@AE@%%@NL@%
  17155. %@AB@%/*            a three-dimensional array               */%@AE@%%@NL@%
  17156. %@NL@%
  17157. main()%@NL@%
  17158. {%@NL@%
  17159.     static int cube[3][3][3] = {%@NL@%
  17160.         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,%@NL@%
  17161.         13, 14, 15, 16, 17, 18, 19, 20, 21, 22,%@NL@%
  17162.         23, 24, 25, 26, 27 };%@NL@%
  17163.     int plane;%@NL@%
  17164.     extern void Draw_plane();%@NL@%
  17165. %@NL@%
  17166.     for (plane = 0; plane < 3; ++plane)%@NL@%
  17167.         {%@NL@%
  17168.         Draw_plane(cube, plane);%@NL@%
  17169.         }%@NL@%
  17170. }%@NL@%
  17171. %@NL@%
  17172. void Draw_plane(int box[3][3][3], int slice)%@NL@%
  17173.     {%@NL@%
  17174.     int row, col;%@NL@%
  17175. %@NL@%
  17176.     printf("Plane[%d] =\n", slice);%@NL@%
  17177.     for (row = 0; row < 3; ++row)%@NL@%
  17178.         {%@NL@%
  17179.         for (col = 0; col < 3; ++col)%@NL@%
  17180.             {%@NL@%
  17181.             printf( "%2d ", box[slice][row][col]);%@NL@%
  17182.             }%@NL@%
  17183.         printf("\n");%@NL@%
  17184.         }%@NL@%
  17185.     printf("\n");%@NL@%
  17186. }%@NL@%
  17187. %@NL@%
  17188. %@NL@%
  17189. %@2@%%@AH@%BREAK.C%@AE@%%@EH@%%@NL@%
  17190. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP05\BREAK.C%@AE@%%@NL@%
  17191. %@NL@%
  17192. %@AB@%/* break.c -- shows how to get out of loop with BREAK */%@AE@%%@NL@%
  17193. %@NL@%
  17194. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  17195. %@AI@%#define %@AE@%TRUE 1 %@NL@%
  17196. %@NL@%
  17197. main()%@NL@%
  17198. {%@NL@%
  17199.     int number;%@NL@%
  17200.     while (TRUE) %@AB@%/* endless loop */%@AE@%%@NL@%
  17201.         {%@NL@%
  17202.         %@AB@%/* get a random number between 0 and 32767 */%@AE@%%@NL@%
  17203.         number = rand();%@NL@%
  17204.         printf("%d\n", number);%@NL@%
  17205. %@NL@%
  17206.         %@AB@%/* break out of loop if random number */%@AE@%%@NL@%
  17207.         %@AB@%/* is greater than 32000              */%@AE@%%@NL@%
  17208.         if (number > 32000)%@NL@%
  17209.             break; %@AB@%/* exit WHILE loop */%@AE@%%@NL@%
  17210.         }%@NL@%
  17211.     printf("Broken out of WHILE loop.\n");%@NL@%
  17212. }%@NL@%
  17213. %@NL@%
  17214. %@NL@%
  17215. %@2@%%@AH@%BUBSORT.C%@AE@%%@EH@%%@NL@%
  17216. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\BUBSORT.C%@AE@%%@NL@%
  17217. %@NL@%
  17218. %@AB@%/* bubsort.c  --  passing an array to a function  */%@AE@%%@NL@%
  17219. %@AB@%/*                affects the original array      */%@AE@%%@NL@%
  17220. %@NL@%
  17221. %@AI@%#define %@AE@%NUMINTS 6 %@NL@%
  17222. %@NL@%
  17223. extern void Bub_sort();%@NL@%
  17224. extern void Triple();%@NL@%
  17225. %@NL@%
  17226. main()%@NL@%
  17227. {%@NL@%
  17228.     int num = 2, i;%@NL@%
  17229.     static int list[NUMINTS] = { 6, 5, 4, 3, 2, 1 };%@NL@%
  17230. %@NL@%
  17231.     printf("num before Triple = %d\n", num);%@NL@%
  17232.     Triple(num);%@NL@%
  17233.     printf("num after Triple  = %d\n", num);%@NL@%
  17234.     printf("list[0] before Triple = %d\n", list[0]);%@NL@%
  17235.     Triple(list[0]);%@NL@%
  17236.     printf("list[0] after Triple  = %d\n", list[0]);%@NL@%
  17237. %@NL@%
  17238.     printf("Before sorting -> ");%@NL@%
  17239.     for (i = 0; i < NUMINTS; ++i)%@NL@%
  17240.         {%@NL@%
  17241.         printf("%d ", list[i]);%@NL@%
  17242.         }%@NL@%
  17243.     printf("\n");%@NL@%
  17244. %@NL@%
  17245.     Bub_sort(list);%@NL@%
  17246.     printf("After sorting ->  ");%@NL@%
  17247.     for (i = 0; i < NUMINTS; ++i)%@NL@%
  17248.         {%@NL@%
  17249.         printf("%d ", list[i]);%@NL@%
  17250.         }%@NL@%
  17251.     printf("\n");%@NL@%
  17252. %@NL@%
  17253. }%@NL@%
  17254. %@NL@%
  17255. void Triple(int x)  %@AB@%/* function doesn't affect original */%@AE@%%@NL@%
  17256. {%@NL@%
  17257.     x *= 3;%@NL@%
  17258. }%@NL@%
  17259. %@NL@%
  17260. void Bub_sort(int vals[NUMINTS]) %@AB@%/* function changes original */%@AE@%%@NL@%
  17261. {%@NL@%
  17262.     int i, j, temp;%@NL@%
  17263. %@NL@%
  17264.     for (i = (NUMINTS - 1); i > 0; --i)%@NL@%
  17265.         {%@NL@%
  17266.         for (j = 0; j < i; ++j)%@NL@%
  17267.             {%@NL@%
  17268.             if (vals[j] > vals[j+1])%@NL@%
  17269.                 {%@NL@%
  17270.                 temp      = vals[j];%@NL@%
  17271.                 vals[j]   = vals[j+1];%@NL@%
  17272.                 vals[j+1] = temp;%@NL@%
  17273.                 }%@NL@%
  17274.             }%@NL@%
  17275.         }%@NL@%
  17276. }%@NL@%
  17277. %@NL@%
  17278. %@NL@%
  17279. %@2@%%@AH@%BUG.C%@AE@%%@EH@%%@NL@%
  17280. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\BUG.C%@AE@%%@NL@%
  17281. %@NL@%
  17282. %@AB@%/* bug.c  --  shows how different levels of debugging  */%@AE@%%@NL@%
  17283. %@AB@%/*            output can be produced using #if         */%@AE@%%@NL@%
  17284. %@NL@%
  17285. DEBUG_LEVEL 2      %@AB@%/* 0 = none, 1-2 for debug  */%@AE@%%@NL@%
  17286. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  17287. %@NL@%
  17288. main()%@NL@%
  17289. {%@NL@%
  17290.     int ret;%@NL@%
  17291. %@NL@%
  17292. %@AI@%#if %@AE@%(DEBUG_LEVEL == 2) %@NL@%
  17293.     fprintf(stderr, "Entering main()\n");%@NL@%
  17294. %@AI@%#endif %@AE@%%@NL@%
  17295. %@NL@%
  17296. %@AI@%#if %@AE@%(DEBUG_LEVEL == 1) %@NL@%
  17297.     fprintf(stderr, "Calling sub()\n");%@NL@%
  17298. %@AI@%#endif %@AE@%%@NL@%
  17299. %@NL@%
  17300.     ret = sub();%@NL@%
  17301. %@NL@%
  17302. %@AI@%#if %@AE@%(DEBUG_LEVEL == 1) %@NL@%
  17303.     fprintf(stderr, "sub() returned %d\n", ret);%@NL@%
  17304. %@AI@%#endif %@AE@%%@NL@%
  17305. %@NL@%
  17306. %@AI@%#if %@AE@%(DEBUG_LEVEL == 2) %@NL@%
  17307.     fprintf(stderr, "Leaving main()\n");%@NL@%
  17308. %@AI@%#endif %@AE@%%@NL@%
  17309. %@NL@%
  17310. }%@NL@%
  17311. %@NL@%
  17312. sub()%@NL@%
  17313. {%@NL@%
  17314.     return (5);%@NL@%
  17315. }%@NL@%
  17316. %@NL@%
  17317. %@NL@%
  17318. %@2@%%@AH@%CARD.C%@AE@%%@EH@%%@NL@%
  17319. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP11\CARD.C%@AE@%%@NL@%
  17320. %@NL@%
  17321. %@AB@%/* card.c  --  demonstrates how to declare structures  */%@AE@%%@NL@%
  17322. %@AB@%/*             and how to use structure members        */%@AE@%%@NL@%
  17323. %@NL@%
  17324. <stdio.h>      %@AB@%/* for NULL  and stdin */%@AE@%%@NL@%
  17325. <string.h>     %@AB@%/* for strdup()        */%@AE@%%@NL@%
  17326. %@NL@%
  17327. %@AI@%#define %@AE@%MAXN 79 %@NL@%
  17328. %@NL@%
  17329. struct cardstruct {                 %@AB@%/* global pattern */%@AE@%%@NL@%
  17330.     char *first, *last, *middle;%@NL@%
  17331.     long street_num;%@NL@%
  17332.     char *street, *city, *state;%@NL@%
  17333.     long zip;%@NL@%
  17334.     int  area_code;%@NL@%
  17335.     long phone;%@NL@%
  17336. };%@NL@%
  17337. %@NL@%
  17338. main()%@NL@%
  17339. {%@NL@%
  17340.     char *Str_Input();%@NL@%
  17341.     long Lint_Input();%@NL@%
  17342.     struct cardstruct card1;%@NL@%
  17343. %@NL@%
  17344.     card1.first         = Str_Input("First Name");%@NL@%
  17345.     card1.last          = Str_Input("Last Name");%@NL@%
  17346.     card1.middle        = Str_Input("Middle Name");%@NL@%
  17347.     card1.street_num    = Lint_Input("Street Number");%@NL@%
  17348.     card1.street        = Str_Input("Street Name");%@NL@%
  17349.     card1.city          = Str_Input("City");%@NL@%
  17350.     card1.state         = Str_Input("State");%@NL@%
  17351.     card1.zip           = Lint_Input("Zip Code");%@NL@%
  17352.     card1.area_code     = (int)Lint_Input("Area Code");%@NL@%
  17353.     card1.phone         = Lint_Input("Phone Number");%@NL@%
  17354. %@NL@%
  17355.     printf("\n\n");%@NL@%
  17356.     printf("%s %s %s\n", card1.first, card1.middle, %@NL@%
  17357.             card1.last);%@NL@%
  17358.     printf("%ld %s, %s, %s %ld\n", card1.street_num, %@NL@%
  17359.             card1.street, card1.city, card1.state,%@NL@%
  17360.             card1.zip);%@NL@%
  17361.     printf("(%d) %ld\n", card1.area_code, card1.phone);%@NL@%
  17362. %@NL@%
  17363.     return (0);%@NL@%
  17364.     }%@NL@%
  17365. %@NL@%
  17366. char *Str_Input(char *prompt)%@NL@%
  17367.     {%@NL@%
  17368.     char buf[MAXN+1], *ptr;%@NL@%
  17369. %@NL@%
  17370.     printf("%s: ", prompt);%@NL@%
  17371.     if (fgets(buf, MAXN, stdin) == NULL)%@NL@%
  17372.         exit(0);%@NL@%
  17373.     buf[strlen(buf) - 1] = '\0'; %@AB@%/* strip '\n' */%@AE@%%@NL@%
  17374.     if (strlen(buf) == 0)%@NL@%
  17375.         exit(0);%@NL@%
  17376.     if ((ptr = strdup(buf)) == NULL)%@NL@%
  17377.         exit(0);%@NL@%
  17378.     return (ptr);%@NL@%
  17379. }%@NL@%
  17380. %@NL@%
  17381. long Lint_Input(char *prompt)%@NL@%
  17382. {%@NL@%
  17383.     char buf[MAXN + 1];%@NL@%
  17384.     long num;%@NL@%
  17385. %@NL@%
  17386.     printf("%s: ", prompt);%@NL@%
  17387.     if (fgets(buf, MAXN, stdin) == NULL)%@NL@%
  17388.         exit(0);%@NL@%
  17389.     if (sscanf(buf, "%ld", &num) != 1)%@NL@%
  17390.         exit(0);%@NL@%
  17391.     return (num);%@NL@%
  17392. }%@NL@%
  17393. %@NL@%
  17394. %@NL@%
  17395. %@2@%%@AH@%CARD2.C%@AE@%%@EH@%%@NL@%
  17396. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP11\CARD2.C%@AE@%%@NL@%
  17397. %@NL@%
  17398. %@AB@%/* card2.c --  demonstrates structure assignment and   */%@AE@%%@NL@%
  17399. %@AB@%/*             how to pass a structure to a function   */%@AE@%%@NL@%
  17400. %@NL@%
  17401. <stdio.h>      %@AB@%/* for NULL  and stdin */%@AE@%%@NL@%
  17402. <string.h>     %@AB@%/* for strdup()        */%@AE@%%@NL@%
  17403. %@NL@%
  17404. %@AI@%#define %@AE@%MAXN 79 %@NL@%
  17405. %@NL@%
  17406. struct cardstruct {                 %@AB@%/* global pattern */%@AE@%%@NL@%
  17407.     char *first, *last, *middle;%@NL@%
  17408.     long street_num;%@NL@%
  17409.     char *street, *city, *state;%@NL@%
  17410.     long zip;%@NL@%
  17411.     int  area_code;%@NL@%
  17412.     long phone;%@NL@%
  17413. };%@NL@%
  17414. %@NL@%
  17415. main()%@NL@%
  17416. {%@NL@%
  17417.     int  i;%@NL@%
  17418.     char *Str_Input();%@NL@%
  17419.     long Lint_Input();%@NL@%
  17420.     struct cardstruct card1, card2;%@NL@%
  17421. %@NL@%
  17422.     for (i = 0; i < 2; i++) %@AB@%/* do twice */%@AE@%%@NL@%
  17423.         {%@NL@%
  17424.         printf("\nCard %d:\n\n", i + 1);%@NL@%
  17425. %@NL@%
  17426.         card1.first         = Str_Input("First Name");%@NL@%
  17427.         card1.last          = Str_Input("Last Name");%@NL@%
  17428.         card1.middle        = Str_Input("Middle Name");%@NL@%
  17429.         card1.street_num    = Lint_Input("Street Number");%@NL@%
  17430.         card1.street        = Str_Input("Street Name");%@NL@%
  17431.         card1.city          = Str_Input("City");%@NL@%
  17432.         card1.state         = Str_Input("State");%@NL@%
  17433.         card1.zip           = Lint_Input("Zip Code");%@NL@%
  17434.         card1.area_code     = (int)Lint_Input("Area Code");%@NL@%
  17435.         card1.phone         = Lint_Input("Phone Number");%@NL@%
  17436. %@NL@%
  17437.         if (i == 0)%@NL@%
  17438.             card2 = card1;      %@AB@%/* structure assignment */%@AE@%%@NL@%
  17439.         }%@NL@%
  17440.     Showcard(card2);%@NL@%
  17441.     Showcard(card1);%@NL@%
  17442. %@NL@%
  17443. }%@NL@%
  17444. %@NL@%
  17445. Showcard(struct cardstruct card)%@NL@%
  17446. {%@NL@%
  17447.     printf("\n\n");%@NL@%
  17448. %@NL@%
  17449.     printf("%s %s %s\n", card.first, card.middle, %@NL@%
  17450.             card.last);%@NL@%
  17451.     printf("%ld %s, %s, %s %ld\n", card.street_num, %@NL@%
  17452.             card.street, card.city, card.state,%@NL@%
  17453.             card.zip);%@NL@%
  17454.     printf("(%d) %ld\n", card.area_code, card.phone);%@NL@%
  17455. }%@NL@%
  17456. %@NL@%
  17457. char *Str_Input(char *prompt)%@NL@%
  17458. {%@NL@%
  17459.     char buf[MAXN + 1], *ptr;%@NL@%
  17460. %@NL@%
  17461.     printf("%s: ", prompt);%@NL@%
  17462.     if (fgets(buf, MAXN, stdin) == NULL)%@NL@%
  17463.         exit(0);%@NL@%
  17464.     buf[strlen(buf) - 1] = '\0'; %@AB@%/* strip '\n' */%@AE@%%@NL@%
  17465.     if (strlen(buf) == 0)%@NL@%
  17466.         exit(0);%@NL@%
  17467.     if ((ptr = strdup(buf)) == NULL)%@NL@%
  17468.         exit(0);%@NL@%
  17469.     return (ptr);%@NL@%
  17470. }%@NL@%
  17471. %@NL@%
  17472. long Lint_Input(char *prompt)%@NL@%
  17473. {%@NL@%
  17474.     char buf[MAXN + 1];%@NL@%
  17475.     long num;%@NL@%
  17476. %@NL@%
  17477.     printf("%s: ", prompt);%@NL@%
  17478.     if (fgets(buf, MAXN, stdin) == NULL)%@NL@%
  17479.         exit(0);%@NL@%
  17480.     if (sscanf(buf, "%ld", &num) != 1)%@NL@%
  17481.         exit(0);%@NL@%
  17482.     return (num);%@NL@%
  17483. }%@NL@%
  17484. %@NL@%
  17485. %@NL@%
  17486. %@2@%%@AH@%CARD3.C%@AE@%%@EH@%%@NL@%
  17487. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP11\CARD3.C%@AE@%%@NL@%
  17488. %@NL@%
  17489. %@AB@%/* card3.c --  demonstrates pointers to structures     */%@AE@%%@NL@%
  17490. %@NL@%
  17491. <stdio.h>      %@AB@%/* for NULL  and stdin */%@AE@%%@NL@%
  17492. <string.h>     %@AB@%/* for strdup()        */%@AE@%%@NL@%
  17493. %@NL@%
  17494. %@AI@%#define %@AE@%MAXN 79 %@NL@%
  17495. %@NL@%
  17496. struct cardstruct {                 %@AB@%/* global pattern */%@AE@%%@NL@%
  17497.     char *first, *last, *middle;%@NL@%
  17498.     long street_num;%@NL@%
  17499.     char *street, *city, *state;%@NL@%
  17500.     long zip;%@NL@%
  17501.     int  area_code;%@NL@%
  17502.     long phone;%@NL@%
  17503. };%@NL@%
  17504. %@NL@%
  17505. main()%@NL@%
  17506. {%@NL@%
  17507.     int  i;%@NL@%
  17508.     char *Str_Input();%@NL@%
  17509.     long Lint_Input();%@NL@%
  17510.     struct cardstruct card1, card2;%@NL@%
  17511. %@NL@%
  17512.     for (i = 0; i < 2; i++) %@AB@%/* do twice */%@AE@%%@NL@%
  17513.         {%@NL@%
  17514.         printf("\nCard %d:\n\n", i + 1);%@NL@%
  17515. %@NL@%
  17516.         card1.first         = Str_Input("First Name");%@NL@%
  17517.         card1.last          = Str_Input("Last Name");%@NL@%
  17518.         card1.middle        = Str_Input("Middle Name");%@NL@%
  17519.         card1.street_num    = Lint_Input("Street Number");%@NL@%
  17520.         card1.street        = Str_Input("Street Name");%@NL@%
  17521.         card1.city          = Str_Input("City");%@NL@%
  17522.         card1.state         = Str_Input("State");%@NL@%
  17523.         card1.zip           = Lint_Input("Zip Code");%@NL@%
  17524.         card1.area_code     = (int)Lint_Input("Area Code");%@NL@%
  17525.         card1.phone         = Lint_Input("Phone Number");%@NL@%
  17526. %@NL@%
  17527.         if (i == 0)%@NL@%
  17528.             card2 = card1;%@NL@%
  17529.         }%@NL@%
  17530.     Showcard(&card2);     %@AB@%/* pass addresses of structures */%@AE@%%@NL@%
  17531.     Showcard(&card1);%@NL@%
  17532. %@NL@%
  17533.     return (0);%@NL@%
  17534. }%@NL@%
  17535. %@NL@%
  17536. Showcard(cardptr)%@NL@%
  17537. struct cardstruct *cardptr; %@AB@%/* pointer receives an address */%@AE@%%@NL@%
  17538.     {%@NL@%
  17539.     printf("\n\n");%@NL@%
  17540. %@NL@%
  17541.     printf("%s %s %s\n", cardptr->first, cardptr->middle, %@NL@%
  17542.             cardptr->last);%@NL@%
  17543.     printf("%ld %s, %s, %s %ld\n", cardptr->street_num, %@NL@%
  17544.             cardptr->street, cardptr->city, cardptr->state,%@NL@%
  17545.             cardptr->zip);%@NL@%
  17546.     printf("(%d) %ld\n", cardptr->area_code, cardptr->phone);%@NL@%
  17547.     }%@NL@%
  17548. %@NL@%
  17549. char *Str_Input(char *prompt)%@NL@%
  17550.     {%@NL@%
  17551.     char buf[MAXN + 1], *ptr;%@NL@%
  17552. %@NL@%
  17553.     printf("%s: ", prompt);%@NL@%
  17554.     if (fgets(buf, MAXN, stdin) == NULL)%@NL@%
  17555.         exit(0);%@NL@%
  17556.     buf[strlen(buf) - 1] = '\0'; %@AB@%/* strip '\n' */%@AE@%%@NL@%
  17557.     if (strlen(buf) == 0)%@NL@%
  17558.         exit(0);%@NL@%
  17559.     if ((ptr = strdup(buf)) == NULL)%@NL@%
  17560.         exit(0);%@NL@%
  17561.     return (ptr);%@NL@%
  17562. }%@NL@%
  17563. %@NL@%
  17564. long Lint_Input(char *prompt)%@NL@%
  17565. {%@NL@%
  17566.     char buf[MAXN + 1];%@NL@%
  17567.     long num;%@NL@%
  17568. %@NL@%
  17569.     printf("%s: ", prompt);%@NL@%
  17570.     if (fgets(buf, MAXN, stdin) == NULL)%@NL@%
  17571.         exit(0);%@NL@%
  17572.     if (sscanf(buf, "%ld", &num) != 1)%@NL@%
  17573.         exit(0);%@NL@%
  17574.     return (num);%@NL@%
  17575. }%@NL@%
  17576. %@NL@%
  17577. %@NL@%
  17578. %@2@%%@AH@%CCOPY.C%@AE@%%@EH@%%@NL@%
  17579. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP10\CCOPY.C%@AE@%%@NL@%
  17580. %@NL@%
  17581. %@AB@%/* ccopy.c  --  Copies a file, cutting blank lines and  */%@AE@%%@NL@%
  17582. %@AB@%/*              leading space from lines of copy        */%@AE@%%@NL@%
  17583. %@NL@%
  17584. <stdio.h>        %@AB@%/* for FILE, BUFSIZ, NULL */%@AE@%%@NL@%
  17585. <ctype.h>        %@AB@%/* for iswhite()          */%@AE@%%@NL@%
  17586. %@NL@%
  17587. main(argc, argv)%@NL@%
  17588. int argc;%@NL@%
  17589. char *argv[];%@NL@%
  17590. {%@NL@%
  17591.     FILE *fp_in, *fp_out;%@NL@%
  17592.     char buf[BUFSIZ];%@NL@%
  17593.     char *cp;%@NL@%
  17594. %@NL@%
  17595.     if (argc != 3)%@NL@%
  17596.         {%@NL@%
  17597.         printf("usage: ccopy infile outfile\n");%@NL@%
  17598.         exit(1);%@NL@%
  17599.         }%@NL@%
  17600.     if ((fp_in = fopen(argv[1], "r")) == NULL)%@NL@%
  17601.         {%@NL@%
  17602.         printf("Can't open %s for reading.\n", argv[1]);%@NL@%
  17603.         exit(1);%@NL@%
  17604.         }%@NL@%
  17605.     if ((fp_out = fopen(argv[2], "w")) == NULL)%@NL@%
  17606.         {%@NL@%
  17607.         printf("Can't open %s for writing.\n", argv[2]);%@NL@%
  17608.         exit(1);%@NL@%
  17609.         }%@NL@%
  17610. %@NL@%
  17611.     printf("Copying and Crushing: %s->%s ...", %@NL@%
  17612.             argv[1], argv[2]);%@NL@%
  17613. %@NL@%
  17614.     while (fgets(buf, BUFSIZ, fp_in) != NULL)%@NL@%
  17615.         {%@NL@%
  17616.         cp = buf;%@NL@%
  17617.         if (*cp == '\n')    %@AB@%/* blank line */%@AE@%%@NL@%
  17618.             continue;%@NL@%
  17619.         while (isspace(*cp))%@NL@%
  17620.             {%@NL@%
  17621.             ++cp;%@NL@%
  17622.             }%@NL@%
  17623.         if (*cp == '\0')    %@AB@%/* empty line */%@AE@%%@NL@%
  17624.             continue;%@NL@%
  17625.         if (fputs(cp, fp_out) == EOF)%@NL@%
  17626.             {%@NL@%
  17627.             printf("\nError writing %s.\n", argv[2]);%@NL@%
  17628.             exit(1);%@NL@%
  17629.             }%@NL@%
  17630.         }%@NL@%
  17631.     printf("Done\n");%@NL@%
  17632. }%@NL@%
  17633. %@NL@%
  17634. %@NL@%
  17635. %@2@%%@AH@%CCOPY2.C%@AE@%%@EH@%%@NL@%
  17636. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP10\CCOPY2.C%@AE@%%@NL@%
  17637. %@NL@%
  17638. %@AB@%/* ccopy2.c  -- copies a file, cutting blank lines and    */%@AE@%%@NL@%
  17639. %@AB@%/*              leading space from lines of copy          */%@AE@%%@NL@%
  17640. %@NL@%
  17641. %@AB@%/*              Modified to demonstrate stdout and stderr */%@AE@%%@NL@%
  17642. %@NL@%
  17643. <stdio.h>        %@AB@%/* for FILE, BUFSIZ, NULL */%@AE@%%@NL@%
  17644. <ctype.h>        %@AB@%/* for iswhite()          */%@AE@%%@NL@%
  17645. %@NL@%
  17646. main(argc, argv)%@NL@%
  17647. int argc;%@NL@%
  17648. char *argv[];%@NL@%
  17649. {%@NL@%
  17650.     FILE *fp_in, *fp_out;%@NL@%
  17651.     char buf[BUFSIZ];%@NL@%
  17652.     char *cp;%@NL@%
  17653. %@NL@%
  17654.     if (argc < 2)%@NL@%
  17655.         {%@NL@%
  17656.         fprintf(stderr, "usage: ccopy infile {outfile}\n");%@NL@%
  17657.         exit(1);%@NL@%
  17658.         }%@NL@%
  17659.     if ((fp_in = fopen(argv[1], "r")) == NULL)%@NL@%
  17660.         {%@NL@%
  17661.         fprintf(stderr, "\"%s\": Can't open.\n", argv[1]);%@NL@%
  17662.         exit(1);%@NL@%
  17663.         }%@NL@%
  17664.     if (argc == 3)%@NL@%
  17665.         {%@NL@%
  17666.         if ((fp_out = fopen(argv[2], "w")) == NULL)%@NL@%
  17667.             {%@NL@%
  17668.             fprintf(stderr, "\"%s\": Can't open.\n", argv[2]);%@NL@%
  17669.             exit(1);%@NL@%
  17670.             }%@NL@%
  17671.         }%@NL@%
  17672.     else%@NL@%
  17673.         fp_out = stdout;%@NL@%
  17674. %@NL@%
  17675.     while (fgets(buf, BUFSIZ, fp_in) != NULL)%@NL@%
  17676.         {%@NL@%
  17677.         cp = buf;%@NL@%
  17678.         if (*cp == '\n')    %@AB@%/* blank line */%@AE@%%@NL@%
  17679.             continue;%@NL@%
  17680.         while (isspace(*cp))%@NL@%
  17681.             {%@NL@%
  17682.             ++cp;%@NL@%
  17683.             }%@NL@%
  17684.         if (*cp == '\0')    %@AB@%/* empty line */%@AE@%%@NL@%
  17685.             continue;%@NL@%
  17686.         if (fputs(cp, fp_out) == EOF)%@NL@%
  17687.             {%@NL@%
  17688.             fprintf(stderr, "Error writing.\n");%@NL@%
  17689.             exit(1);%@NL@%
  17690.             }%@NL@%
  17691.         }%@NL@%
  17692.     if (! feof(fp_in))      %@AB@%/* error reading? */%@AE@%%@NL@%
  17693.         {%@NL@%
  17694.         fprintf(stderr, "\"%s\": Error reading.\n", argv[1]);%@NL@%
  17695.         exit(1);%@NL@%
  17696.         }%@NL@%
  17697. }%@NL@%
  17698. %@NL@%
  17699. %@NL@%
  17700. %@2@%%@AH@%CH2000.C%@AE@%%@EH@%%@NL@%
  17701. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\CH2000.C%@AE@%%@NL@%
  17702. %@NL@%
  17703. %@AB@%/* ch2000.c -- fill screen with 2000 characters        */%@AE@%%@NL@%
  17704. %@AB@%/*    This program demonstrates direct memory access   */%@AE@%%@NL@%
  17705. %@AB@%/*    of video memory.  It is set up for the MDA.      */%@AE@%%@NL@%
  17706. %@AB@%/*    Assign CGAMEM instead of MONMEM to screen for    */%@AE@%%@NL@%
  17707. %@AB@%/*    CGA and CGA-compatible modes.                    */%@AE@%%@NL@%
  17708. %@AB@%/*    Press a key to fill; press Esc to quit.          */%@AE@%%@NL@%
  17709. %@AB@%/* Note: activate Screen Swapping On in Debug menu     */%@AE@%%@NL@%
  17710. %@NL@%
  17711. %@AI@%#include %@AE@%<conio.h> %@NL@%
  17712. %@AI@%#include %@AE@%"scrn.h" %@NL@%
  17713. typedef unsigned short (far * VIDMEM);%@NL@%
  17714. MONMEM ((VIDMEM) (0xB000L << 16)) %@AB@%/* monochrome */%@AE@%%@NL@%
  17715. CGAMEM ((VIDMEM) (0xB800L << 16)) %@AB@%/* cga, ega */%@AE@%%@NL@%
  17716. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  17717. %@AI@%#define %@AE@%CHARS 2000 %@NL@%
  17718. AMASK 0xFF   %@AB@%/* keep attribute in range */%@AE@%%@NL@%
  17719. main()%@NL@%
  17720. {%@NL@%
  17721.      unsigned ch;          %@AB@%/* character to be displayed */%@AE@%%@NL@%
  17722.      unsigned attrib = 7;  %@AB@%/* initial attribute         */%@AE@%%@NL@%
  17723.      VIDMEM screen;        %@AB@%/* pointer to video RAM      */%@AE@%%@NL@%
  17724.      int offset;           %@AB@%/* location on screen        */%@AE@%%@NL@%
  17725. %@NL@%
  17726.      screen = MONMEM;      %@AB@%/* monochrome initialization */%@AE@%%@NL@%
  17727.      while ((ch = getch()) != ESC)%@NL@%
  17728.      {%@NL@%
  17729.         for (offset = 0; offset < CHARS; offset++)%@NL@%
  17730.           *(screen + offset) = ((attrib++ & AMASK) << 8)                              | ch;%@NL@%
  17731.      }%@NL@%
  17732. }%@NL@%
  17733. %@NL@%
  17734. %@NL@%
  17735. %@2@%%@AH@%CH2001.C%@AE@%%@EH@%%@NL@%
  17736. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\CH2001.C%@AE@%%@NL@%
  17737. %@NL@%
  17738. %@AB@%/* ch2001.c -- fill screen with 2000 characters        */%@AE@%%@NL@%
  17739. %@AB@%/*    This program demonstrates direct memory access   */%@AE@%%@NL@%
  17740. %@AB@%/*    of video memory.  It uses the current video mode */%@AE@%%@NL@%
  17741. %@AB@%/*    value to select the proper video RAM address.    */%@AE@%%@NL@%
  17742. %@AB@%/*    Press a key to fill; press Esc to quit.          */%@AE@%%@NL@%
  17743. %@AB@%/* Program list: ch2001.c, scrfun.lib                  */%@AE@%%@NL@%
  17744. %@AB@%/* Note: activate Screen Swapping On in Debug menu     */%@AE@%%@NL@%
  17745. %@NL@%
  17746. %@AI@%#include %@AE@%<conio.h> %@NL@%
  17747. %@AI@%#include %@AE@%"scrn.h" %@NL@%
  17748. typedef unsigned short (far * VIDMEM);%@NL@%
  17749. MONMEM ((VIDMEM) (0xB000L << 16)) %@AB@%/* monochrome */%@AE@%%@NL@%
  17750. CGAMEM ((VIDMEM) (0xB800L << 16)) %@AB@%/* cga, ega */%@AE@%%@NL@%
  17751. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  17752. %@AI@%#define %@AE@%CHARS 2000 %@NL@%
  17753. %@AI@%#define %@AE@%AMASK 0xFF %@NL@%
  17754. main()%@NL@%
  17755. {%@NL@%
  17756.      unsigned ch, mode;%@NL@%
  17757.      unsigned attrib = 7;%@NL@%
  17758.      VIDMEM screen;         %@AB@%/* pointer to video RAM */%@AE@%%@NL@%
  17759.      int offset;%@NL@%
  17760. %@NL@%
  17761.      if ((mode = Getvmode()) == TEXTMONO)%@NL@%
  17762.           screen = MONMEM;%@NL@%
  17763.      else if (mode == TEXTC80 || mode == TEXTBW80)%@NL@%
  17764.           screen = CGAMEM;%@NL@%
  17765.      else%@NL@%
  17766.           exit(1);%@NL@%
  17767.      while ((ch = getch()) != ESC)%@NL@%
  17768.           {%@NL@%
  17769.           for (offset = 0; offset < CHARS; offset++)%@NL@%
  17770.               *(screen + offset) = ((attrib++ & AMASK) << 8) | ch;%@NL@%
  17771.           }%@NL@%
  17772. }%@NL@%
  17773. %@NL@%
  17774. %@NL@%
  17775. %@2@%%@AH@%CHANGE.C%@AE@%%@EH@%%@NL@%
  17776. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\CHANGE.C%@AE@%%@NL@%
  17777. %@NL@%
  17778. %@AB@%/* change.c  -- a changemaking program demonstrates   */%@AE@%%@NL@%
  17779. %@AB@%/*              how pointers advance the correct      */%@AE@%%@NL@%
  17780. %@AB@%/*              number of bytes based on type         */%@AE@%%@NL@%
  17781. %@NL@%
  17782. %@AI@%#define %@AE@%NCOINS (4) %@NL@%
  17783. CENT (0x9b)  %@AB@%/* IBM PC cent character */%@AE@%%@NL@%
  17784. %@AI@%#define %@AE@%WAIT printf("(Press any key to continue)"); \ %@NL@%
  17785.              getch(); printf("\n\n")  %@NL@%
  17786. %@NL@%
  17787. main()%@NL@%
  17788. {%@NL@%
  17789.     static int coins[NCOINS] = {25, 10, 5, 1};%@NL@%
  17790.     int *coin_ptr, i = 0; %@NL@%
  17791.     int pennies1, pennies2, count;%@NL@%
  17792.     float amount;%@NL@%
  17793. %@NL@%
  17794.     printf("Enter an amount and I will ");%@NL@%
  17795.     printf(" give you change.\nAmount: ");%@NL@%
  17796.     if (scanf("%f", &amount) != 1)%@NL@%
  17797.         {%@NL@%
  17798.         printf("I don't know how to change that!\n");%@NL@%
  17799.         exit(1);%@NL@%
  17800.         }%@NL@%
  17801.     pennies2 = pennies1 = (int)(amount * 100.0);%@NL@%
  17802. %@NL@%
  17803.     coin_ptr = coins;%@NL@%
  17804.     for (i = 0; i < NCOINS; ++i)%@NL@%
  17805.         {%@NL@%
  17806.         WAIT;%@NL@%
  17807.         count = 0;%@NL@%
  17808.         while ((pennies1 -= coins[i]) >= -1)%@NL@%
  17809.             ++count;%@NL@%
  17810.         if (count > 0)%@NL@%
  17811.             {%@NL@%
  17812.             printf("%4d %2d%c", count, coins[i], CENT);%@NL@%
  17813.             printf(" coins by array offset.\n");%@NL@%
  17814.             }%@NL@%
  17815.         if (pennies1 == 0)%@NL@%
  17816.             break;%@NL@%
  17817.         pennies1 += coins[i];%@NL@%
  17818. %@NL@%
  17819.         count = 0;%@NL@%
  17820.         while ((pennies2 -= *coin_ptr) >= -1)%@NL@%
  17821.             ++count;%@NL@%
  17822.         if (count > 0)%@NL@%
  17823.             {%@NL@%
  17824.             printf("%4d %2d%c", count, *coin_ptr, CENT);%@NL@%
  17825.             printf(" coins by pointer indirection.\n");%@NL@%
  17826.             }%@NL@%
  17827.         if (pennies2 == 0)%@NL@%
  17828.             break;%@NL@%
  17829.         pennies2 += *coin_ptr;%@NL@%
  17830.         ++coin_ptr;%@NL@%
  17831.         }%@NL@%
  17832. }%@NL@%
  17833. %@NL@%
  17834. %@NL@%
  17835. %@2@%%@AH@%CHANGE2.C%@AE@%%@EH@%%@NL@%
  17836. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\CHANGE2.C%@AE@%%@NL@%
  17837. %@NL@%
  17838. %@AB@%/* change2.c -- modified to demonstrate passing       */%@AE@%%@NL@%
  17839. %@AB@%/*              an address to a function              */%@AE@%%@NL@%
  17840. %@NL@%
  17841. %@AI@%#define %@AE@%NCOINS (4) %@NL@%
  17842. CENT (0x9b)  %@AB@%/* IBM PC cent character */%@AE@%%@NL@%
  17843. %@NL@%
  17844. main()%@NL@%
  17845. {%@NL@%
  17846.     static int coins[NCOINS] = {25, 10, 5, 1};%@NL@%
  17847.     int pennies;%@NL@%
  17848.     float amount;%@NL@%
  17849. %@NL@%
  17850.     printf("Enter an amount and I will ");%@NL@%
  17851.     printf(" give you change.\nAmount: ");%@NL@%
  17852.     if (scanf("%f", &amount) != 1)%@NL@%
  17853.         {%@NL@%
  17854.         printf("I don't know how to change that!\n");%@NL@%
  17855.         exit(1);%@NL@%
  17856.         }%@NL@%
  17857.     pennies = (int)(amount * 100.0);%@NL@%
  17858. %@NL@%
  17859.     Show_change( coins, &coins[NCOINS], pennies);%@NL@%
  17860. %@NL@%
  17861. }%@NL@%
  17862. %@NL@%
  17863. Show_change( int amts[], int *end, int due)%@NL@%
  17864. {%@NL@%
  17865.     int count;%@NL@%
  17866. %@NL@%
  17867.     while ( amts < end )    %@AB@%/* compare pointers */%@AE@%%@NL@%
  17868.         {%@NL@%
  17869.         count = 0;%@NL@%
  17870.         while ((due -= *amts) >= -1)%@NL@%
  17871.             {%@NL@%
  17872.             ++count;%@NL@%
  17873.             }%@NL@%
  17874.         if (count > 0)%@NL@%
  17875.             printf("%4d %2d%c\n", count, *amts, CENT);%@NL@%
  17876.         if (due == 0)%@NL@%
  17877.             break;%@NL@%
  17878.         due += *amts;%@NL@%
  17879. %@NL@%
  17880.         ++amts;             %@AB@%/* increment a pointer */%@AE@%%@NL@%
  17881.         }%@NL@%
  17882. }%@NL@%
  17883. %@NL@%
  17884. %@NL@%
  17885. %@2@%%@AH@%CHARS.C%@AE@%%@EH@%%@NL@%
  17886. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\CHARS.C%@AE@%%@NL@%
  17887. %@NL@%
  17888. %@AB@%/* chars.c -- shows some variables of type char */%@AE@%%@NL@%
  17889. %@AB@%/*            as both characters and integers   */%@AE@%%@NL@%
  17890. %@NL@%
  17891. main()%@NL@%
  17892. {%@NL@%
  17893.     char ch1 = 'A', ch2 = 'a';%@NL@%
  17894. %@NL@%
  17895.     printf("The character %c has ASCII code %d\n", ch1, ch1);%@NL@%
  17896.     printf("If you add ten, you get %c\n", ch1 + 10);%@NL@%
  17897.     printf("The character %c has ASCII code %d\n", ch2, ch2);%@NL@%
  17898. }%@NL@%
  17899. %@NL@%
  17900. %@NL@%
  17901. %@2@%%@AH@%CHOOSE.C%@AE@%%@EH@%%@NL@%
  17902. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\CHOOSE.C%@AE@%%@NL@%
  17903. %@NL@%
  17904. %@AB@%/* choose.c   --  an array of pointers to functions   */%@AE@%%@NL@%
  17905. %@AB@%/*                used to create a menu               */%@AE@%%@NL@%
  17906. %@NL@%
  17907. void Choice1(), Choice2(), Choice3();%@NL@%
  17908. %@NL@%
  17909. void (*Dochoice[3])() = {Choice1, Choice2, Choice3};%@NL@%
  17910. %@NL@%
  17911. main()%@NL@%
  17912.     {%@NL@%
  17913.     int ch;%@NL@%
  17914. %@NL@%
  17915.     printf("Select 1, 2 or 3: ");%@NL@%
  17916.     ch = getch(); putch(ch);%@NL@%
  17917.     ch -= '1';%@NL@%
  17918.     if (ch < 0 || ch > 2)%@NL@%
  17919.         printf("\nNo such choice.\n");%@NL@%
  17920.     else%@NL@%
  17921.         Dochoice[ch]();%@NL@%
  17922. %@NL@%
  17923. }%@NL@%
  17924. %@NL@%
  17925. void Choice1(void) %@NL@%
  17926. {%@NL@%
  17927.         printf("\nThis is choice 1\n");%@NL@%
  17928. }%@NL@%
  17929. %@NL@%
  17930. void Choice2(void) %@NL@%
  17931. {%@NL@%
  17932.         printf("\nThis is choice 2\n");%@NL@%
  17933. }%@NL@%
  17934. %@NL@%
  17935. void Choice3(void) %@NL@%
  17936. {%@NL@%
  17937.         printf("\nThis is choice 3\n");%@NL@%
  17938. }%@NL@%
  17939. %@NL@%
  17940. %@NL@%
  17941. %@2@%%@AH@%COL256.C%@AE@%%@EH@%%@NL@%
  17942. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\COL256.C%@AE@%%@NL@%
  17943. %@NL@%
  17944. %@AB@%/*  col256.c  -- show 256 colors in mode 19             */%@AE@%%@NL@%
  17945. %@AB@%/* If you load graphics.qlb, no program list is needed. */%@AE@%%@NL@%
  17946. %@NL@%
  17947. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  17948. %@AI@%#include %@AE@%<graph.h> %@NL@%
  17949. %@AI@%#include %@AE@%<conio.h> %@NL@%
  17950. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  17951. %@AI@%#define %@AE@%ROWS 16 %@NL@%
  17952. %@AI@%#define %@AE@%COLS 16 %@NL@%
  17953. main()%@NL@%
  17954. {%@NL@%
  17955.     struct videoconfig vc;%@NL@%
  17956.     int mode = _MRES256COLOR;%@NL@%
  17957.     short xmax, ymax;           %@AB@%/* screen size */%@AE@%%@NL@%
  17958.     short xcs[ROWS][COLS];      %@AB@%/* coordinates of the */%@AE@%%@NL@%
  17959.     short ycs[ROWS][COLS];      %@AB@%/* 256 rectangles     */%@AE@%%@NL@%
  17960.     short row, col;%@NL@%
  17961. %@NL@%
  17962. %@NL@%
  17963.     if (_setvideomode(mode) == 0)%@NL@%
  17964.         {%@NL@%
  17965.         fprintf(stderr, "%d mode not supported\n", mode);%@NL@%
  17966.         exit(1);%@NL@%
  17967.         }%@NL@%
  17968.     _getvideoconfig(&vc);%@NL@%
  17969. %@NL@%
  17970.     xmax = vc.numxpixels - 1;%@NL@%
  17971.     ymax = vc.numypixels - 1;%@NL@%
  17972. %@NL@%
  17973.     %@AB@%/* Compute an interior point for each rectangle. */%@AE@%%@NL@%
  17974.     for (col = 0; col < COLS; col++)%@NL@%
  17975.         for (row = 0; row < ROWS; row++)%@NL@%
  17976.             {%@NL@%
  17977.             xcs[row][col] =  col * xmax / COLS + 5;%@NL@%
  17978.             ycs[row][col] =  row * ymax / ROWS + 5;%@NL@%
  17979. %@NL@%
  17980.             }%@NL@%
  17981. %@NL@%
  17982.     %@AB@%/* draw outside boundary */%@AE@%%@NL@%
  17983.     _setcolor(1);%@NL@%
  17984.     _rectangle(_GBORDER, 0, 0, xmax, ymax);%@NL@%
  17985. %@NL@%
  17986.     %@AB@%/* draw gridwork */%@AE@%%@NL@%
  17987.     for (col = 1; col < COLS ; col++)%@NL@%
  17988.         {%@NL@%
  17989.         _moveto(col * (xmax + 1) / COLS, 0);%@NL@%
  17990.         _lineto(col * (xmax + 1) / COLS, ymax);%@NL@%
  17991.         }%@NL@%
  17992.     for (row = 1; row < ROWS;  row++)%@NL@%
  17993.         {%@NL@%
  17994.         _moveto(0, row * (ymax + 1) / ROWS);%@NL@%
  17995.         _lineto(xmax, row * (ymax + 1) / ROWS);%@NL@%
  17996.         }%@NL@%
  17997. %@NL@%
  17998.     %@AB@%/*  fill in rectangles with palette colors */%@AE@%%@NL@%
  17999.     for (col = 0; col < COLS; col++)%@NL@%
  18000.         for (row = 0; row < ROWS; row++)%@NL@%
  18001.             {%@NL@%
  18002.             _setcolor(row * ROWS + col);%@NL@%
  18003.             _floodfill(xcs[row][col], ycs[row][col],1);%@NL@%
  18004.             }%@NL@%
  18005. %@NL@%
  18006.     %@AB@%/* terminate program */%@AE@%%@NL@%
  18007.     getch();%@NL@%
  18008.     _setvideomode(_DEFAULTMODE);%@NL@%
  18009. }%@NL@%
  18010. %@NL@%
  18011. %@NL@%
  18012. %@2@%%@AH@%CONDITN.C%@AE@%%@EH@%%@NL@%
  18013. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\CONDITN.C%@AE@%%@NL@%
  18014. %@NL@%
  18015. %@AB@%/* conditn.c -- attempt to use conditional op */%@AE@%%@NL@%
  18016. %@NL@%
  18017. main()%@NL@%
  18018. { %@NL@%
  18019.     int n, m; %@NL@%
  18020.  %@NL@%
  18021.     n = 2; %@NL@%
  18022.     m = (n != 2) : 0 ? 1;  %@AB@%/* almost right */%@AE@%%@NL@%
  18023.     printf("%d\n", m); %@NL@%
  18024. } %@NL@%
  18025. %@NL@%
  18026. %@NL@%
  18027. %@2@%%@AH@%CONTINUE.C%@AE@%%@EH@%%@NL@%
  18028. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP05\CONTINUE.C%@AE@%%@NL@%
  18029. %@NL@%
  18030. %@AB@%/* continue.c -- shows CONTINUE in loop */%@AE@%%@NL@%
  18031. %@NL@%
  18032. main()%@NL@%
  18033. {%@NL@%
  18034.     int sw = 0;%@NL@%
  18035.     char ch;%@NL@%
  18036.     while (1) %@AB@%/* endless loop */%@AE@%%@NL@%
  18037.     {%@NL@%
  18038.         %@AB@%/* print current status */%@AE@%%@NL@%
  18039.         if (sw)%@NL@%
  18040.             printf("\nSwitch is ON\n");%@NL@%
  18041.         else%@NL@%
  18042.             printf("\nSwitch is OFF\n");%@NL@%
  18043. %@NL@%
  18044.         printf("Do you want to quit? ");%@NL@%
  18045.         if (ch = getche() == 'y')%@NL@%
  18046.             break;    %@AB@%/* exit loop on yes */%@AE@%%@NL@%
  18047. %@NL@%
  18048.         printf("\nDo you want to toggle the switch? ");%@NL@%
  18049.         if (ch = getche() != 'y')%@NL@%
  18050.             continue; %@AB@%/* restart loop on yes */%@AE@%%@NL@%
  18051. %@NL@%
  18052.         sw = !sw;     %@AB@%/* toggle switch */%@AE@%%@NL@%
  18053.         }%@NL@%
  18054. }%@NL@%
  18055. %@NL@%
  18056. %@NL@%
  18057. %@2@%%@AH@%CONTROL.C%@AE@%%@EH@%%@NL@%
  18058. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\CONTROL.C%@AE@%%@NL@%
  18059. %@NL@%
  18060. %@AB@%/* control.c  --  demonstrate string justification */%@AE@%%@NL@%
  18061. %@AB@%/*                using printf()                   */%@AE@%%@NL@%
  18062. %@NL@%
  18063. char Some_text[] = "Some Text";%@NL@%
  18064. char Left_control[] =    "<<%-15s>>";%@NL@%
  18065. char Right_control[] =    "<<%15s>>";%@NL@%
  18066. %@NL@%
  18067. main()%@NL@%
  18068. {%@NL@%
  18069.     char ch;%@NL@%
  18070. %@NL@%
  18071.     while (1)%@NL@%
  18072.         {%@NL@%
  18073.         printf("Select l)eft r)ight or q)uit: ");%@NL@%
  18074.         ch = getch();%@NL@%
  18075.         putch( ch );%@NL@%
  18076. %@NL@%
  18077.         printf("\n\n");%@NL@%
  18078.         switch((int) ch)%@NL@%
  18079.             {%@NL@%
  18080.             case 'l':%@NL@%
  18081.             case 'L':%@NL@%
  18082.                 printf(Left_control, Some_text);%@NL@%
  18083.                 break;%@NL@%
  18084.             case 'r':%@NL@%
  18085.             case 'R':%@NL@%
  18086.                 printf(Right_control, Some_text);%@NL@%
  18087.                 break;%@NL@%
  18088.             case 'q':%@NL@%
  18089.             case 'Q':%@NL@%
  18090.                 exit (0);%@NL@%
  18091.             default:%@NL@%
  18092.                 printf("Huh?");%@NL@%
  18093.                 break;%@NL@%
  18094.             }%@NL@%
  18095.         printf("\n\n");%@NL@%
  18096.         }%@NL@%
  18097. }%@NL@%
  18098. %@NL@%
  18099. %@NL@%
  18100. %@2@%%@AH@%CONVERT.C%@AE@%%@EH@%%@NL@%
  18101. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\CONVERT.C%@AE@%%@NL@%
  18102. %@NL@%
  18103. %@AB@%/* convert.c -- converts Farenheit temprature       */%@AE@%%@NL@%
  18104. %@AB@%/*              to Centigrade; gets value from user */%@AE@%%@NL@%
  18105. %@NL@%
  18106. main()%@NL@%
  18107. {%@NL@%
  18108.     float ftemp, ctemp;%@NL@%
  18109. %@NL@%
  18110.     printf("What is the temprature in Farenheit? ");%@NL@%
  18111.     scanf("%f", &ftemp);%@NL@%
  18112.     ctemp = (ftemp - 32.0) * 5 / 9.0;%@NL@%
  18113. %@NL@%
  18114.     printf("The temprature in Centigrade is %5.2f", ctemp);%@NL@%
  18115. }%@NL@%
  18116. %@NL@%
  18117. %@NL@%
  18118. %@2@%%@AH@%DBLBAR.C%@AE@%%@EH@%%@NL@%
  18119. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\DBLBAR.C%@AE@%%@NL@%
  18120. %@NL@%
  18121. %@AB@%/* dblbar.c -- prints header using */%@AE@%%@NL@%
  18122. %@AB@%/*             line() function     */%@AE@%%@NL@%
  18123. %@NL@%
  18124. %@AI@%#define %@AE@%DOUBLE_BAR 205 %@NL@%
  18125. %@NL@%
  18126. main()%@NL@%
  18127. {%@NL@%
  18128.     void line();       %@AB@%/* declare line() function */%@AE@%%@NL@%
  18129. %@NL@%
  18130.     line(10);          %@AB@%/* call line() function    */%@AE@%%@NL@%
  18131.     printf("dblbar.c -- prints header using\n");%@NL@%
  18132.     printf("line() function\n");%@NL@%
  18133.     line(50);          %@AB@%/* call line() again       */%@AE@%%@NL@%
  18134. }%@NL@%
  18135. %@NL@%
  18136. void line()            %@AB@%/* function definition     */%@AE@%%@NL@%
  18137. {%@NL@%
  18138.     int pos;%@NL@%
  18139.     for (pos = 1; pos <= 40; pos++)%@NL@%
  18140.         putch(DOUBLE_BAR);%@NL@%
  18141.     printf("\n");%@NL@%
  18142. }%@NL@%
  18143. %@NL@%
  18144. %@NL@%
  18145. %@2@%%@AH@%DEBUG.C%@AE@%%@EH@%%@NL@%
  18146. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\DEBUG.C%@AE@%%@NL@%
  18147. %@NL@%
  18148. %@NL@%
  18149. %@AB@%/* DEBUG.C -- for practice with debugger */%@AE@%%@NL@%
  18150. %@NL@%
  18151. main()%@NL@%
  18152. {%@NL@%
  18153.     char response;%@NL@%
  18154.     int number, max_numbers = 5, count = 0, total = 0;%@NL@%
  18155.     float average;%@NL@%
  18156. %@NL@%
  18157.     printf("Continue (y/n)? ");%@NL@%
  18158.     response = getche();%@NL@%
  18159.     while ((response != 'n') && (count < max_numbers))%@NL@%
  18160.         printf("\nEnter a number: ");%@NL@%
  18161.         scanf("%d", &number);%@NL@%
  18162.         total += number;%@NL@%
  18163.         printf("Continue (y/n)? ");%@NL@%
  18164.         response = getche();%@NL@%
  18165.     average = total / count;%@NL@%
  18166.     printf("\nTotal is %d\n", total);%@NL@%
  18167.     printf("Average is %f\n", average);%@NL@%
  18168. }%@NL@%
  18169. %@NL@%
  18170. %@NL@%
  18171. %@NL@%
  18172. %@2@%%@AH@%DIALOG.C%@AE@%%@EH@%%@NL@%
  18173. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\DIALOG.C%@AE@%%@NL@%
  18174. %@NL@%
  18175. %@AB@%/* dialog.c  -- a conversation using gets() and puts()  */%@AE@%%@NL@%
  18176. %@NL@%
  18177. <stdio.h>          %@AB@%/* for NULL and BUFSIZ */%@AE@%%@NL@%
  18178. %@NL@%
  18179. %@AI@%#define %@AE@%THE_QUESTION \ %@NL@%
  18180. "And what is your view on the current price of corn\n\%@NL@%
  18181. and the stability of our trade import balance?"%@NL@%
  18182. %@NL@%
  18183. main()%@NL@%
  18184. {%@NL@%
  18185.     char name[BUFSIZ],%@NL@%
  18186.          buf[BUFSIZ];%@NL@%
  18187.     extern char *gets();%@NL@%
  18188. %@NL@%
  18189.     name[0] = '\0';         %@AB@%/* clear the name */%@AE@%%@NL@%
  18190. %@NL@%
  18191.     puts("\n\nHi there. And what is your name?");%@NL@%
  18192. %@NL@%
  18193.     if (gets(name) != NULL && name[0] != '\0')%@NL@%
  18194.         {%@NL@%
  18195.         printf("\nPleased to meet you, %s.\n", name);%@NL@%
  18196.         puts(THE_QUESTION);%@NL@%
  18197. %@NL@%
  18198.         %@AB@%/*%@NL@%
  18199. %@AB@%         * force an extra <enter> before replying.%@NL@%
  18200. %@AB@%         */%@AE@%%@NL@%
  18201.         do%@NL@%
  18202.             {%@NL@%
  18203.             if (gets(buf) == NULL)%@NL@%
  18204.                 break;%@NL@%
  18205. %@NL@%
  18206.             } while (*buf != '\0');     %@AB@%/* wait for empty line */%@AE@%%@NL@%
  18207. %@NL@%
  18208.         puts("Sorry. I needed to think about that.");%@NL@%
  18209.         printf("Nice talking to you, %s.\n", name);%@NL@%
  18210.         }%@NL@%
  18211.     else%@NL@%
  18212.         puts("How rude!");%@NL@%
  18213. %@NL@%
  18214.     puts("Goodbye.");%@NL@%
  18215. }%@NL@%
  18216. %@NL@%
  18217. %@NL@%
  18218. %@2@%%@AH@%DIGSUM.C%@AE@%%@EH@%%@NL@%
  18219. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\DIGSUM.C%@AE@%%@NL@%
  18220. %@NL@%
  18221. %@AB@%/* digsum.c -- sum digits in input                    */%@AE@%%@NL@%
  18222. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  18223. main()%@NL@%
  18224. {%@NL@%
  18225.     int ch;%@NL@%
  18226.     int digits = 0;  %@AB@%/* number of digits in input */%@AE@%%@NL@%
  18227.     int others = 0;  %@AB@%/* number of non-digits in input */%@AE@%%@NL@%
  18228. %@NL@%
  18229. %@NL@%
  18230.     while ((ch = getchar()) != EOF)%@NL@%
  18231.     if (ch <= '0' && ch >= '9')%@NL@%
  18232.             others++;%@NL@%
  18233.         else%@NL@%
  18234.             digits++;%@NL@%
  18235.     printf("digits = %d, others = %d", digits, others);%@NL@%
  18236. }%@NL@%
  18237. %@NL@%
  18238. %@NL@%
  18239. %@2@%%@AH@%DIRX.C%@AE@%%@EH@%%@NL@%
  18240. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP10\DIRX.C%@AE@%%@NL@%
  18241. %@NL@%
  18242. %@AB@%/* dirx.c  --  directory examples  */%@AE@%%@NL@%
  18243. %@NL@%
  18244. %@AI@%#include %@AE@%<direct.h> %@NL@%
  18245. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  18246. %@NL@%
  18247. %@AI@%#define %@AE@%SUBDIR "SUBDIR" %@NL@%
  18248. %@AI@%#define %@AE@%SUBSUBDIR "SUBSUB" %@NL@%
  18249. %@NL@%
  18250. main()%@NL@%
  18251. {%@NL@%
  18252.     char *current_dir;%@NL@%
  18253.     void Err();%@NL@%
  18254. %@NL@%
  18255.     if ((current_dir = getcwd(NULL, 0)) == NULL)%@NL@%
  18256.         Err("getcwd()", "Can't get current directory.");%@NL@%
  18257. %@NL@%
  18258.     if (mkdir(SUBDIR) != 0)%@NL@%
  18259.         Err( SUBSUBDIR, "Can't make directory." );%@NL@%
  18260. %@NL@%
  18261.     if (chdir(SUBDIR) != 0)%@NL@%
  18262.         Err( SUBDIR, "Can't cd into directory." );%@NL@%
  18263. %@NL@%
  18264.     if (mkdir(SUBSUBDIR) != 0)%@NL@%
  18265.         Err( SUBSUBDIR, "Can't make directory." );%@NL@%
  18266. %@NL@%
  18267.     if (chdir(current_dir) != 0)%@NL@%
  18268.         Err( SUBDIR, "Can't cd back to." );%@NL@%
  18269. %@NL@%
  18270.     if (rmdir(SUBDIR) != 0)%@NL@%
  18271.         Err( SUBDIR, "Can't remove directory." );%@NL@%
  18272. %@NL@%
  18273. }%@NL@%
  18274. %@NL@%
  18275. void Err(char *what, char *msg)%@NL@%
  18276. {%@NL@%
  18277.     fprintf(stderr, "\"%s\": %s\n", what, msg );%@NL@%
  18278.     exit (1);%@NL@%
  18279. }%@NL@%
  18280. %@NL@%
  18281. %@NL@%
  18282. %@2@%%@AH@%DO.C%@AE@%%@EH@%%@NL@%
  18283. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\DO.C%@AE@%%@NL@%
  18284. %@NL@%
  18285. %@NL@%
  18286. %@AB@%/* DO.C -- a simple do-while loop */%@AE@%%@NL@%
  18287. %@NL@%
  18288. main()%@NL@%
  18289. {%@NL@%
  18290.     int i = 1;%@NL@%
  18291.     do%@NL@%
  18292.         {%@NL@%
  18293.         printf("%d\n", i);%@NL@%
  18294.         i++;%@NL@%
  18295.         }%@NL@%
  18296.     while (i < 11);%@NL@%
  18297.     printf("Done!\n");%@NL@%
  18298. }%@NL@%
  18299. %@NL@%
  18300. %@NL@%
  18301. %@NL@%
  18302. %@2@%%@AH@%DOTS.C%@AE@%%@EH@%%@NL@%
  18303. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\DOTS.C%@AE@%%@NL@%
  18304. %@NL@%
  18305. %@AB@%/* dots.c -- illustrates the _setcolor(), _setpixel(), */%@AE@%%@NL@%
  18306. %@AB@%/*           and _selectpalette() functions from the   */%@AE@%%@NL@%
  18307. %@AB@%/*           QuickC graphics library                   */%@AE@%%@NL@%
  18308. %@AB@%/* If you load graphics.qlb, no program list is needed */%@AE@%%@NL@%
  18309. %@NL@%
  18310. %@AI@%#include %@AE@%<conio.h> %@NL@%
  18311. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  18312. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  18313. %@AI@%#include %@AE@%<graph.h> %@NL@%
  18314. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  18315. BKCOLS 8      %@AB@%/* number of background colors */%@AE@%%@NL@%
  18316. PALNUM 4      %@AB@%/* number of palettes */%@AE@%%@NL@%
  18317. long Bkcolors[BKCOLS] = {_BLACK, _BLUE, _GREEN, _CYAN, _RED,%@NL@%
  18318.                          _MAGENTA, _BROWN, _WHITE};%@NL@%
  18319. main (argc, argv)%@NL@%
  18320. int argc;%@NL@%
  18321. char *argv[];%@NL@%
  18322. {%@NL@%
  18323.      struct videoconfig vc;%@NL@%
  18324.      unsigned int col, row;%@NL@%
  18325.      short color = 0;%@NL@%
  18326.      int bkc_index = 1;  %@AB@%/* blue background */%@AE@%%@NL@%
  18327.      short palette = 0;  %@AB@%/* red, green, brown */%@AE@%%@NL@%
  18328.      int firstcol, firstrow, lastrow, lastcol;%@NL@%
  18329.      int mode = _MRES4COLOR;%@NL@%
  18330.      int ch;%@NL@%
  18331. %@NL@%
  18332.      if (argc > 1)%@NL@%
  18333.           mode = atoi(argv[1]);%@NL@%
  18334. %@NL@%
  18335.      if (_setvideomode(mode) == 0)%@NL@%
  18336.      {%@NL@%
  18337.           printf("Can't do that mode.\n");%@NL@%
  18338.           exit(1);%@NL@%
  18339.      }%@NL@%
  18340.      _getvideoconfig(&vc);%@NL@%
  18341.      firstcol = vc.numxpixels / 5;%@NL@%
  18342.      firstrow = vc.numypixels / 5;%@NL@%
  18343.      lastcol = 4 * vc.numxpixels / 5;%@NL@%
  18344.      lastrow = 4 * vc.numypixels / 5;%@NL@%
  18345.      _selectpalette(palette);%@NL@%
  18346.      _setbkcolor (Bkcolors[bkc_index]);%@NL@%
  18347.      for (col = firstcol; col <= lastcol; ++col)%@NL@%
  18348.      {%@NL@%
  18349.           _setcolor((++color / 3) % vc.numcolors);%@NL@%
  18350.           for (row = firstrow; row <= lastrow; ++row)%@NL@%
  18351.                _setpixel(col, row);%@NL@%
  18352.      }%@NL@%
  18353.      while ((ch = getch()) != ESC)%@NL@%
  18354.      {%@NL@%
  18355.           if (ch == 'p')%@NL@%
  18356.                 _selectpalette(++palette % PALNUM);%@NL@%
  18357.           else if (ch == 'b')%@NL@%
  18358.                 _setbkcolor(Bkcolors[++bkc_index % BKCOLS]);%@NL@%
  18359.      }%@NL@%
  18360.      _setvideomode(_DEFAULTMODE);  %@AB@%/* reset orig. mode */%@AE@%%@NL@%
  18361. }%@NL@%
  18362. %@NL@%
  18363. %@NL@%
  18364. %@2@%%@AH@%DOWHILE.C%@AE@%%@EH@%%@NL@%
  18365. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\DOWHILE.C%@AE@%%@NL@%
  18366. %@NL@%
  18367. %@AB@%/* dowhile.c -- misuse of do while loop     */%@AE@%%@NL@%
  18368. %@NL@%
  18369. main()%@NL@%
  18370. {%@NL@%
  18371.     int i = 0;%@NL@%
  18372. %@NL@%
  18373.     do while (i < 10)%@NL@%
  18374.         {%@NL@%
  18375.         printf("Happy Fourth of July!\n");%@NL@%
  18376.         i++;%@NL@%
  18377.         }%@NL@%
  18378.     printf("VOOOM\n");%@NL@%
  18379. }%@NL@%
  18380. %@NL@%
  18381. %@NL@%
  18382. %@NL@%
  18383. %@2@%%@AH@%DOWHILE2.C%@AE@%%@EH@%%@NL@%
  18384. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\DOWHILE2.C%@AE@%%@NL@%
  18385. %@NL@%
  18386. %@AB@%/* dowhile2.c -- ok use of do while loop     */%@AE@%%@NL@%
  18387. %@NL@%
  18388. main()%@NL@%
  18389. {%@NL@%
  18390.     int i = 0;%@NL@%
  18391. %@NL@%
  18392.     do%@NL@%
  18393.         {%@NL@%
  18394.         printf("Happy Fourth of July!\n");%@NL@%
  18395.         i++;%@NL@%
  18396.         } while (i < 10) ;%@NL@%
  18397.     printf("VOOOM\n");%@NL@%
  18398. }%@NL@%
  18399. %@NL@%
  18400. %@NL@%
  18401. %@2@%%@AH@%DRAWCHAR.C%@AE@%%@EH@%%@NL@%
  18402. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\DRAWCHAR.C%@AE@%%@NL@%
  18403. %@NL@%
  18404. %@AB@%/* drawchar.c -- drawing module for grafdraw.c         */%@AE@%%@NL@%
  18405. %@AB@%/* translates keystrokes to graphic characters,        */%@AE@%%@NL@%
  18406. %@AB@%/* manages cursor control and function keys            */%@AE@%%@NL@%
  18407. %@NL@%
  18408. %@AI@%#include %@AE@%<conio.h> %@NL@%
  18409. %@AI@%#include %@AE@%"keys.h" %@NL@%
  18410. %@AI@%#include %@AE@%"scrn.h" %@NL@%
  18411. %@AI@%#include %@AE@%"grafchar.h" %@NL@%
  18412. extern unsigned char Grchr[];  %@AB@%/* defined in grafchar.c */%@AE@%%@NL@%
  18413. %@NL@%
  18414. void Draw_chars()%@NL@%
  18415. {%@NL@%
  18416.     int ch, chout;%@NL@%
  18417.     unsigned char attrib = NORMAL;%@NL@%
  18418.     unsigned char draw = TRUE;%@NL@%
  18419. %@NL@%
  18420.     chout = Grchr[0];  %@AB@%/* default graphics character */%@AE@%%@NL@%
  18421.     while ((ch = getch()) != ESC)%@NL@%
  18422.         {%@NL@%
  18423.         if (ch >= '0' && ch <= '_')%@NL@%
  18424.             chout = Grchr[ch - '0'];%@NL@%
  18425.             %@AB@%/* this maps the 0 key to the first */%@AE@%%@NL@%
  18426.             %@AB@%/* graphics character, etc.         */%@AE@%%@NL@%
  18427.         else if (ch == SPACE)%@NL@%
  18428.             chout = SPACE;%@NL@%
  18429.         else if (ch == 0) %@AB@%/* process cursor keys */%@AE@%%@NL@%
  18430.             {             %@AB@%/* and function keys   */%@AE@%%@NL@%
  18431.             ch = getch();%@NL@%
  18432.             switch (ch)%@NL@%
  18433.                 {%@NL@%
  18434.                 case PU : draw = FALSE;%@NL@%
  18435.                           break;%@NL@%
  18436.                 case PD : draw = TRUE;%@NL@%
  18437.                           break;%@NL@%
  18438.                 case UP : if (draw)%@NL@%
  18439.                               Write_ch_atr(chout, attrib,%@NL@%
  18440.                                            PAGE, 1);%@NL@%
  18441.                           if (!Cursup())%@NL@%
  18442.                               putch(BEEP);%@NL@%
  18443.                           break;%@NL@%
  18444.                 case DN : if (draw)%@NL@%
  18445.                               Write_ch_atr(chout, attrib,%@NL@%
  18446.                                            PAGE, 1);%@NL@%
  18447.                           if (!Cursdn_lim(BOTLINE))%@NL@%
  18448.                               putch(BEEP);%@NL@%
  18449.                           break;%@NL@%
  18450.                 case LT : if (draw)%@NL@%
  18451.                               Write_ch_atr(chout, attrib,%@NL@%
  18452.                                            PAGE, 1);%@NL@%
  18453.                           if (!Curslt())%@NL@%
  18454.                               putch(BEEP);%@NL@%
  18455.                           break;%@NL@%
  18456.                 case RT : if (draw)%@NL@%
  18457.                               Write_ch_atr(chout, attrib,%@NL@%
  18458.                                            PAGE, 1);%@NL@%
  18459.                           if (!Cursrt())%@NL@%
  18460.                               putch(BEEP);%@NL@%
  18461.                           break;%@NL@%
  18462.                 case F1 : attrib = NORMAL; break;%@NL@%
  18463.                 case F2 : attrib = VIDREV; break;%@NL@%
  18464.                 case F3 : attrib ^= BLINK; break;%@NL@%
  18465.                 case F4 : attrib ^= INTENSE; break;%@NL@%
  18466.                 default : putch(BEEP);%@NL@%
  18467.                 }%@NL@%
  18468.             }%@NL@%
  18469.         }%@NL@%
  18470. }%@NL@%
  18471. %@NL@%
  18472. %@NL@%
  18473. %@2@%%@AH@%EGATOVGA.C%@AE@%%@EH@%%@NL@%
  18474. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\EGATOVGA.C%@AE@%%@NL@%
  18475. %@NL@%
  18476. %@AB@%/* egatovga.c -- converts ega color values to vga      */%@AE@%%@NL@%
  18477. %@AB@%/*               color values.                         */%@AE@%%@NL@%
  18478. %@NL@%
  18479. long Ega_to_vga(egacolor)%@NL@%
  18480. int egacolor;        %@AB@%/* ega color value */%@AE@%%@NL@%
  18481. {%@NL@%
  18482.     static long vgavals[6] = {0x2a0000L, 0x002a00L,%@NL@%
  18483.                          0x00002aL, 0x150000L,%@NL@%
  18484.                          0x001500L, 0x000015L};%@NL@%
  18485.      %@AB@%/* array holds VGA equivalents to EGA bits */%@AE@%%@NL@%
  18486.     long vgacolor = 0L; %@AB@%/* vga color value */%@AE@%%@NL@%
  18487.     int bit;%@NL@%
  18488. %@NL@%
  18489.     %@AB@%/* convert each bit to equivalent, and sum */%@AE@%%@NL@%
  18490.     for (bit = 0; bit < 6; bit++)%@NL@%
  18491.         vgacolor += ((egacolor >> bit) &1) * vgavals[bit];%@NL@%
  18492.     return (vgacolor);%@NL@%
  18493. }%@NL@%
  18494. %@NL@%
  18495. %@NL@%
  18496. %@2@%%@AH@%EGGS.C%@AE@%%@EH@%%@NL@%
  18497. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\EGGS.C%@AE@%%@NL@%
  18498. %@NL@%
  18499. %@AB@%/*  eggs.c -- draws colorful eggs                                      */%@AE@%%@NL@%
  18500. %@AB@%/*  This program illustrates use of the video configuration            */%@AE@%%@NL@%
  18501. %@AB@%/*  structure, the _ellipse() function, the effect of over-            */%@AE@%%@NL@%
  18502. %@AB@%/*  lapping solid figures, and a the use of logical coordinates.       */%@AE@%%@NL@%
  18503. %@AB@%/*  If you load graphics.qlb, no program list is needed.               */%@AE@%%@NL@%
  18504. %@NL@%
  18505. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  18506. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  18507. %@AI@%#include %@AE@%<graph.h> %@NL@%
  18508. %@AI@%#include %@AE@%<conio.h> %@NL@%
  18509. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  18510. %@NL@%
  18511. main(argc, argv)%@NL@%
  18512. int argc;%@NL@%
  18513. char *argv[];%@NL@%
  18514. {%@NL@%
  18515.     struct videoconfig vc;%@NL@%
  18516.     int mode = _MRES4COLOR;%@NL@%
  18517.     short xcent[3], ycent[3]; %@AB@%/* egg centers */%@AE@%%@NL@%
  18518.     short xsize, ysize;       %@AB@%/* egg limits  */%@AE@%%@NL@%
  18519.     int egg;%@NL@%
  18520. %@NL@%
  18521.     if (argc > 1)%@NL@%
  18522.         mode = atoi(argv[1]);%@NL@%
  18523.     if (_setvideomode(mode) == 0)%@NL@%
  18524.     {%@NL@%
  18525.         printf("Can't open mode %d\n", mode);%@NL@%
  18526.         exit(1);%@NL@%
  18527.     }%@NL@%
  18528.     _getvideoconfig(&vc);%@NL@%
  18529.     xsize = 0.3 * vc.numxpixels;%@NL@%
  18530.     ysize = 0.3 * vc.numypixels;%@NL@%
  18531.     xcent[0] = 0.3 * vc.numxpixels;%@NL@%
  18532.     xcent[1] = 0.5 * vc.numxpixels;%@NL@%
  18533.     xcent[2] = 0.7 * vc.numxpixels;%@NL@%
  18534.     ycent[0] = ycent[2] = 0.4 * vc.numypixels;%@NL@%
  18535.     ycent[1] = 0.6 * vc.numypixels;%@NL@%
  18536. %@NL@%
  18537.     _selectpalette(0);%@NL@%
  18538.     _setbkcolor(_MAGENTA);%@NL@%
  18539.     for (egg = 0; egg < 3; egg++)%@NL@%
  18540.     {%@NL@%
  18541.         _setlogorg(xcent[egg], ycent[egg]);%@NL@%
  18542.         _setcolor(egg + 1);%@NL@%
  18543.         _ellipse(_GFILLINTERIOR, -xsize, -ysize, xsize, ysize);%@NL@%
  18544.     }%@NL@%
  18545.     _settextposition(24, 0);%@NL@%
  18546.     _settextcolor(1);%@NL@%
  18547.     _outtext("Strike any key to terminate.");%@NL@%
  18548.     getch();%@NL@%
  18549.     _setvideomode(_DEFAULTMODE);%@NL@%
  18550. }%@NL@%
  18551. %@NL@%
  18552. %@NL@%
  18553. %@2@%%@AH@%ERR.C%@AE@%%@EH@%%@NL@%
  18554. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\ERR.C%@AE@%%@NL@%
  18555. %@NL@%
  18556. %@AB@%/* err.c  --  illustrates __FILE__ and __LINE__ in */%@AE@%%@NL@%
  18557. %@AB@%/*            tracing a small program              */%@AE@%%@NL@%
  18558. %@NL@%
  18559. %@AI@%#define %@AE@%ERR printf("Tracing: \"%s\" line %d\n",\ %@NL@%
  18560.                     __FILE__, __LINE__);%@NL@%
  18561. main()%@NL@%
  18562. {%@NL@%
  18563.     ERR%@NL@%
  18564.     err1();%@NL@%
  18565.     ERR%@NL@%
  18566.     err2();%@NL@%
  18567.     ERR%@NL@%
  18568. }%@NL@%
  18569. %@NL@%
  18570. err1()%@NL@%
  18571. {%@NL@%
  18572.     ERR%@NL@%
  18573.     err2();%@NL@%
  18574. }%@NL@%
  18575. %@NL@%
  18576. err2()%@NL@%
  18577. {%@NL@%
  18578.     ERR%@NL@%
  18579. }%@NL@%
  18580. %@NL@%
  18581. %@NL@%
  18582. %@2@%%@AH@%EXPO.C%@AE@%%@EH@%%@NL@%
  18583. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\EXPO.C%@AE@%%@NL@%
  18584. %@NL@%
  18585. %@AB@%/* expo.c -- uses exp() function to  */%@AE@%%@NL@%
  18586. %@AB@%/*            calculate powers       */%@AE@%%@NL@%
  18587. %@NL@%
  18588. main()%@NL@%
  18589. {%@NL@%
  18590.     int expo(number, power);%@NL@%
  18591.     int number, power;%@NL@%
  18592. %@NL@%
  18593.     printf("Enter a number: ");%@NL@%
  18594.     scanf("%d", &number);%@NL@%
  18595.     printf("Raise to what power? ");%@NL@%
  18596.     scanf("%d", &power);%@NL@%
  18597. %@NL@%
  18598.     printf("Result: %d", expo(number, power));%@NL@%
  18599. }%@NL@%
  18600. %@NL@%
  18601. int expo(number, power)%@NL@%
  18602. {%@NL@%
  18603.     int count, value;%@NL@%
  18604.     int total = 1;      %@AB@%/* store value of calculation */%@AE@%%@NL@%
  18605.     if (power < 0)      %@AB@%/* reject negative exponents  */%@AE@%%@NL@%
  18606.         {%@NL@%
  18607.         printf("Error in expo(): negative exponent\n");%@NL@%
  18608.         return(0);%@NL@%
  18609.         }%@NL@%
  18610. %@NL@%
  18611.     if (power == 0) %@AB@%/* any number to 0 power is 1 */%@AE@%%@NL@%
  18612.         return(1);%@NL@%
  18613. %@NL@%
  18614.     if (power == 1) %@AB@%/* any number to 1 power is itself */%@AE@%%@NL@%
  18615.         return(number);%@NL@%
  18616. %@NL@%
  18617.     %@AB@%/* calculate for power > 1 */%@AE@%%@NL@%
  18618.     for (count = 1; count <= power; count++)%@NL@%
  18619.         total *= number;%@NL@%
  18620.     return(total);%@NL@%
  18621. }%@NL@%
  18622. %@NL@%
  18623. %@NL@%
  18624. %@2@%%@AH@%EXTERNAL.C%@AE@%%@EH@%%@NL@%
  18625. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\EXTERNAL.C%@AE@%%@NL@%
  18626. %@NL@%
  18627. %@AB@%/* external.c -- shows an external variable */%@AE@%%@NL@%
  18628. %@NL@%
  18629. %@AI@%#define %@AE@%PI 3.14159 %@NL@%
  18630. int length; %@AB@%/* external (global) variable */%@AE@%%@NL@%
  18631.             %@AB@%/* declared before main ()    */%@AE@%%@NL@%
  18632. %@NL@%
  18633. main()%@NL@%
  18634.     {%@NL@%
  18635.     void square(), triangle(), circle();%@NL@%
  18636. %@NL@%
  18637.     printf("What length do you want to use? ");%@NL@%
  18638.     scanf("%d", &length);%@NL@%
  18639. %@NL@%
  18640.     square();   %@AB@%/* calculate areas */%@AE@%%@NL@%
  18641.     triangle();%@NL@%
  18642.     circle();%@NL@%
  18643.     }%@NL@%
  18644. %@NL@%
  18645. void square()%@NL@%
  18646.     {%@NL@%
  18647.     float area;%@NL@%
  18648.     area = length * length;%@NL@%
  18649.     printf("A square with sides of %d has an area of %f\n",%@NL@%
  18650.             length, area);%@NL@%
  18651.     }%@NL@%
  18652. %@NL@%
  18653. void triangle()%@NL@%
  18654.     {%@NL@%
  18655.     float area;%@NL@%
  18656.     area = (length * length) / 2;%@NL@%
  18657.     printf("A right triangle with sides of %d has an area %f\n",%@NL@%
  18658.             length, area);%@NL@%
  18659.     }%@NL@%
  18660. %@NL@%
  18661. void circle()%@NL@%
  18662.     {%@NL@%
  18663.     float area;%@NL@%
  18664.     area = (length * length * PI);%@NL@%
  18665.     printf("A circle with radius of %d has area of %f\n",%@NL@%
  18666.             length, area);%@NL@%
  18667.     }%@NL@%
  18668. %@NL@%
  18669. %@NL@%
  18670. %@2@%%@AH@%FIELDS.C%@AE@%%@EH@%%@NL@%
  18671. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\FIELDS.C%@AE@%%@NL@%
  18672. %@NL@%
  18673. %@AB@%/* fields.c -- shows the same number with different */%@AE@%%@NL@%
  18674. %@AB@%/*             field widths and number of decimals  */%@AE@%%@NL@%
  18675. %@NL@%
  18676. main()%@NL@%
  18677. {%@NL@%
  18678.     float f = 123.4560;%@NL@%
  18679. %@NL@%
  18680.     printf("%12.6f\n", f);%@NL@%
  18681.     printf("%8.4f\n", f);%@NL@%
  18682.     printf("%8.3f\n", f);%@NL@%
  18683.     printf("%8.2f\n", f);%@NL@%
  18684. }%@NL@%
  18685. %@NL@%
  18686. %@NL@%
  18687. %@2@%%@AH@%FILE.C%@AE@%%@EH@%%@NL@%
  18688. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\FILE.C%@AE@%%@NL@%
  18689. %@NL@%
  18690. %@AB@%/* file.c  --  the file I/O routines for texed */%@AE@%%@NL@%
  18691. %@NL@%
  18692. Load_file()%@NL@%
  18693. {%@NL@%
  18694.     printf("\nLoading ..... done.\n");%@NL@%
  18695. }%@NL@%
  18696. %@NL@%
  18697. Save_file()%@NL@%
  18698. {%@NL@%
  18699.     printf("\nSaving ...... done.\n");%@NL@%
  18700. }%@NL@%
  18701. %@NL@%
  18702. %@NL@%
  18703. %@2@%%@AH@%FLOATS.C%@AE@%%@EH@%%@NL@%
  18704. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\FLOATS.C%@AE@%%@NL@%
  18705. %@NL@%
  18706. %@AB@%/* floats.c  -- show floating values in regular */%@AE@%%@NL@%
  18707. %@AB@%/*              and exponential format          */%@AE@%%@NL@%
  18708. %@NL@%
  18709. main()%@NL@%
  18710. {%@NL@%
  18711.     float f1 = 2500.125, f2 = 0.0033, f3 = -50.99;%@NL@%
  18712. %@NL@%
  18713.     printf("%f\t %e\n\n", f1, f1);%@NL@%
  18714.     printf("%f\t %e\n\n", f2, f2);%@NL@%
  18715.     printf("%f\t %e\n", f3, f3);%@NL@%
  18716. }%@NL@%
  18717. %@NL@%
  18718. %@NL@%
  18719. %@2@%%@AH@%FMENU.C%@AE@%%@EH@%%@NL@%
  18720. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP10\FMENU.C%@AE@%%@NL@%
  18721. %@NL@%
  18722. %@AB@%/* fmenu.c  --  demonstrates file renaming, etc. */%@AE@%%@NL@%
  18723. %@NL@%
  18724. %@AI@%#include %@AE@%<direct.h> %@NL@%
  18725. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  18726. %@AI@%#include %@AE@%<string.h> %@NL@%
  18727. %@NL@%
  18728. %@AI@%#define %@AE@%MAXPATH (80) %@NL@%
  18729. char From_name[MAXPATH],%@NL@%
  18730.      To_name[MAXPATH];%@NL@%
  18731. %@NL@%
  18732. int Input(char *prompt, char buf[])%@NL@%
  18733. {%@NL@%
  18734.     printf("%s: ", prompt);%@NL@%
  18735.     if (gets(buf) == NULL || *buf == '\0')%@NL@%
  18736.         return (0);%@NL@%
  18737.     return (1);%@NL@%
  18738. }%@NL@%
  18739. void Rename(void)%@NL@%
  18740. {%@NL@%
  18741.     printf("->Rename/move\n");%@NL@%
  18742.     if (!Input("From", From_name)) return;%@NL@%
  18743.     if (!Input("To", To_name)) return;%@NL@%
  18744.     if (rename(From_name, To_name) != 0)%@NL@%
  18745.         perror("RENAME");%@NL@%
  18746.     else%@NL@%
  18747.         printf("Renamed: \"%s\" -> \"%s\"\n", %@NL@%
  18748.                 From_name, To_name);%@NL@%
  18749. }%@NL@%
  18750. void Remove(void)%@NL@%
  18751. {%@NL@%
  18752.     printf("->Remove\n");%@NL@%
  18753.     if (!Input("Remove", From_name)) return;%@NL@%
  18754.     if (!Input("Are You Sure", To_name)) return;%@NL@%
  18755.     if (*To_name != 'y' && *To_name != 'Y')%@NL@%
  18756.         return;%@NL@%
  18757.     if (remove(From_name) != 0)%@NL@%
  18758.         perror(From_name);%@NL@%
  18759.     else%@NL@%
  18760.         printf("Removed: \"%s\"\n", From_name);%@NL@%
  18761. }%@NL@%
  18762. void Maketemp(void)%@NL@%
  18763. {%@NL@%
  18764.     printf("->Maketemp\n");%@NL@%
  18765.     if (!Input("In What Directory", From_name))%@NL@%
  18766.         return;%@NL@%
  18767.     (void)strcat(From_name, "\\XXXXXX");%@NL@%
  18768.     if (mktemp(From_name) == NULL)%@NL@%
  18769.         printf("Can't create a unique name.\n");%@NL@%
  18770.     else%@NL@%
  18771.         printf("Created: \"%s\"\n", From_name);%@NL@%
  18772. }%@NL@%
  18773. void Quit(void)%@NL@%
  18774. {%@NL@%
  18775.     printf("->Quit\n");%@NL@%
  18776.     if (!Input("Are You Sure", From_name))%@NL@%
  18777.         return;%@NL@%
  18778.     if (*From_name != 'y' && *From_name != 'Y')%@NL@%
  18779.         return;%@NL@%
  18780.     exit(0);%@NL@%
  18781. }%@NL@%
  18782. %@NL@%
  18783. main()%@NL@%
  18784. {%@NL@%
  18785.     static void (*doit[])() = {Rename, Remove, Maketemp, Quit};%@NL@%
  18786.     int ch;%@NL@%
  18787. %@NL@%
  18788.     while (1)%@NL@%
  18789.         {%@NL@%
  18790.         printf("--------------------------------------------\n");%@NL@%
  18791.         printf("1) Rename/move a file or rename a directory.\n");%@NL@%
  18792.         printf("2) Remove a file.\n");%@NL@%
  18793.         printf("3) Make a unique temporary file.\n");%@NL@%
  18794.         printf("4) Quit.\n");%@NL@%
  18795.         printf("--------------------------------------------\n");%@NL@%
  18796.         printf("Select: ");%@NL@%
  18797. %@NL@%
  18798.         do%@NL@%
  18799.             {%@NL@%
  18800.             ch = getchar();%@NL@%
  18801.             } while (ch < '1' || ch > '4');%@NL@%
  18802.         getchar();    %@AB@%/* gobble trailing newline */%@AE@%%@NL@%
  18803.         printf("%c\n\n", ch);%@NL@%
  18804.         ch -= '1';%@NL@%
  18805.         doit[ch]();%@NL@%
  18806.         }%@NL@%
  18807. }%@NL@%
  18808. %@NL@%
  18809. %@NL@%
  18810. %@2@%%@AH@%FORLOOP.C%@AE@%%@EH@%%@NL@%
  18811. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\FORLOOP.C%@AE@%%@NL@%
  18812. %@NL@%
  18813. %@NL@%
  18814. %@AB@%/* FORLOOP.C -- a simple for loop that   */%@AE@%%@NL@%
  18815. %@AB@%/*              counts to ten            */%@AE@%%@NL@%
  18816. %@NL@%
  18817. main()%@NL@%
  18818. {%@NL@%
  18819.     int i;%@NL@%
  18820.     for (i = 1; i <= 10; i++)%@NL@%
  18821.         {%@NL@%
  18822.         printf("%d\n",i);  %@AB@%/* body of loop */%@AE@%%@NL@%
  18823.         }%@NL@%
  18824.     printf("All done!\n");%@NL@%
  18825.     %@AB@%/* executed when i > 10 */%@AE@%%@NL@%
  18826. }%@NL@%
  18827. %@NL@%
  18828. %@NL@%
  18829. %@NL@%
  18830. %@2@%%@AH@%FORMATS.C%@AE@%%@EH@%%@NL@%
  18831. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\FORMATS.C%@AE@%%@NL@%
  18832. %@NL@%
  18833. %@AB@%/* formats.c -- shows what happens when format */%@AE@%%@NL@%
  18834. %@AB@%/*              doesn't match data type        */%@AE@%%@NL@%
  18835. %@NL@%
  18836. main()%@NL@%
  18837. {%@NL@%
  18838.     int i = 5;%@NL@%
  18839.     printf("As integer:      %d\n", i);%@NL@%
  18840.     printf("As long integer: %ld\n", i);%@NL@%
  18841.     printf("As exponential:  %e\n", i);%@NL@%
  18842.     printf("As float:        %f\n", i);%@NL@%
  18843. }%@NL@%
  18844. %@NL@%
  18845. %@NL@%
  18846. %@2@%%@AH@%GALAX.C%@AE@%%@EH@%%@NL@%
  18847. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP05\GALAX.C%@AE@%%@NL@%
  18848. %@NL@%
  18849. %@AB@%/* GALAX.C -- creates an ellipse by selecting */%@AE@%%@NL@%
  18850. %@AB@%/*              from random pixels            */%@AE@%%@NL@%
  18851. %@NL@%
  18852. <graph.h> %@AB@%/* for graphics */%@AE@%%@NL@%
  18853. <math.h>  %@AB@%/* for sqrt() */%@AE@%%@NL@%
  18854. <conio.h> %@AB@%/* for kbhit() */%@AE@%%@NL@%
  18855. %@NL@%
  18856. main()%@NL@%
  18857. {%@NL@%
  18858.     int pixels, radius = 50;%@NL@%
  18859.     double center_x = 320, center_y = 100,%@NL@%
  18860.         xpos, ypos;%@NL@%
  18861.     srand(0);%@NL@%
  18862.     _setvideomode(_HRESBW);%@NL@%
  18863.     _setcolor(1);%@NL@%
  18864.     for (pixels = 1; pixels < 25000; pixels++)%@NL@%
  18865.         {%@NL@%
  18866.         %@AB@%/* draws filled ellipse, due */%@AE@%%@NL@%
  18867.         %@AB@%/* to dimensions of hires screen */%@AE@%%@NL@%
  18868.         %@AB@%/* generate random location */%@AE@%%@NL@%
  18869.         xpos = rand() % 639;%@NL@%
  18870.         ypos = rand() % 199;%@NL@%
  18871.         if (sqrt %@AB@%/* is distance within radius? */%@AE@%%@NL@%
  18872.               ((xpos - center_x) * (xpos - center_x)%@NL@%
  18873.               + (ypos - center_y) * (ypos - center_y))%@NL@%
  18874.               < radius)%@NL@%
  18875.            _setpixel(xpos, ypos);%@NL@%
  18876.         if (kbhit() )%@NL@%
  18877.              break; %@AB@%/* exit if key pressed */%@AE@%%@NL@%
  18878.         }%@NL@%
  18879.     getch(); %@AB@%/* freeze screen until key pressed */%@AE@%%@NL@%
  18880.     _setvideomode(_DEFAULTMODE);%@NL@%
  18881. }%@NL@%
  18882. %@NL@%
  18883. %@NL@%
  18884. %@2@%%@AH@%GETCH.C%@AE@%%@EH@%%@NL@%
  18885. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\GETCH.C%@AE@%%@NL@%
  18886. %@NL@%
  18887. %@AB@%/*     getch.c -- using getch()             */%@AE@%%@NL@%
  18888. %@AI@%#include %@AE@%<conio.h> %@NL@%
  18889. main()%@NL@%
  18890. {%@NL@%
  18891.     int count = 1;%@NL@%
  18892. %@NL@%
  18893.     printf("Please enter a word.\n");%@NL@%
  18894.     while (getch() != '\r')%@NL@%
  18895.         printf("%d.. ", count++);%@NL@%
  18896.     printf("\n%d characters altogether\n", count - 1);%@NL@%
  18897. }%@NL@%
  18898. %@NL@%
  18899. %@NL@%
  18900. %@2@%%@AH@%GETCHAR.C%@AE@%%@EH@%%@NL@%
  18901. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\GETCHAR.C%@AE@%%@NL@%
  18902. %@NL@%
  18903. %@AB@%/*      getchar.c -- using getchar()         */%@AE@%%@NL@%
  18904. %@NL@%
  18905. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  18906. main()%@NL@%
  18907. {%@NL@%
  18908.     int count = 1;%@NL@%
  18909. %@NL@%
  18910.     printf("Please enter a word.\n");%@NL@%
  18911.     while (getchar() != '\n')       %@AB@%/* here it is */%@AE@%%@NL@%
  18912.         printf("%d.. ", count++);%@NL@%
  18913.     printf("\n%d characters altogether\n", count - 1);%@NL@%
  18914. }%@NL@%
  18915. %@NL@%
  18916. %@NL@%
  18917. %@2@%%@AH@%GETCHE.C%@AE@%%@EH@%%@NL@%
  18918. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\GETCHE.C%@AE@%%@NL@%
  18919. %@NL@%
  18920. %@AB@%/*     getche.c -- using getche()             */%@AE@%%@NL@%
  18921. <conio.h>     %@AB@%/* note different file included */%@AE@%%@NL@%
  18922. main()%@NL@%
  18923. {%@NL@%
  18924.     int count = 1;%@NL@%
  18925. %@NL@%
  18926.     printf("Please enter a word.\n");%@NL@%
  18927.     while (getche() != '\r')    %@AB@%/* changed comparison */%@AE@%%@NL@%
  18928.         printf("%d.. ", count++); %@NL@%
  18929.     printf("\n%d characters altogether\n", count - 1);%@NL@%
  18930. }%@NL@%
  18931. %@NL@%
  18932. %@NL@%
  18933. %@2@%%@AH@%GETCLOSE.C%@AE@%%@EH@%%@NL@%
  18934. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\GETCLOSE.C%@AE@%%@NL@%
  18935. %@NL@%
  18936. %@AB@%/* getclose.c -- a number game using */%@AE@%%@NL@%
  18937. %@AB@%/*               random numbers      */%@AE@%%@NL@%
  18938. %@NL@%
  18939. %@AI@%#define %@AE@%TRUE 1 %@NL@%
  18940. %@AI@%#define %@AE@%FALSE 0 %@NL@%
  18941. %@NL@%
  18942.     %@AB@%/* external variables */%@AE@%%@NL@%
  18943.     int number,     %@AB@%/* total number in current game   */%@AE@%%@NL@%
  18944.         moves,      %@AB@%/* number of moves in current game*/%@AE@%%@NL@%
  18945.         target,     %@AB@%/* target number to reach         */%@AE@%%@NL@%
  18946.         done,       %@AB@%/* true if game is over           */%@AE@%%@NL@%
  18947.         score,      %@AB@%/* score of current game          */%@AE@%%@NL@%
  18948.         wins = 0,   %@AB@%/* number of games won            */%@AE@%%@NL@%
  18949.         losses = 0, %@AB@%/* number of games lost           */%@AE@%%@NL@%
  18950.         total = 0;  %@AB@%/* total score                    */%@AE@%%@NL@%
  18951. %@NL@%
  18952.     char move;%@NL@%
  18953. %@NL@%
  18954.     %@AB@%/* function prototype declarations */%@AE@%%@NL@%
  18955.     void intro(void);       %@AB@%/* tell player about game   */%@AE@%%@NL@%
  18956.     char getyn(void);       %@AB@%/* get yes/no response      */%@AE@%%@NL@%
  18957.     int  random(int num);   %@AB@%/* random between 1 and num */%@AE@%%@NL@%
  18958.     void new_target(void);  %@AB@%/* target number for game   */%@AE@%%@NL@%
  18959.     char get_move(void);    %@AB@%/* get player's move        */%@AE@%%@NL@%
  18960.     void do_move(void);     %@AB@%/* generate num from move   */%@AE@%%@NL@%
  18961.     void check_move(void);  %@AB@%/* won, lost, or continue?  */%@AE@%%@NL@%
  18962.     void show_score(void);  %@AB@%/* show score for game      */%@AE@%%@NL@%
  18963.     void show_total(void);  %@AB@%/* show total score         */%@AE@%%@NL@%
  18964. %@NL@%
  18965. main()%@NL@%
  18966. {%@NL@%
  18967.     intro();         %@AB@%/* print instructions */%@AE@%%@NL@%
  18968. %@NL@%
  18969.     while (TRUE)     %@AB@%/* new games until user quits */%@AE@%%@NL@%
  18970.         {%@NL@%
  18971.         printf("\nDo you want to continue? ");%@NL@%
  18972.         if (getyn() != 'y')%@NL@%
  18973.             break;   %@AB@%/* exit program */%@AE@%%@NL@%
  18974. %@NL@%
  18975.         done = FALSE;%@NL@%
  18976.         number = moves = score = 0;%@NL@%
  18977.         new_target();  %@AB@%/* target number for this game */%@AE@%%@NL@%
  18978.         while (!done)  %@AB@%/* play one game */%@AE@%%@NL@%
  18979.             {%@NL@%
  18980.             get_move();%@AB@%/* user selects random number  */%@AE@%%@NL@%
  18981.             do_move(); %@AB@%/* generate random number      */%@AE@%%@NL@%
  18982.                        %@AB@%/* and add                     */%@AE@%%@NL@%
  18983.             check_move();  %@AB@%/* win, lose, or continue? */%@AE@%%@NL@%
  18984.             }%@NL@%
  18985.         show_score();  %@AB@%/* score for this game */%@AE@%%@NL@%
  18986.         show_total();  %@AB@%/* total score         */%@AE@%%@NL@%
  18987.         }%@NL@%
  18988. }%@NL@%
  18989. %@NL@%
  18990. void intro(void)%@NL@%
  18991. {%@NL@%
  18992.     printf("Welcome to Getclose\n\n");%@NL@%
  18993.     printf("The object of this game is to\n");%@NL@%
  18994.     printf("try to get as close to the target\n");%@NL@%
  18995.     printf("number as possible in as few\n");%@NL@%
  18996.     printf("moves as possible by choosing from\n");%@NL@%
  18997.     printf("various ranges of random numbers.\n");%@NL@%
  18998.     printf("You score if you get within 4 of the\n");%@NL@%
  18999.     printf("target; a 100 point bonus for hitting\n");%@NL@%
  19000.     printf("the target, but no score if you go over.\n\n");%@NL@%
  19001.     }%@NL@%
  19002. %@NL@%
  19003. char getyn(void)%@NL@%
  19004.                     %@AB@%/* get yes or no answer      */%@AE@%%@NL@%
  19005.                     %@AB@%/* repeats until valid entry */%@AE@%%@NL@%
  19006. {%@NL@%
  19007.     char ch;  %@AB@%/* character to read and return */%@AE@%%@NL@%
  19008. %@NL@%
  19009.     while (TRUE)%@NL@%
  19010.         {%@NL@%
  19011.         printf(" (y or n) ");%@NL@%
  19012.         ch = getche();%@NL@%
  19013.         printf("\n");%@NL@%
  19014.         if ((ch == 'y') || (ch == 'n'))%@NL@%
  19015.         %@AB@%/* valid response, break out of loop */%@AE@%%@NL@%
  19016.             break;%@NL@%
  19017.         %@AB@%/* give error message and loop again */%@AE@%%@NL@%
  19018.         printf("please enter ");%@NL@%
  19019.         }%@NL@%
  19020.     return(ch);%@NL@%
  19021.     }%@NL@%
  19022. %@NL@%
  19023. int random(int num)%@NL@%
  19024.     %@AB@%/* generate random number between 1 and num     */%@AE@%%@NL@%
  19025.     %@AB@%/* doesn't use library function srand() because */%@AE@%%@NL@%
  19026.     %@AB@%/* we don't want the same seed each time        */%@AE@%%@NL@%
  19027. {%@NL@%
  19028.     long seconds, result;%@NL@%
  19029.     time(&seconds);   %@AB@%/* randomize with system time */%@AE@%%@NL@%
  19030.     return (abs ((int)seconds * rand() % num) + 1);%@NL@%
  19031. }%@NL@%
  19032. %@NL@%
  19033. void new_target(void)%@NL@%
  19034.              %@AB@%/* generate a new target number */%@AE@%%@NL@%
  19035.              %@AB@%/* between 50 and 99            */%@AE@%%@NL@%
  19036. {%@NL@%
  19037.     target = 50 + random(49);%@NL@%
  19038.     printf("\nYour target for this game is %d\n",%@NL@%
  19039.         target);%@NL@%
  19040. }%@NL@%
  19041. %@NL@%
  19042. char get_move(void)%@NL@%
  19043.     {%@NL@%
  19044.     while (TRUE)%@NL@%
  19045.         {%@NL@%
  19046.         printf("\nPick a random number from 1 to\n");%@NL@%
  19047.         printf("a) 5  b) 10  c) 25  d) 50  e) 100 ");%@NL@%
  19048.         move = getche();%@NL@%
  19049.         if ((move >= 'a') && (move <= 'e'))%@NL@%
  19050.             {%@NL@%
  19051.             ++moves; %@AB@%/* count the move */%@AE@%%@NL@%
  19052.             break;   %@AB@%/* valid response */%@AE@%%@NL@%
  19053.             }%@NL@%
  19054.         %@AB@%/* invalid response, try again */%@AE@%%@NL@%
  19055.         printf("\nPlease type a, b, c, d, or e\n");%@NL@%
  19056.         }%@NL@%
  19057. }%@NL@%
  19058. %@NL@%
  19059. void do_move(void)%@NL@%
  19060.     {%@NL@%
  19061.     int num = 0;  %@AB@%/* random value to obtain */%@AE@%%@NL@%
  19062.     switch (move)%@NL@%
  19063.         {%@NL@%
  19064.         case 'a' :%@NL@%
  19065.             num = random(5);%@NL@%
  19066.             break;%@NL@%
  19067.         case 'b' :%@NL@%
  19068.             num = random(10);%@NL@%
  19069.             break;%@NL@%
  19070.         case 'c' :%@NL@%
  19071.             num = random(25);%@NL@%
  19072.             break;%@NL@%
  19073.         case 'd' :%@NL@%
  19074.             num = random(50);%@NL@%
  19075.             break;%@NL@%
  19076.         case 'e' :%@NL@%
  19077.             num = random(100);%@NL@%
  19078.             break;%@NL@%
  19079.         }%@NL@%
  19080.     number += num;  %@AB@%/* add new number to total */%@AE@%%@NL@%
  19081.     printf("\n\nYou got a %d. Number is now: %d ", num, number);%@NL@%
  19082.     printf("(Target is %d)\n", target);%@NL@%
  19083. }%@NL@%
  19084. %@NL@%
  19085. void check_move(void)%@NL@%
  19086. {%@NL@%
  19087.     int temp;%@NL@%
  19088.     if (number > target)%@NL@%
  19089.         {%@NL@%
  19090.         printf("\nYou went over! ");%@NL@%
  19091.         printf("No score this game.\n");%@NL@%
  19092.         losses++;%@NL@%
  19093.         done = TRUE; %@AB@%/* to break out of loop */%@AE@%%@NL@%
  19094.         }%@NL@%
  19095.     if (number == target)%@NL@%
  19096.         {%@NL@%
  19097.         printf("\nYou hit the target ");%@NL@%
  19098.         printf("for 100 bonus points!\n");%@NL@%
  19099.         score = (100 / moves) + 100;%@NL@%
  19100.         total += score;%@NL@%
  19101.         wins++;%@NL@%
  19102.         done = TRUE;%@NL@%
  19103.         }%@NL@%
  19104.     if ((number >= (target - 4)) && (number < target))%@NL@%
  19105.         {%@NL@%
  19106.         temp = 100 / moves;%@NL@%
  19107.         %@AB@%/* does player want to go for broke? */%@AE@%%@NL@%
  19108.         printf("\nTake %d points (y) or continue (n)? ",%@NL@%
  19109.             temp);%@NL@%
  19110.         if (getyn() == 'y')%@NL@%
  19111.             {%@NL@%
  19112.             score = temp;%@NL@%
  19113.             total += score;%@NL@%
  19114.             wins++;%@NL@%
  19115.             done = TRUE;%@NL@%
  19116.             }%@NL@%
  19117.        }%@NL@%
  19118. }%@NL@%
  19119. %@NL@%
  19120. void show_score(void)%@NL@%
  19121. {%@NL@%
  19122.     printf("\nYou scored %d points in %d moves.\n",%@NL@%
  19123.         score, moves);%@NL@%
  19124. }%@NL@%
  19125. %@NL@%
  19126. void show_total(void)%@NL@%
  19127. {%@NL@%
  19128.     printf("You have won %d games ", wins);%@NL@%
  19129.     printf("and lost %d.\n", losses);%@NL@%
  19130.     printf("Your total score is %d\n", total);%@NL@%
  19131. }%@NL@%
  19132. %@NL@%
  19133. %@NL@%
  19134. %@2@%%@AH@%GETPUT.C%@AE@%%@EH@%%@NL@%
  19135. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\GETPUT.C%@AE@%%@NL@%
  19136. %@NL@%
  19137. %@AB@%/*  getput.c -- illustrates _getimage(), _putimage(), */%@AE@%%@NL@%
  19138. %@AB@%/*              the image-background interaction, and */%@AE@%%@NL@%
  19139. %@AB@%/*              the aspect ratio                      */%@AE@%%@NL@%
  19140. %@AB@%/* If you load graphics.qlb, no program list is needed*/%@AE@%%@NL@%
  19141. %@NL@%
  19142. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  19143. <stdlib.h>  %@AB@%/* declares malloc()  */%@AE@%%@NL@%
  19144. %@AI@%#include %@AE@%<graph.h> %@NL@%
  19145. %@AI@%#include %@AE@%<conio.h> %@NL@%
  19146. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  19147. %@NL@%
  19148. %@AB@%/* The following variables describe various          */%@AE@%%@NL@%
  19149. %@AB@%/* coordinates and sizes.                            */%@AE@%%@NL@%
  19150. %@AB@%/* They are declared externally so that they can be  */%@AE@%%@NL@%
  19151. %@AB@%/* shared easily by several functions.               */%@AE@%%@NL@%
  19152. int X1, Yb1, X2, Y2, Xdelta, Xside, Yside; %@AB@%/* image  */%@AE@%%@NL@%
  19153. int Xmid, Xmax, Ymid, Ymax;           %@AB@%/* background  */%@AE@%%@NL@%
  19154. int Xps, Xpr, Xand, Xor, Xxor, Ytop, Ybot; %@AB@%/* copies */%@AE@%%@NL@%
  19155. int X[3], Y[3];%@NL@%
  19156. float Ar;    %@AB@%/* aspect ratio */%@AE@%%@NL@%
  19157. %@NL@%
  19158. struct videoconfig Vc;%@NL@%
  19159. char Mask[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0};%@NL@%
  19160. void Initialize(void), Drawfig(void),%@NL@%
  19161.       Drawbackground(void), Drawcopies(void);%@NL@%
  19162. %@NL@%
  19163. main(argc, argv)%@NL@%
  19164. int argc;%@NL@%
  19165. char *argv[];%@NL@%
  19166. {%@NL@%
  19167.     int mode = _MRES4COLOR;%@NL@%
  19168. %@NL@%
  19169.     if (argc > 1)%@NL@%
  19170.         mode = atoi(argv[1]);%@NL@%
  19171.     if (_setvideomode(mode) == 0)%@NL@%
  19172.     {%@NL@%
  19173.         fprintf(stderr, "Can't handle mode %d\n", mode);%@NL@%
  19174.         exit(1);%@NL@%
  19175.     }%@NL@%
  19176.     Initialize();%@NL@%
  19177.     Drawfig();%@NL@%
  19178.     Drawbackground();%@NL@%
  19179.     Drawcopies();%@NL@%
  19180.     _settextposition(1, 1);%@NL@%
  19181.     _outtext("Press a key to end");%@NL@%
  19182.     _settextposition(3, 1);%@NL@%
  19183.     _outtext("_GPSET _GPRESET _GAND");%@NL@%
  19184.     _settextposition(11, 5);%@NL@%
  19185.     _outtext("_GOR _GXOR");%@NL@%
  19186.     getch();%@NL@%
  19187.     _setvideomode(_DEFAULTMODE);%@NL@%
  19188. }%@NL@%
  19189. %@NL@%
  19190. void Initialize()%@NL@%
  19191. {%@NL@%
  19192.     _getvideoconfig(&Vc);%@NL@%
  19193.     Ar = (float) (10 * Vc.numypixels)/%@NL@%
  19194.           (6.5 * Vc.numxpixels);%@NL@%
  19195.     _setlogorg(0, 0);%@NL@%
  19196.     Xmid = Vc.numxpixels / 2;%@NL@%
  19197.     Ymid = Vc.numypixels / 2;%@NL@%
  19198.     Xmax = Vc.numxpixels - 1;%@NL@%
  19199.     Ymax = Vc.numypixels - 1;%@NL@%
  19200.     %@AB@%/* locate three background rectangles */%@AE@%%@NL@%
  19201.     X[0] = Xmid;%@NL@%
  19202.     Y[0] = 0;%@NL@%
  19203.     X[1] = Xmid;%@NL@%
  19204.     Y[1] = Ymid;%@NL@%
  19205.     X[2] = 0;%@NL@%
  19206.     Y[2] = Ymid;%@NL@%
  19207.     X1 = 0.2 * Vc.numxpixels;%@NL@%
  19208.     Yb1 = 0.2 * Vc.numypixels;%@NL@%
  19209.     Xdelta = 0.033 * Vc.numxpixels;%@NL@%
  19210.     Xside = 3 * Xdelta;%@NL@%
  19211.     Yside = 3 * Ar * Xdelta;%@NL@%
  19212.     X2 = X1 + Xside;%@NL@%
  19213.     Y2 = Yb1 + Yside;%@NL@%
  19214.     %@AB@%/* offsets for _putimage() */%@AE@%%@NL@%
  19215.     Xps = .05 * Vc.numxpixels;%@NL@%
  19216.     Xpr = .20 * Vc.numxpixels;%@NL@%
  19217.     Xand = 0.35 * Vc.numxpixels;%@NL@%
  19218.     Xor = .10 * Vc.numxpixels;%@NL@%
  19219.     Xxor = .30 * Vc.numxpixels;%@NL@%
  19220.     Ytop = .05 * Vc.numypixels;%@NL@%
  19221.     Ybot = 2 * Ytop + Yside;%@NL@%
  19222.     _selectpalette(0);%@NL@%
  19223. }%@NL@%
  19224. %@NL@%
  19225. void Drawfig()%@NL@%
  19226. {%@NL@%
  19227.     _setcolor(1);%@NL@%
  19228.     _rectangle(_GFILLINTERIOR, X1, Yb1,%@NL@%
  19229.                X1 + Xdelta , Y2);%@NL@%
  19230.     _setcolor(2);%@NL@%
  19231.     _rectangle(_GFILLINTERIOR,X1 + Xdelta + 1, Yb1,%@NL@%
  19232.                X1 + 2 * Xdelta, Y2);%@NL@%
  19233.     _setcolor(3);%@NL@%
  19234.     _rectangle(_GFILLINTERIOR,X1 +  2 * Xdelta + 1,%@NL@%
  19235.                Yb1, X2, Y2);%@NL@%
  19236. %@NL@%
  19237. }%@NL@%
  19238. %@NL@%
  19239. void Drawbackground()%@NL@%
  19240. {%@NL@%
  19241.     _setfillmask(Mask);%@NL@%
  19242.     _setcolor(1);%@NL@%
  19243.     _rectangle(_GFILLINTERIOR, Xmid, 0, Xmax - 1, Ymid - 1);%@NL@%
  19244.     _setcolor(2);%@NL@%
  19245.     _rectangle(_GFILLINTERIOR, Xmid, Ymid, Xmax, Ymax);%@NL@%
  19246.     _setcolor (3);%@NL@%
  19247.     _rectangle(_GFILLINTERIOR, 0, Ymid, Xmid - 1, Ymax);%@NL@%
  19248. %@NL@%
  19249. }%@NL@%
  19250. %@NL@%
  19251. void Drawcopies()%@NL@%
  19252. {%@NL@%
  19253.     int quad;   %@AB@%/* quadrant used */%@AE@%%@NL@%
  19254.     char far *storage;%@NL@%
  19255. %@NL@%
  19256.     storage = (char far *) malloc((unsigned)_imagesize(%@NL@%
  19257.                X1, Yb1, X2, Y2));%@NL@%
  19258.     _getimage(X1, Yb1, X2, Y2, storage);%@NL@%
  19259. %@NL@%
  19260.     for (quad = 0; quad < 3; quad++)%@NL@%
  19261.         {%@NL@%
  19262.         _putimage(X[quad] + Xps, Y[quad] + Ytop,%@NL@%
  19263.                   storage, _GPSET);%@NL@%
  19264.         _putimage(X[quad] + Xpr, Y[quad] + Ytop,%@NL@%
  19265.                   storage, _GPRESET);%@NL@%
  19266.         _putimage (X[quad] + Xand, Y[quad] + Ytop,%@NL@%
  19267.                    storage, _GAND);%@NL@%
  19268.         _putimage (X[quad] + Xor, Y[quad] + Ybot,%@NL@%
  19269.                    storage, _GOR);%@NL@%
  19270.         _putimage(X[quad] + Xxor, Y[quad] + Ybot,%@NL@%
  19271.                   storage, _GXOR);%@NL@%
  19272.         }%@NL@%
  19273. }%@NL@%
  19274. %@NL@%
  19275. %@NL@%
  19276. %@2@%%@AH@%GETYN.C%@AE@%%@EH@%%@NL@%
  19277. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\GETYN.C%@AE@%%@NL@%
  19278. %@NL@%
  19279. %@AB@%/* getyn.c -- calls char function getyn()    */%@AE@%%@NL@%
  19280. %@AB@%/*               with error checking         */%@AE@%%@NL@%
  19281. %@NL@%
  19282. %@AI@%#define %@AE@%TRUE 1 %@NL@%
  19283. %@NL@%
  19284. main()%@NL@%
  19285. {%@NL@%
  19286.     char ch;%@NL@%
  19287.     char getyn();%@NL@%
  19288. %@NL@%
  19289.     printf("Do you want to continue? ");%@NL@%
  19290.     if ((ch = getyn()) == 'y')%@NL@%
  19291.         printf("Answer was y\n");%@NL@%
  19292.     else%@NL@%
  19293.         printf("Answer was n\n");%@NL@%
  19294.     printf("Value of ch was %c\n", ch);%@NL@%
  19295. }%@NL@%
  19296. %@NL@%
  19297. char getyn()%@NL@%
  19298. {%@NL@%
  19299.     char ch;%@NL@%
  19300.     while (TRUE)%@NL@%
  19301.         {%@NL@%
  19302.         printf(" (y or n) ");%@NL@%
  19303.         ch = getche();%@NL@%
  19304.         printf("\n");%@NL@%
  19305.         if ((ch == 'y') || (ch == 'n'))%@NL@%
  19306.         %@AB@%/* valid response, break out of loop */%@AE@%%@NL@%
  19307.             break;%@NL@%
  19308.         %@AB@%/* give error message and loop again */%@AE@%%@NL@%
  19309.         printf("please enter ");%@NL@%
  19310.         }%@NL@%
  19311.     return(ch);%@NL@%
  19312. }%@NL@%
  19313. %@NL@%
  19314. %@NL@%
  19315. %@2@%%@AH@%GRAPHBOX.C%@AE@%%@EH@%%@NL@%
  19316. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\GRAPHBOX.C%@AE@%%@NL@%
  19317. %@NL@%
  19318. %@NL@%
  19319. %@AB@%/* GRAPHBOX.C -- defined to use PC-specific */%@AE@%%@NL@%
  19320. %@AB@%/*               graphics characters        */%@AE@%%@NL@%
  19321. %@NL@%
  19322. %@AI@%#define %@AE@%NL 10 %@NL@%
  19323. %@AI@%#define %@AE@%CR 13 %@NL@%
  19324. %@AI@%#define %@AE@%BLANK 32 %@NL@%
  19325. %@AI@%#define %@AE@%UPLEFT 201 %@NL@%
  19326. %@AI@%#define %@AE@%UPRIGHT 187 %@NL@%
  19327. %@AI@%#define %@AE@%LOWLEFT 200 %@NL@%
  19328. %@AI@%#define %@AE@%LOWRIGHT 188 %@NL@%
  19329. %@AI@%#define %@AE@%LINE 205 %@NL@%
  19330. %@AI@%#define %@AE@%SIDE 186 %@NL@%
  19331. %@NL@%
  19332. main()%@NL@%
  19333. {%@NL@%
  19334.     int i, j, height, width;%@NL@%
  19335. %@NL@%
  19336.     %@AB@%/* get height and width from user */%@AE@%%@NL@%
  19337.     printf("How high a box do you want? ");%@NL@%
  19338.     scanf("%d", &height);%@NL@%
  19339.     printf("How wide do you want it to be? ");%@NL@%
  19340.     scanf("%d", &width);%@NL@%
  19341. %@NL@%
  19342.     %@AB@%/* draw top of box */%@AE@%%@NL@%
  19343.     putch(UPLEFT);%@NL@%
  19344.     for (i = 0; i < (width - 2); i++)%@NL@%
  19345.         putch(LINE);%@NL@%
  19346.     putch(UPRIGHT);%@NL@%
  19347.     putch(NL);%@NL@%
  19348.     putch(CR); %@AB@%/* go to next line */%@AE@%%@NL@%
  19349. %@NL@%
  19350.     %@AB@%/* draw sides of box */%@AE@%%@NL@%
  19351.     for (i = 0; i < height - 2; i++)  %@AB@%/* outer loop */%@AE@%%@NL@%
  19352.         {%@NL@%
  19353.         putch(SIDE); %@AB@%/* left side */%@AE@%%@NL@%
  19354.         for (j = 0; j < (width - 2); j++) %@AB@%/* inner loop */%@AE@%%@NL@%
  19355.             {%@NL@%
  19356.             putch(BLANK);%@NL@%
  19357.             }%@NL@%
  19358.         putch(SIDE); %@AB@%/* right side */%@AE@%%@NL@%
  19359.         putch(NL);%@NL@%
  19360.         putch(CR); %@AB@%/* move to next line */%@AE@%%@NL@%
  19361.         }%@NL@%
  19362. %@NL@%
  19363.     %@AB@%/* draw bottom of box */%@AE@%%@NL@%
  19364.     putch(LOWLEFT);%@NL@%
  19365.     for (i = 0; i < (width - 2); i++)%@NL@%
  19366.         putch(LINE);%@NL@%
  19367.     putch(LOWRIGHT);%@NL@%
  19368.     putch(NL);%@NL@%
  19369.     putch(CR); %@AB@%/* box is done, move cursor to new line */%@AE@%%@NL@%
  19370. }%@NL@%
  19371. %@NL@%
  19372. %@NL@%
  19373. %@NL@%
  19374. %@2@%%@AH@%GRAPHCHA.C%@AE@%%@EH@%%@NL@%
  19375. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\GRAPHCHA.C%@AE@%%@NL@%
  19376. %@NL@%
  19377. %@AB@%/*   grafchar.c -- draws graphics characters with      */%@AE@%%@NL@%
  19378. %@AB@%/*                 attributes on the screen            */%@AE@%%@NL@%
  19379. %@AB@%/*  Program list : grafchar.c, initstuf.c, drawchar.c, */%@AE@%%@NL@%
  19380. %@AB@%/*                  scrfun.c                           */%@AE@%%@NL@%
  19381. %@AB@%/*  User include files: keys.h, scrn.h, grafchar.h     */%@AE@%%@NL@%
  19382. %@AB@%/*  Note: activate Screen Swapping On in Debug menu    */%@AE@%%@NL@%
  19383.  %@NL@%
  19384. %@AI@%#include %@AE@%"grafchar.h" %@NL@%
  19385. unsigned char Grchr[NUMCHARS];  %@AB@%/* to store graphics set */%@AE@%%@NL@%
  19386. void Init_stuff(void);      %@AB@%/* in initstuf.c */%@AE@%%@NL@%
  19387. void Draw_chars(void);      %@AB@%/* in drawchar.c */%@AE@%%@NL@%
  19388. %@NL@%
  19389. main()%@NL@%
  19390. {%@NL@%
  19391.     Init_stuff();  %@AB@%/* initialize vital elements */%@AE@%%@NL@%
  19392.     Draw_chars();  %@AB@%/* map keys to graphics characters */%@AE@%%@NL@%
  19393. }%@NL@%
  19394. %@NL@%
  19395. %@NL@%
  19396. %@2@%%@AH@%HARDWARE.C%@AE@%%@EH@%%@NL@%
  19397. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\HARDWARE.C%@AE@%%@NL@%
  19398. %@NL@%
  19399. %@AB@%/* hardware.c -- shows a mixture of int, */%@AE@%%@NL@%
  19400. %@AB@%/*               float, and char types   */%@AE@%%@NL@%
  19401. %@NL@%
  19402. main()%@NL@%
  19403. {%@NL@%
  19404.     int threads    = 8;       %@AB@%/* threads per inch */%@AE@%%@NL@%
  19405.     float length   = 1.25,    %@AB@%/* length in inches */%@AE@%%@NL@%
  19406.           diameter = 0.425,   %@AB@%/* diameter in inches */%@AE@%%@NL@%
  19407.           price    = 0.89;    %@AB@%/* price per hundred */%@AE@%%@NL@%
  19408.     char bin = 'A';           %@AB@%/* kept in bin A */%@AE@%%@NL@%
  19409.     long quantity = 42300;    %@AB@%/* number in bin */%@AE@%%@NL@%
  19410. %@NL@%
  19411.     printf("Screws: %d threads/inch\n%f inches long\n",%@NL@%
  19412.             threads, length);%@NL@%
  19413.     printf("%f diameter\n\n", diameter);%@NL@%
  19414.     printf("Price per 100: %f\n", price);%@NL@%
  19415.     printf("Stored in bin: %c\nQuantity on hand: %ld",%@NL@%
  19416.             bin, quantity);%@NL@%
  19417. }%@NL@%
  19418. %@NL@%
  19419. %@NL@%
  19420. %@2@%%@AH@%HELLO.C%@AE@%%@EH@%%@NL@%
  19421. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\HELLO.C%@AE@%%@NL@%
  19422. %@NL@%
  19423. %@AB@%/* hello.c  --  legal ways to initialize strings as */%@AE@%%@NL@%
  19424. %@AB@%/*              arrays of char values               */%@AE@%%@NL@%
  19425. %@NL@%
  19426. char Gphrase[] = { %@NL@%
  19427.     'H','e','l','l','o','\n','\0' };    %@AB@%/* global initialization */%@AE@%%@NL@%
  19428. %@NL@%
  19429. main()%@NL@%
  19430. {%@NL@%
  19431.     static char gphrase[] = {%@NL@%
  19432.         'h','e','l','l','o','\n','\0' };    %@AB@%/* local initialization */%@AE@%%@NL@%
  19433. %@NL@%
  19434.     printf("Global: %s\n", Gphrase);%@NL@%
  19435.     printf("Local:  %s\n", gphrase);%@NL@%
  19436. %@NL@%
  19437. }%@NL@%
  19438. %@NL@%
  19439. %@NL@%
  19440. %@2@%%@AH@%HELP.C%@AE@%%@EH@%%@NL@%
  19441. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\HELP.C%@AE@%%@NL@%
  19442. %@NL@%
  19443. %@AB@%/*  help.c -- uses paging and direct memory access    */%@AE@%%@NL@%
  19444. %@AB@%/*            to display a help screen                */%@AE@%%@NL@%
  19445. %@AB@%/*  Program list: help.c, writestr.c, writechr.c,     */%@AE@%%@NL@%
  19446. %@AB@%/*                scrfun.c                            */%@AE@%%@NL@%
  19447. %@AB@%/*  User include files: scrn.h, color.h               */%@AE@%%@NL@%
  19448. %@AB@%/*  Note: activate Screen Swapping On in Debug menu   */%@AE@%%@NL@%
  19449. %@NL@%
  19450. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  19451. %@AI@%#include %@AE@%<conio.h> %@NL@%
  19452. %@AI@%#include %@AE@%"color.h" %@NL@%
  19453. %@AI@%#include %@AE@%"scrn.h" %@NL@%
  19454. typedef unsigned int (far * VIDMEM);%@NL@%
  19455. %@AI@%#define %@AE@%CGAMEM ((VIDMEM) (0xB800L << 16)) %@NL@%
  19456. %@AI@%#define %@AE@%PAGESIZE 2000 %@NL@%
  19457. %@AI@%#define %@AE@%PAGEOFFSET  0x800L %@NL@%
  19458. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  19459. %@AI@%#define %@AE@%ATTR1 (BG_BLUE | YELLOW) %@NL@%
  19460. %@AI@%#define %@AE@%ATTR2 (BG_YELLOW | BLUE) %@NL@%
  19461. %@AI@%#define %@AE@%ATTR3 (BG_RED | YELLOW | BLINK | INTENSE) %@NL@%
  19462. %@AI@%#define %@AE@%CH1  (unsigned short) '\xB1' %@NL@%
  19463. char *str1 = "Press ? key for help.";%@NL@%
  19464. char *str2 = "Press Enter key to return.";%@NL@%
  19465. char *str3 = "Press Esc key to quit.";%@NL@%
  19466. char *str4 = "\xB1HELP!\xB1";%@NL@%
  19467. void Write_chars(VIDMEM, unsigned short, unsigned%@NL@%
  19468.                     short, unsigned short);%@NL@%
  19469. void Write_str(VIDMEM, unsigned short, char *);%@NL@%
  19470. %@NL@%
  19471. main()%@NL@%
  19472. {%@NL@%
  19473.     int ch;%@NL@%
  19474.     unsigned char page = 0;%@NL@%
  19475.     unsigned char mode;%@NL@%
  19476. %@NL@%
  19477.     mode = Getvmode();%@NL@%
  19478.     if (mode != TEXTC80 && mode != TEXTBW80)%@NL@%
  19479.     {%@NL@%
  19480.         printf("Only modes 2 and 3 supported. Bye.\n");%@NL@%
  19481.         exit(1);%@NL@%
  19482.     }%@NL@%
  19483.     Setpage (page);%@NL@%
  19484.     Write_chars(CGAMEM, '\0', ATTR2, PAGESIZE);%@NL@%
  19485.     Write_str(CGAMEM + 2 * COLS, ATTR1, str1);%@NL@%
  19486.     Write_str(CGAMEM + 2 * COLS, ATTR1, str1);%@NL@%
  19487.     Write_str(CGAMEM + 22 * COLS, ATTR1, str3);%@NL@%
  19488.     Write_chars(CGAMEM + PAGEOFFSET, '\0', ATTR1, PAGESIZE);%@NL@%
  19489.     Write_str(CGAMEM + PAGEOFFSET + 20 * COLS, ATTR2, str2);%@NL@%
  19490.     Write_str(CGAMEM + PAGEOFFSET + 22 * COLS, ATTR1, str3);%@NL@%
  19491.     Write_chars(CGAMEM + PAGEOFFSET + 10 * COLS + 36,%@NL@%
  19492.                 CH1, ATTR3, 7);%@NL@%
  19493.     Write_str(CGAMEM + PAGEOFFSET + 11 * COLS + 36,%@NL@%
  19494.               ATTR3, str4);%@NL@%
  19495.     Write_chars(CGAMEM + PAGEOFFSET + 12 * COLS + 36,%@NL@%
  19496.                 CH1, ATTR3, 7);%@NL@%
  19497. %@NL@%
  19498.     while ((ch = getch()) != ESC)%@NL@%
  19499.         {%@NL@%
  19500.         if (ch == '?' && page == 0)%@NL@%
  19501.             Setpage (page = 1);%@NL@%
  19502.         else if (ch == '\r' && page == 1)%@NL@%
  19503.             Setpage(page = 0);%@NL@%
  19504.         }%@NL@%
  19505.     Write_chars(CGAMEM, '\0', NORMAL, PAGESIZE);%@NL@%
  19506.     Write_chars(CGAMEM + PAGEOFFSET, '\0', NORMAL, PAGESIZE);%@NL@%
  19507. }%@NL@%
  19508. %@NL@%
  19509. %@NL@%
  19510. %@2@%%@AH@%HEXOUT.C%@AE@%%@EH@%%@NL@%
  19511. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\HEXOUT.C%@AE@%%@NL@%
  19512. %@NL@%
  19513. %@AB@%/* hexout.c --  print a floating point variable in */%@AE@%%@NL@%
  19514. %@AB@%/*              hexadecimal format                 */%@AE@%%@NL@%
  19515. %@NL@%
  19516. extern void Hexout();%@NL@%
  19517. %@NL@%
  19518. main()%@NL@%
  19519. {%@NL@%
  19520.     float fary[1];%@NL@%
  19521. %@NL@%
  19522.     printf("Enter a floating point number\n");%@NL@%
  19523.     printf("(Any nonnumeric entry quits)\n\n");%@NL@%
  19524.     while (scanf("%f", &fary[0]) == 1)%@NL@%
  19525.         {%@NL@%
  19526.         Hexout(fary);%@NL@%
  19527.         }%@NL@%
  19528.     return (0);%@NL@%
  19529. }%@NL@%
  19530. %@NL@%
  19531. void Hexout(unsigned char chary[])%@NL@%
  19532. {%@NL@%
  19533.     int i;%@NL@%
  19534. %@NL@%
  19535.     for (i = 0; i < sizeof(float); ++i)%@NL@%
  19536.         {%@NL@%
  19537.         printf("%02X ", chary[i]);%@NL@%
  19538.         }%@NL@%
  19539.     printf("\n\n");%@NL@%
  19540. }%@NL@%
  19541. %@NL@%
  19542. %@NL@%
  19543. %@2@%%@AH@%IBMIQ.C%@AE@%%@EH@%%@NL@%
  19544. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\IBMIQ.C%@AE@%%@NL@%
  19545. %@NL@%
  19546. %@AB@%/* ibmiq.c -- a short dialog                          */%@AE@%%@NL@%
  19547. %@NL@%
  19548. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  19549. main()%@NL@%
  19550. {%@NL@%
  19551.     char name[80];%@NL@%
  19552.     int iq;%@NL@%
  19553. %@NL@%
  19554.     printf("Enter your first name: -> ");%@NL@%
  19555.     scanf("%s", name);%@NL@%
  19556.     printf("Enter your IQ: -> ");%@NL@%
  19557.     scanf("%d", iq);%@NL@%
  19558.     printf("Well, %s, my IQ is %d!", name, 2 * iq - 1 );%@NL@%
  19559. }%@NL@%
  19560. %@NL@%
  19561. %@NL@%
  19562. %@2@%%@AH@%IF.C%@AE@%%@EH@%%@NL@%
  19563. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP05\IF.C%@AE@%%@NL@%
  19564. %@NL@%
  19565. %@AB@%/* if.c -- simple IF statement */%@AE@%%@NL@%
  19566. %@NL@%
  19567. char ch;%@NL@%
  19568. main()%@NL@%
  19569. {%@NL@%
  19570.     printf("Do you want to continue y/n? "); %@AB@%/* prompt */%@AE@%%@NL@%
  19571.     if (ch = getche() == 'y')%@NL@%
  19572.         printf("\nLet's continue ...\n");    %@AB@%/* if true */%@AE@%%@NL@%
  19573.     printf("\nAll done.\n");                 %@AB@%/* always executed */%@AE@%%@NL@%
  19574. }%@NL@%
  19575. %@NL@%
  19576. %@NL@%
  19577. %@2@%%@AH@%IFELSE.C%@AE@%%@EH@%%@NL@%
  19578. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP05\IFELSE.C%@AE@%%@NL@%
  19579. %@NL@%
  19580. %@AB@%/* ifelse.c -- IF with ELSE */%@AE@%%@NL@%
  19581. %@NL@%
  19582. char ch;%@NL@%
  19583. int num;%@NL@%
  19584. main()%@NL@%
  19585. {%@NL@%
  19586.     printf("Are you a new user? y/n? ");%@NL@%
  19587.     if (ch = getche() == 'y')%@NL@%
  19588.         {%@NL@%
  19589.         %@AB@%/* executed if IF is true */%@AE@%%@NL@%
  19590.         printf("\n\nYou must register to use this\n");%@NL@%
  19591.         printf("bulletin board. Please read\n");%@NL@%
  19592.         printf("Bulletin #1 first. Thank You.\n");%@NL@%
  19593.         }%@NL@%
  19594.     else%@NL@%
  19595.         %@AB@%/* executed if IF is false */%@AE@%%@NL@%
  19596.         {%@NL@%
  19597.         printf("\n\nEnter your secret number: ");%@NL@%
  19598.         scanf("d", &num);%@NL@%
  19599.         }%@NL@%
  19600. }%@NL@%
  19601. %@NL@%
  19602. %@NL@%
  19603. %@2@%%@AH@%INCDEC.C%@AE@%%@EH@%%@NL@%
  19604. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\INCDEC.C%@AE@%%@NL@%
  19605. %@NL@%
  19606. %@AB@%/* incdec.c -- shows effect of            */%@AE@%%@NL@%
  19607. %@AB@%/*             increments and decrements  */%@AE@%%@NL@%
  19608. %@NL@%
  19609. main()%@NL@%
  19610. {%@NL@%
  19611.     int a = 10;%@NL@%
  19612. %@NL@%
  19613.     printf("a is %d\n", a);%@NL@%
  19614.     printf("++a is %d\n", ++a);%@NL@%
  19615.     printf("--a sets a back to %d\n", --a);%@NL@%
  19616. }%@NL@%
  19617. %@NL@%
  19618. %@NL@%
  19619. %@2@%%@AH@%INDEXER.C%@AE@%%@EH@%%@NL@%
  19620. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\INDEXER.C%@AE@%%@NL@%
  19621. %@NL@%
  19622. %@AB@%/* indexer.c -- use indices to display an array      */%@AE@%%@NL@%
  19623. %@NL@%
  19624. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  19625. int code1[] = {2, 4, 6, 8};%@NL@%
  19626. int code2[] = {1, 3, 7, 9};%@NL@%
  19627. int code3[] = {5, 10, 15, 20};%@NL@%
  19628. main()%@NL@%
  19629. {%@NL@%
  19630.     int index;%@NL@%
  19631.     int size = (sizeof code2) / (sizeof (int));%@NL@%
  19632. %@NL@%
  19633.     for ( index = 1; index <= size; size++)%@NL@%
  19634.        printf("%3d ", code2[index]);%@NL@%
  19635.     putchar('\n');%@NL@%
  19636. }%@NL@%
  19637. %@NL@%
  19638. %@NL@%
  19639. %@NL@%
  19640. %@2@%%@AH@%INDEXER2.C%@AE@%%@EH@%%@NL@%
  19641. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\INDEXER2.C%@AE@%%@NL@%
  19642. %@NL@%
  19643. %@AB@%/* indexer2.c -- use indices to display an array      */%@AE@%%@NL@%
  19644. %@NL@%
  19645. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  19646. int code1[] = {2, 4, 6, 8};%@NL@%
  19647. int code2[] = {1, 3, 7, 9};%@NL@%
  19648. int code3[] = {5, 10, 15, 20};%@NL@%
  19649. main()%@NL@%
  19650. {%@NL@%
  19651.     int index;%@NL@%
  19652.     int size = (sizeof code2) / (sizeof (int));%@NL@%
  19653.                 %@AB@%/* get number of elements in array */%@AE@%%@NL@%
  19654. %@NL@%
  19655.     for ( index = 1; index <= size; index++)%@NL@%
  19656.        printf("%3d ", code2[index]);%@NL@%
  19657.     putchar('\n');%@NL@%
  19658. }%@NL@%
  19659. %@NL@%
  19660. %@NL@%
  19661. %@2@%%@AH@%INFLATE.C%@AE@%%@EH@%%@NL@%
  19662. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\INFLATE.C%@AE@%%@NL@%
  19663. %@NL@%
  19664. %@NL@%
  19665. %@AB@%/* INFLATE.C -- shows multiple initialization */%@AE@%%@NL@%
  19666. %@AB@%/*              and calculations in for loop  */%@AE@%%@NL@%
  19667. %@NL@%
  19668. main()%@NL@%
  19669. {%@NL@%
  19670.     int year;%@NL@%
  19671.     float value, rate;%@NL@%
  19672.     printf("What do you think the inflation rate will be?");%@NL@%
  19673.     scanf("%f", &rate);%@NL@%
  19674.     printf("If the dollar is worth 100 cents in 1987\n");%@NL@%
  19675.     printf("and the inflation rate is %2.2f, then:\n", rate);%@NL@%
  19676. %@NL@%
  19677.     for (year=1988, value = 1.0; year <=1999;%@NL@%
  19678.          value *= (1.0 - rate),%@NL@%
  19679.          printf("in %d the dollar will be worth", year),%@NL@%
  19680.          printf(" %2.0f cents\n", value * 100),%@NL@%
  19681.          ++year%@NL@%
  19682.          );%@NL@%
  19683. }%@NL@%
  19684. %@NL@%
  19685. %@NL@%
  19686. %@NL@%
  19687. %@2@%%@AH@%INITSTUF.C%@AE@%%@EH@%%@NL@%
  19688. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\INITSTUF.C%@AE@%%@NL@%
  19689. %@NL@%
  19690. %@AB@%/*  initstuf.c -- initializing module for grafchar.c */%@AE@%%@NL@%
  19691. %@AB@%/*  assign graphics character codes to an array      */%@AE@%%@NL@%
  19692. %@AB@%/*  and initialize screen                            */%@AE@%%@NL@%
  19693. %@NL@%
  19694. "scrn.h"   %@AB@%/* Clearscr(), Home(), Setcurs() */%@AE@%%@NL@%
  19695. %@AI@%#include %@AE@%"grafchar.h" %@NL@%
  19696. extern unsigned char Grchr[];  %@AB@%/* defined in grafchar.c */%@AE@%%@NL@%
  19697. void Print_attr(char *, unsigned char, unsigned char);%@NL@%
  19698. void Init_stuff()%@NL@%
  19699. {%@NL@%
  19700.     int i;%@NL@%
  19701. %@NL@%
  19702.     %@AB@%/* initialize array with graphics characters */%@AE@%%@NL@%
  19703.     for (i = 0; i < NUMCHARS; i++)%@NL@%
  19704.         Grchr[i] = GCSTART + i;%@NL@%
  19705.     Clearscr();%@NL@%
  19706.     Home();%@NL@%
  19707. %@NL@%
  19708.     %@AB@%/*   show key meanings at bottom of screen */%@AE@%%@NL@%
  19709.     Setcurs(BOTLINE + 1, 0, PAGE);%@NL@%
  19710.     for (i = 0; i < 40; i++) %@AB@%/* graphics chars */%@AE@%%@NL@%
  19711.     {%@NL@%
  19712.         putch(Grchr[i]);%@NL@%
  19713.         putch(SPACE);%@NL@%
  19714.     }%@NL@%
  19715.     Setcurs(BOTLINE + 2, 0, PAGE);%@NL@%
  19716.     for (i = 0; i < 40; i++)  %@AB@%/* key assignments */%@AE@%%@NL@%
  19717.     {%@NL@%
  19718.         putch('0' + i);%@NL@%
  19719.         putch(SPACE);%@NL@%
  19720.     }%@NL@%
  19721.     Setcurs(BOTLINE + 3, 0, PAGE);%@NL@%
  19722.     for (i = 40; i < NUMCHARS; i++) %@AB@%/* second row */%@AE@%%@NL@%
  19723.     {%@NL@%
  19724.         putch(Grchr[i]);%@NL@%
  19725.         putch(SPACE);%@NL@%
  19726.     }%@NL@%
  19727.         %@AB@%/* show function key assignments */%@AE@%%@NL@%
  19728.     printf(" SPACE : ERASE  PgUp : No Draw ");%@NL@%
  19729.     printf(" PgDn : Draw  ESC : Quit");%@NL@%
  19730.     Setcurs(BOTLINE + 4, 0, PAGE);%@NL@%
  19731.     for (i = 40; i < NUMCHARS; i++) %@AB@%/* second row */%@AE@%%@NL@%
  19732.     {%@NL@%
  19733.         putch('0' + i);%@NL@%
  19734.         putch(SPACE);%@NL@%
  19735.     }%@NL@%
  19736.        %@AB@%/* more function key assignments */%@AE@%%@NL@%
  19737.     Print_attr("F1 : Normal ", NORMAL, PAGE);%@NL@%
  19738.     Print_attr("F2 : Reverse Video ", VIDREV, PAGE);%@NL@%
  19739.     Setcurs(BOTLINE + 5, 16, PAGE);%@NL@%
  19740.     Print_attr("F3 : Blinking ", NORMAL | BLINK, PAGE);%@NL@%
  19741.     Print_attr("F4 : Intense ", NORMAL | INTENSE, PAGE);%@NL@%
  19742.     Home();%@NL@%
  19743. }%@NL@%
  19744. %@NL@%
  19745. void Print_attr(str, attr, page)%@NL@%
  19746. char *str;%@NL@%
  19747. unsigned char attr, page;%@NL@%
  19748. {%@NL@%
  19749.     while (*str != '\0')%@NL@%
  19750.     {%@NL@%
  19751.         Write_ch_atr(*str++, attr, page, 1);%@NL@%
  19752.         Cursrt();%@NL@%
  19753.     }%@NL@%
  19754. }%@NL@%
  19755. %@NL@%
  19756. %@NL@%
  19757. %@2@%%@AH@%INTVARS.C%@AE@%%@EH@%%@NL@%
  19758. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\INTVARS.C%@AE@%%@NL@%
  19759. %@NL@%
  19760. %@AB@%/* intvars.c -- declare, define, and print */%@AE@%%@NL@%
  19761. %@AB@%/*              some integer variables     */%@AE@%%@NL@%
  19762. %@NL@%
  19763. main()%@NL@%
  19764. {%@NL@%
  19765.     %@AB@%/* declare variables */%@AE@%%@NL@%
  19766.     int length, beam;%@NL@%
  19767.     unsigned int displacement;%@NL@%
  19768.     %@AB@%/* assign values to variables */%@AE@%%@NL@%
  19769.     length = 824;%@NL@%
  19770.     beam = 118;%@NL@%
  19771.     displacement = 41676;%@NL@%
  19772. %@NL@%
  19773.     %@AB@%/* print out values */%@AE@%%@NL@%
  19774.     printf("The battleship Bismarck was %d feet long",%@NL@%
  19775.             length);%@NL@%
  19776.     printf(" with a beam of %d feet,\n", beam);%@NL@%
  19777.     printf("and displaced %u tons.\n", displacement);%@NL@%
  19778. }%@NL@%
  19779. %@NL@%
  19780. %@NL@%
  19781. %@2@%%@AH@%INVERT.C%@AE@%%@EH@%%@NL@%
  19782. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\INVERT.C%@AE@%%@NL@%
  19783. %@NL@%
  19784. %@AB@%/* invert.c  --  combines character classification and */%@AE@%%@NL@%
  19785. %@AB@%/*               transformation to invert text         */%@AE@%%@NL@%
  19786. %@NL@%
  19787. <stdio.h>         %@AB@%/* for NULL           */%@AE@%%@NL@%
  19788. <ctype.h>         %@AB@%/* for toupper, et al */%@AE@%%@NL@%
  19789. %@NL@%
  19790. main()%@NL@%
  19791. {%@NL@%
  19792.     char buf[BUFSIZ];%@NL@%
  19793.     int i;%@NL@%
  19794. %@NL@%
  19795.     printf("Type in a line of text and I will invert it.\n");%@NL@%
  19796. %@NL@%
  19797.     if (gets(buf) == NULL)%@NL@%
  19798.         exit(1);%@NL@%
  19799. %@NL@%
  19800.     %@AB@%/* Print the string backwards. */%@AE@%%@NL@%
  19801.     for (i = (strlen(buf) -1); i >= 0; --i)%@NL@%
  19802.         {%@NL@%
  19803.         if (isupper(buf[i]))            %@AB@%/* upper to lower */%@AE@%%@NL@%
  19804.             putchar(tolower(buf[i]));%@NL@%
  19805.         else if (islower(buf[i]))       %@AB@%/* lower to upper */%@AE@%%@NL@%
  19806.             putchar(toupper(buf[i]));%@NL@%
  19807.         else%@NL@%
  19808.             putchar(buf[i]);%@NL@%
  19809.         }%@NL@%
  19810.     putchar('\n');%@NL@%
  19811. %@NL@%
  19812. }%@NL@%
  19813. %@NL@%
  19814. %@NL@%
  19815. %@2@%%@AH@%KEYS.C%@AE@%%@EH@%%@NL@%
  19816. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\KEYS.C%@AE@%%@NL@%
  19817. %@NL@%
  19818. %@AB@%/* keys.c  --   The keyboard input-handling routines  */%@AE@%%@NL@%
  19819. %@AB@%/*              for the texed editor.                 */%@AE@%%@NL@%
  19820. %@NL@%
  19821. Edit_file()%@NL@%
  19822. {%@NL@%
  19823.     char ch;%@NL@%
  19824. %@NL@%
  19825.     printf("\nYou are now in the editor.\n");%@NL@%
  19826.     printf("Press 'Q' to exit back to main menu.\n");%@NL@%
  19827. %@NL@%
  19828.     do%@NL@%
  19829.         {%@NL@%
  19830.         ch = getch();%@NL@%
  19831.         putch(ch);%@NL@%
  19832.         } while (ch != 'Q');%@NL@%
  19833. %@NL@%
  19834.     printf("\n\n");%@NL@%
  19835. }%@NL@%
  19836. %@NL@%
  19837. %@NL@%
  19838. %@2@%%@AH@%L2WORDS.C%@AE@%%@EH@%%@NL@%
  19839. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\L2WORDS.C%@AE@%%@NL@%
  19840. %@NL@%
  19841. %@AB@%/* l2words.c  --  employ an array of pointers to   */%@AE@%%@NL@%
  19842. %@AB@%/*                strings to bust a line of text   */%@AE@%%@NL@%
  19843. %@AB@%/*                into its component words         */%@AE@%%@NL@%
  19844. %@NL@%
  19845. <stdio.h>          %@AB@%/* for NULL and BUFSIZ */%@AE@%%@NL@%
  19846. %@NL@%
  19847. main()%@NL@%
  19848. {%@NL@%
  19849.     char **Line2words();    %@AB@%/* declare function type */%@AE@%%@NL@%
  19850.     char **list;            %@AB@%/* pointer to pointer    */%@AE@%%@NL@%
  19851.     char buf[BUFSIZ];       %@AB@%/* buffer for input      */%@AE@%%@NL@%
  19852.     int  count, i, quote_flag;%@NL@%
  19853. %@NL@%
  19854.     printf("Enter a line of text and I will break\n");%@NL@%
  19855.     printf("it up for you.\n");%@NL@%
  19856. %@NL@%
  19857.     if (gets(buf) == NULL)%@NL@%
  19858.         exit(1);%@NL@%
  19859.     %@NL@%
  19860.     list = Line2words(buf, &count);%@NL@%
  19861. %@NL@%
  19862.     for (i = 0; i < count; i++)%@NL@%
  19863.         {%@NL@%
  19864.         quote_flag = 0;%@NL@%
  19865.         printf("<");%@NL@%
  19866.         if (list[i] != buf)%@NL@%
  19867.             {%@NL@%
  19868.             if( list[i][-1] == '"')    %@AB@%/* negative subscript */%@AE@%%@NL@%
  19869.                 {%@NL@%
  19870.                 ++quote_flag;%@NL@%
  19871.                 printf("\"");%@NL@%
  19872.                 }%@NL@%
  19873.             }%@NL@%
  19874.         printf("%s", list[i]);%@NL@%
  19875. %@NL@%
  19876.         if (quote_flag)%@NL@%
  19877.             printf("\"");%@NL@%
  19878. %@NL@%
  19879.         printf(">\n");%@NL@%
  19880.         }%@NL@%
  19881. }%@NL@%
  19882. %@NL@%
  19883. %@AI@%#define %@AE@%MAXW 64 %@NL@%
  19884. %@NL@%
  19885. char **Line2words(char *line, int  *count)%@NL@%
  19886. {%@NL@%
  19887.     static char *words[MAXW];%@NL@%
  19888.     int  index;%@NL@%
  19889. %@NL@%
  19890.     index = 0;        %@AB@%/* zero internal index */%@AE@%%@NL@%
  19891. %@NL@%
  19892.     while (*line != '\0')%@NL@%
  19893.         {%@NL@%
  19894.         %@AB@%/* turn spaces and tabs into zeros */%@AE@%%@NL@%
  19895.         if (*line == ' ' || *line == '\t')%@NL@%
  19896.             {%@NL@%
  19897.             *(line++) = '\0';%@NL@%
  19898.             continue;%@NL@%
  19899.             }%@NL@%
  19900.         words[index] = line++;    %@AB@%/* found a word */%@AE@%%@NL@%
  19901. %@NL@%
  19902.         %@AB@%/* is it quoted? */%@AE@%%@NL@%
  19903.         if ( *(words[index]) == '"')%@NL@%
  19904.             {%@NL@%
  19905.             %@AB@%/* Yes, advance pointer to just past quote. */%@AE@%%@NL@%
  19906.             ++words[index];%@NL@%
  19907. %@NL@%
  19908.             %@AB@%/* find next quote. */%@AE@%%@NL@%
  19909.             while (*line && *line != '"')%@NL@%
  19910.                 {%@NL@%
  19911.                 ++line;%@NL@%
  19912.                 }%@NL@%
  19913. %@NL@%
  19914.             %@AB@%/* and turn it into a '\0'. */%@AE@%%@NL@%
  19915.             if (*line)%@NL@%
  19916.                 *(line++) = '\0';%@NL@%
  19917.             }%@NL@%
  19918.         else%@NL@%
  19919.             {%@NL@%
  19920.             %@AB@%/* otherwise skip to next space */%@AE@%%@NL@%
  19921.             while (*line && *line != ' ' && *line != '\t')%@NL@%
  19922.                 {%@NL@%
  19923.                 ++line;%@NL@%
  19924.                 }%@NL@%
  19925.             }%@NL@%
  19926.         if (++index == MAXW)%@NL@%
  19927.             break;%@NL@%
  19928.         }%@NL@%
  19929.     *count = index;        %@AB@%/* set count via pointer   */%@AE@%%@NL@%
  19930.     return (words);        %@AB@%/* return address of array */%@AE@%%@NL@%
  19931. }%@NL@%
  19932. %@NL@%
  19933. %@NL@%
  19934. %@2@%%@AH@%LEFTSTR.C%@AE@%%@EH@%%@NL@%
  19935. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\LEFTSTR.C%@AE@%%@NL@%
  19936. %@NL@%
  19937. %@AB@%/* leftstr.c -- a C version of BASIC's LEFT$ */%@AE@%%@NL@%
  19938. %@NL@%
  19939. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  19940. %@NL@%
  19941. char *Leftstr(char *str, int cnt)%@NL@%
  19942. {%@NL@%
  19943.     static char *cp = NULL;%@NL@%
  19944.     char *malloc();%@NL@%
  19945. %@NL@%
  19946.     if (cnt > strlen(str))%@NL@%
  19947.         cnt = strlen(str);%@NL@%
  19948.     if (cp != NULL)%@NL@%
  19949.         free(cp);%@NL@%
  19950.     if ((cp = malloc(cnt + 1)) == NULL)%@NL@%
  19951.         return (NULL);%@NL@%
  19952.     strncpy(cp, str, cnt);%@NL@%
  19953.     return (cp);%@NL@%
  19954. }%@NL@%
  19955. %@NL@%
  19956. %@NL@%
  19957. %@2@%%@AH@%LINES.C%@AE@%%@EH@%%@NL@%
  19958. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\LINES.C%@AE@%%@NL@%
  19959. %@NL@%
  19960. %@AB@%/* lines.c -- calls line() with */%@AE@%%@NL@%
  19961. %@AB@%/*            five parameters   */%@AE@%%@NL@%
  19962. %@NL@%
  19963. %@AI@%#include %@AE@%<graph.h> %@NL@%
  19964. %@NL@%
  19965. main()%@NL@%
  19966. {%@NL@%
  19967.     void line (x1, y1, x2, y2, color);%@NL@%
  19968. %@NL@%
  19969.     int x1, x2, y1, y2, i, color;%@NL@%
  19970. %@NL@%
  19971.     _setvideomode(_MRES16COLOR); %@AB@%/* 320 x 200  16 col. */%@AE@%%@NL@%
  19972.     srand(2);                    %@AB@%/* new random seed    */%@AE@%%@NL@%
  19973.     for (i = 0; i < 100; i++)%@NL@%
  19974.         {%@NL@%
  19975.         x1 = rand() % 319;       %@AB@%/* random coordinates */%@AE@%%@NL@%
  19976.         x2 = rand() % 319;%@NL@%
  19977.         y1 = rand() % 199;%@NL@%
  19978.         y2 = rand() % 199;%@NL@%
  19979.         color = (rand() % 14) + 1; %@AB@%/* random color 1-15 */%@AE@%%@NL@%
  19980.         line(x1, y1, x2, y2, color); %@AB@%/* draw a line   */%@AE@%%@NL@%
  19981.         }%@NL@%
  19982.     while(!kbhit() ); %@AB@%/* wait for key to be hit */%@AE@%%@NL@%
  19983. %@NL@%
  19984.     _setvideomode(_DEFAULTMODE); %@AB@%/* restore video mode */%@AE@%%@NL@%
  19985. }%@NL@%
  19986. %@NL@%
  19987. void line (x1, y1, x2, y2, color)%@NL@%
  19988. int x1, y1, x2, y2, color;%@NL@%
  19989. {%@NL@%
  19990.     _moveto(x1, y1); %@AB@%/* position at first endpoint   */%@AE@%%@NL@%
  19991.     _setcolor(color);%@NL@%
  19992.     _lineto(x2, y2); %@AB@%/* draw line to second endpoint */%@AE@%%@NL@%
  19993. }%@NL@%
  19994. %@NL@%
  19995. %@NL@%
  19996. %@2@%%@AH@%LINES43.C%@AE@%%@EH@%%@NL@%
  19997. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\LINES43.C%@AE@%%@NL@%
  19998. %@NL@%
  19999. %@AB@%/* lines43.c -- leaves EGA in 43-line mode */%@AE@%%@NL@%
  20000. %@NL@%
  20001. %@AI@%#include %@AE@%<dos.h> %@NL@%
  20002. %@AI@%#include %@AE@%<conio.h> %@NL@%
  20003. %@AI@%#define %@AE@%VIDEO 0x10 %@NL@%
  20004. %@AI@%#define %@AE@%SETVMODE 0 %@NL@%
  20005. CHAR_GEN 0x11   %@AB@%/* an EGA BIOS function number */%@AE@%%@NL@%
  20006. %@AI@%#define %@AE@%ROM8X8   0x12 %@NL@%
  20007. %@AI@%#define %@AE@%BLOCK 0 %@NL@%
  20008. %@AI@%#define %@AE@%TEXTC80 3 %@NL@%
  20009. %@NL@%
  20010. main()%@NL@%
  20011. {%@NL@%
  20012.      union REGS reg;%@NL@%
  20013. %@NL@%
  20014.      reg.h.ah = SETVMODE;    %@AB@%/* set text mode */%@AE@%%@NL@%
  20015.      reg.h.al = TEXTC80;%@NL@%
  20016.      int86(VIDEO, ®, ®);%@NL@%
  20017. %@NL@%
  20018.      reg.h.ah = CHAR_GEN;   %@AB@%/* char generator routine */%@AE@%%@NL@%
  20019.      reg.h.al = ROM8X8;  %@AB@%/* use 8x8 ROM character box */%@AE@%%@NL@%
  20020.      reg.h.bl = BLOCK;   %@AB@%/* copy to block 0 */%@AE@%%@NL@%
  20021.      int86(VIDEO, ®, ®);%@NL@%
  20022. }%@NL@%
  20023. %@NL@%
  20024. %@NL@%
  20025. %@2@%%@AH@%LINE_CNT.C%@AE@%%@EH@%%@NL@%
  20026. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\LINE_CNT.C%@AE@%%@NL@%
  20027. %@NL@%
  20028. %@AB@%/*  line_cnt.c -- An overly active line counter       */%@AE@%%@NL@%
  20029. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  20030. main()%@NL@%
  20031. {%@NL@%
  20032.     int ch;%@NL@%
  20033.     int lines = 0;%@NL@%
  20034. %@NL@%
  20035.     while ( (ch = getchar() ) != EOF )%@NL@%
  20036.         if ( ch = '\n')%@NL@%
  20037.             lines++;%@NL@%
  20038.     printf("There were %d lines\n", lines);%@NL@%
  20039. }%@NL@%
  20040. %@NL@%
  20041. %@NL@%
  20042. %@NL@%
  20043. %@2@%%@AH@%LOCAL.C%@AE@%%@EH@%%@NL@%
  20044. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\LOCAL.C%@AE@%%@NL@%
  20045. %@NL@%
  20046. %@AB@%/* local.c -- local variables defined */%@AE@%%@NL@%
  20047. %@AB@%/*            within functions        */%@AE@%%@NL@%
  20048. %@NL@%
  20049. main()%@NL@%
  20050.     {%@NL@%
  20051.     int n = 12;%@NL@%
  20052.     int func1(), funct2();%@NL@%
  20053.     printf("n in main(): val %d ", n);%@NL@%
  20054.     printf("address %d\n", &n);%@NL@%
  20055. %@NL@%
  20056.     printf("Calling func1()\n");%@NL@%
  20057.     func1();%@NL@%
  20058.     printf("Calling func2()\n");%@NL@%
  20059.     func2();%@NL@%
  20060.     }%@NL@%
  20061. %@NL@%
  20062. int func1()%@NL@%
  20063.     {%@NL@%
  20064.     int n = 8; %@AB@%/* local variable */%@AE@%%@NL@%
  20065.     printf("n in func1(): val %d ", n);%@NL@%
  20066.     printf("address %d\n", &n);%@NL@%
  20067.     }%@NL@%
  20068. %@NL@%
  20069. int func2()%@NL@%
  20070.     {%@NL@%
  20071.     int n = 20; %@AB@%/* local variable */%@AE@%%@NL@%
  20072.     printf("n in func2(): val %d ", n);%@NL@%
  20073.     printf("address %d\n", &n);%@NL@%
  20074.     }%@NL@%
  20075. %@NL@%
  20076. %@NL@%
  20077. %@2@%%@AH@%M.C%@AE@%%@EH@%%@NL@%
  20078. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP05\M.C%@AE@%%@NL@%
  20079. %@NL@%
  20080. %@AB@%/* m.c -- draw a letter M        */%@AE@%%@NL@%
  20081. %@AB@%/*        using IF and CONTINUE  */%@AE@%%@NL@%
  20082. %@NL@%
  20083. %@AB@%/* define characters */%@AE@%%@NL@%
  20084. %@NL@%
  20085. CH 'M'    %@AB@%/* character to "draw" with */%@AE@%%@NL@%
  20086. %@AI@%#define %@AE@%BLANK ' ' %@NL@%
  20087. %@AI@%#define %@AE@%NL 10 %@NL@%
  20088. %@AI@%#define %@AE@%CR 13 %@NL@%
  20089. LEFT 20   %@AB@%/* left side of M   */%@AE@%%@NL@%
  20090. RIGHT 46  %@AB@%/* right side of M  */%@AE@%%@NL@%
  20091. BOTTOM 22 %@AB@%/* last line to use */%@AE@%%@NL@%
  20092. main()%@NL@%
  20093. {%@NL@%
  20094.     int pos, line;%@NL@%
  20095.     %@AB@%/* space to left side */%@AE@%%@NL@%
  20096.     for (line = 1; line <= BOTTOM; line++)%@NL@%
  20097.         {%@NL@%
  20098.         for (pos = 1; pos < LEFT; pos++)%@NL@%
  20099.             {%@NL@%
  20100.             putch(BLANK);%@NL@%
  20101.             }%@NL@%
  20102.         putch(CH); %@AB@%/* draw left side */%@AE@%%@NL@%
  20103. %@NL@%
  20104.         %@AB@%/* are we past midpoint? */%@AE@%%@NL@%
  20105.         if (line > ((RIGHT - LEFT) / 2))%@NL@%
  20106.             {%@NL@%
  20107.             %@AB@%/* yes, so just draw right side */%@AE@%%@NL@%
  20108.             for (pos = LEFT; pos < RIGHT; pos++)%@NL@%
  20109.                 {%@NL@%
  20110.                 putch(BLANK);%@NL@%
  20111.                 }%@NL@%
  20112.             putch(CH);%@NL@%
  20113.             putch(NL);%@NL@%
  20114.             putch(CR);%@NL@%
  20115.             continue; %@AB@%/* start loop over, do next line */%@AE@%%@NL@%
  20116.             }%@NL@%
  20117.             %@AB@%/* not past midpoint, check for interior */%@AE@%%@NL@%
  20118.         for (pos = LEFT; pos < RIGHT; pos++)%@NL@%
  20119.             {%@NL@%
  20120.             if ((pos == (LEFT + line )) ||%@NL@%
  20121.                  (pos == (RIGHT - line )))%@NL@%
  20122.                 putch(CH);%@NL@%
  20123.             else%@NL@%
  20124.                 putch(BLANK);%@NL@%
  20125.             }%@NL@%
  20126.        putch(CH);%@NL@%
  20127.        putch(NL);%@NL@%
  20128.        putch(CR); %@AB@%/* could also use printf("\n"); */%@AE@%%@NL@%
  20129.        }%@NL@%
  20130. }%@NL@%
  20131. %@NL@%
  20132. %@NL@%
  20133. %@2@%%@AH@%MAGIC.C%@AE@%%@EH@%%@NL@%
  20134. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\MAGIC.C%@AE@%%@NL@%
  20135. %@NL@%
  20136. %@AB@%/* magic.c  --  demonstrate use of a two-dimensional  */%@AE@%%@NL@%
  20137. %@AB@%/*              array of type int                     */%@AE@%%@NL@%
  20138. %@NL@%
  20139. main()%@NL@%
  20140. {%@NL@%
  20141.     static int square[3][3] = { 5, 8, 3, 4, 2, 0, 7, 1, 6 };%@NL@%
  20142.     int zrow = 1, zcol = 2;  %@AB@%/* location of the zero */%@AE@%%@NL@%
  20143.     int num, row, col, i , j, rowdist, coldist;%@NL@%
  20144. %@NL@%
  20145.     while (1)%@NL@%
  20146.         {%@NL@%
  20147.         printf("Swap what with zero?\n");%@NL@%
  20148.         printf("(Q to quit)\n");%@NL@%
  20149. %@NL@%
  20150.         %@AB@%/* Print the square. */%@AE@%%@NL@%
  20151.         for (i = 0; i < 3; ++i)%@NL@%
  20152.             {%@NL@%
  20153.             for (j = 0; j < 3; ++j)%@NL@%
  20154.                 {%@NL@%
  20155.                 printf(" %d ", square[i][j] );%@NL@%
  20156.                 }%@NL@%
  20157.             printf("\n");%@NL@%
  20158.             }%@NL@%
  20159. %@NL@%
  20160.         %@AB@%/* Input the user number. */%@AE@%%@NL@%
  20161.         if ((num = getch()) == 'Q')%@NL@%
  20162.             exit(0);%@NL@%
  20163.         num -= '0';%@NL@%
  20164.         if (num < 1 || num > 9)%@NL@%
  20165.             {%@NL@%
  20166.             printf("Not a legal number.\n\n");%@NL@%
  20167.             continue;%@NL@%
  20168.             }%@NL@%
  20169. %@NL@%
  20170.         %@AB@%/* Find that square. */%@AE@%%@NL@%
  20171.         for (row = 0; row < 3; ++row)%@NL@%
  20172.             {%@NL@%
  20173.             for(col = 0; col < 3; ++col)%@NL@%
  20174.                 {%@NL@%
  20175.                 if (num == square[row][col])%@NL@%
  20176.                     {%@NL@%
  20177.                     goto GOTIT;%@NL@%
  20178.                     }%@NL@%
  20179.                 }%@NL@%
  20180.             }%@NL@%
  20181. GOTIT:%@NL@%
  20182.         %@AB@%/* Check for a legal move. */%@AE@%%@NL@%
  20183.         if (row > 2 || col > 2)%@NL@%
  20184.             {%@NL@%
  20185.             printf("Bad Box Specification\n\n");%@NL@%
  20186.             continue;%@NL@%
  20187.             }%@NL@%
  20188.         rowdist = zrow - row;%@NL@%
  20189.         if (rowdist < 0) %@NL@%
  20190.             rowdist *= -1;%@NL@%
  20191.         coldist = zcol - col;%@NL@%
  20192.         if (coldist < 0) %@NL@%
  20193.             coldist *= -1;%@NL@%
  20194.         if (rowdist > 1 || coldist > 1)%@NL@%
  20195.             {%@NL@%
  20196.             printf("Not A Neighbor\n\n");%@NL@%
  20197.             continue;%@NL@%
  20198.             }%@NL@%
  20199. %@NL@%
  20200.         %@AB@%/* Make the move. */%@AE@%%@NL@%
  20201.         square[zrow][zcol] = square[row][col];%@NL@%
  20202.         square[row][col] = 0;%@NL@%
  20203.         zrow = row;%@NL@%
  20204.         zcol = col;%@NL@%
  20205. %@NL@%
  20206.         %@AB@%/* See if done, and solved. */%@AE@%%@NL@%
  20207.         for (i = 0; i < 3; ++i)%@NL@%
  20208.             {%@NL@%
  20209.             for (j = 0; j < 3; ++j)%@NL@%
  20210.                 {%@NL@%
  20211.                 if (square[i][j] != ((i * 3) + j))%@NL@%
  20212.                     {%@NL@%
  20213.                     break;%@NL@%
  20214.                     }%@NL@%
  20215.                 }%@NL@%
  20216.             }%@NL@%
  20217.         if ((i * j) == 9)%@NL@%
  20218.             break;%@NL@%
  20219.         }%@NL@%
  20220.     printf("\n\aYOU GOT IT !!!\n");%@NL@%
  20221. }%@NL@%
  20222. %@NL@%
  20223. %@NL@%
  20224. %@2@%%@AH@%MASKS.C%@AE@%%@EH@%%@NL@%
  20225. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\MASKS.C%@AE@%%@NL@%
  20226. %@NL@%
  20227. %@AB@%/*  masks.c -- illustrates _setfillmask() and         */%@AE@%%@NL@%
  20228. %@AB@%/*             _floodfill()                           */%@AE@%%@NL@%
  20229. %@AB@%/* Program list: masks.c                              */%@AE@%%@NL@%
  20230. %@AB@%/* If you load graphics.qlb, no program list is needed*/%@AE@%%@NL@%
  20231. %@NL@%
  20232. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  20233. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  20234. %@AI@%#include %@AE@%<conio.h> %@NL@%
  20235. %@AI@%#include %@AE@%<graph.h> %@NL@%
  20236. unsigned char Inversemask[8];%@NL@%
  20237. unsigned char Masks[3][8] = {%@NL@%
  20238.             {0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00},%@NL@%
  20239.             {0xff,0x80,0x80,0x80,0xff,0x08,0x08,0x08},%@NL@%
  20240.             {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa}};%@NL@%
  20241. main(argc, argv)%@NL@%
  20242. int argc;%@NL@%
  20243. char *argv[];%@NL@%
  20244. {%@NL@%
  20245.     struct videoconfig vc;%@NL@%
  20246.     int mode = _MRES4COLOR;%@NL@%
  20247.     short xc, yc;%@NL@%
  20248.     short box, i;%@NL@%
  20249. %@NL@%
  20250.     if (argc > 1)%@NL@%
  20251.         mode = atoi(argv[1]);%@NL@%
  20252.     if (_setvideomode(mode) == 0)%@NL@%
  20253.     {%@NL@%
  20254.         fprintf(stderr,"Can't set mode %d\n", mode);%@NL@%
  20255.         exit(1);%@NL@%
  20256.     }%@NL@%
  20257.     _getvideoconfig(&vc);%@NL@%
  20258.     xc = vc.numxpixels / 2;%@NL@%
  20259.     yc = vc.numypixels / 2;%@NL@%
  20260.     for (i = 0; i < 8; i++)%@NL@%
  20261.         Inversemask[i] = ~Masks[1][i];%@NL@%
  20262.     _setlogorg(xc, yc);%@NL@%
  20263.     _selectpalette(0);%@NL@%
  20264.     _setcolor(1);%@NL@%
  20265.     _rectangle(_GBORDER, -xc + 1, -yc + 1, xc - 1, yc - 1);%@NL@%
  20266.     _moveto(-xc + 1, -yc / 3);%@NL@%
  20267.     _lineto(xc -1, -yc / 3);%@NL@%
  20268.     _moveto(-xc + 1, yc / 3);%@NL@%
  20269.     _lineto(xc -1, yc / 3);%@NL@%
  20270.     for (box = 0; box < 3; box++)%@NL@%
  20271.     {%@NL@%
  20272.          _setcolor(box + 1);%@NL@%
  20273.          _setfillmask(Masks[box]);%@NL@%
  20274.          _floodfill(0, (box - 1) * yc / 2, 1);%@NL@%
  20275.     }%@NL@%
  20276.     _settextposition(5, 10);%@NL@%
  20277.     _outtext("Press a key to continue");%@NL@%
  20278.     getch();%@NL@%
  20279.     _setcolor(3);%@NL@%
  20280.     _setfillmask(Inversemask);%@NL@%
  20281.     _floodfill (0, 0, 1);%@NL@%
  20282.     _setcolor(2);%@NL@%
  20283.     _setfillmask(Masks[0]);%@NL@%
  20284.     _floodfill(0, yc / 2, 1);%@NL@%
  20285.     _settextposition(5, 10);%@NL@%
  20286.     _outtext("Press a key to terminate");%@NL@%
  20287.     getch();%@NL@%
  20288.     _setvideomode(_DEFAULTMODE);%@NL@%
  20289. }%@NL@%
  20290. %@NL@%
  20291. %@NL@%
  20292. %@2@%%@AH@%MATH.C%@AE@%%@EH@%%@NL@%
  20293. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\MATH.C%@AE@%%@NL@%
  20294. %@NL@%
  20295. %@AB@%/* math.c -- shows arithmetic and       */%@AE@%%@NL@%
  20296. %@AB@%/*           precedence via expressions */%@AE@%%@NL@%
  20297. %@NL@%
  20298. main()%@NL@%
  20299. {%@NL@%
  20300.     int a = 10, b = 4, c = 2;%@NL@%
  20301. %@NL@%
  20302.     %@AB@%/* simple arithmetic expressions */%@AE@%%@NL@%
  20303.     printf("99 + 2 = %d\n", 99 + 2);  %@AB@%/* ints */%@AE@%%@NL@%
  20304.     printf("5 - 12 = %d\n", 5 - 12);%@NL@%
  20305.     printf("7.25 + 3.5 = %f\n", 7.25 + 3.5);%@NL@%
  20306.                                         %@AB@%/* floats */%@AE@%%@NL@%
  20307. %@NL@%
  20308.     %@AB@%/* compare presedence on these */%@AE@%%@NL@%
  20309.     printf("20 * 20 + 40 = %d\n", 20 * 20 + 40);%@NL@%
  20310.     printf("20 * (20 + 40) = %d\n", 20 * (20 + 40));%@NL@%
  20311.     printf("a * a - c + b = %d\n", a * a - c + b);%@NL@%
  20312.     printf("a * (a - (c + b)) = %d\n",%@NL@%
  20313.             a * (a - (c + b)));%@NL@%
  20314. %@NL@%
  20315.     %@AB@%/* compare integer and float division */%@AE@%%@NL@%
  20316. %@NL@%
  20317.     printf("Integers: 5 / 2 = %d\n", 5 / 2);%@NL@%
  20318.     printf("Floats: 5.0 / 2.0 = %f\n", 5.0 / 2.0);%@NL@%
  20319. }%@NL@%
  20320. %@NL@%
  20321. %@NL@%
  20322. %@2@%%@AH@%MATHTEST.C%@AE@%%@EH@%%@NL@%
  20323. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\MATHTEST.C%@AE@%%@NL@%
  20324. %@NL@%
  20325. %@AB@%/* mathtest.c -- driver for do_math()                 */%@AE@%%@NL@%
  20326. %@AB@%/* Program list: mathtest.c (to link math functions)  */%@AE@%%@NL@%
  20327. %@NL@%
  20328. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  20329. double do_math(double);%@NL@%
  20330. main()%@NL@%
  20331. {%@NL@%
  20332.     double input, result;%@NL@%
  20333. %@NL@%
  20334.     printf("Enter a number: ");%@NL@%
  20335.     while (scanf("%lf", &input) == 1)%@NL@%
  20336.         {%@NL@%
  20337.         result = do_math(input);%@NL@%
  20338.         printf("input = %.2e, result = %.2e\n", input,%@NL@%
  20339.                 result);%@NL@%
  20340.         printf("Next number (q to quit): ");%@NL@%
  20341.         }%@NL@%
  20342. }%@NL@%
  20343. %@NL@%
  20344. %@AI@%#include %@AE@%<math.h> %@NL@%
  20345. double do_math(x)%@NL@%
  20346. double x;%@NL@%
  20347. {%@NL@%
  20348.     return (sin (x) * exp (-x));%@NL@%
  20349. }%@NL@%
  20350. %@NL@%
  20351. %@NL@%
  20352. %@NL@%
  20353. %@2@%%@AH@%MENU.C%@AE@%%@EH@%%@NL@%
  20354. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\MENU.C%@AE@%%@NL@%
  20355. %@NL@%
  20356. %@AB@%/*  menu.c -- uses ansi.sys for cursor control and   */%@AE@%%@NL@%
  20357. %@AB@%/*            for video reverse in a sample menu     */%@AE@%%@NL@%
  20358. %@AB@%/*  Note: Requires that ANSI.SYS be installed.       */%@AE@%%@NL@%
  20359. %@NL@%
  20360. %@AI@%#include %@AE@%<conio.h> %@NL@%
  20361. ITEMS 5           %@AB@%/* number of menu items */%@AE@%%@NL@%
  20362. UP 72             %@AB@%/* scan code for up arrow */%@AE@%%@NL@%
  20363. DOWN 80           %@AB@%/* scan code for down arrow */%@AE@%%@NL@%
  20364. VIDREV "\033[7m"  %@AB@%/* reverse video attribute */%@AE@%%@NL@%
  20365. ATTOFF "\033[0m"  %@AB@%/* turn attributes off   */%@AE@%%@NL@%
  20366. ED()   printf("\033[2J")  %@AB@%/* erase display */%@AE@%%@NL@%
  20367. HOME() printf("\033[H")  %@AB@%/* home the cursor */%@AE@%%@NL@%
  20368. CUU(Y) printf("\033[%dA",Y);  %@AB@%/* cursor up */%@AE@%%@NL@%
  20369. CUD(Y) printf("\033[%dB",Y);  %@AB@%/* cursor down */%@AE@%%@NL@%
  20370. %@NL@%
  20371. char *Menu[ITEMS] = {"Add a number to the list",%@NL@%
  20372.                      "Delete a number from the list",%@NL@%
  20373.                      "Clear the list",%@NL@%
  20374.                      "Sum the list",%@NL@%
  20375.                      "Quit"};%@NL@%
  20376. char *Heading =%@NL@%
  20377. "Use arrow keys to highlight choice. "%@NL@%
  20378. "Use Enter key to select choice.";%@NL@%
  20379. %@NL@%
  20380. void showmenu(int);%@NL@%
  20381. int getmesg(int);%@NL@%
  20382. main()%@NL@%
  20383. {%@NL@%
  20384.     int messno = 0; %@AB@%/* message to be highlighted */%@AE@%%@NL@%
  20385.     ED();%@NL@%
  20386.     showmenu(messno);%@NL@%
  20387.     while (messno != ITEMS - 1)%@NL@%
  20388.         {%@NL@%
  20389.         messno = getmesg(messno);%@NL@%
  20390.         ED();%@NL@%
  20391.         switch (messno)%@NL@%
  20392.            {%@NL@%
  20393.            case 0 :%@NL@%
  20394.            case 1 :%@NL@%
  20395.            case 2 :%@NL@%
  20396.            case 3 : printf("...pretending to work ...");%@NL@%
  20397.                     printf("Hit any key to continue\n");%@NL@%
  20398.                     getch();%@NL@%
  20399.                     ED();%@NL@%
  20400.                     showmenu(messno);%@NL@%
  20401.                     break;%@NL@%
  20402.            case 4 : printf("Quitting!\n");%@NL@%
  20403.                     break;%@NL@%
  20404.            default: printf("Programming error!\n");%@NL@%
  20405.                     break;%@NL@%
  20406.            }%@NL@%
  20407.         }%@NL@%
  20408. }%@NL@%
  20409. %@NL@%
  20410. %@AB@%/* showmenu() displays the menu  */%@AE@%%@NL@%
  20411. void showmenu(highlite)%@NL@%
  20412. int highlite;   %@AB@%/* message number to be highlighted */%@AE@%%@NL@%
  20413. {%@NL@%
  20414.     int n;%@NL@%
  20415.     char *start;%@NL@%
  20416. %@NL@%
  20417.     HOME();%@NL@%
  20418.     printf("%s", Heading);%@NL@%
  20419.     for (n = 0; n < ITEMS; n++)%@NL@%
  20420.     {%@NL@%
  20421.         if (n == highlite)%@NL@%
  20422.             start = VIDREV; %@AB@%/* turn on reverse video */%@AE@%%@NL@%
  20423.         else%@NL@%
  20424.             start = ATTOFF;%@NL@%
  20425.         printf("\n\n%s%s%s", start, Menu[n], ATTOFF);%@NL@%
  20426.     }%@NL@%
  20427.     HOME();%@NL@%
  20428.     CUD(2 + 2 * highlite);%@NL@%
  20429. }%@NL@%
  20430. %@NL@%
  20431. %@AB@%/*  getmesg() selects a menu item */%@AE@%%@NL@%
  20432. int getmesg(mnum)%@NL@%
  20433. int mnum; %@AB@%/* current message number */%@AE@%%@NL@%
  20434. {%@NL@%
  20435.     char ch;%@NL@%
  20436. %@NL@%
  20437.     while ((ch = getch()) != '\r')%@NL@%
  20438.         if (ch == 0)%@NL@%
  20439.             {%@NL@%
  20440.             ch = getch();%@NL@%
  20441.             switch (ch)%@NL@%
  20442.                 {%@NL@%
  20443.                 case UP   : if (mnum > 0)%@NL@%
  20444.                                 {%@NL@%
  20445.                                 CUU(2);%@NL@%
  20446.                                 showmenu (--mnum);%@NL@%
  20447.                                 }%@NL@%
  20448.                             else%@NL@%
  20449.                                 {%@NL@%
  20450.                                 CUD(2 * ITEMS - 2);%@NL@%
  20451.                                 showmenu(mnum=ITEMS-1);%@NL@%
  20452.                                 }%@NL@%
  20453.                             break;%@NL@%
  20454.                 case DOWN : if (mnum < ITEMS - 1)%@NL@%
  20455.                                 {%@NL@%
  20456.                                 CUD(2);%@NL@%
  20457.                                 showmenu(++mnum);%@NL@%
  20458.                                 }%@NL@%
  20459.                             else%@NL@%
  20460.                                 {%@NL@%
  20461.                                 CUU(2 * ITEMS - 2);%@NL@%
  20462.                                 showmenu(mnum = 0);%@NL@%
  20463.                                 }%@NL@%
  20464.                             break;%@NL@%
  20465.                 }%@NL@%
  20466.             }%@NL@%
  20467.         return mnum;%@NL@%
  20468. }%@NL@%
  20469. %@NL@%
  20470. %@NL@%
  20471. %@2@%%@AH@%MIDSTR.C%@AE@%%@EH@%%@NL@%
  20472. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\MIDSTR.C%@AE@%%@NL@%
  20473. %@NL@%
  20474. %@AB@%/* midstr.c -- a C version of BASIC's MID$  */%@AE@%%@NL@%
  20475. %@NL@%
  20476. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  20477. %@NL@%
  20478. char *Midstr(char *str, int where, int cnt)%@NL@%
  20479. {%@NL@%
  20480.     static char *cp = NULL;%@NL@%
  20481.     char *malloc();%@NL@%
  20482. %@NL@%
  20483.     if (cnt > strlen(str + where))%@NL@%
  20484.         cnt = strlen(str + where);%@NL@%
  20485.     if (cp != NULL)%@NL@%
  20486.         free(cp);%@NL@%
  20487.     if ((cp = malloc(cnt + 1)) == NULL)%@NL@%
  20488.         return (NULL);%@NL@%
  20489.     strncpy(cp, str+where, cnt);%@NL@%
  20490.     return (cp);%@NL@%
  20491. }%@NL@%
  20492. %@NL@%
  20493. %@NL@%
  20494. %@NL@%
  20495. %@2@%%@AH@%MISIDENT.C%@AE@%%@EH@%%@NL@%
  20496. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\MISIDENT.C%@AE@%%@NL@%
  20497. %@NL@%
  20498. %@AB@%/* misident.c -- careless typing              */%@AE@%%@NL@%
  20499. %@NL@%
  20500. %@AI@%#defne %@AE@%BIG 3 %@NL@%
  20501. main() %@NL@%
  20502. { %@NL@%
  20503.     char ltr; %@NL@%
  20504.     integer num; %@NL@%
  20505.  %@NL@%
  20506.     num = 2 + BIG; %@NL@%
  20507.     lrt = 'a';  %@NL@%
  20508.     printf("%c %d\n", ltr, num); %@NL@%
  20509. } %@NL@%
  20510. %@NL@%
  20511. %@NL@%
  20512. %@NL@%
  20513. %@2@%%@AH@%MIXED.C%@AE@%%@EH@%%@NL@%
  20514. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\MIXED.C%@AE@%%@NL@%
  20515. %@NL@%
  20516. %@AB@%/* mixed.c -- shows the effects of mixing */%@AE@%%@NL@%
  20517. %@AB@%/*            data types in an expression */%@AE@%%@NL@%
  20518. %@NL@%
  20519. main()%@NL@%
  20520. {%@NL@%
  20521.     int     i = 50, iresult;%@NL@%
  20522.     long    l = 1000000, lresult;%@NL@%
  20523.     float   f = 10.5, fresult;%@NL@%
  20524.     double  d = 1000.005, dresult;%@NL@%
  20525. %@NL@%
  20526.     fresult = i + f;         %@AB@%/* int + float to float */%@AE@%%@NL@%
  20527.     printf("%f\n", fresult);%@NL@%
  20528. %@NL@%
  20529.     fresult = i * f;         %@AB@%/* int * float to float */%@AE@%%@NL@%
  20530.     printf("%f\n", fresult);%@NL@%
  20531. %@NL@%
  20532.     lresult = l + f;         %@AB@%/* long + int to long */%@AE@%%@NL@%
  20533.     printf("%f\n", lresult);%@NL@%
  20534. %@NL@%
  20535.     printf("%f\n", d * f);   %@AB@%/* double * float */%@AE@%%@NL@%
  20536.                                  %@AB@%/* to double */%@AE@%%@NL@%
  20537.     fresult = d * f;         %@AB@%/* assigned to a float */%@AE@%%@NL@%
  20538.     printf("%f\n", fresult); %@AB@%/* loses some precision */%@AE@%%@NL@%
  20539. %@NL@%
  20540.     %@AB@%/* debugging a division problem */%@AE@%%@NL@%
  20541. %@NL@%
  20542.     iresult = i / l;          %@AB@%/* int / long to int */%@AE@%%@NL@%
  20543.     printf("%d\n", iresult);  %@AB@%/* whoops! loses result */%@AE@%%@NL@%
  20544.     printf("%ld\n", iresult); %@AB@%/* this won't fix it */%@AE@%%@NL@%
  20545.     fresult = i / l;          %@AB@%/* store in float result */%@AE@%%@NL@%
  20546.     printf("%f\n", fresult);  %@AB@%/* doesn't work */%@AE@%%@NL@%
  20547.     dresult = i / l;          %@AB@%/* try a double */%@AE@%%@NL@%
  20548.     printf("%f\n", dresult);  %@AB@%/* doesn't work */%@AE@%%@NL@%
  20549.     fresult = (float) i / l;  %@AB@%/* try type cast */%@AE@%%@NL@%
  20550.     printf("%f\n", fresult);  %@AB@%/* correct result */%@AE@%%@NL@%
  20551. }%@NL@%
  20552. %@NL@%
  20553. %@NL@%
  20554. %@2@%%@AH@%MIXLOOPS.C%@AE@%%@EH@%%@NL@%
  20555. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\MIXLOOPS.C%@AE@%%@NL@%
  20556. %@NL@%
  20557. %@NL@%
  20558. %@AB@%/* MIXLOOPS.C -- reads characters,       */%@AE@%%@NL@%
  20559. %@AB@%/*               beeps for ASCII count   */%@AE@%%@NL@%
  20560. %@AB@%/*               uses a while and a for  */%@AE@%%@NL@%
  20561. %@NL@%
  20562. %@AI@%#include %@AE@%<conio.h> %@NL@%
  20563. %@NL@%
  20564. main()%@NL@%
  20565. {%@NL@%
  20566.     char ch;%@NL@%
  20567.     int  i;%@NL@%
  20568. %@NL@%
  20569.     while ((ch = getche()) != ' ')  %@AB@%/* get a char. */%@AE@%%@NL@%
  20570.         {%@NL@%
  20571.         for (i = 'a'; i <= ch; ++i) %@AB@%/* count up to */%@AE@%%@NL@%
  20572.             {                       %@AB@%/* alphabet pos.*/%@AE@%%@NL@%
  20573.             printf("In FOR loop!\n");%@NL@%
  20574.             printf("\a");  %@AB@%/* sound beep each time */%@AE@%%@NL@%
  20575.             }%@NL@%
  20576.         }%@NL@%
  20577. }%@NL@%
  20578. %@NL@%
  20579. %@NL@%
  20580. %@NL@%
  20581. %@2@%%@AH@%MIXTYPES.C%@AE@%%@EH@%%@NL@%
  20582. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\MIXTYPES.C%@AE@%%@NL@%
  20583. %@NL@%
  20584. %@AB@%/* mixtypes.c -- shows problem with calling           */%@AE@%%@NL@%
  20585. %@AB@%/*               a function with wrong type parameter */%@AE@%%@NL@%
  20586. %@NL@%
  20587. main()%@NL@%
  20588. {%@NL@%
  20589.     %@AB@%/* didn't bother to declare int function */%@AE@%%@NL@%
  20590.     float n = 5.0;%@NL@%
  20591.     int i;%@NL@%
  20592. %@NL@%
  20593.     printf("n in main() is %f\n", n);%@NL@%
  20594.     i = examine(n);    %@AB@%/* pass float to function */%@AE@%%@NL@%
  20595.     printf("examine() returned n as %d\n", i);%@NL@%
  20596. }%@NL@%
  20597. %@NL@%
  20598. examine(num)  %@AB@%/* function didn't declare return type */%@AE@%%@NL@%
  20599. {%@NL@%
  20600.     printf("examine() says n is %d\n", num);%@NL@%
  20601.     return(num);%@NL@%
  20602. }%@NL@%
  20603. %@NL@%
  20604. %@NL@%
  20605. %@NL@%
  20606. %@2@%%@AH@%MODEINFO.C%@AE@%%@EH@%%@NL@%
  20607. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\MODEINFO.C%@AE@%%@NL@%
  20608. %@NL@%
  20609. %@AB@%/* modeinfo.c -- set modes and obtain information      */%@AE@%%@NL@%
  20610. %@AB@%/* Demonstrates _setvideomode() and _getvideoconfig()  */%@AE@%%@NL@%
  20611. %@AB@%/* If you load graphics.qlb, no program list is needed.*/%@AE@%%@NL@%
  20612. %@NL@%
  20613. %@AI@%#include %@AE@%<conio.h> %@NL@%
  20614. %@AI@%#include %@AE@%<graph.h> %@NL@%
  20615. struct videoconfig vc;%@NL@%
  20616. int modes[15] ={_TEXTBW40, _TEXTC40, _TEXTBW80, _TEXTC80,%@NL@%
  20617.         _MRES4COLOR, _MRESNOCOLOR, _HRESBW, _TEXTMONO,%@NL@%
  20618.         _MRES16COLOR, _HRES16COLOR, _ERESNOCOLOR, _ERESCOLOR,%@NL@%
  20619.         _VRES2COLOR, _VRES16COLOR, _MRES256COLOR};%@NL@%
  20620. char *Adapt(short), *Display(short);%@NL@%
  20621. main()%@NL@%
  20622. {%@NL@%
  20623.     int i;%@NL@%
  20624. %@NL@%
  20625.     for (i = 0; i < 15; i++)%@NL@%
  20626.     {%@NL@%
  20627.         if (_setvideomode(modes[i]))%@NL@%
  20628.         {%@NL@%
  20629.             _getvideoconfig(&vc);%@NL@%
  20630.             printf("video mode is %d\n", vc.mode);%@NL@%
  20631.             printf("number of columns is %d\n", vc.numtextcols);%@NL@%
  20632.             printf("number of colors is %d\n", vc.numcolors);%@NL@%
  20633.             printf("number of pages is %d\n", vc.numvideopages);%@NL@%
  20634.             printf("adapter is %s\n", Adapt(vc.adapter));%@NL@%
  20635.             printf("display is %s\n", Display(vc.monitor));%@NL@%
  20636.             printf("the adapter has %dK of memory\n",%@NL@%
  20637.                     vc.memory);%@NL@%
  20638.         }%@NL@%
  20639.         else%@NL@%
  20640.             printf("mode %d not supported\n", modes[i]);%@NL@%
  20641.         printf("strike a key for next mode\n");%@NL@%
  20642.         getch();%@NL@%
  20643.     }%@NL@%
  20644.     _setvideomode (_DEFAULTMODE);%@NL@%
  20645. }%@NL@%
  20646. %@NL@%
  20647. %@AB@%/* Adapt() returns a pointer to a string describing   */%@AE@%%@NL@%
  20648. %@AB@%/* the adapter characterized by adapt_num.            */%@AE@%%@NL@%
  20649. char *Adapt(adapt_num)%@NL@%
  20650. short adapt_num; %@AB@%/* videoconfig.adapter value         */%@AE@%%@NL@%
  20651. {%@NL@%
  20652.     static char *anames[6] = {"Monochrome", "CGA", "EGA",%@NL@%
  20653.                               "MCGA", "VGA", "Not known"};%@NL@%
  20654.     char *point;%@NL@%
  20655. %@NL@%
  20656.     switch (adapt_num)%@NL@%
  20657.     {%@NL@%
  20658.         case _MDPA : point = anames[0];%@NL@%
  20659.                      break;%@NL@%
  20660.         case _CGA  : point = anames[1];%@NL@%
  20661.                      break;%@NL@%
  20662.         case _EGA  : point = anames[2];%@NL@%
  20663.                      break;%@NL@%
  20664.         case _MCGA : point = anames[3];%@NL@%
  20665.                      break;%@NL@%
  20666.         case _VGA  : point = anames[4];%@NL@%
  20667.                      break;%@NL@%
  20668.         default    : point = anames[5];%@NL@%
  20669.     }%@NL@%
  20670.     return point;%@NL@%
  20671. }%@NL@%
  20672. %@NL@%
  20673. %@AB@%/* Display() returns a pointer to a string describing  */%@AE@%%@NL@%
  20674. %@AB@%/* the monitor characterized by disp.                  */%@AE@%%@NL@%
  20675. char *Display(disp)%@NL@%
  20676. short disp;  %@AB@%/* videoconfig.monitor value              */%@AE@%%@NL@%
  20677. {%@NL@%
  20678.     static char *types[5] = {"monochrome", "color",%@NL@%
  20679.                              "enhanced color", "analog",%@NL@%
  20680.                              "unknown"};%@NL@%
  20681.     char *point;%@NL@%
  20682. %@NL@%
  20683.     if (disp & _MONO)%@NL@%
  20684.         point = types[0];%@NL@%
  20685.     else if (disp & _COLOR)%@NL@%
  20686.         point = types[1];%@NL@%
  20687.     else if (disp & _ENHCOLOR)%@NL@%
  20688.         point = types[2];%@NL@%
  20689.     else if (disp & _ANALOG)%@NL@%
  20690.         point = types[3];%@NL@%
  20691.     else%@NL@%
  20692.         point = types[4];%@NL@%
  20693.     return point;%@NL@%
  20694. }%@NL@%
  20695. %@NL@%
  20696. %@NL@%
  20697. %@2@%%@AH@%MOIRE.C%@AE@%%@EH@%%@NL@%
  20698. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\MOIRE.C%@AE@%%@NL@%
  20699. %@NL@%
  20700. %@AB@%/*   moire.c -- variation on dots.c                   */%@AE@%%@NL@%
  20701. %@AB@%/* If you load graphics.qlb, no program list is needed*/%@AE@%%@NL@%
  20702. %@AI@%#include %@AE@%<conio.h> %@NL@%
  20703. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  20704. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  20705. %@AI@%#include %@AE@%<graph.h> %@NL@%
  20706. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  20707. BKCOLS 8      %@AB@%/* number of background colors */%@AE@%%@NL@%
  20708. PALNUM 4      %@AB@%/* number of palettes */%@AE@%%@NL@%
  20709. long Bkcolors[BKCOLS] = {_BLACK, _BLUE, _GREEN, _CYAN, _RED,%@NL@%
  20710.                          _MAGENTA, _BROWN, _WHITE};%@NL@%
  20711. main (argc, argv)%@NL@%
  20712. int argc;%@NL@%
  20713. char *argv[];%@NL@%
  20714. {%@NL@%
  20715.      struct videoconfig vc;%@NL@%
  20716.      unsigned int col, row;%@NL@%
  20717.      short color = 0;%@NL@%
  20718.      int bkc_index = 1;  %@AB@%/* blue background */%@AE@%%@NL@%
  20719.      short palette = 0;  %@AB@%/* red, green, brown */%@AE@%%@NL@%
  20720.      int firstcol, firstrow, lastrow, lastcol;%@NL@%
  20721.      int mode = _MRES4COLOR;%@NL@%
  20722.      int ch;%@NL@%
  20723. %@NL@%
  20724.      if (argc > 1)%@NL@%
  20725.           mode = atoi(argv[1]);%@NL@%
  20726. %@NL@%
  20727.      if (_setvideomode(mode) == 0)%@NL@%
  20728.           {%@NL@%
  20729.           printf("Can't do that mode.\n");%@NL@%
  20730.           exit(1);%@NL@%
  20731.           }%@NL@%
  20732.      _getvideoconfig(&vc);%@NL@%
  20733.      firstcol = vc.numxpixels / 5;%@NL@%
  20734.      firstrow = vc.numypixels / 5;%@NL@%
  20735.      lastcol = 4 * vc.numxpixels / 5;%@NL@%
  20736.      lastrow = 4 * vc.numypixels / 5;%@NL@%
  20737.      _selectpalette(palette);%@NL@%
  20738.      _setbkcolor (Bkcolors[bkc_index]);%@NL@%
  20739.      for (col = firstcol; col <= lastcol; ++col)%@NL@%
  20740.           {%@NL@%
  20741. %@NL@%
  20742.           for (row = firstrow; row <= lastrow; ++row)%@NL@%
  20743.                {%@NL@%
  20744.                _setcolor(((row*row + col*col)/10)%vc.numcolors);%@NL@%
  20745.                _setpixel(col, row);%@NL@%
  20746.                }%@NL@%
  20747.           }%@NL@%
  20748.      while ((ch = getch()) != ESC)%@NL@%
  20749.           {%@NL@%
  20750.           if (ch == 'p')%@NL@%
  20751.                 _selectpalette(++palette % PALNUM);%@NL@%
  20752.           else if (ch == 'b')%@NL@%
  20753.                 _setbkcolor(Bkcolors[++bkc_index % BKCOLS]);%@NL@%
  20754.           }%@NL@%
  20755.      _setvideomode(_DEFAULTMODE);  %@AB@%/* reset orig. mode */%@AE@%%@NL@%
  20756. }%@NL@%
  20757. %@NL@%
  20758. %@NL@%
  20759. %@2@%%@AH@%NARROW.C%@AE@%%@EH@%%@NL@%
  20760. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\NARROW.C%@AE@%%@NL@%
  20761. %@NL@%
  20762. %@AB@%/* narrow.c -- a choppy C program */%@AE@%%@NL@%
  20763. %@NL@%
  20764. main( )%@NL@%
  20765. {%@NL@%
  20766. printf%@NL@%
  20767. ("Hello, and welcome to QuickC!\n");%@NL@%
  20768. }%@NL@%
  20769. %@NL@%
  20770. %@NL@%
  20771. %@2@%%@AH@%ONELINE.C%@AE@%%@EH@%%@NL@%
  20772. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\ONELINE.C%@AE@%%@NL@%
  20773. %@NL@%
  20774. %@AB@%/* oneline.c -- shows how printf() continues  */%@AE@%%@NL@%
  20775. %@AB@%/*              on the same line              */%@AE@%%@NL@%
  20776. %@NL@%
  20777. main()%@NL@%
  20778. {%@NL@%
  20779.     printf("All displayed on");%@NL@%
  20780.     printf("the same line, with no space");%@NL@%
  20781.     printf(" unless specified.");%@NL@%
  20782.             %@AB@%/* note added space in line above */%@AE@%%@NL@%
  20783. }%@NL@%
  20784. %@NL@%
  20785. %@NL@%
  20786. %@2@%%@AH@%OPEQUAL.C%@AE@%%@EH@%%@NL@%
  20787. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\OPEQUAL.C%@AE@%%@NL@%
  20788. %@NL@%
  20789. %@AB@%/* opequal.c -- shows combination math/assignment */%@AE@%%@NL@%
  20790. %@AB@%/*              operators and increment operators */%@AE@%%@NL@%
  20791. %@NL@%
  20792. main()%@NL@%
  20793. {%@NL@%
  20794.     int m = 10, n = 5;%@NL@%
  20795.     printf("Starting values: m = %d n = %d\n",%@NL@%
  20796.             m, n);%@NL@%
  20797. %@NL@%
  20798.     %@AB@%/* combination of arithmetic and assignment */%@AE@%%@NL@%
  20799.     printf("m += 2 makes m %d\n", m += 2);%@NL@%
  20800.     printf("m -= n makes m %d\n", m -= n);%@NL@%
  20801.     printf("m *= 2 makes m %d\n", m *= 2);%@NL@%
  20802. %@NL@%
  20803.     %@AB@%/* two ways to increment m */%@AE@%%@NL@%
  20804.     printf("m = m + 1 makes m %d\n",%@NL@%
  20805.             m = m + 1);%@NL@%
  20806.     printf("m += 1 makes m %d\n",%@NL@%
  20807.             m += 1);%@NL@%
  20808. }%@NL@%
  20809. %@NL@%
  20810. %@NL@%
  20811. %@2@%%@AH@%PACK.C%@AE@%%@EH@%%@NL@%
  20812. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\PACK.C%@AE@%%@NL@%
  20813. %@NL@%
  20814. %@AB@%/* pack.c  --  demonstrates structure packing with */%@AE@%%@NL@%
  20815. %@AB@%/*             the #pragma pack() directive        */%@AE@%%@NL@%
  20816. %@NL@%
  20817. pack(4)        %@AB@%/* 1, 2 or 4 */%@AE@%%@NL@%
  20818. %@NL@%
  20819. main()%@NL@%
  20820. {%@NL@%
  20821.     struct {%@NL@%
  20822.         char ch1;%@NL@%
  20823.         int  int1;%@NL@%
  20824.         char ch2;%@NL@%
  20825.         long int2;%@NL@%
  20826.     } s;%@NL@%
  20827. %@NL@%
  20828.     printf("ch1  -> %lu\n", (unsigned long)(&s.ch1));%@NL@%
  20829.     printf("int1 -> %lu\n", (unsigned long)(&s.int1));%@NL@%
  20830.     printf("ch2  -> %lu\n", (unsigned long)(&s.ch2));%@NL@%
  20831.     printf("int2 -> %lu\n", (unsigned long)(&s.int2));%@NL@%
  20832. }%@NL@%
  20833. %@NL@%
  20834. %@NL@%
  20835. %@2@%%@AH@%PASSWORD.C%@AE@%%@EH@%%@NL@%
  20836. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\PASSWORD.C%@AE@%%@NL@%
  20837. %@NL@%
  20838. %@AB@%/*  password.c -- requires a password to complete the   */%@AE@%%@NL@%
  20839. %@AB@%/*                program; illustrates a use of getch() */%@AE@%%@NL@%
  20840. %@NL@%
  20841. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  20842. %@AI@%#include %@AE@%<conio.h> %@NL@%
  20843. %@AI@%#include %@AE@%<string.h> %@NL@%
  20844. %@AI@%#define %@AE@%GUESS_LIMIT 4 %@NL@%
  20845. WORD_LIMIT 10  %@AB@%/* maximum length of password */%@AE@%%@NL@%
  20846. %@AI@%#define %@AE@%TRUE 1 %@NL@%
  20847. %@AI@%#define %@AE@%FALSE 0 %@NL@%
  20848. char *Password = "I'mOk";%@NL@%
  20849. main()%@NL@%
  20850. {%@NL@%
  20851.      int g_count = 0;           %@AB@%/* guesses taken */%@AE@%%@NL@%
  20852.      int w_count;               %@AB@%/* letters accepted */%@AE@%%@NL@%
  20853.      int in_count;              %@AB@%/* letters entered  */%@AE@%%@NL@%
  20854.      char entry[WORD_LIMIT + 1];%@NL@%
  20855.      char ch;%@NL@%
  20856.      int correct, go_on;%@NL@%
  20857. %@NL@%
  20858.      do%@NL@%
  20859.           {%@NL@%
  20860.           puts("Enter the secret password.");%@NL@%
  20861.           in_count = w_count = 0;%@NL@%
  20862.           %@AB@%/* the following loop accepts no more chars */%@AE@%%@NL@%
  20863.           %@AB@%/* than entry[] will hold, but keeps track  */%@AE@%%@NL@%
  20864.           %@AB@%/* of total number typed                    */%@AE@%%@NL@%
  20865.           while ((ch = getch()) != '\r')%@NL@%
  20866.                {%@NL@%
  20867.                if (w_count < WORD_LIMIT)%@NL@%
  20868.                     entry[w_count++] = ch;%@NL@%
  20869.                in_count++;%@NL@%
  20870.                }%@NL@%
  20871.           entry[w_count] = '\0';%@NL@%
  20872.           if (in_count != w_count)%@NL@%
  20873.                correct = FALSE;    %@AB@%/* too many chars */%@AE@%%@NL@%
  20874.           else%@NL@%
  20875.                correct = (strcmp(entry, Password) == 0);%@NL@%
  20876.           g_count++;%@NL@%
  20877.           go_on = !correct && g_count < GUESS_LIMIT;%@NL@%
  20878.           if (go_on)%@NL@%
  20879.                puts("\nNo good; try again.");%@NL@%
  20880.           } while (go_on);%@NL@%
  20881.      if (!correct)%@NL@%
  20882.           {%@NL@%
  20883.           puts("Sorry, no more guesses.  Bye.");%@NL@%
  20884.           return(1);%@NL@%
  20885.           }%@NL@%
  20886.      puts("Welcome to Swiss bank account 2929100.");%@NL@%
  20887.      puts("Your current balance is $10,232,862.61.");%@NL@%
  20888.      return(0);%@NL@%
  20889. }%@NL@%
  20890. %@NL@%
  20891. %@NL@%
  20892. %@2@%%@AH@%PEEK.C%@AE@%%@EH@%%@NL@%
  20893. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\PEEK.C%@AE@%%@NL@%
  20894. %@NL@%
  20895. %@AB@%/* peek.c    -- demonstrates how to cast an int to a  */%@AE@%%@NL@%
  20896. %@AB@%/*              pointer                               */%@AE@%%@NL@%
  20897. %@NL@%
  20898. main()%@NL@%
  20899. {%@NL@%
  20900.     char *mem_ptr;%@NL@%
  20901.     unsigned int address;%@NL@%
  20902. %@NL@%
  20903.     while (1)%@NL@%
  20904.         {%@NL@%
  20905.         printf("Examine what memory location?\n");%@NL@%
  20906.         printf("Enter location in decimal: ");%@NL@%
  20907.         if (scanf("%u", &address) != 1)%@NL@%
  20908.             break;%@NL@%
  20909. %@NL@%
  20910.         mem_ptr = (char *)address;   %@AB@%/* cast  */%@AE@%%@NL@%
  20911. %@NL@%
  20912.         printf("The value in %u is 0x%02X\n",%@NL@%
  20913.                 address, (unsigned char)*mem_ptr);%@NL@%
  20914.         }%@NL@%
  20915. }%@NL@%
  20916. %@NL@%
  20917. %@NL@%
  20918. %@2@%%@AH@%PHONE.C%@AE@%%@EH@%%@NL@%
  20919. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP10\PHONE.C%@AE@%%@NL@%
  20920. %@NL@%
  20921. %@AB@%/* phone.c  -- a telephone number mini-database that  */%@AE@%%@NL@%
  20922. %@AB@%/*             demonstrates fseek()                   */%@AE@%%@NL@%
  20923. %@NL@%
  20924. <stdio.h>          %@AB@%/* for FILE, BUFSIZ, NULL */%@AE@%%@NL@%
  20925. %@NL@%
  20926. %@AI@%#define %@AE@%MAXL (128) %@NL@%
  20927. char Name[MAXL];%@NL@%
  20928. char Number[MAXL];%@NL@%
  20929. char File[] = "C:\\TMP\\PHONE.DB";%@NL@%
  20930. int  Count;%@NL@%
  20931. FILE *Fp;%@NL@%
  20932. int  Distance = (MAXL * MAXL);%@NL@%
  20933. %@NL@%
  20934. main(argc, argv)%@NL@%
  20935. int argc;%@NL@%
  20936. char *argv[];%@NL@%
  20937. {%@NL@%
  20938.     if (argc == 1)%@NL@%
  20939.         Ask();%@NL@%
  20940.     else%@NL@%
  20941.         Find(argv[1]);%@NL@%
  20942. %@NL@%
  20943.     return (0);%@NL@%
  20944.     }%@NL@%
  20945. %@NL@%
  20946. Find(char *str)%@NL@%
  20947.     {%@NL@%
  20948.     int i;%@NL@%
  20949. %@NL@%
  20950.     if ((Fp = fopen(File, "r")) == NULL)%@NL@%
  20951.         {%@NL@%
  20952.         fprintf(stderr, "\"%s\": Can't Read\n", File);%@NL@%
  20953.         exit (1);%@NL@%
  20954.         }%@NL@%
  20955.     if (fread(&Count, 1, sizeof(int), Fp) != sizeof(int))%@NL@%
  20956.         {%@NL@%
  20957.         fprintf(stderr,"\"%s\": Error Reading\n", File);%@NL@%
  20958.         exit (1);%@NL@%
  20959.         }%@NL@%
  20960.     for (i = 0; i < Count; i++)%@NL@%
  20961.         {%@NL@%
  20962.         fread(Name, 1, MAXL, Fp);%@NL@%
  20963.         fread(Number, 1, MAXL, Fp);%@NL@%
  20964.         if (ferror(Fp))%@NL@%
  20965.             {%@NL@%
  20966.             fprintf(stderr, "\"%s\": Error Reading.\n", File);%@NL@%
  20967.             exit (1);%@NL@%
  20968.             }%@NL@%
  20969.         if (strcmp(*str, *Name) == 0)%@NL@%
  20970.             {%@NL@%
  20971.             printf("Name: %s\n", Name);%@NL@%
  20972.             printf("Number: %s\n", Number);%@NL@%
  20973.             return;%@NL@%
  20974.             }%@NL@%
  20975.         }%@NL@%
  20976.     fprintf(stderr, "\"%s\": Not in database.\n", str);%@NL@%
  20977.     return;%@NL@%
  20978. }%@NL@%
  20979. %@NL@%
  20980. Ask()%@NL@%
  20981. {%@NL@%
  20982.     if ((Fp = fopen(File, "r+")) == NULL)%@NL@%
  20983.         Make();%@NL@%
  20984.     else if (fread(&Count, 1, sizeof(int),Fp) != sizeof(int))%@NL@%
  20985.         {%@NL@%
  20986.         fprintf(stderr, "\"%s\": Error Reading\n", File);%@NL@%
  20987.         exit (1);%@NL@%
  20988.         }%@NL@%
  20989.     printf("Name: ");%@NL@%
  20990.     if (gets(Name) == NULL || *Name == '\0')%@NL@%
  20991.         return;%@NL@%
  20992.     printf("Number: ");%@NL@%
  20993.     if (gets(Number) == NULL || *Number == '\0')%@NL@%
  20994.         return;%@NL@%
  20995.     if (fseek(Fp, (long)(Distance * Count), SEEK_CUR) != 0)%@NL@%
  20996.         {%@NL@%
  20997.         fprintf(stderr, "\"%s\": Error Seeking.\n", File);%@NL@%
  20998.         exit (1);%@NL@%
  20999.         }%@NL@%
  21000.     fwrite(Name, 1, MAXL, Fp);%@NL@%
  21001.     fwrite(Number, 1, MAXL, Fp);%@NL@%
  21002.     if (ferror(Fp))%@NL@%
  21003.         {%@NL@%
  21004.         fprintf(stderr, "\"%s\": Error Writing.\n", File);%@NL@%
  21005.         exit (1);%@NL@%
  21006.         }%@NL@%
  21007.     if (fseek(Fp, 0L, SEEK_SET) != 0)%@NL@%
  21008.         {%@NL@%
  21009.         fprintf(stderr, "\"%s\": Error Seeking.\n", File);%@NL@%
  21010.         exit (1);%@NL@%
  21011.         }%@NL@%
  21012.     ++Count;%@NL@%
  21013.     if (fwrite(&Count, 1, sizeof(int),Fp) != sizeof(int))%@NL@%
  21014.         {%@NL@%
  21015.         fprintf(stderr, "\"%s\": Error Writing\n", File);%@NL@%
  21016.         exit (1);%@NL@%
  21017.         }%@NL@%
  21018.     return;%@NL@%
  21019. }%@NL@%
  21020. %@NL@%
  21021. Make()%@NL@%
  21022. {%@NL@%
  21023.     if ((Fp = fopen(File, "w+")) == NULL)%@NL@%
  21024.         {%@NL@%
  21025.         fprintf(stderr, "\"%s\": Can't Create\n", File);%@NL@%
  21026.         exit (1);%@NL@%
  21027.         }%@NL@%
  21028.     Count = 0;%@NL@%
  21029.     if (fwrite(&Count, 1, sizeof(int), Fp) != sizeof(int))%@NL@%
  21030.         {%@NL@%
  21031.         fprintf(stderr," \"%s\": Error Creating\n", File);%@NL@%
  21032.         exit (1);%@NL@%
  21033.         }%@NL@%
  21034. }%@NL@%
  21035. %@NL@%
  21036. %@NL@%
  21037. %@2@%%@AH@%PHWORD.C%@AE@%%@EH@%%@NL@%
  21038. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\PHWORD.C%@AE@%%@NL@%
  21039. %@NL@%
  21040. %@AB@%/* phword.c   --  generate all the possible words     */%@AE@%%@NL@%
  21041. %@AB@%/*                in a phone number; demonstrates     */%@AE@%%@NL@%
  21042. %@AB@%/*                functions that return addresses     */%@AE@%%@NL@%
  21043. %@NL@%
  21044. MAXD (7)    %@AB@%/* 7 digits max */%@AE@%%@NL@%
  21045. %@NL@%
  21046. main()%@NL@%
  21047. {%@NL@%
  21048.     int digits[MAXD], ndigits = 0, line = 0;%@NL@%
  21049.     char *letters; %@NL@%
  21050.     signed char digit;%@NL@%
  21051.     int a, b, c, d, e, f, g;%@NL@%
  21052.     extern char *Range();%@NL@%
  21053. %@NL@%
  21054.     printf("Enter Phone Number (7 digits): ");%@NL@%
  21055.     do%@NL@%
  21056.         {%@NL@%
  21057.         digit = getch() - '0';%@NL@%
  21058.         if (digit == ('-' - '0'))%@NL@%
  21059.             continue;%@NL@%
  21060.         if (digit < 0 || digit > 9)%@NL@%
  21061.             {%@NL@%
  21062.             printf("\nAborted: Non Digit\n");%@NL@%
  21063.             return(1);%@NL@%
  21064.             }%@NL@%
  21065.         digits[ndigits++] = digit;%@NL@%
  21066.         printf("%d", digit);%@NL@%
  21067.         } while (ndigits < 7);%@NL@%
  21068.     printf("\n");%@NL@%
  21069. %@NL@%
  21070.     for( a = 0; a < 3; ++a)%@NL@%
  21071.      for( b = 0; b < 3; ++b)%@NL@%
  21072.       for( c = 0; c < 3; ++c)%@NL@%
  21073.        for( d = 0; d < 3; ++d)%@NL@%
  21074.         for( e = 0; e < 3; ++e)%@NL@%
  21075.          for( f = 0; f < 3; ++f)%@NL@%
  21076.           for( g = 0; g < 3; ++g)%@NL@%
  21077.               {%@NL@%
  21078.               printf("%c", Range(digits[0])[a]);%@NL@%
  21079.               printf("%c", Range(digits[1])[b]);%@NL@%
  21080.               printf("%c", Range(digits[2])[c]);%@NL@%
  21081.               printf("%c", Range(digits[3])[d]);%@NL@%
  21082.               printf("%c", Range(digits[4])[e]);%@NL@%
  21083.               printf("%c", Range(digits[5])[f]);%@NL@%
  21084.               printf("%c", Range(digits[6])[g]);%@NL@%
  21085.               printf("\n");%@NL@%
  21086.               if (++line == 20)%@NL@%
  21087.                   {%@NL@%
  21088.                   printf("Press Any key for More");%@NL@%
  21089.                   printf(" (or q to quit): ");%@NL@%
  21090.                   if (getch() == 'q')%@NL@%
  21091.                       return (0);%@NL@%
  21092.                   printf("\n");%@NL@%
  21093.                   line = 0;%@NL@%
  21094.                   }%@NL@%
  21095.               }%@NL@%
  21096. }%@NL@%
  21097. %@NL@%
  21098. char *Range(int key)%@NL@%
  21099.     {%@NL@%
  21100.     static char keys[10][3] = {%@NL@%
  21101.         {'0', '0', '0' },%@NL@%
  21102.         {'1', '1', '1' },%@NL@%
  21103.         {'a', 'b', 'c' },%@NL@%
  21104.         {'d', 'e', 'f' },%@NL@%
  21105.         {'g', 'h', 'i' },%@NL@%
  21106.         {'j', 'k', 'l' },%@NL@%
  21107.         {'m', 'n', 'o' },%@NL@%
  21108.         {'p', 'r', 's' },%@NL@%
  21109.         {'t', 'u', 'v' },%@NL@%
  21110.         {'w', 'x', 'y' }%@NL@%
  21111.     };%@NL@%
  21112. %@NL@%
  21113.     return (keys[key]);%@NL@%
  21114. }%@NL@%
  21115. %@NL@%
  21116. %@NL@%
  21117. %@2@%%@AH@%PIXELS.C%@AE@%%@EH@%%@NL@%
  21118. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP05\PIXELS.C%@AE@%%@NL@%
  21119. %@NL@%
  21120. %@AB@%/* pixels.c -- create shapes       */%@AE@%%@NL@%
  21121. %@AB@%/*             from random pixels  */%@AE@%%@NL@%
  21122. %@NL@%
  21123. <graph.h> %@AB@%/* for graphics */%@AE@%%@NL@%
  21124. %@NL@%
  21125. main()%@NL@%
  21126. {%@NL@%
  21127.     int pixels, xpos, ypos;%@NL@%
  21128.     %@AB@%/* window coordinates */%@AE@%%@NL@%
  21129.     int xmin = 100, xmax = 540;%@NL@%
  21130.     int ymin = 50,  ymax = 150;%@NL@%
  21131. %@NL@%
  21132.     srand(0);               %@AB@%/* init random nums */%@AE@%%@NL@%
  21133.     _setvideomode(_HRESBW); %@AB@%/* CGA 640 x 200    */%@AE@%%@NL@%
  21134.     _setcolor(1);           %@AB@%/* white foreground */%@AE@%%@NL@%
  21135. %@NL@%
  21136.     %@AB@%/* generate random pixel locations      */%@AE@%%@NL@%
  21137.     for (pixels = 1; pixels < 10000; pixels++)%@NL@%
  21138.         {%@NL@%
  21139.         xpos = rand() % 639;%@NL@%
  21140.         ypos = rand() % 199;%@NL@%
  21141. %@NL@%
  21142.         %@AB@%/* set pixel if within window */%@AE@%%@NL@%
  21143.         if ((xpos > xmin && xpos < xmax) &&%@NL@%
  21144.              (ypos > ymin && ypos < ymax))%@NL@%
  21145.            _setpixel(xpos, ypos);%@NL@%
  21146.         }%@NL@%
  21147.     getch(); %@AB@%/* freeze screen until key pressed */%@AE@%%@NL@%
  21148.              %@AB@%/* restore original video mode */%@AE@%%@NL@%
  21149.     _setvideomode(_DEFAULTMODE);%@NL@%
  21150. }%@NL@%
  21151. %@NL@%
  21152. %@NL@%
  21153. %@2@%%@AH@%POINTER.C%@AE@%%@EH@%%@NL@%
  21154. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\POINTER.C%@AE@%%@NL@%
  21155. %@NL@%
  21156. %@AB@%/* pointer.c  --  demonstrates pointer declaration,   */%@AE@%%@NL@%
  21157. %@AB@%/*                assignment, and use                 */%@AE@%%@NL@%
  21158. %@NL@%
  21159. %@AI@%#define %@AE@%WAIT printf("(press any key)"); getch(); \ %@NL@%
  21160.              printf("\n\n")%@NL@%
  21161. %@NL@%
  21162. main()%@NL@%
  21163. {%@NL@%
  21164.     int num, *address_var;%@NL@%
  21165. %@NL@%
  21166.     num = 0;%@NL@%
  21167.     address_var = #%@NL@%
  21168. %@NL@%
  21169.     printf("The address of the variable ");%@NL@%
  21170.     printf("\"num\" is:  0x%04X\n", &num );%@NL@%
  21171.     printf("The value in the pointer ");%@NL@%
  21172.     printf("\"address_var\" is:  0x%04X\n", address_var);%@NL@%
  21173.     printf("The value in the variable ");%@NL@%
  21174.     printf("\"num\" is: %d\n", num );%@NL@%
  21175.     WAIT;%@NL@%
  21176.     printf("Since \"address_var\" points to \"num\"\n");%@NL@%
  21177.     printf("the value in ");%@NL@%
  21178.     printf("\"*address_var\" is: %d\n", *address_var);%@NL@%
  21179.     WAIT;%@NL@%
  21180.     printf("To verify this, let's store 3 in\n");%@NL@%
  21181.     printf("\"*address_var\", then print out ");%@NL@%
  21182.     printf("\"num\" and \"*address_var\"\n");%@NL@%
  21183.     printf("again.\n");%@NL@%
  21184.     WAIT;%@NL@%
  21185. %@NL@%
  21186.     printf("Doing: *address_var = 3;\n\n");%@NL@%
  21187.     *address_var = 3;%@NL@%
  21188. %@NL@%
  21189.     printf("The address of the variable ");%@NL@%
  21190.     printf("\"num\" is:  0x%04X\n", &num);%@NL@%
  21191.     printf("The value in the pointer ");%@NL@%
  21192.     printf("\"address_var\" is:  0x%04X\n", address_var);%@NL@%
  21193.     printf("The value in the variable ");%@NL@%
  21194.     printf("\"num\" is: %d\n", num);%@NL@%
  21195.     WAIT;%@NL@%
  21196.     printf("Since \"address_var\" points to \"num\"\n");%@NL@%
  21197.     printf("the value in ");%@NL@%
  21198.     printf("\"*address_var\" is: %d\n", *address_var);%@NL@%
  21199.     WAIT;%@NL@%
  21200. %@NL@%
  21201.     printf("Now we will add 15 to \"num\" and print\n");%@NL@%
  21202.     printf("\"num\" and \"*address_var\" again.\n");%@NL@%
  21203.     WAIT;%@NL@%
  21204. %@NL@%
  21205.     printf("Doing: num += 15;\n\n");%@NL@%
  21206.     num += 15;%@NL@%
  21207. %@NL@%
  21208.     printf("The address of the variable ");%@NL@%
  21209.     printf("\"num\" is:  0x%04X\n", &num);%@NL@%
  21210.     printf("The value in the pointer ");%@NL@%
  21211.     printf("\"address_var\" is:  0x%04X\n", address_var);%@NL@%
  21212.     printf("The value in the variable ");%@NL@%
  21213.     printf("\"num\" is: %d\n", num);%@NL@%
  21214.     WAIT;%@NL@%
  21215.     printf("Since \"address_var\" points to \"num\"\n");%@NL@%
  21216.     printf("the value in ");%@NL@%
  21217.     printf("\"*address_var\" is: %d\n", *address_var);%@NL@%
  21218.     WAIT;%@NL@%
  21219. %@NL@%
  21220.     printf("Doing: return (*address_var);\n\n");%@NL@%
  21221.     return (*address_var);%@NL@%
  21222. }%@NL@%
  21223. %@NL@%
  21224. %@NL@%
  21225. %@2@%%@AH@%PORTINFO.C%@AE@%%@EH@%%@NL@%
  21226. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\PORTINFO.C%@AE@%%@NL@%
  21227. %@NL@%
  21228. %@AB@%/* portinfo.c -- reads port values                    */%@AE@%%@NL@%
  21229. %@AB@%/* program list -- portinfo.c (inp() not in core lib) */%@AE@%%@NL@%
  21230. %@NL@%
  21231. %@AI@%#include %@AE@%<conio.h> %@NL@%
  21232. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  21233. main()%@NL@%
  21234. {%@NL@%
  21235.     unsigned int portnum;%@NL@%
  21236.     int regvalue;%@NL@%
  21237.     %@NL@%
  21238.     printf("Enter number (in hex) of the port ");%@NL@%
  21239.     printf("you wish to read: ");%@NL@%
  21240.     while (scanf("%x", &portnum) == 1)%@NL@%
  21241.         {%@NL@%
  21242.     regvalue = inp(portnum);%@NL@%
  21243.     printf("\nValue returned for port %x is %d (decimal)"%@NL@%
  21244.            "  %x (hex)\n", portnum, regvalue, regvalue);%@NL@%
  21245.     printf("Next port? (q to quit): ");%@NL@%
  21246.         }%@NL@%
  21247. }%@NL@%
  21248. %@NL@%
  21249. %@NL@%
  21250. %@2@%%@AH@%POWER.C%@AE@%%@EH@%%@NL@%
  21251. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\POWER.C%@AE@%%@NL@%
  21252. %@NL@%
  21253. %@AB@%/* power.c -- attempting to raise to a power   */%@AE@%%@NL@%
  21254. %@NL@%
  21255. main()%@NL@%
  21256. { %@NL@%
  21257.     int number; %@NL@%
  21258.  %@NL@%
  21259.     number = 10**3; %@AB@%/* raise 10 to 3rd power? */%@AE@%%@NL@%
  21260.     printf("%d\n", number); %@NL@%
  21261. }%@NL@%
  21262. %@NL@%
  21263. %@NL@%
  21264. %@2@%%@AH@%PREPOST.C%@AE@%%@EH@%%@NL@%
  21265. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\PREPOST.C%@AE@%%@NL@%
  21266. %@NL@%
  21267. %@AB@%/* prepost.c -- shows effect of pre- */%@AE@%%@NL@%
  21268. %@AB@%/*              and post-increments  */%@AE@%%@NL@%
  21269. %@AB@%/*              and decrements       */%@AE@%%@NL@%
  21270. %@NL@%
  21271. main()%@NL@%
  21272. {%@NL@%
  21273.     int b = 100;%@NL@%
  21274. %@NL@%
  21275.     printf("b is %d\n", b);%@NL@%
  21276.     printf("b++ is still %d\n", b++);%@NL@%
  21277.     printf("but after it's used, ");%@NL@%
  21278.     printf("b is incremented to %d\n\n, b);%@NL@%
  21279. %@NL@%
  21280.     printf("++b, on the other hand, ");%@NL@%
  21281.     printf("is immediately %d\n", ++b);%@NL@%
  21282. }%@NL@%
  21283. %@NL@%
  21284. %@NL@%
  21285. %@2@%%@AH@%PROTO.C%@AE@%%@EH@%%@NL@%
  21286. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\PROTO.C%@AE@%%@NL@%
  21287. %@NL@%
  21288. %@AB@%/* proto.c -- demonstrate function prototyping */%@AE@%%@NL@%
  21289. %@AB@%/*            and parameter checking           */%@AE@%%@NL@%
  21290. %@NL@%
  21291. main()%@NL@%
  21292. {%@NL@%
  21293.     float n = 9995.997;%@NL@%
  21294.     int i;%@NL@%
  21295.     int examine(int num);  %@AB@%/* declare function */%@AE@%%@NL@%
  21296.                            %@AB@%/* with prototype   */%@AE@%%@NL@%
  21297. %@NL@%
  21298.     printf("n in main() is %f\n", n);%@NL@%
  21299.     i = examine(n);    %@AB@%/* pass float to function */%@AE@%%@NL@%
  21300.     printf("examine() returned n as %d\n", i);%@NL@%
  21301. }%@NL@%
  21302. %@NL@%
  21303. int examine(num)%@NL@%
  21304. {%@NL@%
  21305.     printf("examine() says n is %d\n", num);%@NL@%
  21306.     return(num);%@NL@%
  21307. }%@NL@%
  21308. %@NL@%
  21309. %@NL@%
  21310. %@2@%%@AH@%QCHELLO.C%@AE@%%@EH@%%@NL@%
  21311. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP02\QCHELLO.C%@AE@%%@NL@%
  21312. %@NL@%
  21313. %@AB@%/* qchello.c -- a simple C program */%@AE@%%@NL@%
  21314. %@NL@%
  21315. main()%@NL@%
  21316. {%@NL@%
  21317.     printf("Hello, and welcome to QuickC!\n");%@NL@%
  21318. }%@NL@%
  21319. %@NL@%
  21320. %@NL@%
  21321. %@2@%%@AH@%RACE.C%@AE@%%@EH@%%@NL@%
  21322. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\RACE.C%@AE@%%@NL@%
  21323. %@NL@%
  21324. %@AB@%/* race.c -- race of the patterned circles             */%@AE@%%@NL@%
  21325. %@AB@%/*    Illustrates animation with _getimage() and       */%@AE@%%@NL@%
  21326. %@AB@%/*    _putimage(), random number use with srand() and  */%@AE@%%@NL@%
  21327. %@AB@%/*    rand(), and system clock use with time() and     */%@AE@%%@NL@%
  21328. %@AB@%/*    ftime().%@NL@%
  21329. %@AB@%/* Program list: race.c (for srand(), rand(), and      */%@AE@%%@NL@%
  21330. %@AB@%/*                       ftime())                      */%@AE@%%@NL@%
  21331. %@NL@%
  21332. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  21333. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  21334. %@AI@%#include %@AE@%<conio.h> %@NL@%
  21335. %@AI@%#include %@AE@%<graph.h> %@NL@%
  21336. %@AI@%#include %@AE@%<time.h> %@NL@%
  21337. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  21338. %@AI@%#include %@AE@%<sys\timeb.h> %@NL@%
  21339. %@NL@%
  21340. %@AI@%#define %@AE@%END 25 %@NL@%
  21341. %@AI@%#define %@AE@%FIGNUM 3 %@NL@%
  21342. typedef char far *PTFRCHAR;%@NL@%
  21343. PTFRCHAR Bufs[FIGNUM];%@NL@%
  21344. unsigned char Masks[FIGNUM][8] = {%@NL@%
  21345.             {0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00},%@NL@%
  21346.             {0xF0,0xF0,0xF0,0xF0,0x0F,0x0F,0x0F,0x0F},%@NL@%
  21347.             {0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA}};%@NL@%
  21348. short Xul[FIGNUM], Yul[FIGNUM];  %@AB@%/* figure locations */%@AE@%%@NL@%
  21349. short Xsize, Ysize;              %@AB@%/* figure size      */%@AE@%%@NL@%
  21350. struct videoconfig Vc;%@NL@%
  21351. void Initialize(void);%@NL@%
  21352. void Draw_n_store(void);%@NL@%
  21353. void Move_figs(void);%@NL@%
  21354. void Wait(double);%@NL@%
  21355. %@NL@%
  21356. main(argc, argv)%@NL@%
  21357. int argc;%@NL@%
  21358. char *argv[];%@NL@%
  21359. {%@NL@%
  21360.     int mode = _MRES4COLOR;%@NL@%
  21361. %@NL@%
  21362.     if (argc > 1)%@NL@%
  21363.         mode = atoi(argv[1]);%@NL@%
  21364.     if (_setvideomode(mode) == 0)%@NL@%
  21365.     {%@NL@%
  21366.         fprintf(stderr,"mode %d not supported\n",mode);%@NL@%
  21367.         exit(1);%@NL@%
  21368.     }%@NL@%
  21369.     Initialize();%@NL@%
  21370.     Draw_n_store();%@NL@%
  21371.     _settextcolor(2);%@NL@%
  21372.     _settextposition(1, 1);%@NL@%
  21373.     _outtext("Place your bets and type a key");%@NL@%
  21374.     _settextposition(25, 1);%@NL@%
  21375.     _outtext("Type a key again when done");%@NL@%
  21376.     getch();%@NL@%
  21377.     Move_figs();%@NL@%
  21378.     getch();%@NL@%
  21379.     _setvideomode(_DEFAULTMODE);%@NL@%
  21380. }%@NL@%
  21381. %@NL@%
  21382. void Initialize()%@NL@%
  21383. {%@NL@%
  21384.     int i;%@NL@%
  21385.     float ar;  %@AB@%/* aspect ratio */%@AE@%%@NL@%
  21386. %@NL@%
  21387.     _getvideoconfig(&Vc);%@NL@%
  21388.     ar = (float)(10 * Vc.numypixels) / (6.5 * Vc.numxpixels);%@NL@%
  21389.       %@AB@%/* set size, initial positions */%@AE@%%@NL@%
  21390.     Xsize = Vc.numxpixels / 30;%@NL@%
  21391.     Ysize = ar * Xsize;%@NL@%
  21392.     for(i = 0; i < FIGNUM; i++)%@NL@%
  21393.     {%@NL@%
  21394.         Xul[i] = 0;%@NL@%
  21395.         Yul[i] = (i + 1) * Vc.numypixels /%@NL@%
  21396.                       (FIGNUM + 1);%@NL@%
  21397.     }%@NL@%
  21398.     _selectpalette(0);%@NL@%
  21399.     _setcolor(1);%@NL@%
  21400.         %@AB@%/*  draw finish line */%@AE@%%@NL@%
  21401.     _moveto(END * Xsize, 0);%@NL@%
  21402.     _lineto(END * Xsize, Vc.numypixels - 1);%@NL@%
  21403. }%@NL@%
  21404. %@NL@%
  21405. void Draw_n_store() %@AB@%/* draw images, save them */%@AE@%%@NL@%
  21406. {%@NL@%
  21407.     int i;%@NL@%
  21408. %@NL@%
  21409.     for (i = 0; i < FIGNUM; i++)%@NL@%
  21410.         {%@NL@%
  21411.         _setcolor(i + 1);%@NL@%
  21412.         _setfillmask(Masks[i]);%@NL@%
  21413.         _ellipse(_GFILLINTERIOR ,Xul[i], Yul[i],%@NL@%
  21414.                  Xul[i] + Xsize, Yul[i] + Ysize);%@NL@%
  21415.         _ellipse(_GBORDER ,Xul[i], Yul[i],%@NL@%
  21416.                  Xul[i] + Xsize, Yul[i] + Ysize);%@NL@%
  21417.         Bufs[i] = (PTFRCHAR) malloc((unsigned int)%@NL@%
  21418.                    _imagesize(0,Yul[i], Xul[i] +%@NL@%
  21419.                    Xsize, Yul[i] + Ysize));%@NL@%
  21420.         _getimage(Xul[i],Yul[i], Xul[i] + Xsize, Yul[i] +%@NL@%
  21421.                   Ysize, Bufs[i]);%@NL@%
  21422. %@NL@%
  21423.         }%@NL@%
  21424. }%@NL@%
  21425. void Move_figs()%@NL@%
  21426. {%@NL@%
  21427.     int i, j;%@NL@%
  21428.     static int dx[FIGNUM] = {0, 0, 0}; %@AB@%/* displacements */%@AE@%%@NL@%
  21429.     time_t tval;%@NL@%
  21430. %@NL@%
  21431.     time(&tval);    %@AB@%/*   use the current time value   */%@AE@%%@NL@%
  21432.     srand(tval);    %@AB@%/*   to initialize rand()         */%@AE@%%@NL@%
  21433.     while (dx[0] < END && dx[1] < END && dx[2] < END)%@NL@%
  21434.         {%@NL@%
  21435.         for (i = 0; i < FIGNUM; i++)%@NL@%
  21436.             {%@NL@%
  21437.             %@AB@%/* advance the figure one position if  */%@AE@%%@NL@%
  21438.             %@AB@%/* rand() returns an even number       */%@AE@%%@NL@%
  21439.             if (rand() % 2 == 0)%@NL@%
  21440.                 {%@NL@%
  21441.                 %@AB@%/* erase old image */%@AE@%%@NL@%
  21442.                 _putimage(dx[i] * Xsize, Yul[i],%@NL@%
  21443.                           Bufs[i], _GXOR);%@NL@%
  21444.                 %@AB@%/* redraw in new position */%@AE@%%@NL@%
  21445.                 _putimage((1 + dx[i]) * Xsize, Yul[i],%@NL@%
  21446.                           Bufs[i], _GPSET);%@NL@%
  21447.                 dx[i]++;%@NL@%
  21448.                 }%@NL@%
  21449.             }%@NL@%
  21450.         Wait(0.15);%@NL@%
  21451.         }%@NL@%
  21452.     for (j = 0; j < 5; j++)%@NL@%
  21453.         {%@NL@%
  21454.         for(i = 0; i < FIGNUM; i++)%@NL@%
  21455.             {%@NL@%
  21456.             %@AB@%/* flash winning figure */%@AE@%%@NL@%
  21457.             if (dx[i] >= END)%@NL@%
  21458.                 {%@NL@%
  21459.                 Wait(0.2);%@NL@%
  21460.                 _putimage(dx[i] * Xsize,Yul[i],%@NL@%
  21461.                           Bufs[i], _GPRESET);%@NL@%
  21462.                 Wait(0.2);%@NL@%
  21463.                 _putimage(dx[i] * Xsize,Yul[i],%@NL@%
  21464.                           Bufs[i], _GPSET);%@NL@%
  21465.                 }%@NL@%
  21466.             }%@NL@%
  21467.         }%@NL@%
  21468. }%@NL@%
  21469. %@NL@%
  21470. void Wait(pause) %@AB@%/* wait for pause seconds */%@AE@%%@NL@%
  21471. double pause;%@NL@%
  21472. {%@NL@%
  21473.     struct timeb start, end;%@NL@%
  21474.     long delay;%@NL@%
  21475. %@NL@%
  21476.     delay = 1000 * pause;  %@AB@%/* convert to milliseconds */%@AE@%%@NL@%
  21477.     ftime(&start);%@NL@%
  21478.     ftime(&end);%@NL@%
  21479.     while ((1000 * (end.time - start.time) +%@NL@%
  21480.             + end.millitm - start.millitm) < delay)%@NL@%
  21481.         ftime(&end);%@NL@%
  21482. }%@NL@%
  21483. %@NL@%
  21484. %@NL@%
  21485. %@NL@%
  21486. %@2@%%@AH@%READKEY.C%@AE@%%@EH@%%@NL@%
  21487. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\READKEY.C%@AE@%%@NL@%
  21488. %@NL@%
  21489. %@AB@%/* readkey.c -- contains the Readkey() function     */%@AE@%%@NL@%
  21490. %@AI@%#include %@AE@%<dos.h> %@NL@%
  21491. KEYINTR 0x16  %@AB@%/* keyboard read interrupt */%@AE@%%@NL@%
  21492. GETCHAR 0     %@AB@%/* read scancode function  */%@AE@%%@NL@%
  21493. struct SCANCODE {%@NL@%
  21494.                 unsigned char ascii;  %@AB@%/* ascii code */%@AE@%%@NL@%
  21495.                 unsigned char scan;   %@AB@%/* scan code  */%@AE@%%@NL@%
  21496.                 };%@NL@%
  21497. %@NL@%
  21498. struct SCANCODE Readkey()%@NL@%
  21499. {%@NL@%
  21500.     union REGS reg;%@NL@%
  21501.     struct SCANCODE scancode;%@NL@%
  21502. %@NL@%
  21503.     reg.h.ah = GETCHAR;         %@AB@%/* specify function */%@AE@%%@NL@%
  21504.     int86(KEYINTR, ®, ®); %@AB@%/* note use of & oper.*/%@AE@%%@NL@%
  21505.     scancode.ascii = reg.h.al;%@NL@%
  21506.     scancode.scan = reg.h.ah;%@NL@%
  21507.     return (scancode);%@NL@%
  21508. }%@NL@%
  21509. %@NL@%
  21510. %@NL@%
  21511. %@2@%%@AH@%RECALL.C%@AE@%%@EH@%%@NL@%
  21512. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\RECALL.C%@AE@%%@NL@%
  21513. %@NL@%
  21514. %@AB@%/* recall.c -- displays previously stored screen,     */%@AE@%%@NL@%
  21515. %@AB@%/*             including attributes.  Uses DMA.       */%@AE@%%@NL@%
  21516. %@AB@%/* Program list: recall.c, scrfun.c                   */%@AE@%%@NL@%
  21517. %@AB@%/* User include files: scrn.h, grafchar.h             */%@AE@%%@NL@%
  21518. %@AB@%/* Note: activate Screen Swapping On in Debug menu    */%@AE@%%@NL@%
  21519. %@NL@%
  21520. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  21521. %@AI@%#include %@AE@%<conio.h> %@NL@%
  21522. %@AI@%#include %@AE@%"scrn.h" %@NL@%
  21523. %@AI@%#include %@AE@%"grafchar.h" %@NL@%
  21524. %@NL@%
  21525. main(ac, ar)%@NL@%
  21526. int ac;%@NL@%
  21527. char *ar[];%@NL@%
  21528. {%@NL@%
  21529.      unsigned char mode;%@NL@%
  21530.      unsigned short char_attr;%@NL@%
  21531.      FILE *save;%@NL@%
  21532.      unsigned int offset;%@NL@%
  21533.      char filename[81];%@NL@%
  21534.      VIDMEM screen;%@NL@%
  21535. %@NL@%
  21536.      if (ac < 2)%@NL@%
  21537.           {%@NL@%
  21538.           fprintf(stderr, "Usage: %s filename\n", ar[0]);%@NL@%
  21539.           exit(1);%@NL@%
  21540.           }%@NL@%
  21541. %@NL@%
  21542.      if ((save = fopen(ar[1],"rb")) == NULL)%@NL@%
  21543.           {%@NL@%
  21544.           fprintf(stderr, "Can't open %s\n", ar[1]);%@NL@%
  21545.           exit(1);%@NL@%
  21546.           }%@NL@%
  21547. %@NL@%
  21548.      if ((mode = Getvmode()) == TEXTMONO)%@NL@%
  21549.           screen = MONMEM;%@NL@%
  21550.      else if (mode == TEXTC80 || mode == TEXTBW80)%@NL@%
  21551.           screen = CGAMEM;%@NL@%
  21552.      else%@NL@%
  21553.         exit(1);%@NL@%
  21554. %@NL@%
  21555.      Clearscr();%@NL@%
  21556.      for (offset = 0; offset < CHARS; offset++)%@NL@%
  21557.           {%@NL@%
  21558.           fread(&char_attr, 2, 1, save);%@NL@%
  21559.           screen[offset] = char_attr;%@NL@%
  21560.           }%@NL@%
  21561.      fclose(save);%@NL@%
  21562.      Setcurs(23, 0, PAGE);%@NL@%
  21563.      getch();   %@AB@%/* anti scrolling for QC environment */%@AE@%%@NL@%
  21564. }%@NL@%
  21565. %@NL@%
  21566. %@NL@%
  21567. %@2@%%@AH@%RECEIPTS.C%@AE@%%@EH@%%@NL@%
  21568. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\RECEIPTS.C%@AE@%%@NL@%
  21569. %@NL@%
  21570. %@AB@%/* receipts.c -- calculates gross and net */%@AE@%%@NL@%
  21571. %@AB@%/*               receipts on sales        */%@AE@%%@NL@%
  21572. %@NL@%
  21573. main()%@NL@%
  21574. {%@NL@%
  21575.     int units = 38;       %@AB@%/* number sold */%@AE@%%@NL@%
  21576.     float price = 149.99, %@AB@%/* price per item */%@AE@%%@NL@%
  21577.     rate = 0.06;          %@AB@%/* sales tax rate */%@AE@%%@NL@%
  21578. %@NL@%
  21579.     %@AB@%/* variables to hold calculated totals */%@AE@%%@NL@%
  21580.     float gross, tax, net;%@NL@%
  21581. %@NL@%
  21582.     %@AB@%/* perform calculations */%@AE@%%@NL@%
  21583.     net = units * price;%@NL@%
  21584.     tax = net * rate;%@NL@%
  21585.     gross = net + tax;%@NL@%
  21586. %@NL@%
  21587.     %@AB@%/* print results */%@AE@%%@NL@%
  21588.     printf("\tSales Report\n");%@NL@%
  21589.     printf("Net sales: \t%6.2f\n", net);%@NL@%
  21590.     printf("Tax:\t\t %5.2f\n", tax);%@NL@%
  21591.     printf("Gross sales:\t%6.2f\n", gross);%@NL@%
  21592. }%@NL@%
  21593. %@NL@%
  21594. %@NL@%
  21595. %@2@%%@AH@%RECT.C%@AE@%%@EH@%%@NL@%
  21596. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\RECT.C%@AE@%%@NL@%
  21597. %@NL@%
  21598. %@AB@%/*  rect.c -- illustrates logical coordinates,         */%@AE@%%@NL@%
  21599. %@AB@%/*            the _rectangle() and _setlinestyle()     */%@AE@%%@NL@%
  21600. %@AB@%/*            functions                                */%@AE@%%@NL@%
  21601. %@AB@%/* If you load graphics.qlb, no program list is needed */%@AE@%%@NL@%
  21602. %@NL@%
  21603. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  21604. %@AI@%#include %@AE@%<graph.h> %@NL@%
  21605. %@AI@%#include %@AE@%<conio.h> %@NL@%
  21606. %@AI@%#define %@AE@%STYLES 5 %@NL@%
  21607. short Linestyles[STYLES] = {0xFFFF, 0x8888, 0x7777,%@NL@%
  21608.                             0x00FF, 0x8787};%@NL@%
  21609. %@NL@%
  21610. main(argc, argv)%@NL@%
  21611. int argc;%@NL@%
  21612. char *argv[];%@NL@%
  21613. {%@NL@%
  21614.     struct videoconfig vc;%@NL@%
  21615.     int mode = _MRES4COLOR;%@NL@%
  21616.     int xcent, ycent;%@NL@%
  21617.     int xsize, ysize;%@NL@%
  21618.     int i;%@NL@%
  21619. %@NL@%
  21620.     if (argc > 1)%@NL@%
  21621.         mode = atoi(argv[1]);%@NL@%
  21622.     if (_setvideomode(mode) == 0)%@NL@%
  21623.     {%@NL@%
  21624.         printf("Can't open that mode.\n");%@NL@%
  21625.         exit(1);%@NL@%
  21626.     }%@NL@%
  21627.     _getvideoconfig(&vc);%@NL@%
  21628.     xcent = vc.numxpixels / 2 - 1;%@NL@%
  21629.     ycent = vc.numypixels / 2 - 1;%@NL@%
  21630.     _setlogorg(xcent, ycent);%@NL@%
  21631.     xsize = 0.9 * xcent;%@NL@%
  21632.     ysize = 0.9 * ycent;%@NL@%
  21633.     _selectpalette(1);%@NL@%
  21634.     _setcolor(3);%@NL@%
  21635.     _rectangle(_GBORDER, -xsize, -ysize, xsize, ysize);%@NL@%
  21636.     xsize *= 0.9;%@NL@%
  21637.     ysize *= 0.9;%@NL@%
  21638.     _setcolor(1);%@NL@%
  21639.     _rectangle(_GFILLINTERIOR, -xsize, -ysize, xsize, ysize);%@NL@%
  21640.     for (i = 0; i < 16; i++)%@NL@%
  21641.     {%@NL@%
  21642.         _setcolor(((i % 2) == 0) ? 2 : 3);%@NL@%
  21643.         _setlinestyle(Linestyles[ i % 5 ]);%@NL@%
  21644.         xsize *= 0.9;%@NL@%
  21645.         ysize *= 0.9;%@NL@%
  21646.         _rectangle(_GBORDER, -xsize, -ysize, xsize, ysize);%@NL@%
  21647.     }%@NL@%
  21648.     getch();      %@AB@%/* Type a key to terminate. */%@AE@%%@NL@%
  21649.     _setvideomode(_DEFAULTMODE);%@NL@%
  21650. }%@NL@%
  21651. %@NL@%
  21652. %@NL@%
  21653. %@2@%%@AH@%RECURSE.C%@AE@%%@EH@%%@NL@%
  21654. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\RECURSE.C%@AE@%%@NL@%
  21655. %@NL@%
  21656. %@AB@%/* recurse.c -- demonstrates recursion */%@AE@%%@NL@%
  21657. %@NL@%
  21658. int level = 1; %@AB@%/* recursion level */%@AE@%%@NL@%
  21659. main()%@NL@%
  21660. {%@NL@%
  21661.     int num, result;%@NL@%
  21662. %@NL@%
  21663.     printf("Factorial of what number? ");%@NL@%
  21664.     scanf("%d", &num);%@NL@%
  21665.     result = factorial(num);%@NL@%
  21666.     printf("Result is: %d\n", result);%@NL@%
  21667. }%@NL@%
  21668. %@NL@%
  21669. factorial(number)%@NL@%
  21670. {%@NL@%
  21671.     int result;%@NL@%
  21672.     printf("entering: ");%@NL@%
  21673.     printf("level %d. number = %d. &number = %d\n",%@NL@%
  21674.             level++, number, &number);%@NL@%
  21675. %@NL@%
  21676.     if (number == 0)%@NL@%
  21677.         result = 1;%@NL@%
  21678.     else%@NL@%
  21679.         result = number * factorial(number - 1);%@NL@%
  21680. %@NL@%
  21681.     printf("exiting : ");%@NL@%
  21682.     printf("level %d. number = %d. &number = %d. ",%@NL@%
  21683.             --level, number, &number);%@NL@%
  21684.     printf("result = %d\n", result);%@NL@%
  21685. %@NL@%
  21686.     return(result);%@NL@%
  21687. }%@NL@%
  21688. %@NL@%
  21689. %@NL@%
  21690. %@2@%%@AH@%REKEY.C%@AE@%%@EH@%%@NL@%
  21691. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\REKEY.C%@AE@%%@NL@%
  21692. %@NL@%
  21693. %@AB@%/* rekey.c -- transliterates typed input             */%@AE@%%@NL@%
  21694. %@AB@%/*    This program illustrates getch() and putch().  */%@AE@%%@NL@%
  21695. %@NL@%
  21696. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  21697. %@AI@%#include %@AE@%<conio.h> %@NL@%
  21698. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  21699. ESC '\033'   %@AB@%/* the escape key */%@AE@%%@NL@%
  21700. char Newchars[] = "qwertyuiopasdfghjklzxcvbnm";%@NL@%
  21701.  %@AB@%/* values to be assigned to the a,b,c keys, etc. */%@AE@%%@NL@%
  21702. main()%@NL@%
  21703. {%@NL@%
  21704.     char ch;%@NL@%
  21705. %@NL@%
  21706.     printf("Type characters and see them transformed;\n");%@NL@%
  21707.     printf("Press the Esc key to terminate.\n");%@NL@%
  21708.     while ((ch = getch()) != ESC)%@NL@%
  21709.         if (islower(ch))%@NL@%
  21710.             putch(Newchars[ch - 'a']);%@NL@%
  21711.         else if (isupper(ch))%@NL@%
  21712.             {%@NL@%
  21713.             ch = tolower(ch);%@NL@%
  21714.             putch(toupper(Newchars[ch - 'a']));%@NL@%
  21715.             }%@NL@%
  21716.         else if (ch == '\r')%@NL@%
  21717.             {%@NL@%
  21718.             putch('\n');%@NL@%
  21719.             putch('\r');%@NL@%
  21720.             }%@NL@%
  21721.         else%@NL@%
  21722.             putch(ch);%@NL@%
  21723. }%@NL@%
  21724. %@NL@%
  21725. %@NL@%
  21726. %@2@%%@AH@%RELATION.C%@AE@%%@EH@%%@NL@%
  21727. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\RELATION.C%@AE@%%@NL@%
  21728. %@NL@%
  21729. %@AB@%/* relation.c -- shows effect of      */%@AE@%%@NL@%
  21730. %@AB@%/*               relational operators */%@AE@%%@NL@%
  21731. %@NL@%
  21732. main()%@NL@%
  21733. {%@NL@%
  21734.     int a = 5, b = 3, c = 4;%@NL@%
  21735.     printf("a = %d\t b = %d\t c = %d\n", a, b, c);%@NL@%
  21736. %@NL@%
  21737.     printf("Expression a > b has a value of %d\n",%@NL@%
  21738.             a > b);%@NL@%
  21739.     printf("Expression a == c has a value of %d\n",%@NL@%
  21740.             a == c);%@NL@%
  21741.     printf("Expression a > (b + c) has a value of %d\n",%@NL@%
  21742.             a > (b + c));%@NL@%
  21743.     printf("Expression a = b has a value of %d\n",%@NL@%
  21744.             a = b); %@AB@%/* what happened here? */%@AE@%%@NL@%
  21745. }%@NL@%
  21746. %@NL@%
  21747. %@NL@%
  21748. %@2@%%@AH@%REMOIRE.C%@AE@%%@EH@%%@NL@%
  21749. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\REMOIRE.C%@AE@%%@NL@%
  21750. %@NL@%
  21751. %@AB@%/* remoire.c -- adds palette remapping to moire.c      */%@AE@%%@NL@%
  21752. %@AI@%#include %@AE@%<conio.h> %@NL@%
  21753. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  21754. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  21755. %@AI@%#include %@AE@%<graph.h> %@NL@%
  21756. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  21757. %@AI@%#define %@AE@%MAXCOLORS 64 %@NL@%
  21758. %@AI@%#define %@AE@%PALCOLORS 16 %@NL@%
  21759. long Ega_to_vga(int);%@NL@%
  21760. %@NL@%
  21761. main (argc, argv)%@NL@%
  21762. int argc;%@NL@%
  21763. char *argv[];%@NL@%
  21764. {%@NL@%
  21765.     struct videoconfig vc;%@NL@%
  21766.     unsigned int col, row;%@NL@%
  21767.     long colors[MAXCOLORS];%@NL@%
  21768.     long palette[PALCOLORS];%@NL@%
  21769.     int index;%@NL@%
  21770.     int shift = 1;%@NL@%
  21771.     int firstcol, firstrow, lastrow, lastcol;%@NL@%
  21772.     int mode = _ERESCOLOR;%@NL@%
  21773. %@NL@%
  21774. %@NL@%
  21775.     if (argc > 1)%@NL@%
  21776.         mode = atoi(argv[1]);%@NL@%
  21777. %@NL@%
  21778.     if (_setvideomode(mode) == 0)%@NL@%
  21779.         {%@NL@%
  21780.         printf("Can't do that mode.\n");%@NL@%
  21781.         exit(1);%@NL@%
  21782.         }%@NL@%
  21783.     %@AB@%/* Create array of all 64 color values. */%@AE@%%@NL@%
  21784.     for (index = 0; index < MAXCOLORS; index++)%@NL@%
  21785.         colors[index] = Ega_to_vga(index);%@NL@%
  21786.     %@AB@%/* Create array of 16 palette choices. */%@AE@%%@NL@%
  21787.     for (index = 0; index < PALCOLORS; index++)%@NL@%
  21788.         palette[index] = colors[index];%@NL@%
  21789.     _remapallpalette(palette);%@NL@%
  21790.     _getvideoconfig(&vc);%@NL@%
  21791.     firstcol = vc.numxpixels / 5;%@NL@%
  21792.     firstrow = vc.numypixels / 5;%@NL@%
  21793.     lastcol = 4 * vc.numxpixels / 5;%@NL@%
  21794.     lastrow = 4 * vc.numypixels / 5;%@NL@%
  21795. %@NL@%
  21796.     for (col = firstcol; col <= lastcol; ++col)%@NL@%
  21797.         {%@NL@%
  21798.         for (row = firstrow; row <= lastrow; ++row)%@NL@%
  21799.             {%@NL@%
  21800.             _setcolor(((row * row + col * col) / 10)%@NL@%
  21801.                        % vc.numcolors);%@NL@%
  21802.             _setpixel(col, row);%@NL@%
  21803.             }%@NL@%
  21804.         }%@NL@%
  21805.     _settextposition(1, 1);%@NL@%
  21806.     _outtext("Type a key to stop or start.");%@NL@%
  21807.     _settextposition(2, 1);%@NL@%
  21808.     _outtext("Type [Esc] while paused to quit.");%@NL@%
  21809.     do%@NL@%
  21810.         {%@NL@%
  21811.         while (!kbhit())%@NL@%
  21812.             {%@NL@%
  21813.             %@AB@%/*  Set palette array to new color values. */%@AE@%%@NL@%
  21814.             for (index = 1; index < PALCOLORS; index++)%@NL@%
  21815.                 palette[index] = (colors[(index + shift)%@NL@%
  21816.                                   % MAXCOLORS]);%@NL@%
  21817.             _remapallpalette(palette);%@NL@%
  21818.             shift++;%@NL@%
  21819.              }%@NL@%
  21820.          getch();  %@AB@%/* pause until key is typed */%@AE@%%@NL@%
  21821.         }  while (getch() != ESC);%@NL@%
  21822. %@NL@%
  21823.     _setvideomode(_DEFAULTMODE);  %@AB@%/* reset orig. mode */%@AE@%%@NL@%
  21824. }%@NL@%
  21825. %@NL@%
  21826. long Ega_to_vga(egacolor)%@NL@%
  21827. int egacolor;       %@AB@%/* ega color value */%@AE@%%@NL@%
  21828. {%@NL@%
  21829.     static long vgavals[6] = {0x2A0000L, 0x002A00L, 0x00002AL,%@NL@%
  21830.                               0x150000L, 0x001500L, 0x000015L};%@NL@%
  21831.     long vgacolor = 0L; %@AB@%/* vga color value */%@AE@%%@NL@%
  21832.     int bit;%@NL@%
  21833. %@NL@%
  21834.     for (bit = 0; bit < 6; bit++)%@NL@%
  21835.         vgacolor += ((egacolor >> bit) &1) * vgavals[bit];%@NL@%
  21836.     return (vgacolor);%@NL@%
  21837. }%@NL@%
  21838. %@NL@%
  21839. %@NL@%
  21840. %@2@%%@AH@%REVERSE.C%@AE@%%@EH@%%@NL@%
  21841. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\REVERSE.C%@AE@%%@NL@%
  21842. %@NL@%
  21843. %@AB@%/* reverse.c -- demonstrates an array of pointers     */%@AE@%%@NL@%
  21844. %@AB@%/*              by reversing lines of text            */%@AE@%%@NL@%
  21845. %@NL@%
  21846. <stdio.h>         %@AB@%/* for NULL   */%@AE@%%@NL@%
  21847. <malloc.h>        %@AB@%/* for size_t */%@AE@%%@NL@%
  21848. %@NL@%
  21849. %@AI@%#define %@AE@%MAXL 20 %@NL@%
  21850. %@NL@%
  21851. main()%@NL@%
  21852. {%@NL@%
  21853.     char *cptrs[MAXL];     %@AB@%/* array of pointers */%@AE@%%@NL@%
  21854.     char *cp;%@NL@%
  21855.     int count, i, j, ch;%@NL@%
  21856.     extern char *Getbyte();%@NL@%
  21857. %@NL@%
  21858. %@NL@%
  21859.     printf("Type in several lines of text, and I will\n");%@NL@%
  21860.     printf("print them back out in reverse order.\n");%@NL@%
  21861.     printf("(Any blank line ends input):\n");%@NL@%
  21862. %@NL@%
  21863.     for (i = 0; i < MAXL; ++i)%@NL@%
  21864.         {%@NL@%
  21865.         cp = Getbyte();%@NL@%
  21866.         cptrs[i] = cp;     %@AB@%/* assign address to pointer */%@AE@%%@NL@%
  21867.         count = 0;%@NL@%
  21868.         while ((ch = getchar()) != '\n') %@AB@%/* gather line */%@AE@%%@NL@%
  21869.             {%@NL@%
  21870.             *cp = ch;%@NL@%
  21871.             cp = Getbyte();%@NL@%
  21872.             ++count;%@NL@%
  21873.             }%@NL@%
  21874.         *cp = '\0';%@NL@%
  21875.         if (count == 0)    %@AB@%/* all done if blank line */%@AE@%%@NL@%
  21876.             break;%@NL@%
  21877.         }%@NL@%
  21878.     printf("---------<reversed>---------\n");%@NL@%
  21879.     for (j = i-1; j >= 0; --j)%@NL@%
  21880.         {%@NL@%
  21881.         printf("%s\n", cptrs[j]);%@NL@%
  21882.         }%@NL@%
  21883. }%@NL@%
  21884. %@NL@%
  21885. char *Getbyte(void)%@NL@%
  21886. {%@NL@%
  21887.     char *cp;%@NL@%
  21888. %@NL@%
  21889.     if ((cp = sbrk(1)) == (char *)-1)%@NL@%
  21890.         {%@NL@%
  21891.         printf("Panic: sbrk failed\n");%@NL@%
  21892.         exit(1);%@NL@%
  21893.         }%@NL@%
  21894.     return (cp);%@NL@%
  21895. }%@NL@%
  21896. %@NL@%
  21897. %@NL@%
  21898. %@2@%%@AH@%REVERSE2.C%@AE@%%@EH@%%@NL@%
  21899. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\REVERSE2.C%@AE@%%@NL@%
  21900. %@NL@%
  21901. %@AB@%/* reverse2.c -- demonstrates a pointer to a pointer  */%@AE@%%@NL@%
  21902. %@NL@%
  21903. <stdio.h>         %@AB@%/* for NULL   */%@AE@%%@NL@%
  21904. <malloc.h>        %@AB@%/* for size_t */%@AE@%%@NL@%
  21905. %@NL@%
  21906. %@AI@%#define %@AE@%MAXL 20 %@NL@%
  21907. %@NL@%
  21908. main()%@NL@%
  21909. {%@NL@%
  21910.     char *cptrs[MAXL];%@NL@%
  21911.     char **pp;             %@AB@%/* pointer to pointer */%@AE@%%@NL@%
  21912.     char *cp;%@NL@%
  21913.     int count, i, ch;%@NL@%
  21914.     extern char *Getbyte();%@NL@%
  21915. %@NL@%
  21916.     printf("Type in several lines of text, and I will\n");%@NL@%
  21917.     printf("print them back out in reverse order.\n");%@NL@%
  21918.     printf("(Any blank line ends input):\n");%@NL@%
  21919. %@NL@%
  21920.     for (i = 0; i < MAXL; ++i)%@NL@%
  21921.         {%@NL@%
  21922.         cp = Getbyte();%@NL@%
  21923.         cptrs[i] = cp;     %@AB@%/* assign address to pointer */%@AE@%%@NL@%
  21924.         count = 0;%@NL@%
  21925.         while ((ch = getchar()) != '\n') %@AB@%/* gather line */%@AE@%%@NL@%
  21926.             {%@NL@%
  21927.             *cp = ch;%@NL@%
  21928.             cp = Getbyte();%@NL@%
  21929.             ++count;%@NL@%
  21930.             }%@NL@%
  21931.         *cp = '\0';%@NL@%
  21932.         if (count == 0)    %@AB@%/* all done if blank line */%@AE@%%@NL@%
  21933.             break;%@NL@%
  21934.         }%@NL@%
  21935.     printf("---------<reversed>---------\n");%@NL@%
  21936.     pp = &cptrs[i];%@NL@%
  21937.     while (pp >= cptrs)%@NL@%
  21938.         {%@NL@%
  21939.         printf("%s\n", *(pp--));%@NL@%
  21940.         }%@NL@%
  21941. }%@NL@%
  21942. %@NL@%
  21943. char *Getbyte(void)%@NL@%
  21944. {%@NL@%
  21945.     char *cp;%@NL@%
  21946. %@NL@%
  21947.     if ((cp = sbrk(1)) == (char *)-1)%@NL@%
  21948.         {%@NL@%
  21949.         printf("Panic: sbrk failed\n");%@NL@%
  21950.         exit(1);%@NL@%
  21951.         }%@NL@%
  21952.     return (cp);%@NL@%
  21953. }%@NL@%
  21954. %@NL@%
  21955. %@NL@%
  21956. %@2@%%@AH@%RIGHTSTR.C%@AE@%%@EH@%%@NL@%
  21957. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\RIGHTSTR.C%@AE@%%@NL@%
  21958. %@NL@%
  21959. %@AB@%/* rightstr.c -- a C version of BASIC's RIGHT$ */%@AE@%%@NL@%
  21960. %@NL@%
  21961. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  21962. %@NL@%
  21963. char *Rightstr(char *str, int cnt)%@NL@%
  21964. {%@NL@%
  21965.     static char *cp = NULL;%@NL@%
  21966.     char *malloc();%@NL@%
  21967. %@NL@%
  21968.     if (cnt > strlen(str))%@NL@%
  21969.         cnt = strlen(str);%@NL@%
  21970.     if (cp != NULL)%@NL@%
  21971.         free(cp);%@NL@%
  21972.     if ((cp = malloc(cnt + 1)) == NULL)%@NL@%
  21973.         return (NULL);%@NL@%
  21974.     strcpy(cp, str + strlen(str) - cnt);%@NL@%
  21975.     return (cp);%@NL@%
  21976. }%@NL@%
  21977. %@NL@%
  21978. %@NL@%
  21979. %@2@%%@AH@%RINGS.C%@AE@%%@EH@%%@NL@%
  21980. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\RINGS.C%@AE@%%@NL@%
  21981. %@NL@%
  21982. %@AB@%/*  rings.c -- shoots colored rings                   */%@AE@%%@NL@%
  21983. %@AB@%/*  This program illustrates _remapallpalette() and   */%@AE@%%@NL@%
  21984. %@AB@%/*  how it can be used to produce the appearance of   */%@AE@%%@NL@%
  21985. %@AB@%/*  motion. The program is intended for EGA modes 13, */%@AE@%%@NL@%
  21986. %@AB@%/*  14, and 16.                                       */%@AE@%%@NL@%
  21987. %@AB@%/* Program list: rings.c                              */%@AE@%%@NL@%
  21988. %@AB@%/* If you load graphics.qlb, no program list is needed*/%@AE@%%@NL@%
  21989. %@NL@%
  21990. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  21991. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  21992. %@AI@%#include %@AE@%<conio.h> %@NL@%
  21993. %@AI@%#include %@AE@%<graph.h> %@NL@%
  21994. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  21995. %@NL@%
  21996. long Colors[16] = {_BLACK, _BLUE, _GREEN, _CYAN,%@NL@%
  21997.                  _RED, _MAGENTA, _BROWN, _WHITE,%@NL@%
  21998.                  _GRAY, _LIGHTBLUE, _LIGHTGREEN,%@NL@%
  21999.                  _LIGHTCYAN, _LIGHTRED, _LIGHTMAGENTA,%@NL@%
  22000.                  _LIGHTYELLOW,_BRIGHTWHITE };%@NL@%
  22001. %@NL@%
  22002. main(argc, argv)%@NL@%
  22003. int argc;%@NL@%
  22004. char *argv[];%@NL@%
  22005. {%@NL@%
  22006.     struct videoconfig vc;%@NL@%
  22007.     float aspect;%@NL@%
  22008.     short xmax, ymax;%@NL@%
  22009.     long int newpalette[16];%@NL@%
  22010.     long int temp;%@NL@%
  22011.     int index;%@NL@%
  22012.     int hot1 = 1;  %@AB@%/* first colored ring  */%@AE@%%@NL@%
  22013.     int hot2 = 8;  %@AB@%/* second colored ring */%@AE@%%@NL@%
  22014.     int mode = _ERESCOLOR;%@NL@%
  22015.     int ch;%@NL@%
  22016. %@NL@%
  22017.     if (argc > 1)%@NL@%
  22018.         mode = atoi(argv[1]);%@NL@%
  22019.     if (mode < 13)%@NL@%
  22020.     {%@NL@%
  22021.         fprintf(stderr,"Requires EGA or VGA mode\n");%@NL@%
  22022.         exit(1);%@NL@%
  22023.     }%@NL@%
  22024.     if (_setvideomode(mode) == 0)%@NL@%
  22025.     {%@NL@%
  22026.         fprintf(stderr,"% d mode unavailable\n", mode);%@NL@%
  22027.         exit(2);%@NL@%
  22028.     }%@NL@%
  22029.     _getvideoconfig(&vc);%@NL@%
  22030.     _setlogorg(vc.numxpixels / 2 - 1, vc.numypixels / 2 - 1);%@NL@%
  22031.     aspect = (10.0 * vc.numypixels) / (6.5 * vc.numxpixels);%@NL@%
  22032.     ymax = vc.numypixels / 2 - 2;%@NL@%
  22033.     xmax = ymax / aspect;%@NL@%
  22034.     for (index = 2; index < 16; index++)%@NL@%
  22035.         newpalette[index] = _LIGHTBLUE;%@NL@%
  22036.     newpalette[0] = _GRAY;%@NL@%
  22037.     newpalette[hot1] = _RED;%@NL@%
  22038.     newpalette[hot2] = _LIGHTRED;%@NL@%
  22039.     _remapallpalette(newpalette);  %@AB@%/* set initial palette */%@AE@%%@NL@%
  22040.     _setcolor(1);%@NL@%
  22041.     _ellipse(_GFILLINTERIOR, -xmax, -ymax, xmax, ymax);%@NL@%
  22042.     %@AB@%/* draw concentric circles */%@AE@%%@NL@%
  22043.     for (index = 2; index < 16; index++)%@NL@%
  22044.         {%@NL@%
  22045.         xmax /= 1.4;%@NL@%
  22046.         ymax /= 1.4;%@NL@%
  22047.         _setcolor(index);%@NL@%
  22048.         _ellipse(_GFILLINTERIOR, -xmax, -ymax, xmax, ymax);%@NL@%
  22049.         }%@NL@%
  22050.     do%@NL@%
  22051.         {%@NL@%
  22052.         while (!kbhit())%@NL@%
  22053.             {%@NL@%
  22054.             temp = newpalette[15];%@NL@%
  22055.             for(index = 15; index > 1; index--)%@NL@%
  22056.                 newpalette[index] = newpalette[index - 1];%@NL@%
  22057.             newpalette[1] = temp;%@NL@%
  22058.             _remapallpalette(newpalette);%@NL@%
  22059.             hot1 = hot1 % 15 + 1;  %@AB@%/* index of colored ring */%@AE@%%@NL@%
  22060.             hot2 = hot2 % 15 + 1;%@NL@%
  22061.             }%@NL@%
  22062.         ch = getch();%@NL@%
  22063.         if (ch > '1' && ch < '8')  %@AB@%/* reassign colors */%@AE@%%@NL@%
  22064.             {%@NL@%
  22065.             newpalette[hot1] = Colors[ ch - '0'];%@NL@%
  22066.             newpalette[hot2] = Colors[ ch - '0' + 8];%@NL@%
  22067.             }%@NL@%
  22068.          } while (ch != ESC);%@NL@%
  22069.     _clearscreen(_GCLEARSCREEN);%@NL@%
  22070.     _setvideomode(_DEFAULTMODE);%@NL@%
  22071. }%@NL@%
  22072. %@NL@%
  22073. %@NL@%
  22074. %@2@%%@AH@%ROAMSCRN.C%@AE@%%@EH@%%@NL@%
  22075. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\ROAMSCRN.C%@AE@%%@NL@%
  22076. %@NL@%
  22077. %@AB@%/*   roamscrn.c  -- puts text on screen, positions    */%@AE@%%@NL@%
  22078. %@AB@%/*                  cursor with arrow keys, uses F1   */%@AE@%%@NL@%
  22079. %@AB@%/*                  and F2 to control video inverse.  */%@AE@%%@NL@%
  22080. %@AB@%/*   program list -- roamscreen.c, scrfun.lib         */%@AE@%%@NL@%
  22081. %@AB@%/*   user include files -- keys.h, scrn.h             */%@AE@%%@NL@%
  22082. %@AB@%/*  Note: Activate Screen Swapping On in Debug menu   */%@AE@%%@NL@%
  22083. %@AI@%#include %@AE@%<conio.h> %@NL@%
  22084. %@AI@%#include %@AE@%"keys.h" %@NL@%
  22085. %@AI@%#include %@AE@%"scrn.h" %@NL@%
  22086. %@AI@%#define %@AE@%BELL '\a' %@NL@%
  22087. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  22088. %@AI@%#define %@AE@%PAGE 0 %@NL@%
  22089. %@NL@%
  22090. char *Heading =%@NL@%
  22091. "Use standard keys to enter text. Use arrow keys to "%@NL@%
  22092. "reposition cursor.\nUse F2 to turn on video inverse "%@NL@%
  22093. "and F1 to turn it off.\nHit the ESC key to quit.\n";%@NL@%
  22094. %@NL@%
  22095. main()%@NL@%
  22096. {%@NL@%
  22097.     int ch;%@NL@%
  22098.     unsigned char atr = NORMAL;%@NL@%
  22099. %@NL@%
  22100.     Clearscr();%@NL@%
  22101.     Home();%@NL@%
  22102.     printf("%s", Heading);%@NL@%
  22103.     while ((ch = getch()) != ESC)%@NL@%
  22104.         {%@NL@%
  22105.         if (ch == '\r')%@NL@%
  22106.             {%@NL@%
  22107.             putch('\n');%@NL@%
  22108.             putch('\r');%@NL@%
  22109.             }%@NL@%
  22110.         else if (ch != 0)%@NL@%
  22111.             {%@NL@%
  22112.                Write_ch_atr(ch, atr, PAGE, 1);%@NL@%
  22113.             if (!Cursrt())%@NL@%
  22114.                 putch(BELL);%@NL@%
  22115.             }%@NL@%
  22116.         else%@NL@%
  22117.             {%@NL@%
  22118.             ch = getch();%@NL@%
  22119.             switch (ch)%@NL@%
  22120.                 {%@NL@%
  22121.                 case F1 : atr = NORMAL; break;%@NL@%
  22122.                 case F2 : atr = VIDREV; break;%@NL@%
  22123.                     case UP : Rewrite(atr, PAGE);%@NL@%
  22124.                           if (!Cursup())%@NL@%
  22125.                             putch(BELL);%@NL@%
  22126.                           break;%@NL@%
  22127.                     case DN : Rewrite(atr, PAGE);%@NL@%
  22128.                           if (!Cursdn())%@NL@%
  22129.                             putch(BELL);%@NL@%
  22130.                           break;%@NL@%
  22131.                     case LT : Rewrite(atr, PAGE);%@NL@%
  22132.                           if (!Curslt())%@NL@%
  22133.                             putch(BELL);%@NL@%
  22134.                           break;%@NL@%
  22135.                     case RT : Rewrite(atr, PAGE);%@NL@%
  22136.                           if (!Cursrt())%@NL@%
  22137.                             putch(BELL);%@NL@%
  22138.                           break;%@NL@%
  22139.                 default : break;%@NL@%
  22140.                 }%@NL@%
  22141.             }%@NL@%
  22142.         }%@NL@%
  22143. }%@NL@%
  22144. %@NL@%
  22145. %@NL@%
  22146. %@2@%%@AH@%ROLO.C%@AE@%%@EH@%%@NL@%
  22147. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP11\ROLO.C%@AE@%%@NL@%
  22148. %@NL@%
  22149. %@AB@%/* rolo.c  --  demonstrates pointers to structures     */%@AE@%%@NL@%
  22150. %@NL@%
  22151. <stdio.h>      %@AB@%/* for NULL  and stdin */%@AE@%%@NL@%
  22152. <string.h>     %@AB@%/* for strdup()        */%@AE@%%@NL@%
  22153. %@NL@%
  22154. %@AI@%#define %@AE@%MAXN 79 %@NL@%
  22155. %@AI@%#define %@AE@%MAXCARDS 3 %@NL@%
  22156. %@NL@%
  22157. struct cardstruct {                 %@AB@%/* global pattern */%@AE@%%@NL@%
  22158.     char first[MAXN],%@NL@%
  22159.          last[MAXN],%@NL@%
  22160.          middle[MAXN];%@NL@%
  22161.     unsigned long street_no;%@NL@%
  22162.     char street[MAXN],%@NL@%
  22163.          city[MAXN],%@NL@%
  22164.          state[MAXN];%@NL@%
  22165.     unsigned long zip;%@NL@%
  22166.     unsigned int area;%@NL@%
  22167.     unsigned long phone;%@NL@%
  22168. };%@NL@%
  22169. %@NL@%
  22170. struct cardstruct cards[MAXCARDS];%@NL@%
  22171. %@NL@%
  22172. main()%@NL@%
  22173. {%@NL@%
  22174.     int i;%@NL@%
  22175. %@NL@%
  22176.     for (i = 0; i < MAXCARDS; ++i)%@NL@%
  22177.         {%@NL@%
  22178.         printf("\n<card %d of %d>\n", i + 1, MAXCARDS);%@NL@%
  22179.         Input(&cards[i]);%@NL@%
  22180.         }%@NL@%
  22181.     for (i = 0; i < MAXCARDS; ++i)%@NL@%
  22182.         {%@NL@%
  22183.         printf("\n<%d> ", i + 1);%@NL@%
  22184.         Showcard(&cards[i]);%@NL@%
  22185.         }%@NL@%
  22186. }%@NL@%
  22187. %@NL@%
  22188. Input(struct cardstruct *cardp)%@NL@%
  22189. {%@NL@%
  22190.     char *Str_Input();%@NL@%
  22191.     long Lint_Input();%@NL@%
  22192. %@NL@%
  22193.     strcpy(cardp->first,Str_Input("First Name"));%@NL@%
  22194.     strcpy(cardp->last,Str_Input("Last Name"));%@NL@%
  22195.     strcpy(cardp->middle,Str_Input("Middle Name"));%@NL@%
  22196.     cardp->street_no = Lint_Input("Street Number");%@NL@%
  22197.     strcpy(cardp->street,Str_Input("Street"));%@NL@%
  22198.     strcpy(cardp->city,Str_Input("City"));%@NL@%
  22199.     strcpy(cardp->state,Str_Input("State"));%@NL@%
  22200.     cardp->zip = Lint_Input("Zip Code");%@NL@%
  22201.     cardp->area = (int)Lint_Input("Area Code");%@NL@%
  22202.     cardp->phone = Lint_Input("Phone Number");%@NL@%
  22203. }%@NL@%
  22204. %@NL@%
  22205. char *Str_Input(char *prompt)%@NL@%
  22206. {%@NL@%
  22207.     char buf[MAXN + 1], *ptr;%@NL@%
  22208. %@NL@%
  22209.     printf("%s: ", prompt);%@NL@%
  22210.     if (fgets(buf, MAXN, stdin) == NULL)%@NL@%
  22211.         exit(0);%@NL@%
  22212.     buf[strlen(buf) - 1] = '\0'; %@AB@%/* strip '\n' */%@AE@%%@NL@%
  22213.     if (strlen(buf) == 0)%@NL@%
  22214.         exit(0);%@NL@%
  22215.     if ((ptr = strdup(buf)) == NULL)%@NL@%
  22216.         exit(0);%@NL@%
  22217.     return (ptr);%@NL@%
  22218. }%@NL@%
  22219. %@NL@%
  22220. long Lint_Input(char *prompt)%@NL@%
  22221. {%@NL@%
  22222.     char buf[MAXN + 1];%@NL@%
  22223.     long  num;%@NL@%
  22224. %@NL@%
  22225.     printf("%s: ", prompt);%@NL@%
  22226.     if (fgets(buf, MAXN, stdin) == NULL)%@NL@%
  22227.         exit(0);%@NL@%
  22228.     if (sscanf(buf, "%ld", &num) != 1)%@NL@%
  22229.         exit(0);%@NL@%
  22230.     return (num);%@NL@%
  22231. }%@NL@%
  22232. %@NL@%
  22233. Showcard(struct cardstruct *cardptr) %@NL@%
  22234. {%@NL@%
  22235.     printf("\n\n");%@NL@%
  22236.     printf("%s %s %s\n", cardptr->first, cardptr->middle, %@NL@%
  22237.             cardptr->last);%@NL@%
  22238.     printf("%ld %s, %s, %s %ld\n", cardptr->street_no, %@NL@%
  22239.             cardptr->street, cardptr->city, cardptr->state,%@NL@%
  22240.             cardptr->zip);%@NL@%
  22241.     printf("(%d) %ld\n", cardptr->area, cardptr->phone);%@NL@%
  22242. }%@NL@%
  22243. %@NL@%
  22244. %@NL@%
  22245. %@2@%%@AH@%ROLO2.C%@AE@%%@EH@%%@NL@%
  22246. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP11\ROLO2.C%@AE@%%@NL@%
  22247. %@NL@%
  22248. %@AB@%/* rolo2.c --  demonstrates a linked list */%@AE@%%@NL@%
  22249. %@NL@%
  22250. <stdio.h>      %@AB@%/* for NULL  and stdin */%@AE@%%@NL@%
  22251. <string.h>     %@AB@%/* for strdup()        */%@AE@%%@NL@%
  22252. <malloc.h>     %@AB@%/* for malloc()        */%@AE@%%@NL@%
  22253. %@NL@%
  22254. %@AI@%#define %@AE@%MAXN 79 %@NL@%
  22255. %@NL@%
  22256. struct cardstruct {          %@AB@%/* global pattern */%@AE@%%@NL@%
  22257.     char first[MAXN],%@NL@%
  22258.          last[MAXN],%@NL@%
  22259.          middle[MAXN];%@NL@%
  22260.     unsigned long street_no;%@NL@%
  22261.     char street[MAXN],%@NL@%
  22262.          city[MAXN],%@NL@%
  22263.          state[MAXN];%@NL@%
  22264.     unsigned long zip;%@NL@%
  22265.     unsigned int area;%@NL@%
  22266.     unsigned long phone;%@NL@%
  22267.     struct   cardstruct *nextcard;%@NL@%
  22268. };%@NL@%
  22269. %@NL@%
  22270. main()%@NL@%
  22271. {%@NL@%
  22272.     int i;%@NL@%
  22273.     struct cardstruct card, *first, *current;%@NL@%
  22274. %@NL@%
  22275.     first = (struct cardstruct *)malloc(sizeof(struct cardstruct));%@NL@%
  22276.     if(first == NULL)%@NL@%
  22277.         exit(1);%@NL@%
  22278.     if (Input(&card) != 0)%@NL@%
  22279.         exit(1);%@NL@%
  22280.     *first = card;%@NL@%
  22281.     current = first;%@NL@%
  22282. %@NL@%
  22283.     while (Input(&card) == 0)%@NL@%
  22284.         {%@NL@%
  22285.         current->nextcard =%@NL@%
  22286.             (struct cardstruct *)malloc(sizeof(struct cardstruct));%@NL@%
  22287.         if(current->nextcard == NULL)%@NL@%
  22288.             exit(1);%@NL@%
  22289.         current = current->nextcard;%@NL@%
  22290.         *current = card;%@NL@%
  22291.         }%@NL@%
  22292.     current->nextcard = NULL;%@NL@%
  22293. %@NL@%
  22294.     Dumplist(first);%@NL@%
  22295. }%@NL@%
  22296. %@NL@%
  22297. Dumplist(struct cardstruct *head)%@NL@%
  22298. {%@NL@%
  22299.     do%@NL@%
  22300.         {%@NL@%
  22301.         Showcard(head);%@NL@%
  22302.         } while ((head = head->nextcard) != NULL);%@NL@%
  22303. }%@NL@%
  22304. %@NL@%
  22305. Showcard(struct cardstruct *cardptr)%@NL@%
  22306. {%@NL@%
  22307.     printf("\n\n");%@NL@%
  22308. %@NL@%
  22309.     printf("%s %s %s\n", cardptr->first, cardptr->middle, %@NL@%
  22310.             cardptr->last);%@NL@%
  22311.     printf("%ld %s, %s, %s %ld\n", cardptr->street_no, %@NL@%
  22312.             cardptr->street, cardptr->city, cardptr->state,%@NL@%
  22313.             cardptr->zip);%@NL@%
  22314.     printf("(%d) %ld\n", cardptr->area, cardptr->phone);%@NL@%
  22315. }%@NL@%
  22316. %@NL@%
  22317. Input(struct cardstruct *cardp)%@NL@%
  22318. {%@NL@%
  22319.     char *Str_Input();%@NL@%
  22320.     long Lint_Input();%@NL@%
  22321. %@NL@%
  22322.     printf("\n<new card> (Empty first name Quits)\n");%@NL@%
  22323.     strcpy(cardp->first,Str_Input("First Name"));%@NL@%
  22324.     if (*(cardp->first) == '\0')%@NL@%
  22325.         return (1);%@NL@%
  22326.     strcpy(cardp->last,Str_Input("Last Name"));%@NL@%
  22327.     strcpy(cardp->middle,Str_Input("Middle Name"));%@NL@%
  22328.     cardp->street_no = Lint_Input("Street Number");%@NL@%
  22329.     strcpy(cardp->street,Str_Input("Street"));%@NL@%
  22330.     strcpy(cardp->city,Str_Input("City"));%@NL@%
  22331.     strcpy(cardp->state,Str_Input("State"));%@NL@%
  22332.     cardp->zip = Lint_Input("Zip Code");%@NL@%
  22333.     cardp->area = (int)Lint_Input("Area Code");%@NL@%
  22334.     cardp->phone = Lint_Input("Phone Number");%@NL@%
  22335.     return (0);%@NL@%
  22336. }%@NL@%
  22337. %@NL@%
  22338. char *Str_Input(char *prompt)%@NL@%
  22339. {%@NL@%
  22340.     char buf[MAXN + 1], *ptr;%@NL@%
  22341. %@NL@%
  22342.     printf("%s: ", prompt);%@NL@%
  22343.     if (fgets(buf, MAXN, stdin) == NULL)%@NL@%
  22344.         exit(0);%@NL@%
  22345.     buf[strlen(buf) - 1] = '\0'; %@AB@%/* strip '\n' */%@AE@%%@NL@%
  22346.     if ((ptr = strdup(buf)) == NULL)%@NL@%
  22347.         exit(0);%@NL@%
  22348.     return (ptr);%@NL@%
  22349. }%@NL@%
  22350. %@NL@%
  22351. long Lint_Input(char *prompt)%@NL@%
  22352. {%@NL@%
  22353.     char buf[MAXN + 1];%@NL@%
  22354.     long  num;%@NL@%
  22355. %@NL@%
  22356.     printf("%s: ", prompt);%@NL@%
  22357.     if (fgets(buf, MAXN, stdin) == NULL)%@NL@%
  22358.         exit(0);%@NL@%
  22359.     if (sscanf(buf, "%ld", &num) != 1)%@NL@%
  22360.         num = 0;%@NL@%
  22361.     return (num);%@NL@%
  22362. }%@NL@%
  22363. %@NL@%
  22364. %@NL@%
  22365. %@2@%%@AH@%SADD.C%@AE@%%@EH@%%@NL@%
  22366. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\SADD.C%@AE@%%@NL@%
  22367. %@NL@%
  22368. %@AB@%/* sadd.c -- a small adding machine that illustrates  */%@AE@%%@NL@%
  22369. %@AB@%/*           the need for array bounds checking.      */%@AE@%%@NL@%
  22370. %@NL@%
  22371. main()%@NL@%
  22372. {%@NL@%
  22373.     int offset = 0, i, result = 0;%@NL@%
  22374.     int stack[3];%@NL@%
  22375. %@NL@%
  22376.     while (scanf("%d", &stack[offset]) == 1)%@NL@%
  22377.         {%@NL@%
  22378.         ++offset;%@NL@%
  22379.         }%@NL@%
  22380.     for (i = 0; i < offset; ++i)%@NL@%
  22381.         {%@NL@%
  22382.         result += stack[i];%@NL@%
  22383.         }%@NL@%
  22384.     printf("-------------\n");%@NL@%
  22385.     printf("%d\n", result);%@NL@%
  22386. %@NL@%
  22387. }%@NL@%
  22388. %@NL@%
  22389. %@NL@%
  22390. %@2@%%@AH@%SADD2.C%@AE@%%@EH@%%@NL@%
  22391. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\SADD2.C%@AE@%%@NL@%
  22392. %@NL@%
  22393. %@AB@%/* sadd2.c -- a small adding machine that includes */%@AE@%%@NL@%
  22394. %@AB@%/*            array bounds checking.               */%@AE@%%@NL@%
  22395. %@NL@%
  22396. %@AI@%#define %@AE@%MAXSTAK 3 %@NL@%
  22397. %@NL@%
  22398. main()%@NL@%
  22399. {%@NL@%
  22400.     int offset = 0, i, result = 0;%@NL@%
  22401.     int stack[MAXSTAK];%@NL@%
  22402. %@NL@%
  22403.     while (scanf("%d", &stack[offset]) == 1)%@NL@%
  22404.         {%@NL@%
  22405.         if (++offset >= MAXSTAK)%@NL@%
  22406.             {%@NL@%
  22407.             printf("Stack Full\n");%@NL@%
  22408.             break;%@NL@%
  22409.             }%@NL@%
  22410.         }%@NL@%
  22411.     for (i = 0; i < offset; ++i)%@NL@%
  22412.         {%@NL@%
  22413.         result += stack[i];%@NL@%
  22414.         }%@NL@%
  22415.     printf("-------------\n");%@NL@%
  22416.     printf("%d\n", result);%@NL@%
  22417. %@NL@%
  22418. }%@NL@%
  22419. %@NL@%
  22420. %@NL@%
  22421. %@2@%%@AH@%SAVEGRAF.C%@AE@%%@EH@%%@NL@%
  22422. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\SAVEGRAF.C%@AE@%%@NL@%
  22423. %@NL@%
  22424. %@AB@%/* savegraf.c -- uses DMA to save screen of graphics  */%@AE@%%@NL@%
  22425. %@AB@%/*               characters and attributes            */%@AE@%%@NL@%
  22426. %@AB@%/* Program list - savegraf.c, initstuf.c, drawchar.c, */%@AE@%%@NL@%
  22427. %@AB@%/*                savescrn.c, scrfun.c                */%@AE@%%@NL@%
  22428. %@AB@%/* User include files - scrn.h, keys.h, grafchar.h    */%@AE@%%@NL@%
  22429. %@AB@%/* Note: activate Screen Swapping On in Debug menu    */%@AE@%%@NL@%
  22430. %@NL@%
  22431. %@NL@%
  22432. %@AI@%#include %@AE@%"grafchar.h" %@NL@%
  22433. unsigned char Grchr[NUMCHARS];  %@AB@%/* to store graphics set */%@AE@%%@NL@%
  22434. void Init_stuff(void);%@NL@%
  22435. void Draw_chars(void);%@NL@%
  22436. void Save_screen(void);  %@AB@%/* in savescrn.c */%@AE@%%@NL@%
  22437. %@NL@%
  22438. main()%@NL@%
  22439. {%@NL@%
  22440.     int ch;%@NL@%
  22441. %@NL@%
  22442.     Init_stuff();  %@AB@%/* initialize vital elements */%@AE@%%@NL@%
  22443.     Draw_chars();  %@AB@%/* map keys to graphics characters */%@AE@%%@NL@%
  22444.     Setcurs(BOTLINE + 1, 0, PAGE);%@NL@%
  22445.     printf("%-80s", "Save screen? <y/n> ");%@NL@%
  22446.     Setcurs(BOTLINE + 1, 20, PAGE);%@NL@%
  22447.     ch = getche();%@NL@%
  22448.     if (ch == 'y' || ch == 'Y')%@NL@%
  22449.         Save_screen();%@NL@%
  22450.     Setcurs(BOTLINE + 2, 0, PAGE);%@NL@%
  22451.     printf("%-80s\n", "BYE!");%@NL@%
  22452. }%@NL@%
  22453. %@NL@%
  22454. %@NL@%
  22455. %@2@%%@AH@%SAVESCRN.C%@AE@%%@EH@%%@NL@%
  22456. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\SAVESCRN.C%@AE@%%@NL@%
  22457. %@NL@%
  22458. %@AB@%/* savescrn.c -- saves screen, including attribute    */%@AE@%%@NL@%
  22459. %@AB@%/*               values, in a file                    */%@AE@%%@NL@%
  22460. %@AB@%/*               Uses direct memory access.           */%@AE@%%@NL@%
  22461. %@NL@%
  22462. <stdio.h>  %@AB@%/* for file-handling */%@AE@%%@NL@%
  22463. %@AI@%#include %@AE@%"scrn.h" %@NL@%
  22464. %@AI@%#include %@AE@%"grafchar.h" %@NL@%
  22465. void Save_screen()%@NL@%
  22466. {%@NL@%
  22467.     FILE *save;%@NL@%
  22468.     char filename[80];%@NL@%
  22469.     unsigned char mode;%@NL@%
  22470.     unsigned short char_attr;  %@AB@%/* character,attribute */%@AE@%%@NL@%
  22471.     int offset;%@NL@%
  22472.     VIDMEM screen;%@NL@%
  22473. %@NL@%
  22474.      if ((mode = Getvmode()) == TEXTMONO)%@NL@%
  22475.           screen = MONMEM;%@NL@%
  22476.      else if (mode == TEXTC80 || mode == TEXTBW80)%@NL@%
  22477.           screen = CGAMEM;%@NL@%
  22478.      else%@NL@%
  22479.         exit(1);%@NL@%
  22480.     Setcurs(BOTLINE + 1, 0, PAGE);%@NL@%
  22481.     printf("Please enter name for save file: ");%@NL@%
  22482.     scanf("%s", filename);%@NL@%
  22483.     if ((save = fopen(filename,"wb")) == NULL)%@NL@%
  22484.         {%@NL@%
  22485.         fprintf(stderr,"Can't open %s\n", filename);%@NL@%
  22486.         exit(1);%@NL@%
  22487.         }%@NL@%
  22488.    for (offset = 0; offset < CHARS; offset++)%@NL@%
  22489.         {%@NL@%
  22490.         char_attr = screen[offset];%@NL@%
  22491.         fwrite(&char_attr, 2, 1, save);%@NL@%
  22492.         }%@NL@%
  22493.    fclose(save);%@NL@%
  22494. }%@NL@%
  22495. %@NL@%
  22496. %@NL@%
  22497. %@2@%%@AH@%SCANCODE.C%@AE@%%@EH@%%@NL@%
  22498. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\SCANCODE.C%@AE@%%@NL@%
  22499. %@NL@%
  22500. %@AB@%/*   scancode.c -- displays ASCII or scan code         */%@AE@%%@NL@%
  22501. %@AB@%/*   This program illustrates using getch() to detect  */%@AE@%%@NL@%
  22502. %@AB@%/*   special keys such as function keys.               */%@AE@%%@NL@%
  22503. %@NL@%
  22504. %@AI@%#include %@AE@%<conio.h> %@NL@%
  22505. ESC '\033'     %@AB@%/* ESC key */%@AE@%%@NL@%
  22506. main()%@NL@%
  22507. {%@NL@%
  22508.      int ch;%@NL@%
  22509. %@NL@%
  22510.      printf("Strike keys and see the codes!\n");%@NL@%
  22511.      printf("Press the Esc key to quit.\n");%@NL@%
  22512. %@NL@%
  22513.      while ((ch = getch()) != ESC)%@NL@%
  22514.           {%@NL@%
  22515.           if (ch != 0)%@NL@%
  22516.                {%@NL@%
  22517.                if (ch <= 32)    %@AB@%/* control characters */%@AE@%%@NL@%
  22518.                     printf("^%c has ASCII code %d\n",%@NL@%
  22519.                             ch + 64, ch);%@NL@%
  22520.                else%@NL@%
  22521.                     printf("%c has ASCII code %d\n", ch, ch);%@NL@%
  22522.                }%@NL@%
  22523.           else              %@AB@%/* ch IS 0 */%@AE@%%@NL@%
  22524.                {%@NL@%
  22525.                ch = getch();  %@AB@%/* get scan code */%@AE@%%@NL@%
  22526.                printf("Scan code is %d\n", ch);%@NL@%
  22527.                }%@NL@%
  22528.           }%@NL@%
  22529. }%@NL@%
  22530. %@NL@%
  22531. %@NL@%
  22532. %@2@%%@AH@%SCANLINE.C%@AE@%%@EH@%%@NL@%
  22533. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\SCANLINE.C%@AE@%%@NL@%
  22534. %@NL@%
  22535. %@AB@%/* scanline.c  -- demonstrates how scanf() reads  */%@AE@%%@NL@%
  22536. %@AB@%/*                the individual words of a line  */%@AE@%%@NL@%
  22537. %@NL@%
  22538. %@AI@%#define %@AE@%INTRO \ %@NL@%
  22539. "Type in lines of text. They will be printed out\n\%@NL@%
  22540. one word per line, thus demonstrating scanf().\n\%@NL@%
  22541. (Type Ctrl-Z to Quit)\n"%@NL@%
  22542. %@NL@%
  22543. main()%@NL@%
  22544. {%@NL@%
  22545.     char buf[512];    %@AB@%/* should be big enough */%@AE@%%@NL@%
  22546. %@NL@%
  22547.     printf(INTRO);%@NL@%
  22548. %@NL@%
  22549.     %@AB@%/*%@NL@%
  22550. %@AB@%     * scanf() returns the number of items%@NL@%
  22551. %@AB@%     * its control string matched.%@NL@%
  22552. %@AB@%     */%@AE@%%@NL@%
  22553.     while (scanf("%s", buf) == 1)%@NL@%
  22554.         {%@NL@%
  22555.         printf("%s\n", buf);%@NL@%
  22556.         }%@NL@%
  22557.     %@NL@%
  22558. }%@NL@%
  22559. %@NL@%
  22560. %@NL@%
  22561. %@2@%%@AH@%SCAPE.C%@AE@%%@EH@%%@NL@%
  22562. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\SCAPE.C%@AE@%%@NL@%
  22563. %@NL@%
  22564. %@AB@%/* scape.c -- uses nondefault EGA colors              */%@AE@%%@NL@%
  22565. %@AB@%/* If you load graphics.qlb, no program list is needed.*/%@AE@%%@NL@%
  22566. %@NL@%
  22567. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  22568. %@AI@%#include %@AE@%<graph.h> %@NL@%
  22569. %@AI@%#include %@AE@%"egacolor.h" %@NL@%
  22570. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  22571. %@AI@%#include %@AE@%<conio.h> %@NL@%
  22572. %@AI@%#define %@AE@%SKY (b | B | g) %@NL@%
  22573. %@AI@%#define %@AE@%OCEAN b %@NL@%
  22574. %@AI@%#define %@AE@%SAND (R | g | b) %@NL@%
  22575. %@AI@%#define %@AE@%SUN (R | G | r | g) %@NL@%
  22576. %@NL@%
  22577. main(argc, argv)%@NL@%
  22578. int argc;%@NL@%
  22579. char *argv[];%@NL@%
  22580. {%@NL@%
  22581.     struct videoconfig vc;%@NL@%
  22582.     int mode = _ERESCOLOR;%@NL@%
  22583.     short xmax, ymax, sunx, suny, sunsizex, sunsizey;%@NL@%
  22584.     float ar;%@NL@%
  22585. %@NL@%
  22586. %@NL@%
  22587.     if (argc > 1)%@NL@%
  22588.         mode = atoi(argv[1]);%@NL@%
  22589.     if (_setvideomode(mode) == 0)%@NL@%
  22590.         {%@NL@%
  22591.         fprintf(stderr,"mode %d not supported\n", mode);%@NL@%
  22592.         exit(1);%@NL@%
  22593.         }%@NL@%
  22594.     _getvideoconfig(&vc);%@NL@%
  22595.     xmax = vc.numxpixels - 1;%@NL@%
  22596.     ymax = vc.numypixels - 1;%@NL@%
  22597.     sunx = 0.7 * xmax;%@NL@%
  22598.     suny = 0.2 * ymax;%@NL@%
  22599.     ar = (float)(10 * vc.numypixels) / (6.5 * vc.numxpixels);%@NL@%
  22600.     sunsizex = xmax / 30;%@NL@%
  22601.     sunsizey = ar * sunsizex;%@NL@%
  22602.     _remappalette(1, SKY);%@NL@%
  22603.     _remappalette(2, OCEAN);%@NL@%
  22604.     _remappalette(3, SAND);%@NL@%
  22605.     _remappalette(4, SUN);%@NL@%
  22606.     _setcolor(1);%@NL@%
  22607.     _rectangle(_GFILLINTERIOR, 0, 0, xmax, 2 * ymax / 5);%@NL@%
  22608.     _setcolor(4);%@NL@%
  22609.     _ellipse(_GFILLINTERIOR, sunx - sunsizex, suny -%@NL@%
  22610.              sunsizey, sunx + sunsizex, suny + sunsizey);%@NL@%
  22611.     _setcolor(2);%@NL@%
  22612.     _rectangle(_GFILLINTERIOR, 0, 2 * ymax / 5, xmax,%@NL@%
  22613.                2 * ymax / 3);%@NL@%
  22614.     _setcolor(3);%@NL@%
  22615.     _rectangle(_GFILLINTERIOR, 0, 2 * ymax / 3, xmax, ymax);%@NL@%
  22616.     getch();%@NL@%
  22617.     _setvideomode(_DEFAULTMODE);%@NL@%
  22618. }%@NL@%
  22619. %@NL@%
  22620. %@NL@%
  22621. %@2@%%@AH@%SCORE.C%@AE@%%@EH@%%@NL@%
  22622. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\SCORE.C%@AE@%%@NL@%
  22623. %@NL@%
  22624. %@AB@%/* score.c -- defines and prints   */%@AE@%%@NL@%
  22625. %@AB@%/*            int and long vars    */%@AE@%%@NL@%
  22626. %@NL@%
  22627. main()%@NL@%
  22628. {%@NL@%
  22629.     %@AB@%/* declare some int variables and assign values */%@AE@%%@NL@%
  22630.     %@AB@%/* to them in the same statement                */%@AE@%%@NL@%
  22631. %@NL@%
  22632.     int home = 5, visitors = 2, inning =7, attendance = 31300;%@NL@%
  22633.     long total_attendance = 1135477;  %@AB@%/* long int */%@AE@%%@NL@%
  22634. %@NL@%
  22635.     %@AB@%/* print out the values */%@AE@%%@NL@%
  22636. %@NL@%
  22637.     printf("The score after %d innings is \n", inning);%@NL@%
  22638.     printf("Home team %d, Visitors %d.\n\n", home, visitors);%@NL@%
  22639.     printf("The attendance today is %d.\n", attendance);%@NL@%
  22640.     printf("Attendance this year to date is %ld.",%@NL@%
  22641.             total_attendance);%@NL@%
  22642. }%@NL@%
  22643. %@NL@%
  22644. %@NL@%
  22645. %@2@%%@AH@%SCRANGE.C%@AE@%%@EH@%%@NL@%
  22646. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\SCRANGE.C%@AE@%%@NL@%
  22647. %@NL@%
  22648. %@AB@%/* scrange.c  --  illustrates scanf()'s control */%@AE@%%@NL@%
  22649. %@AB@%/*                directives                    */%@AE@%%@NL@%
  22650. %@NL@%
  22651. main()%@NL@%
  22652. {%@NL@%
  22653.     char buf[512],    %@AB@%/* should be big enough */%@AE@%%@NL@%
  22654.          dummy[2];    %@AB@%/* for \n and \0        */%@AE@%%@NL@%
  22655.     int  num;%@NL@%
  22656. %@NL@%
  22657.     do%@NL@%
  22658.         {%@NL@%
  22659.         printf("Running:\n");%@NL@%
  22660.         printf("\tscanf(\"%%d\", &num);\n");%@NL@%
  22661.         printf("\tscanf(\"%%[^\\n]\", buf);\n");%@NL@%
  22662.         printf("\tscanf(\"%%[\\n]\", dummy);\n");%@NL@%
  22663. %@NL@%
  22664.         printf("\nType enough to satisfy this:\n");%@NL@%
  22665.         printf("(Set num equal to zero to quit)\n");%@NL@%
  22666. %@NL@%
  22667.         scanf("%d", &num);%@NL@%
  22668.         scanf("%[^\n]", buf);%@NL@%
  22669.         scanf("%[\n]", dummy);%@NL@%
  22670. %@NL@%
  22671.         printf("\n\tnum = %d\n", num);%@NL@%
  22672.         printf("\tbuf[] = \"%s\"\n", buf);%@NL@%
  22673.         printf("\n\n");%@NL@%
  22674. %@NL@%
  22675.         } while (num != 0) ;%@NL@%
  22676. %@NL@%
  22677. }%@NL@%
  22678. %@NL@%
  22679. %@NL@%
  22680. %@2@%%@AH@%SCRFUN.C%@AE@%%@EH@%%@NL@%
  22681. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\SCRFUN.C%@AE@%%@NL@%
  22682. %@NL@%
  22683. %@AB@%/*  scrfun.c -- contains several video BIOS calls     */%@AE@%%@NL@%
  22684. %@AB@%/*  Setcurs() sets the cursor position                */%@AE@%%@NL@%
  22685. %@AB@%/*  Getcurs() gets the cursor postion                 */%@AE@%%@NL@%
  22686. %@AB@%/*  Setpage() sets the current video page             */%@AE@%%@NL@%
  22687. %@AB@%/*  Setvmode() sets the video mode                    */%@AE@%%@NL@%
  22688. %@AB@%/*  Clearscr() clears the screen                      */%@AE@%%@NL@%
  22689. %@AB@%/*  Read_ch_atr() reads the character and             */%@AE@%%@NL@%
  22690. %@AB@%/*                attribute at the cursor             */%@AE@%%@NL@%
  22691. %@AB@%/*  Write_ch_atr() writes a character and             */%@AE@%%@NL@%
  22692. %@AB@%/*                 attribute at the cursor            */%@AE@%%@NL@%
  22693. %@AB@%/*  Rewrite() rewrites a screen character             */%@AE@%%@NL@%
  22694. %@AB@%/*            with a new attribute                    */%@AE@%%@NL@%
  22695. %@AB@%/*  Getvmode() gets the current video mode            */%@AE@%%@NL@%
  22696. %@AB@%/*  Getpage() gets the current video page             */%@AE@%%@NL@%
  22697. %@AB@%/*                                                    */%@AE@%%@NL@%
  22698. %@AB@%/*  The following functions use Setcurs() to move the */%@AE@%%@NL@%
  22699. %@AB@%/*  one position at a time up to a limit.             */%@AE@%%@NL@%
  22700. %@AB@%/*  Curslt_lim() moves cursor one column left         */%@AE@%%@NL@%
  22701. %@AB@%/*  Cursrt_lim() moves cursor one column right        */%@AE@%%@NL@%
  22702. %@AB@%/*  Cursup_lim() moves cursor one line up             */%@AE@%%@NL@%
  22703. %@AB@%/*  Cursdn_lim() moves cursor one line down           */%@AE@%%@NL@%
  22704. %@AB@%/*                                                    */%@AE@%%@NL@%
  22705. %@AB@%/*  Programs using these functions should include the */%@AE@%%@NL@%
  22706. %@AB@%/*  scrn.h file                                       */%@AE@%%@NL@%
  22707. %@NL@%
  22708. %@AI@%#include %@AE@%<dos.h> %@NL@%
  22709. %@AI@%#include %@AE@%"scrn.h" %@NL@%
  22710. %@NL@%
  22711. %@AB@%/* sets cursor to row, column, and page */%@AE@%%@NL@%
  22712. void Setcurs(row, col, page)%@NL@%
  22713. unsigned char row, col, page;%@NL@%
  22714. {%@NL@%
  22715.     union REGS reg;%@NL@%
  22716. %@NL@%
  22717.     reg.h.ah = SETCURSOR;%@NL@%
  22718.     reg.h.dh = row;%@NL@%
  22719.     reg.h.dl = col;%@NL@%
  22720.     reg.h.bh = page;%@NL@%
  22721.     int86(VIDEO, ®, ®);%@NL@%
  22722. }%@NL@%
  22723. %@NL@%
  22724. %@AB@%/* gets current cursor row, column for given page */%@AE@%%@NL@%
  22725. void Getcurs(pr, pc, page)%@NL@%
  22726. unsigned char *pr, *pc, page;%@NL@%
  22727. {%@NL@%
  22728.     union REGS reg;%@NL@%
  22729. %@NL@%
  22730.     reg.h.ah = GETCURSOR;%@NL@%
  22731.     reg.h.bh = page;%@NL@%
  22732.     int86(VIDEO, ®, ®);%@NL@%
  22733.     *pr = reg.h.dh;  %@AB@%/* row number */%@AE@%%@NL@%
  22734.     *pc = reg.h.dl;   %@AB@%/* column number */%@AE@%%@NL@%
  22735. }%@NL@%
  22736. %@NL@%
  22737. %@NL@%
  22738. %@AB@%/* sets page to given value */%@AE@%%@NL@%
  22739. void Setpage(page)%@NL@%
  22740. unsigned char page;%@NL@%
  22741. {%@NL@%
  22742.     union REGS reg;%@NL@%
  22743. %@NL@%
  22744.     reg.h.ah = SETPAGE;%@NL@%
  22745.     reg.h.al = page;%@NL@%
  22746.     int86(VIDEO, ®, ®);%@NL@%
  22747. }%@NL@%
  22748. %@NL@%
  22749. %@AB@%/* sets video mode to given mode */%@AE@%%@NL@%
  22750. void Setvmode(mode)%@NL@%
  22751. unsigned char mode;%@NL@%
  22752. {%@NL@%
  22753.     union REGS reg;%@NL@%
  22754. %@NL@%
  22755.     reg.h.ah = SETMODE;%@NL@%
  22756.     reg.h.al = mode;%@NL@%
  22757.     int86(VIDEO, ®, ®);%@NL@%
  22758. }%@NL@%
  22759. %@NL@%
  22760. %@AB@%/* clear the screen */%@AE@%%@NL@%
  22761. void Clearscr()%@NL@%
  22762. {%@NL@%
  22763.     union REGS reg;%@NL@%
  22764. %@NL@%
  22765.     reg.h.ah = SCROLL;%@NL@%
  22766.     reg.h.al = 0;%@NL@%
  22767.     reg.h.ch = 0;%@NL@%
  22768.     reg.h.cl = 0;%@NL@%
  22769.     reg.h.dh = ROWS - 1;%@NL@%
  22770.     reg.h.dl = COLS - 1;%@NL@%
  22771.     reg.h.bh = NORMAL;%@NL@%
  22772.     int86(VIDEO, ®, ®);%@NL@%
  22773. }%@NL@%
  22774. %@NL@%
  22775. %@AB@%/* reads the character and attribute at the cursor */%@AE@%%@NL@%
  22776. %@AB@%/* position on a given page                        */%@AE@%%@NL@%
  22777. void Read_ch_atr(pc, pa, page)%@NL@%
  22778. unsigned char *pc, *pa;%@NL@%
  22779. unsigned char page;%@NL@%
  22780. {%@NL@%
  22781.     union REGS reg;%@NL@%
  22782. %@NL@%
  22783.     reg.h.ah = READCHATR;%@NL@%
  22784.     reg.h.bh = page;%@NL@%
  22785.     int86(VIDEO, ®, ®);%@NL@%
  22786.     *pc = reg.h.al;  %@AB@%/* character at cursor */%@AE@%%@NL@%
  22787.     *pa = reg.h.ah;  %@AB@%/* attribute at cursor */%@AE@%%@NL@%
  22788. }%@NL@%
  22789. %@NL@%
  22790. %@AB@%/* writes a given character and attribute at the */%@AE@%%@NL@%
  22791. %@AB@%/* cursor on a given page for num times          */%@AE@%%@NL@%
  22792. void Write_ch_atr(ch, atr, page, num)%@NL@%
  22793. unsigned char ch, atr, page;%@NL@%
  22794. unsigned int num;%@NL@%
  22795. {%@NL@%
  22796.     union REGS reg;%@NL@%
  22797. %@NL@%
  22798.     reg.h.ah = WRITECHATR;%@NL@%
  22799.     reg.h.al = ch;%@NL@%
  22800.     reg.h.bl = atr;%@NL@%
  22801.     reg.h.bh = page;%@NL@%
  22802.     reg.x.cx = num;%@NL@%
  22803.     int86(VIDEO, ®, ®);%@NL@%
  22804. }%@NL@%
  22805. %@NL@%
  22806. %@AB@%/* rewrites the character at the cursor using    */%@AE@%%@NL@%
  22807. %@AB@%/* attribute at                                  */%@AE@%%@NL@%
  22808. void Rewrite(at, page)%@NL@%
  22809. unsigned char at, page;%@NL@%
  22810. {%@NL@%
  22811.      unsigned char ch, atr;%@NL@%
  22812. %@NL@%
  22813.      Read_ch_atr(&ch, &atr, page);%@NL@%
  22814.      Write_ch_atr(ch, at, page, 1);%@NL@%
  22815. }%@NL@%
  22816. %@NL@%
  22817. %@AB@%/* obtains the current video mode */%@AE@%%@NL@%
  22818. unsigned char Getvmode()%@NL@%
  22819. {%@NL@%
  22820.     union REGS reg;%@NL@%
  22821. %@NL@%
  22822.     reg.h.ah = GETMODE;%@NL@%
  22823.     int86(VIDEO, ®, ®);%@NL@%
  22824.     return reg.h.al;%@NL@%
  22825. }%@NL@%
  22826. %@NL@%
  22827. %@AB@%/* obtains the current video page */%@AE@%%@NL@%
  22828. unsigned char Getpage()%@NL@%
  22829. {%@NL@%
  22830.     union REGS reg;%@NL@%
  22831. %@NL@%
  22832.     reg.h.ah = GETMODE;%@NL@%
  22833.     int86(VIDEO, ®, ®);%@NL@%
  22834.     return reg.h.bh;%@NL@%
  22835. }%@NL@%
  22836. %@NL@%
  22837. %@AB@%/* moves cursor one column left, but not past */%@AE@%%@NL@%
  22838. %@AB@%/* the given limit                            */%@AE@%%@NL@%
  22839. unsigned char Curslt_lim(limit)%@NL@%
  22840. unsigned char limit;%@NL@%
  22841. {%@NL@%
  22842.     unsigned char row, col, page;%@NL@%
  22843.     unsigned char status = 1;%@NL@%
  22844. %@NL@%
  22845.     Getcurs(&row, &col, page = Getpage());%@NL@%
  22846.     if (col > limit)%@NL@%
  22847.         Setcurs(row, col - 1, page);%@NL@%
  22848.     else%@NL@%
  22849.         status = 0;%@NL@%
  22850.     return status;%@NL@%
  22851. }%@NL@%
  22852. %@NL@%
  22853. %@AB@%/* moves cursor one column right, but not past */%@AE@%%@NL@%
  22854. %@AB@%/* the given limit                             */%@AE@%%@NL@%
  22855. unsigned char Cursrt_lim(limit)%@NL@%
  22856. unsigned char limit;%@NL@%
  22857. {%@NL@%
  22858.     unsigned char row, col, page;%@NL@%
  22859.     unsigned char status = 1;%@NL@%
  22860. %@NL@%
  22861.     Getcurs(&row, &col, page = Getpage());%@NL@%
  22862.     if (col < limit)%@NL@%
  22863.         Setcurs(row, col + 1, page);%@NL@%
  22864.     else%@NL@%
  22865.         status = 0;%@NL@%
  22866.     return status;%@NL@%
  22867. }%@NL@%
  22868. %@NL@%
  22869. %@AB@%/* move cursor one row down, but not past */%@AE@%%@NL@%
  22870. %@AB@%/* the given limit                        */%@AE@%%@NL@%
  22871. unsigned char Cursup_lim(limit)%@NL@%
  22872. unsigned char limit;%@NL@%
  22873. {%@NL@%
  22874.     unsigned char row, col, page;%@NL@%
  22875.     unsigned char status = 1;%@NL@%
  22876. %@NL@%
  22877.     Getcurs(&row, &col, page = Getpage());%@NL@%
  22878.     if (row > limit)%@NL@%
  22879.         Setcurs(row - 1, col, page);%@NL@%
  22880.     else%@NL@%
  22881.         status = 0;%@NL@%
  22882.     return status;%@NL@%
  22883. }%@NL@%
  22884. %@NL@%
  22885. %@AB@%/* move cursor one row down, but not past */%@AE@%%@NL@%
  22886. %@AB@%/* the given limit                        */%@AE@%%@NL@%
  22887. unsigned char Cursdn_lim(limit)%@NL@%
  22888. unsigned char limit;%@NL@%
  22889. {%@NL@%
  22890.     unsigned char row, col, page;%@NL@%
  22891.     unsigned char status = 1;%@NL@%
  22892. %@NL@%
  22893.     Getcurs(&row, &col, page = Getpage());%@NL@%
  22894.     if (row < limit)%@NL@%
  22895.         Setcurs(row + 1, col, page);%@NL@%
  22896.     else%@NL@%
  22897.         status = 0;%@NL@%
  22898.     return status;%@NL@%
  22899. }%@NL@%
  22900. %@NL@%
  22901. %@NL@%
  22902. %@2@%%@AH@%SCRFUN.C%@AE@%%@EH@%%@NL@%
  22903. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\SCRFUN.C%@AE@%%@NL@%
  22904. %@NL@%
  22905. %@AB@%/*  scrfun.c -- contains several video BIOS calls     */%@AE@%%@NL@%
  22906. %@AB@%/*  Setcurs() sets the cursor position                */%@AE@%%@NL@%
  22907. %@AB@%/*  Getcurs() gets the cursor postion                 */%@AE@%%@NL@%
  22908. %@AB@%/*  Setpage() sets the current video page             */%@AE@%%@NL@%
  22909. %@AB@%/*  Setvmode() sets the video mode                    */%@AE@%%@NL@%
  22910. %@AB@%/*  Clearscr() clears the screen                      */%@AE@%%@NL@%
  22911. %@AB@%/*  Read_ch_atr() reads the character and             */%@AE@%%@NL@%
  22912. %@AB@%/*                attribute at the cursor             */%@AE@%%@NL@%
  22913. %@AB@%/*  Write_ch_atr() writes a character and             */%@AE@%%@NL@%
  22914. %@AB@%/*                 attribute at the cursor            */%@AE@%%@NL@%
  22915. %@AB@%/*  Rewrite() rewrites a screen character             */%@AE@%%@NL@%
  22916. %@AB@%/*            with a new attribute                    */%@AE@%%@NL@%
  22917. %@AB@%/*  Getvmode() gets the current video mode            */%@AE@%%@NL@%
  22918. %@AB@%/*  Getpage() gets the current video page             */%@AE@%%@NL@%
  22919. %@AB@%/*                                                    */%@AE@%%@NL@%
  22920. %@AB@%/*  The following functions use Setcurs() to move the */%@AE@%%@NL@%
  22921. %@AB@%/*  one position at a time up to a limit.             */%@AE@%%@NL@%
  22922. %@AB@%/*  Curslt_lim() moves cursor one column left         */%@AE@%%@NL@%
  22923. %@AB@%/*  Cursrt_lim() moves cursor one column right        */%@AE@%%@NL@%
  22924. %@AB@%/*  Cursup_lim() moves cursor one line up             */%@AE@%%@NL@%
  22925. %@AB@%/*  Cursdn_lim() moves cursor one line down           */%@AE@%%@NL@%
  22926. %@AB@%/*                                                    */%@AE@%%@NL@%
  22927. %@AB@%/*  Programs using these functions should include the */%@AE@%%@NL@%
  22928. %@AB@%/*  scrn.h file                                       */%@AE@%%@NL@%
  22929. %@NL@%
  22930. %@AI@%#include %@AE@%<dos.h> %@NL@%
  22931. %@AI@%#include %@AE@%"scrn.h" %@NL@%
  22932. %@NL@%
  22933. %@AB@%/* sets cursor to row, column, and page */%@AE@%%@NL@%
  22934. void Setcurs(row, col, page)%@NL@%
  22935. unsigned char row, col, page;%@NL@%
  22936. {%@NL@%
  22937.     union REGS reg;%@NL@%
  22938. %@NL@%
  22939.     reg.h.ah = SETCURSOR;%@NL@%
  22940.     reg.h.dh = row;%@NL@%
  22941.     reg.h.dl = col;%@NL@%
  22942.     reg.h.bh = page;%@NL@%
  22943.     int86(VIDEO, ®, ®);%@NL@%
  22944. }%@NL@%
  22945. %@NL@%
  22946. %@AB@%/* gets current cursor row, column for given page */%@AE@%%@NL@%
  22947. void Getcurs(pr, pc, page)%@NL@%
  22948. unsigned char *pr, *pc, page;%@NL@%
  22949. {%@NL@%
  22950.     union REGS reg;%@NL@%
  22951. %@NL@%
  22952.     reg.h.ah = GETCURSOR;%@NL@%
  22953.     reg.h.bh = page;%@NL@%
  22954.     int86(VIDEO, ®, ®);%@NL@%
  22955.     *pr = reg.h.dh;  %@AB@%/* row number */%@AE@%%@NL@%
  22956.     *pc = reg.h.dl;   %@AB@%/* column number */%@AE@%%@NL@%
  22957. }%@NL@%
  22958. %@NL@%
  22959. %@NL@%
  22960. %@AB@%/* sets page to given value */%@AE@%%@NL@%
  22961. void Setpage(page)%@NL@%
  22962. unsigned char page;%@NL@%
  22963. {%@NL@%
  22964.     union REGS reg;%@NL@%
  22965. %@NL@%
  22966.     reg.h.ah = SETPAGE;%@NL@%
  22967.     reg.h.al = page;%@NL@%
  22968.     int86(VIDEO, ®, ®);%@NL@%
  22969. }%@NL@%
  22970. %@NL@%
  22971. %@AB@%/* sets video mode to given mode */%@AE@%%@NL@%
  22972. void Setvmode(mode)%@NL@%
  22973. unsigned char mode;%@NL@%
  22974. {%@NL@%
  22975.     union REGS reg;%@NL@%
  22976. %@NL@%
  22977.     reg.h.ah = SETMODE;%@NL@%
  22978.     reg.h.al = mode;%@NL@%
  22979.     int86(VIDEO, ®, ®);%@NL@%
  22980. }%@NL@%
  22981. %@NL@%
  22982. %@AB@%/* clear the screen */%@AE@%%@NL@%
  22983. void Clearscr()%@NL@%
  22984. {%@NL@%
  22985.     union REGS reg;%@NL@%
  22986. %@NL@%
  22987.     reg.h.ah = SCROLL;%@NL@%
  22988.     reg.h.al = 0;%@NL@%
  22989.     reg.h.ch = 0;%@NL@%
  22990.     reg.h.cl = 0;%@NL@%
  22991.     reg.h.dh = ROWS - 1;%@NL@%
  22992.     reg.h.dl = COLS - 1;%@NL@%
  22993.     reg.h.bh = NORMAL;%@NL@%
  22994.     int86(VIDEO, ®, ®);%@NL@%
  22995. }%@NL@%
  22996. %@NL@%
  22997. %@AB@%/* reads the character and attribute at the cursor */%@AE@%%@NL@%
  22998. %@AB@%/* position on a given page                        */%@AE@%%@NL@%
  22999. void Read_ch_atr(pc, pa, page)%@NL@%
  23000. unsigned char *pc, *pa;%@NL@%
  23001. unsigned char page;%@NL@%
  23002. {%@NL@%
  23003.     union REGS reg;%@NL@%
  23004. %@NL@%
  23005.     reg.h.ah = READCHATR;%@NL@%
  23006.     reg.h.bh = page;%@NL@%
  23007.     int86(VIDEO, ®, ®);%@NL@%
  23008.     *pc = reg.h.al;  %@AB@%/* character at cursor */%@AE@%%@NL@%
  23009.     *pa = reg.h.ah;  %@AB@%/* attribute at cursor */%@AE@%%@NL@%
  23010. }%@NL@%
  23011. %@NL@%
  23012. %@AB@%/* writes a given character and attribute at the */%@AE@%%@NL@%
  23013. %@AB@%/* cursor on a given page for num times          */%@AE@%%@NL@%
  23014. void Write_ch_atr(ch, atr, page, num)%@NL@%
  23015. unsigned char ch, atr, page;%@NL@%
  23016. unsigned int num;%@NL@%
  23017. {%@NL@%
  23018.     union REGS reg;%@NL@%
  23019. %@NL@%
  23020.     reg.h.ah = WRITECHATR;%@NL@%
  23021.     reg.h.al = ch;%@NL@%
  23022.     reg.h.bl = atr;%@NL@%
  23023.     reg.h.bh = page;%@NL@%
  23024.     reg.x.cx = num;%@NL@%
  23025.     int86(VIDEO, ®, ®);%@NL@%
  23026. }%@NL@%
  23027. %@NL@%
  23028. %@AB@%/* rewrites the character at the cursor using    */%@AE@%%@NL@%
  23029. %@AB@%/* attribute at                                  */%@AE@%%@NL@%
  23030. void Rewrite(at, page)%@NL@%
  23031. unsigned char at, page;%@NL@%
  23032. {%@NL@%
  23033.      unsigned char ch, atr;%@NL@%
  23034. %@NL@%
  23035.      Read_ch_atr(&ch, &atr, page);%@NL@%
  23036.      Write_ch_atr(ch, at, page, 1);%@NL@%
  23037. }%@NL@%
  23038. %@NL@%
  23039. %@AB@%/* obtains the current video mode */%@AE@%%@NL@%
  23040. unsigned char Getvmode()%@NL@%
  23041. {%@NL@%
  23042.     union REGS reg;%@NL@%
  23043. %@NL@%
  23044.     reg.h.ah = GETMODE;%@NL@%
  23045.     int86(VIDEO, ®, ®);%@NL@%
  23046.     return reg.h.al;%@NL@%
  23047. }%@NL@%
  23048. %@NL@%
  23049. %@AB@%/* obtains the current video page */%@AE@%%@NL@%
  23050. unsigned char Getpage()%@NL@%
  23051. {%@NL@%
  23052.     union REGS reg;%@NL@%
  23053. %@NL@%
  23054.     reg.h.ah = GETMODE;%@NL@%
  23055.     int86(VIDEO, ®, ®);%@NL@%
  23056.     return reg.h.bh;%@NL@%
  23057. }%@NL@%
  23058. %@NL@%
  23059. %@AB@%/* moves cursor one column left, but not past */%@AE@%%@NL@%
  23060. %@AB@%/* the given limit                            */%@AE@%%@NL@%
  23061. unsigned char Curslt_lim(limit)%@NL@%
  23062. unsigned char limit;%@NL@%
  23063. {%@NL@%
  23064.     unsigned char row, col, page;%@NL@%
  23065.     unsigned char status = 1;%@NL@%
  23066. %@NL@%
  23067.     Getcurs(&row, &col, page = Getpage());%@NL@%
  23068.     if (col > limit)%@NL@%
  23069.         Setcurs(row, col - 1, page);%@NL@%
  23070.     else%@NL@%
  23071.         status = 0;%@NL@%
  23072.     return status;%@NL@%
  23073. }%@NL@%
  23074. %@NL@%
  23075. %@AB@%/* moves cursor one column right, but not past */%@AE@%%@NL@%
  23076. %@AB@%/* the given limit                             */%@AE@%%@NL@%
  23077. unsigned char Cursrt_lim(limit)%@NL@%
  23078. unsigned char limit;%@NL@%
  23079. {%@NL@%
  23080.     unsigned char row, col, page;%@NL@%
  23081.     unsigned char status = 1;%@NL@%
  23082. %@NL@%
  23083.     Getcurs(&row, &col, page = Getpage());%@NL@%
  23084.     if (col < limit)%@NL@%
  23085.         Setcurs(row, col + 1, page);%@NL@%
  23086.     else%@NL@%
  23087.         status = 0;%@NL@%
  23088.     return status;%@NL@%
  23089. }%@NL@%
  23090. %@NL@%
  23091. %@AB@%/* move cursor one row down, but not past */%@AE@%%@NL@%
  23092. %@AB@%/* the given limit                        */%@AE@%%@NL@%
  23093. unsigned char Cursup_lim(limit)%@NL@%
  23094. unsigned char limit;%@NL@%
  23095. {%@NL@%
  23096.     unsigned char row, col, page;%@NL@%
  23097.     unsigned char status = 1;%@NL@%
  23098. %@NL@%
  23099.     Getcurs(&row, &col, page = Getpage());%@NL@%
  23100.     if (row > limit)%@NL@%
  23101.         Setcurs(row - 1, col, page);%@NL@%
  23102.     else%@NL@%
  23103.         status = 0;%@NL@%
  23104.     return status;%@NL@%
  23105. }%@NL@%
  23106. %@NL@%
  23107. %@AB@%/* move cursor one row down, but not past */%@AE@%%@NL@%
  23108. %@AB@%/* the given limit                        */%@AE@%%@NL@%
  23109. unsigned char Cursdn_lim(limit)%@NL@%
  23110. unsigned char limit;%@NL@%
  23111. {%@NL@%
  23112.     unsigned char row, col, page;%@NL@%
  23113.     unsigned char status = 1;%@NL@%
  23114. %@NL@%
  23115.     Getcurs(&row, &col, page = Getpage());%@NL@%
  23116.     if (row < limit)%@NL@%
  23117.         Setcurs(row + 1, col, page);%@NL@%
  23118.     else%@NL@%
  23119.         status = 0;%@NL@%
  23120.     return status;%@NL@%
  23121. }%@NL@%
  23122. %@NL@%
  23123. %@NL@%
  23124. %@2@%%@AH@%SCRINV.C%@AE@%%@EH@%%@NL@%
  23125. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\SCRINV.C%@AE@%%@NL@%
  23126. %@NL@%
  23127. %@AB@%/* scrinv.c  --  using a far pointer to access text   */%@AE@%%@NL@%
  23128. %@AB@%/*               screen memory                        */%@AE@%%@NL@%
  23129. %@NL@%
  23130. %@AI@%#define %@AE@%ROWS 25 %@NL@%
  23131. %@AI@%#define %@AE@%COLS 80 %@NL@%
  23132. %@NL@%
  23133. main()%@NL@%
  23134. {%@NL@%
  23135.     int far *screenp;%@NL@%
  23136.     int temp, i;%@NL@%
  23137. %@NL@%
  23138.     do%@NL@%
  23139.         {%@NL@%
  23140.         %@AB@%/* use 0xB800000 for EGA or VGA */%@AE@%%@NL@%
  23141.         screenp = (int far *)0xB0000000;%@NL@%
  23142. %@NL@%
  23143.         for (i = 0; i < ((ROWS*COLS)/2); ++i)%@NL@%
  23144.             {%@NL@%
  23145.             temp = screenp[i];%@NL@%
  23146.             screenp[i] = screenp[(ROWS*COLS)-i-1];%@NL@%
  23147.             screenp[(ROWS*COLS)-i-1] = temp;%@NL@%
  23148.             }%@NL@%
  23149.         } while (getch() != 'Q');%@NL@%
  23150. %@NL@%
  23151. }%@NL@%
  23152. %@NL@%
  23153. %@NL@%
  23154. %@2@%%@AH@%SCRMENU.C%@AE@%%@EH@%%@NL@%
  23155. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP11\SCRMENU.C%@AE@%%@NL@%
  23156. %@NL@%
  23157. %@AB@%/* scrmenu.c  -- uses bit fields to modify your text   */%@AE@%%@NL@%
  23158. %@AB@%/*               screen's attributes                   */%@AE@%%@NL@%
  23159. %@NL@%
  23160. char *Choice_Words[] = {%@NL@%
  23161.     "Quit",%@NL@%
  23162.     "Foreground",%@NL@%
  23163.     "Intensity",%@NL@%
  23164.     "Background",%@NL@%
  23165.     "Blinking"%@NL@%
  23166. };%@NL@%
  23167. enum Choices { %@NL@%
  23168.     Quit, %@NL@%
  23169.     Foreground, %@NL@%
  23170.     Intensity, %@NL@%
  23171.     Background, %@NL@%
  23172.     Blinking %@NL@%
  23173. };%@NL@%
  23174. %@NL@%
  23175. %@AB@%/* use 0xB800000 for EGA or VGA */%@AE@%%@NL@%
  23176. %@AI@%#define %@AE@%SCR_START (0xB0000000) %@NL@%
  23177. %@AI@%#define %@AE@%SCR_SIZE (25 * 80) %@NL@%
  23178. %@NL@%
  23179. main()%@NL@%
  23180. {%@NL@%
  23181.     enum Choices choice;%@NL@%
  23182. %@NL@%
  23183.     printf("Select from the following by number:\n");%@NL@%
  23184. %@NL@%
  23185.     for (choice = Quit; choice <= Blinking; ++choice)%@NL@%
  23186.         {%@NL@%
  23187.         printf("%d. %s\n", choice, Choice_Words[choice]);%@NL@%
  23188.         }%@NL@%
  23189. %@NL@%
  23190.     printf("\nWhich: ");%@NL@%
  23191.     do %@NL@%
  23192.         {%@NL@%
  23193.         choice = getch();%@NL@%
  23194.         choice -= '0';%@NL@%
  23195.         if (choice < Foreground || choice > Blinking)%@NL@%
  23196.             continue;%@NL@%
  23197.         Redraw(choice);%@NL@%
  23198.         } while (choice != Quit);%@NL@%
  23199. %@NL@%
  23200. }%@NL@%
  23201. %@NL@%
  23202. Redraw(enum Choices field)%@NL@%
  23203. {%@NL@%
  23204.     struct screen_char {%@NL@%
  23205.         unsigned int character  :8,%@NL@%
  23206.                      foreground :3,%@NL@%
  23207.                      intensity  :1,%@NL@%
  23208.                      background :3,%@NL@%
  23209.                      blink      :1;%@NL@%
  23210.     } scrchar, far *sp, far *ep;%@NL@%
  23211. %@NL@%
  23212.     sp = (struct screen_char far *)SCR_START;%@NL@%
  23213.     ep = sp + SCR_SIZE;%@NL@%
  23214. %@NL@%
  23215.     while (sp < ep)%@NL@%
  23216.         {%@NL@%
  23217.         scrchar = *sp;%@NL@%
  23218.         switch (field)%@NL@%
  23219.             {%@NL@%
  23220.             case Foreground:%@NL@%
  23221.                 scrchar.foreground = (scrchar.foreground)? 0 : 7;%@NL@%
  23222.                 break;%@NL@%
  23223.             case Intensity:%@NL@%
  23224.                 scrchar.intensity = (scrchar.intensity)? 0 : 1;%@NL@%
  23225.                 break;%@NL@%
  23226.             case Background:%@NL@%
  23227.                 scrchar.background = (scrchar.background)? 0 : 7;%@NL@%
  23228.                 break;%@NL@%
  23229.             case Blinking:%@NL@%
  23230.                 scrchar.blink = (scrchar.blink)? 0 : 1;%@NL@%
  23231.                 break;%@NL@%
  23232.             }%@NL@%
  23233.         *(sp++) = scrchar;%@NL@%
  23234.         }%@NL@%
  23235. }%@NL@%
  23236. %@NL@%
  23237. %@NL@%
  23238. %@2@%%@AH@%SCRREST.C%@AE@%%@EH@%%@NL@%
  23239. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP10\SCRREST.C%@AE@%%@NL@%
  23240. %@NL@%
  23241. %@AB@%/* scrrest.c  --  demonstrates read() by restoring */%@AE@%%@NL@%
  23242. %@AB@%/*                text screen from any file.       */%@AE@%%@NL@%
  23243. %@NL@%
  23244. <stdio.h>        %@AB@%/* for stderr              */%@AE@%%@NL@%
  23245. <fcntl.h>        %@AB@%/* for O_RDONLY | O_BINARY */%@AE@%%@NL@%
  23246. %@NL@%
  23247. %@AI@%#define %@AE@%SCRCHARS  (25 * 80) %@NL@%
  23248. int Buf[SCRCHARS];%@NL@%
  23249. %@NL@%
  23250. main(argc, argv)%@NL@%
  23251. int argc;%@NL@%
  23252. char *argv[];%@NL@%
  23253. {%@NL@%
  23254.     int *cp, *ep;%@NL@%
  23255.     int far *sp;%@NL@%
  23256.     int fd_in, bytes;%@NL@%
  23257. %@NL@%
  23258.     if (argc != 2)%@NL@%
  23259.         {%@NL@%
  23260.         fprintf(stderr, "usage: scrrest file.scr\n");%@NL@%
  23261.         exit(0);%@NL@%
  23262.         }%@NL@%
  23263.     if ((fd_in = open(argv[1], O_RDONLY | O_BINARY)) < 0)%@NL@%
  23264.         {%@NL@%
  23265.         fprintf(stderr, "\"%s\": Can't open to read.\n", argv[1]);%@NL@%
  23266.         exit(1);%@NL@%
  23267.         }%@NL@%
  23268.     %@AB@%/* Read it. */%@AE@%%@NL@%
  23269.     bytes = read(fd_in, Buf, SCRCHARS * 2);%@NL@%
  23270.     if (bytes < 0)%@NL@%
  23271.         {%@NL@%
  23272.         fprintf(stderr, "\"%s\": Error Reading.\n", argv[1]);%@NL@%
  23273.         exit(1);%@NL@%
  23274.         }%@NL@%
  23275.     if (bytes == 0)%@NL@%
  23276.         {%@NL@%
  23277.         fprintf(stderr, "\"%s\": Empty File.\n", argv[1]);%@NL@%
  23278.         exit(1);%@NL@%
  23279.         }%@NL@%
  23280.     %@AB@%/* Copy the buffer to screen memory. */%@AE@%%@NL@%
  23281.     ep = &Buf[bytes / 2];%@NL@%
  23282.     cp = Buf;%@NL@%
  23283. %@NL@%
  23284.     %@AB@%/* use 0xB800000 for EGA or VGA */%@AE@%%@NL@%
  23285.     sp = (int far *)(0xB0000000);%@NL@%
  23286.     for (; cp < ep; ++cp, ++sp)%@NL@%
  23287.         *sp = *cp;%@NL@%
  23288. }%@NL@%
  23289. %@NL@%
  23290. %@NL@%
  23291. %@2@%%@AH@%SCRSAVE.C%@AE@%%@EH@%%@NL@%
  23292. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP10\SCRSAVE.C%@AE@%%@NL@%
  23293. %@NL@%
  23294. %@AB@%/* scrsave.c  --  demonstrates write() by saving the */%@AE@%%@NL@%
  23295. %@AB@%/*                text screen to a file.             */%@AE@%%@NL@%
  23296. %@NL@%
  23297. <stdio.h>        %@AB@%/* for stderr              */%@AE@%%@NL@%
  23298. <fcntl.h>        %@AB@%/* for O_CREAT | O_BINARY  */%@AE@%%@NL@%
  23299. <sys\types.h>    %@AB@%/* for stat.h              */%@AE@%%@NL@%
  23300. <sys\stat.h>     %@AB@%/* for S_IREAD | S_IWRITE  */%@AE@%%@NL@%
  23301. %@NL@%
  23302. %@AI@%#define %@AE@%SCRCHARS  (25 * 80) %@NL@%
  23303. int Buf[SCRCHARS];%@NL@%
  23304. %@NL@%
  23305. main(argc, argv)%@NL@%
  23306. int argc;%@NL@%
  23307. char *argv[];%@NL@%
  23308. {%@NL@%
  23309.     int *cp, *ep, fname[16];%@NL@%
  23310.     int far *sp;%@NL@%
  23311.     int fd_out, bytes;%@NL@%
  23312. %@NL@%
  23313.     if (argc != 2)%@NL@%
  23314.         {%@NL@%
  23315.         fprintf(stderr, "usage: scrsave file\n");%@NL@%
  23316.         exit(0);%@NL@%
  23317.         }%@NL@%
  23318.     if (strlen(argv[1]) > 8)%@NL@%
  23319.         {%@NL@%
  23320.         fprintf(stderr, "\"%s\": File name too long.\n", argv[1]);%@NL@%
  23321.         exit(1);%@NL@%
  23322.         }%@NL@%
  23323.     strcpy(fname, argv[1]);%@NL@%
  23324.     strcat(fname, ".SCR");%@NL@%
  23325.     if (access(fname, 0) == 0)%@NL@%
  23326.         {%@NL@%
  23327.         fprintf(stderr, "\"%s\": Won't overwrite.\n", fname);%@NL@%
  23328.         exit(1);%@NL@%
  23329.         }%@NL@%
  23330.     if ((fd_out = open(fname, O_WRONLY|O_CREAT|O_BINARY,%@NL@%
  23331.                        S_IREAD|S_IWRITE)) < 0)%@NL@%
  23332.         {%@NL@%
  23333.         fprintf(stderr, "\"%s\": Can't create.\n", fname);%@NL@%
  23334.         exit(1);%@NL@%
  23335.         }%@NL@%
  23336.     %@AB@%/* Copy the screen into a near buffer. */%@AE@%%@NL@%
  23337.     ep = &Buf[SCRCHARS - 1];%@NL@%
  23338.     cp = Buf;%@NL@%
  23339.     %@AB@%/* use 0xB800000 for EGA or VGA */%@AE@%%@NL@%
  23340.     sp = (int far *)(0xB0000000);%@NL@%
  23341.     for (; cp < ep; ++cp, ++sp)%@NL@%
  23342.         *cp = *sp;%@NL@%
  23343.     %@AB@%/* Write it. */%@AE@%%@NL@%
  23344.     bytes = write(fd_out, Buf, SCRCHARS * 2);%@NL@%
  23345.     if (bytes != SCRCHARS * 2)%@NL@%
  23346.         {%@NL@%
  23347.         fprintf(stderr, "\"%s\": Error writing.\n", fname);%@NL@%
  23348.         exit(1);%@NL@%
  23349.         }%@NL@%
  23350. }%@NL@%
  23351. %@NL@%
  23352. %@NL@%
  23353. %@2@%%@AH@%SETCURS.C%@AE@%%@EH@%%@NL@%
  23354. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\SETCURS.C%@AE@%%@NL@%
  23355. %@NL@%
  23356. %@AB@%/* setcurs.c -- moves cursor, checks out Setcurs() */%@AE@%%@NL@%
  23357. %@AI@%#include %@AE@%<dos.h> %@NL@%
  23358. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  23359. %@AI@%#define %@AE@%VIDEO 0x10 %@NL@%
  23360. %@AI@%#define %@AE@%SETCURSOR 2 %@NL@%
  23361. void Setcurs(unsigned char, unsigned char,%@NL@%
  23362.              unsigned char);%@NL@%
  23363. main()%@NL@%
  23364. {%@NL@%
  23365.     int row, col;%@NL@%
  23366. %@NL@%
  23367.     printf("Enter row and column: (q to quit)\n");%@NL@%
  23368.     while (scanf("%d %d", &row, &col) == 2)%@NL@%
  23369.     {%@NL@%
  23370.         Setcurs(row, col, 0);%@NL@%
  23371.         printf("Enter row and column: (q to quit)");%@NL@%
  23372.     }%@NL@%
  23373. }%@NL@%
  23374. %@NL@%
  23375. %@AB@%/* Setcurs() -- sets cursor to row, column, and page */%@AE@%%@NL@%
  23376. void Setcurs(row, col, page)%@NL@%
  23377. unsigned char row, col, page;%@NL@%
  23378. {%@NL@%
  23379.     union REGS reg;%@NL@%
  23380. %@NL@%
  23381.     reg.h.ah = SETCURSOR;%@NL@%
  23382.     reg.h.dh = row;%@NL@%
  23383.     reg.h.dl = col;%@NL@%
  23384.     reg.h.bh = page;%@NL@%
  23385.     int86(VIDEO, ®, ®);%@NL@%
  23386. }%@NL@%
  23387. %@NL@%
  23388. %@NL@%
  23389. %@2@%%@AH@%SHIFTADD.C%@AE@%%@EH@%%@NL@%
  23390. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\SHIFTADD.C%@AE@%%@NL@%
  23391. %@NL@%
  23392. %@AB@%/* shiftadd.c -- shift and add numbers               */%@AE@%%@NL@%
  23393. %@NL@%
  23394. main()%@NL@%
  23395. {%@NL@%
  23396.     int x = 0x12;%@NL@%
  23397.     int y;%@NL@%
  23398. %@NL@%
  23399.     y = x << 8 + 2;%@NL@%
  23400.     printf("y is 0x%x\n", y);%@NL@%
  23401. }%@NL@%
  23402. %@NL@%
  23403. %@NL@%
  23404. %@NL@%
  23405. %@2@%%@AH@%SHORTIF.C%@AE@%%@EH@%%@NL@%
  23406. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP05\SHORTIF.C%@AE@%%@NL@%
  23407. %@NL@%
  23408. %@AB@%/* shortif.c -- shows 'shorthand' IF / ELSE   */%@AE@%%@NL@%
  23409. %@AB@%/*           -- gets absolute value of number */%@AE@%%@NL@%
  23410. %@NL@%
  23411. main()%@NL@%
  23412. {%@NL@%
  23413.     int num, pos, abs;%@NL@%
  23414.     printf("Enter a whole number: ");%@NL@%
  23415.     scanf("%d", &num);%@NL@%
  23416. %@NL@%
  23417.     pos = (num >= 0); %@AB@%/* is number positive? */%@AE@%%@NL@%
  23418. %@NL@%
  23419.     abs = (pos) ? num : -num;  %@AB@%/* assigns negative of */%@AE@%%@NL@%
  23420.                       %@AB@%/* number if number is negative */%@AE@%%@NL@%
  23421.     if (pos)%@NL@%
  23422.         printf ("The number is positive.\n");%@NL@%
  23423.     else%@NL@%
  23424.         printf("The number is negative.\n");%@NL@%
  23425.     printf("Absolute value of number is: %d\n", abs);%@NL@%
  23426. }%@NL@%
  23427. %@NL@%
  23428. %@NL@%
  23429. %@2@%%@AH@%SHOW2.C%@AE@%%@EH@%%@NL@%
  23430. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\SHOW2.C%@AE@%%@NL@%
  23431. %@NL@%
  23432. %@AB@%/* show2.c  --     shows how to use main()'s envp */%@AE@%%@NL@%
  23433. %@NL@%
  23434. <stdio.h>        %@AB@%/* for NULL */%@AE@%%@NL@%
  23435. %@NL@%
  23436. main(argc, argv, envp)%@NL@%
  23437. int argc;%@NL@%
  23438. char *argv[], *envp[];%@NL@%
  23439. {%@NL@%
  23440.     int i;%@NL@%
  23441. %@NL@%
  23442.     printf("argc = %d\n", argc);%@NL@%
  23443.     printf("\n");%@NL@%
  23444. %@NL@%
  23445.     for (i = 0; i < argc; ++i)%@NL@%
  23446.         {%@NL@%
  23447.         printf("argv[%d] -> \"%s\"\n", i, argv[i]);%@NL@%
  23448.         }%@NL@%
  23449.     printf("argv[%d] -> NULL\n", i);%@NL@%
  23450.     printf("\n");%@NL@%
  23451. %@NL@%
  23452.     for (i= 0; envp[i] != NULL; ++i)%@NL@%
  23453.         {%@NL@%
  23454.         printf("envp[%d] -> \"%s\"\n", i, envp[i]);%@NL@%
  23455.         }%@NL@%
  23456.     printf("envp[%d] -> NULL\n", i);%@NL@%
  23457. %@NL@%
  23458. }%@NL@%
  23459. %@NL@%
  23460. %@NL@%
  23461. %@2@%%@AH@%SHOWARGS.C%@AE@%%@EH@%%@NL@%
  23462. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\SHOWARGS.C%@AE@%%@NL@%
  23463. %@NL@%
  23464. %@AB@%/* showargs.c  --  shows how to access the arguments   */%@AE@%%@NL@%
  23465. %@AB@%/*                 passed to main()                    */%@AE@%%@NL@%
  23466. %@NL@%
  23467. <stdio.h>        %@AB@%/* for NULL */%@AE@%%@NL@%
  23468. %@NL@%
  23469. main(argc, argv)%@NL@%
  23470. int argc;%@NL@%
  23471. char *argv[];%@NL@%
  23472. {%@NL@%
  23473.     int i;%@NL@%
  23474. %@NL@%
  23475.     printf("argc = %d\n", argc);%@NL@%
  23476.     printf("\n");%@NL@%
  23477. %@NL@%
  23478.     for (i = 0; i < argc; ++i)%@NL@%
  23479.         {%@NL@%
  23480.         printf("argv[%d] -> \"%s\"\n", i, argv[i]);%@NL@%
  23481.         }%@NL@%
  23482.     printf("argv[%d] -> NULL\n", i);%@NL@%
  23483.     printf("\n");%@NL@%
  23484. %@NL@%
  23485. }%@NL@%
  23486. %@NL@%
  23487. %@NL@%
  23488. %@2@%%@AH@%SHOWCODE.C%@AE@%%@EH@%%@NL@%
  23489. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\SHOWCODE.C%@AE@%%@NL@%
  23490. %@NL@%
  23491. %@AB@%/*  showcode.c -- shows ASCII and scan codes for    */%@AE@%%@NL@%
  23492. %@AB@%/*                keystrokes                        */%@AE@%%@NL@%
  23493. %@AB@%/* Note: Set Screen Swapping On in the Debug menu.  */%@AE@%%@NL@%
  23494. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  23495. %@AI@%#include %@AE@%<dos.h> %@NL@%
  23496. KEYINTR 0x16  %@AB@%/* keyboard read interrupt */%@AE@%%@NL@%
  23497. GETCHAR 0     %@AB@%/* read scancode function  */%@AE@%%@NL@%
  23498. ESC '\033'    %@AB@%/* escape key              */%@AE@%%@NL@%
  23499. struct SCANCODE {%@NL@%
  23500.                 unsigned char ascii;  %@AB@%/* ascii code */%@AE@%%@NL@%
  23501.                 unsigned char scan;   %@AB@%/* scan code  */%@AE@%%@NL@%
  23502.                 };%@NL@%
  23503. struct SCANCODE Readkey();%@NL@%
  23504. %@NL@%
  23505. main()%@NL@%
  23506. {%@NL@%
  23507.     struct SCANCODE keys;%@NL@%
  23508. %@NL@%
  23509.     printf("Press keys to see their scancodes. ");%@NL@%
  23510.     printf("Press the Esc key to quit.\n");%@NL@%
  23511.     do  {%@NL@%
  23512.         keys = Readkey();%@NL@%
  23513.         if (keys.ascii > 0 && keys.ascii < 040)%@NL@%
  23514.             printf("^%c: ascii = %3d, scan = %3d\n",%@NL@%
  23515.                     keys.ascii + 0100, keys.ascii,%@NL@%
  23516.                     keys.scan);%@NL@%
  23517.         else if (keys.ascii >= 40)%@NL@%
  23518.             printf(" %c: ascii = %3d, scan = %3d\n",%@NL@%
  23519.                     keys.ascii, keys.ascii, keys.scan);%@NL@%
  23520.         else%@NL@%
  23521.             printf("  : ascii = %3d, scan = %3d\n",%@NL@%
  23522.                     keys.ascii, keys.scan);%@NL@%
  23523.         } while (keys.ascii != ESC);%@NL@%
  23524. }%@NL@%
  23525. %@NL@%
  23526. struct SCANCODE Readkey()%@NL@%
  23527. {%@NL@%
  23528.     union REGS reg;%@NL@%
  23529.     struct SCANCODE scancode;%@NL@%
  23530. %@NL@%
  23531.     reg.h.ah = GETCHAR;%@NL@%
  23532.     int86(KEYINTR, ®, ®);%@NL@%
  23533.     scancode.ascii = reg.h.al;%@NL@%
  23534.     scancode.scan = reg.h.ah;%@NL@%
  23535.     return (scancode);%@NL@%
  23536. }%@NL@%
  23537. %@NL@%
  23538. %@NL@%
  23539. %@2@%%@AH@%SPECS.C%@AE@%%@EH@%%@NL@%
  23540. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\SPECS.C%@AE@%%@NL@%
  23541. %@NL@%
  23542. %@AB@%/* specs.c -- shows printf() format  */%@AE@%%@NL@%
  23543. %@AB@%/*            specifiers for numbers */%@AE@%%@NL@%
  23544. %@NL@%
  23545. main()%@NL@%
  23546. {%@NL@%
  23547.     int    i = 122;       %@AB@%/* ASCII code for 'z' */%@AE@%%@NL@%
  23548.     long   l = 93000000;  %@AB@%/* distance to Sun (miles) */%@AE@%%@NL@%
  23549.     float  f = 192450.88; %@AB@%/* someone's bottom line */%@AE@%%@NL@%
  23550.     double d = 2.0e+030;  %@AB@%/* mass of Sun (kg.) */%@AE@%%@NL@%
  23551. %@NL@%
  23552.     printf("%d\n", i);  %@AB@%/* integer as decimal */%@AE@%%@NL@%
  23553.     printf("%x\n", i);  %@AB@%/* integer as hex */%@AE@%%@NL@%
  23554.     printf("%ld\n", l); %@AB@%/* long */%@AE@%%@NL@%
  23555.     printf("%f\n", f);  %@AB@%/* float as decimal */%@AE@%%@NL@%
  23556.     printf("%e\n", f);  %@AB@%/* float as exponential */%@AE@%%@NL@%
  23557.     printf("%f\n", d);  %@AB@%/* double as decimal */%@AE@%%@NL@%
  23558.     printf("%d\n", d);  %@AB@%/* double as exponential */%@AE@%%@NL@%
  23559. }%@NL@%
  23560. %@NL@%
  23561. %@NL@%
  23562. %@2@%%@AH@%SQUARE.C%@AE@%%@EH@%%@NL@%
  23563. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\SQUARE.C%@AE@%%@NL@%
  23564. %@NL@%
  23565. %@AB@%/* square.c  --  a quiz to demonstrate passing        */%@AE@%%@NL@%
  23566. %@AB@%/*               pointers and addresses in functions  */%@AE@%%@NL@%
  23567. %@NL@%
  23568. main()%@NL@%
  23569. {%@NL@%
  23570.     int val, count, guess;%@NL@%
  23571. %@NL@%
  23572.     for (count = 1; count < 255; ++count)%@NL@%
  23573.         {%@NL@%
  23574.         val = count;%@NL@%
  23575.         printf("\nWhat is the square of %d?\n", val);%@NL@%
  23576.         if (scanf("%d", &guess) != 1)%@NL@%
  23577.             return(0);           %@AB@%/* non-number exits   */%@AE@%%@NL@%
  23578. %@NL@%
  23579.         if(Square(&val) != 0)    %@AB@%/* pass val's address */%@AE@%%@NL@%
  23580.             {%@NL@%
  23581.             printf("Range Error\n");%@NL@%
  23582.             exit(1);%@NL@%
  23583.             }%@NL@%
  23584.         if (val != guess)%@NL@%
  23585.             printf("Wrong. It is %d.\n", val);%@NL@%
  23586.         else%@NL@%
  23587.             printf("Right!\n");%@NL@%
  23588.         printf("Continue? ");%@NL@%
  23589.         if (getche() != 'y')%@NL@%
  23590.             break;%@NL@%
  23591.         }%@NL@%
  23592. }%@NL@%
  23593. %@NL@%
  23594. int Square(int *where)%@NL@%
  23595. {%@NL@%
  23596.     if (*where > 181 || *where < -181)%@NL@%
  23597.         return (-1);%@NL@%
  23598.     *where = (*where) * (*where);%@NL@%
  23599.     return (0);%@NL@%
  23600. }%@NL@%
  23601. %@NL@%
  23602. %@NL@%
  23603. %@2@%%@AH@%SQUARE2.C%@AE@%%@EH@%%@NL@%
  23604. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\SQUARE2.C%@AE@%%@NL@%
  23605. %@NL@%
  23606.             %@AB@%/* LISTING 8-2 */%@AE@%%@NL@%
  23607. %@NL@%
  23608. %@AB@%/* square.c  --  A quiz to demonstrate passing        */%@AE@%%@NL@%
  23609. %@AB@%/*               pointers and addresses in functions. */%@AE@%%@NL@%
  23610. %@NL@%
  23611. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  23612. %@NL@%
  23613. main()%@NL@%
  23614.     {%@NL@%
  23615.     int val, count, guess;%@NL@%
  23616. %@NL@%
  23617.     for (count = 1; count < 255; ++count)%@NL@%
  23618.         {%@NL@%
  23619.         val = count;%@NL@%
  23620.         printf("\nWhat is the square of %d?\n", val);%@NL@%
  23621.         if (scanf(" %d", &guess) != 1)%@NL@%
  23622.             return(0);           %@AB@%/* non-number exits   */%@AE@%%@NL@%
  23623. %@NL@%
  23624.         if(Square(&val) != 0)    %@AB@%/* pass val's address */%@AE@%%@NL@%
  23625.             {%@NL@%
  23626.             printf("Range Error\n");%@NL@%
  23627.             exit(1);%@NL@%
  23628.             }%@NL@%
  23629. %@NL@%
  23630.         if (val != guess)%@NL@%
  23631.             printf("Wrong. It is %d.\n", val);%@NL@%
  23632.         else%@NL@%
  23633.             printf("Right!\n");%@NL@%
  23634. %@NL@%
  23635. %@NL@%
  23636.         printf("Continue? ");%@NL@%
  23637.         if (getche() != 'y')%@NL@%
  23638.             break;%@NL@%
  23639.         }%@NL@%
  23640.     return (0);%@NL@%
  23641.     }%@NL@%
  23642. %@NL@%
  23643. int Square(int *where)%@NL@%
  23644.     {%@NL@%
  23645.     if (*where > 181 || *where < -181)%@NL@%
  23646.         return (-1);%@NL@%
  23647.     *where = (*where) * (*where);%@NL@%
  23648.     return (0);%@NL@%
  23649.     }%@NL@%
  23650. %@NL@%
  23651. %@NL@%
  23652. %@2@%%@AH@%STATIC.C%@AE@%%@EH@%%@NL@%
  23653. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\STATIC.C%@AE@%%@NL@%
  23654. %@NL@%
  23655. %@AB@%/* static.c -- demonstrates a static variable */%@AE@%%@NL@%
  23656. %@AB@%/*             that holds count of lines,     */%@AE@%%@NL@%
  23657. %@AB@%/*             words, and characters          */%@AE@%%@NL@%
  23658. %@NL@%
  23659. main()%@NL@%
  23660.     {%@NL@%
  23661.     void countline();%@NL@%
  23662.     printf("Type some lines of text.\n");%@NL@%
  23663.     printf("Start a line with a . to quit.\n\n");%@NL@%
  23664. %@NL@%
  23665.     while (getche() != '.')%@NL@%
  23666.         countline();  %@AB@%/* accumulate word and */%@AE@%%@NL@%
  23667.                       %@AB@%/* line counts         */%@AE@%%@NL@%
  23668.     }%@NL@%
  23669. %@NL@%
  23670. void countline()%@NL@%
  23671.     {%@NL@%
  23672.     static int words = 0; %@AB@%/* static variables */%@AE@%%@NL@%
  23673.     static int chars = 0;%@NL@%
  23674.     char ch;%@NL@%
  23675.     ++chars; %@AB@%/* count char typed when */%@AE@%%@NL@%
  23676.              %@AB@%/* function was called   */%@AE@%%@NL@%
  23677. %@NL@%
  23678.     while ((ch = getche()) != '\r')%@NL@%
  23679.         {%@NL@%
  23680.         ++chars;%@NL@%
  23681.         if (ch == ' ')%@NL@%
  23682.             ++words;%@NL@%
  23683.         }%@NL@%
  23684.         ++words; %@AB@%/* count last word */%@AE@%%@NL@%
  23685. %@NL@%
  23686.     printf("\nWords so far %d. Chars. so far %d\n", words, chars);%@NL@%
  23687.     }%@NL@%
  23688. %@NL@%
  23689. %@NL@%
  23690. %@2@%%@AH@%STRINGS.C%@AE@%%@EH@%%@NL@%
  23691. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\STRINGS.C%@AE@%%@NL@%
  23692. %@NL@%
  23693. %@AB@%/* strings.c -- shows two ways to print */%@AE@%%@NL@%
  23694. %@AB@%/*              a string with printf()  */%@AE@%%@NL@%
  23695. %@NL@%
  23696. main()%@NL@%
  23697. {%@NL@%
  23698.     printf("This uses a string literal by itself\n");%@NL@%
  23699.     printf("%s", "This plugs the literal into %s\n");%@NL@%
  23700. }%@NL@%
  23701. %@NL@%
  23702. %@NL@%
  23703. %@2@%%@AH@%STRINGS.C%@AE@%%@EH@%%@NL@%
  23704. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP10\STRINGS.C%@AE@%%@NL@%
  23705. %@NL@%
  23706. %@AB@%/* strings.c  --  opens a file and searches it for */%@AE@%%@NL@%
  23707. %@AB@%/*                possible strings                 */%@AE@%%@NL@%
  23708. %@NL@%
  23709. <stdio.h>       %@AB@%/* for FILE, BUFSIZ & EOF */%@AE@%%@NL@%
  23710. <ctype.h>       %@AB@%/* for isprint()          */%@AE@%%@NL@%
  23711. %@NL@%
  23712. main(argc, argv)%@NL@%
  23713. int argc;%@NL@%
  23714. char *argv[];%@NL@%
  23715. {%@NL@%
  23716.     FILE *fp;%@NL@%
  23717.     char buf[BUFSIZ];%@NL@%
  23718.     int ch, count;%@NL@%
  23719. %@NL@%
  23720.     if (argc != 2)%@NL@%
  23721.         {%@NL@%
  23722.         fprintf(stderr, "usage: strings file\n");%@NL@%
  23723.         exit(1);%@NL@%
  23724.         }%@NL@%
  23725.     if ((fp = fopen(argv[1], "rb")) == NULL)%@NL@%
  23726.         {%@NL@%
  23727.         fprintf(stderr, "Can't open %s\n", argv[1]);%@NL@%
  23728.         exit(1);%@NL@%
  23729.         }%@NL@%
  23730. %@NL@%
  23731.     count = 0;%@NL@%
  23732.     while ((ch = fgetc(fp)) != EOF)%@NL@%
  23733.         {%@NL@%
  23734.         if (! isprint(ch) || count >= (BUFSIZ - 1))%@NL@%
  23735.             {%@NL@%
  23736.             if (count > 5)%@NL@%
  23737.                 {%@NL@%
  23738.                 buf[count] = 0;%@NL@%
  23739.                 puts(buf);%@NL@%
  23740.                 }%@NL@%
  23741.             count = 0;%@NL@%
  23742.             continue;%@NL@%
  23743.             }%@NL@%
  23744.         buf[count++] = ch;%@NL@%
  23745.         }%@NL@%
  23746. }%@NL@%
  23747. %@NL@%
  23748. %@NL@%
  23749. %@2@%%@AH@%STRIO.C%@AE@%%@EH@%%@NL@%
  23750. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP13\STRIO.C%@AE@%%@NL@%
  23751. %@NL@%
  23752. %@AB@%/* strio.c -- uses cgets() and cputs()                   */%@AE@%%@NL@%
  23753. %@AB@%/* program list -- strio.c (cgets() not in core library) */%@AE@%%@NL@%
  23754. %@AI@%#include %@AE@%<conio.h> %@NL@%
  23755. %@AI@%#define %@AE@%MAXSIZE 6 %@NL@%
  23756. main()%@NL@%
  23757. {%@NL@%
  23758.      char store[MAXSIZE + 2];%@NL@%
  23759. %@NL@%
  23760.      store[0] = MAXSIZE; %@AB@%/* put limit in first element */%@AE@%%@NL@%
  23761.      cputs("What's your name?\n\r");%@NL@%
  23762.      cgets(store);%@NL@%
  23763.      cputs("\n\rI'll remember you, ");%@NL@%
  23764.      cputs(store + 2);%@NL@%
  23765.      cputs("!\n\r");%@NL@%
  23766. }%@NL@%
  23767. %@NL@%
  23768. %@NL@%
  23769. %@2@%%@AH@%STRPOOL.C%@AE@%%@EH@%%@NL@%
  23770. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\STRPOOL.C%@AE@%%@NL@%
  23771. %@NL@%
  23772. %@AB@%/* strpool.c  -- dumps the string pool to show how     */%@AE@%%@NL@%
  23773. %@AB@%/*               quoted strings are stored             */%@AE@%%@NL@%
  23774. %@NL@%
  23775. %@AI@%#define %@AE@%PHRASE \ %@NL@%
  23776. "This is one long sentence that the compiler \%@NL@%
  23777. combines into a single string."%@NL@%
  23778. %@NL@%
  23779. char Start[]        = "start";%@NL@%
  23780. char Long_phrase[]  = PHRASE;%@NL@%
  23781. char Short_phrase[] = "This is a short phrase";%@NL@%
  23782. char Cent_string[]  = "\x9b";%@NL@%
  23783. %@NL@%
  23784. main()%@NL@%
  23785. {%@NL@%
  23786.     static char local_phrase[] = "This is local";%@NL@%
  23787.     char *cp;%@NL@%
  23788. %@NL@%
  23789.     printf("Dump of the string pool:\n");%@NL@%
  23790.     printf("-----------------------\n");%@NL@%
  23791. %@NL@%
  23792.     printf("\"");                %@AB@%/* print leading quote */%@AE@%%@NL@%
  23793. %@NL@%
  23794.     %@AB@%/*%@NL@%
  23795. %@AB@%     * Note that the address of a string can be%@NL@%
  23796. %@AB@%     * assigned to a pointer: cp = Start%@NL@%
  23797. %@AB@%     */%@AE@%%@NL@%
  23798.     for (cp = Start; *cp != '^'; ++cp)%@NL@%
  23799.         {%@NL@%
  23800.         if (*cp == '\0')        %@AB@%/* print '\0' as a quote */%@AE@%%@NL@%
  23801.             printf("\"\n\"");%@NL@%
  23802.         else if (*cp == '\n' )  %@AB@%/* print '\n' as '\' 'n' */%@AE@%%@NL@%
  23803.             printf("\\n");%@NL@%
  23804.         else%@NL@%
  23805.             printf("%c", *cp);%@NL@%
  23806.         }%@NL@%
  23807.     printf("^");                %@AB@%/* marks end */%@AE@%%@NL@%
  23808. }%@NL@%
  23809. %@NL@%
  23810. %@NL@%
  23811. %@2@%%@AH@%SUMNUMS.C%@AE@%%@EH@%%@NL@%
  23812. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\SUMNUMS.C%@AE@%%@NL@%
  23813. %@NL@%
  23814. %@AB@%/* sumnums.c -- type mismatch in function arguments  */%@AE@%%@NL@%
  23815. %@AB@%/*              No function prototyping              */%@AE@%%@NL@%
  23816. %@NL@%
  23817. int sums();%@NL@%
  23818. %@NL@%
  23819. main()%@NL@%
  23820. {%@NL@%
  23821.     float a = 10.0;%@NL@%
  23822.     float b = 20.0;%@NL@%
  23823.     int c;%@NL@%
  23824. %@NL@%
  23825.     c = sums(a, b);%@NL@%
  23826.     printf("sum of %.1f and %.1f is %d\n", a, b, c); ;%@NL@%
  23827. }%@NL@%
  23828. %@NL@%
  23829. int sums(x, y)%@NL@%
  23830. int x, y;%@NL@%
  23831. {%@NL@%
  23832.     return (x + y);%@NL@%
  23833. }%@NL@%
  23834. %@NL@%
  23835. %@NL@%
  23836. %@NL@%
  23837. %@2@%%@AH@%SUMNUMS2.C%@AE@%%@EH@%%@NL@%
  23838. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\SUMNUMS2.C%@AE@%%@NL@%
  23839. %@NL@%
  23840. %@AB@%/* sumnums2.c -- type mismatch in function arguments  */%@AE@%%@NL@%
  23841. %@AB@%/*               Function prototyping              */%@AE@%%@NL@%
  23842. %@NL@%
  23843. int sums(int, int);%@NL@%
  23844. main()%@NL@%
  23845. {%@NL@%
  23846.     float a = 10.0;%@NL@%
  23847.     float b = 20.0;%@NL@%
  23848.     int c;%@NL@%
  23849. %@NL@%
  23850.     c = sums(a, b);%@NL@%
  23851.     printf("sum of %.1f and %.1f is %d\n", a, b, c);%@NL@%
  23852. }%@NL@%
  23853. int sums(x, y)%@NL@%
  23854. int x, y;%@NL@%
  23855. {%@NL@%
  23856.     return ( x + y);%@NL@%
  23857. }%@NL@%
  23858. %@NL@%
  23859. %@NL@%
  23860. %@NL@%
  23861. %@2@%%@AH@%SWITCH.C%@AE@%%@EH@%%@NL@%
  23862. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP05\SWITCH.C%@AE@%%@NL@%
  23863. %@NL@%
  23864. %@AB@%/* switch.c -- demonstrates switch statement */%@AE@%%@NL@%
  23865. %@AB@%/*             prints values according       */%@AE@%%@NL@%
  23866. %@AB@%/*             to user's choice              */%@AE@%%@NL@%
  23867. %@NL@%
  23868. <math.h> %@AB@%/* for sqrt() */%@AE@%%@NL@%
  23869. %@AI@%#define %@AE@%TRUE 1 %@NL@%
  23870. main()%@NL@%
  23871. {%@NL@%
  23872.     char choice;   %@AB@%/* routine wanted by user   */%@AE@%%@NL@%
  23873.     int number;    %@AB@%/* number entered by user   */%@AE@%%@NL@%
  23874. %@NL@%
  23875.     while (TRUE)   %@AB@%/* endless loop */%@AE@%%@NL@%
  23876.     {%@NL@%
  23877.         printf("\nSelect value wanted:\n");%@NL@%
  23878.         printf("o = octal  h = hex   s = square\n");%@NL@%
  23879.         printf("r = square root  q = quit: ");%@NL@%
  23880.         choice = getche(); printf("\n");%@NL@%
  23881. %@NL@%
  23882.         if (choice == 'q')%@NL@%
  23883.             break; %@AB@%/* exits WHILE loop; ends program */%@AE@%%@NL@%
  23884. %@NL@%
  23885.         %@AB@%/* rest of program executed if base <> 'q' */%@AE@%%@NL@%
  23886.             printf("Enter a whole number: ");%@NL@%
  23887.             scanf("%d", &number);%@NL@%
  23888. %@NL@%
  23889.         switch (choice) %@AB@%/* print according to */%@AE@%%@NL@%
  23890.                         %@AB@%/* choice requested   */%@AE@%%@NL@%
  23891.             {%@NL@%
  23892.             case 'o':   %@AB@%/* print octal */%@AE@%%@NL@%
  23893.                 printf("Result: %o\n", number);%@NL@%
  23894.                 break;  %@AB@%/* break here in each case    */%@AE@%%@NL@%
  23895.                         %@AB@%/* exits the switch statement */%@AE@%%@NL@%
  23896. %@NL@%
  23897.             case 'h':   %@AB@%/* print hex */%@AE@%%@NL@%
  23898.                 printf("Result: %x\n", number);%@NL@%
  23899.                 break;%@NL@%
  23900. %@NL@%
  23901.             case 's':   %@AB@%/* square */%@AE@%%@NL@%
  23902.                 printf("Result: %d\n", number * number);%@NL@%
  23903.                 break;%@NL@%
  23904. %@NL@%
  23905.             case 'r':   %@AB@%/* square root */%@AE@%%@NL@%
  23906.                 printf("Result: %f\n", sqrt(number) );%@NL@%
  23907.                 break;%@NL@%
  23908. %@NL@%
  23909.             default:%@NL@%
  23910.                 printf("Choice must be o, h, s, r, or q\n");%@NL@%
  23911.             }%@NL@%
  23912.      }%@NL@%
  23913. }%@NL@%
  23914. %@NL@%
  23915. %@NL@%
  23916. %@2@%%@AH@%TABLE.C%@AE@%%@EH@%%@NL@%
  23917. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\TABLE.C%@AE@%%@NL@%
  23918. %@NL@%
  23919. %@NL@%
  23920. %@AB@%/* TABLE.C -- prints square root, square, and cube */%@AE@%%@NL@%
  23921. %@AB@%/*            for the numbers 1 thru 9             */%@AE@%%@NL@%
  23922. %@NL@%
  23923. <math.h> %@AB@%/* include math functions so we  */%@AE@%%@NL@%
  23924.                   %@AB@%/*  can do square root           */%@AE@%%@NL@%
  23925. %@NL@%
  23926. main()%@NL@%
  23927. {%@NL@%
  23928.     int i;%@NL@%
  23929.     printf("i\t sqrt(i)\tsquare(i)\tcube(i)\n\n");%@NL@%
  23930.     for (i = 1; i < 10; i++)%@NL@%
  23931.         %@AB@%/* beginning of body of loop */%@AE@%%@NL@%
  23932.         {%@NL@%
  23933.         printf("%d\t", i);%@NL@%
  23934.         printf("%f\t", sqrt(i));%@NL@%
  23935.         printf("%d\t\t", i * i);%@NL@%
  23936.         printf("%d\n", i * i * i);%@NL@%
  23937.         }%@NL@%
  23938.         %@AB@%/* end of body of loop */%@AE@%%@NL@%
  23939. }%@NL@%
  23940. %@NL@%
  23941. %@NL@%
  23942. %@NL@%
  23943. %@2@%%@AH@%TABS.C%@AE@%%@EH@%%@NL@%
  23944. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\TABS.C%@AE@%%@NL@%
  23945. %@NL@%
  23946. %@AB@%/* tabs.c -- shows formatting with the \t */%@AE@%%@NL@%
  23947. %@AB@%/*           tab escape sequence          */%@AE@%%@NL@%
  23948. %@NL@%
  23949. main()%@NL@%
  23950. {%@NL@%
  23951.     int    q1 = 338, q2 = 57, q3 = 1048, q4 = 778,%@NL@%
  23952.            %@AB@%/* quantity in bin */%@AE@%%@NL@%
  23953.            t1 = 6, t2 = 8, t3 = 12, t4 = 16;%@NL@%
  23954.            %@AB@%/* threads per inch */%@AE@%%@NL@%
  23955. %@NL@%
  23956.     float  s1 = 0.250, s2 = 0.500, s3 = 0.750, s4 = 1.0;%@NL@%
  23957.            %@AB@%/* size in inches */%@AE@%%@NL@%
  23958. %@NL@%
  23959.     %@AB@%/* print table header */%@AE@%%@NL@%
  23960.     printf("number\t\t size\t\t threads\n");%@NL@%
  23961.     printf("in bin\t\t (inches)\t per inch\n\n");%@NL@%
  23962. %@NL@%
  23963.     %@AB@%/* print lines of table */%@AE@%%@NL@%
  23964.     printf("%d\t\t %f\t %d\n", q1, s1, t1);%@NL@%
  23965.     printf("%d\t\t %f\t %d\n", q2, s2, t2);%@NL@%
  23966.     printf("%d\t\t %f\t %d\n", q3, s3, t3);%@NL@%
  23967.     printf("%d\t\t %f\t %d\n", q4, s4, t4);%@NL@%
  23968. }%@NL@%
  23969. %@NL@%
  23970. %@NL@%
  23971. %@2@%%@AH@%TEST.C%@AE@%%@EH@%%@NL@%
  23972. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\TEST.C%@AE@%%@NL@%
  23973. %@NL@%
  23974. %@AB@%/* test.c -- test the routines in basic.lib */%@AE@%%@NL@%
  23975. %@AB@%/* Program list: test.c and basic.lib       */%@AE@%%@NL@%
  23976. %@NL@%
  23977. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  23978. %@NL@%
  23979. main()%@NL@%
  23980. {%@NL@%
  23981.     static char string[] = "This is a test.";%@NL@%
  23982.     char *cp, *Leftstr(), *Midstr(), *Rightstr();%@NL@%
  23983. %@NL@%
  23984.     printf("Testing: \"%s\"\n", string);%@NL@%
  23985. %@NL@%
  23986.     if ((cp = Leftstr(string, 4)) == NULL)%@NL@%
  23987.         {%@NL@%
  23988.         printf("Error in Leftstr()\n");%@NL@%
  23989.         exit(1);%@NL@%
  23990.         }%@NL@%
  23991.     printf("Leftstr() returned: \"%s\"\n", cp);%@NL@%
  23992. %@NL@%
  23993.     if ((cp = Midstr(string, 4, 5)) == NULL)%@NL@%
  23994.         {%@NL@%
  23995.         printf("Error in Midstr()\n");%@NL@%
  23996.         exit(1);%@NL@%
  23997.         }%@NL@%
  23998.     printf("Midstr() returned: \"%s\"\n", cp);%@NL@%
  23999. %@NL@%
  24000.     if ((cp = Rightstr(string, 5)) == NULL)%@NL@%
  24001.         {%@NL@%
  24002.         printf("Error in Rightstr()\n");%@NL@%
  24003.         exit(1);%@NL@%
  24004.         }%@NL@%
  24005.     printf("Rightstr() returned: \"%s\"\n", cp);%@NL@%
  24006. }%@NL@%
  24007. %@NL@%
  24008. %@NL@%
  24009. %@2@%%@AH@%TESTER.C%@AE@%%@EH@%%@NL@%
  24010. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP16\TESTER.C%@AE@%%@NL@%
  24011. %@NL@%
  24012. %@AB@%/* tester.c -- demonstrates the assert() macro        */%@AE@%%@NL@%
  24013. %@AB@%/* Program list: tester.c (to link math functions)    */%@AE@%%@NL@%
  24014. %@NL@%
  24015. %@AI@%#include %@AE@%<assert.h> %@NL@%
  24016. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  24017. %@AI@%#include %@AE@%<math.h> %@NL@%
  24018. main()%@NL@%
  24019. {%@NL@%
  24020.     float s1 = 3.0;%@NL@%
  24021.     float s2 = 4.0;%@NL@%
  24022.     float sumsq;%@NL@%
  24023.     float hypot;%@NL@%
  24024. %@NL@%
  24025.     sumsq = s1*s1 - s2*s2;%@NL@%
  24026.     assert(sumsq >= 0);%@NL@%
  24027.     hypot = sqrt(sumsq);%@NL@%
  24028.     printf("hypotenuse is %.2f\n", hypot);%@NL@%
  24029. }%@NL@%
  24030. %@NL@%
  24031. %@NL@%
  24032. %@2@%%@AH@%TEXED.C%@AE@%%@EH@%%@NL@%
  24033. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP12\TEXED.C%@AE@%%@NL@%
  24034. %@NL@%
  24035. %@AB@%/* texed.c  --  main entry point to the editor; the   */%@AE@%%@NL@%
  24036. %@AB@%/*              menu and signal handlers are here     */%@AE@%%@NL@%
  24037. %@NL@%
  24038. main(argc, argv)%@NL@%
  24039. int argc;%@NL@%
  24040. char *argv[];%@NL@%
  24041. {%@NL@%
  24042.     char ch;%@NL@%
  24043. %@NL@%
  24044.     while (1)%@NL@%
  24045.         {%@NL@%
  24046.         printf("\nTexEd Main Menu\n");%@NL@%
  24047.         printf("Select from:\n");%@NL@%
  24048.         printf("0) Quit\n\n");%@NL@%
  24049.         printf("1) Load File\n");%@NL@%
  24050.         printf("2) Save File\n");%@NL@%
  24051.         printf("3) Edit File\n");%@NL@%
  24052.         printf("Which: ");%@NL@%
  24053.         do%@NL@%
  24054.             {%@NL@%
  24055.             ch = getch();%@NL@%
  24056.             ch -= '0';%@NL@%
  24057.             } while (ch < 0 || ch > 3);%@NL@%
  24058.         printf("%d\n\n", (int)ch);%@NL@%
  24059.         switch(ch)%@NL@%
  24060.             {%@NL@%
  24061.             case 0: exit(0);%@NL@%
  24062.             case 1: Load_file(); break;%@NL@%
  24063.             case 2: Save_file(); break;%@NL@%
  24064.             case 3: Edit_file(); break;%@NL@%
  24065.             }%@NL@%
  24066.         }%@NL@%
  24067. }%@NL@%
  24068. %@NL@%
  24069. %@NL@%
  24070. %@2@%%@AH@%TIMER.C%@AE@%%@EH@%%@NL@%
  24071. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\TIMER.C%@AE@%%@NL@%
  24072. %@NL@%
  24073. %@NL@%
  24074. %@AB@%/* TIMER.C -- uses do loop to    */%@AE@%%@NL@%
  24075. %@AB@%/*            check elapsed time */%@AE@%%@NL@%
  24076. %@NL@%
  24077. %@AI@%#include %@AE@%<time.h> %@NL@%
  24078. %@NL@%
  24079. main()%@NL@%
  24080. {%@NL@%
  24081.     long start, end, %@AB@%/* starting and ending times */%@AE@%%@NL@%
  24082.                      %@AB@%/* measured in seconds since */%@AE@%%@NL@%
  24083.                      %@AB@%/* Jan. 1, 1970 */%@AE@%%@NL@%
  24084.          ltime;      %@AB@%/* used to get val from time function */%@AE@%%@NL@%
  24085.     int seconds;     %@AB@%/* elapsed time to be set */%@AE@%%@NL@%
  24086. %@NL@%
  24087.     printf("QuickC Egg Timer\n");%@NL@%
  24088.     printf("Enter time to set in seconds: ");%@NL@%
  24089.     scanf("%d", &seconds);%@NL@%
  24090.     start = time(<ime);  %@AB@%/* get system elapsed seconds */%@AE@%%@NL@%
  24091.                            %@AB@%/* since 1-1-70 */%@AE@%%@NL@%
  24092.     end = start + seconds; %@AB@%/* calculate alarm time */%@AE@%%@NL@%
  24093. %@NL@%
  24094.     do%@NL@%
  24095.        ;                       %@AB@%/* null statement for loop body */%@AE@%%@NL@%
  24096.     while (time(<ime) < end);   %@AB@%/* wait for alarm time  */%@AE@%%@NL@%
  24097. %@NL@%
  24098.     printf("Time's Up!\a\a\a\n");%@NL@%
  24099. }%@NL@%
  24100. %@NL@%
  24101. %@NL@%
  24102. %@NL@%
  24103. %@2@%%@AH@%TIMER2.C%@AE@%%@EH@%%@NL@%
  24104. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP06\TIMER2.C%@AE@%%@NL@%
  24105. %@NL@%
  24106. %@AB@%/* timer2.c -- interval timer             */%@AE@%%@NL@%
  24107. %@AB@%/*             calls delay(), uses beep() */%@AE@%%@NL@%
  24108. %@NL@%
  24109. main()%@NL@%
  24110. {%@NL@%
  24111.     %@AB@%/* function declarations */%@AE@%%@NL@%
  24112.     void beep (times);%@NL@%
  24113.     void delay (seconds);%@NL@%
  24114. %@NL@%
  24115.     %@AB@%/* variable declarations */%@AE@%%@NL@%
  24116.     int seconds, interval, tick;%@NL@%
  24117. %@NL@%
  24118.     printf("Set for how many seconds? ");%@NL@%
  24119.     scanf("%d", &seconds);%@NL@%
  24120.     printf("Interval to show in seconds? ");%@NL@%
  24121.     scanf("%d", &interval);%@NL@%
  24122.     printf("Press a key to start timing\n");%@NL@%
  24123.     getch();%@NL@%
  24124. %@NL@%
  24125.     tick = 0;                %@AB@%/* run "clock" for */%@AE@%%@NL@%
  24126.     while (tick < seconds)   %@AB@%/* time specified  */%@AE@%%@NL@%
  24127.         {%@NL@%
  24128.         delay(interval); %@AB@%/* wait interval seconds */%@AE@%%@NL@%
  24129.         tick += interval;%@NL@%
  24130.         printf("%d\n", tick);%@NL@%
  24131.         beep(1);%@NL@%
  24132.         }%@NL@%
  24133.     beep(3);%@NL@%
  24134. }%@NL@%
  24135. %@NL@%
  24136. void delay(seconds)%@NL@%
  24137.                %@AB@%/* wait for number of seconds specified */%@AE@%%@NL@%
  24138.                %@AB@%/* see TIMER.C in chapter 4 for details */%@AE@%%@NL@%
  24139.                %@AB@%/* on the library function time().      */%@AE@%%@NL@%
  24140. %@NL@%
  24141. int seconds;   %@AB@%/* parameter declaration */%@AE@%%@NL@%
  24142. {%@NL@%
  24143.     %@AB@%/* variable declarations */%@AE@%%@NL@%
  24144.     long start, end, %@AB@%/* starting and ending times */%@AE@%%@NL@%
  24145.                      %@AB@%/* measured in seconds since */%@AE@%%@NL@%
  24146.                      %@AB@%/* Jan. 1, 1970 */%@AE@%%@NL@%
  24147.          ltime;      %@AB@%/* used to get val from time function */%@AE@%%@NL@%
  24148. %@NL@%
  24149.     start = time(<ime);  %@AB@%/* get system elapsed seconds */%@AE@%%@NL@%
  24150.                            %@AB@%/* since 1-1-70 */%@AE@%%@NL@%
  24151.     end = start + seconds; %@AB@%/* calculate alarm time */%@AE@%%@NL@%
  24152. %@NL@%
  24153.     do%@NL@%
  24154.        ;                          %@AB@%/* null statement for loop body */%@AE@%%@NL@%
  24155.     while (time(<ime) < end);   %@AB@%/* wait for end of time  */%@AE@%%@NL@%
  24156. }%@NL@%
  24157. %@NL@%
  24158. void beep(times)%@NL@%
  24159.     %@AB@%/* parameter declaration */%@AE@%%@NL@%
  24160. int times;%@NL@%
  24161. {%@NL@%
  24162.     %@AB@%/* variable declaration  */%@AE@%%@NL@%
  24163.     int count;%@NL@%
  24164. %@NL@%
  24165. %@NL@%
  24166.     %@AB@%/* check that parameter is between 1 and 4 */%@AE@%%@NL@%
  24167.     if ((times < 1) || (times > 4))%@NL@%
  24168.         {%@NL@%
  24169.         printf("Error in beep(): %d beeps specified.\n",%@NL@%
  24170.             times);%@NL@%
  24171.         printf("Specify one to four beeps");%@NL@%
  24172.         }%@NL@%
  24173.     else %@AB@%/* sound the beeps */%@AE@%%@NL@%
  24174.         for (count = 1; count <= times; count++)%@NL@%
  24175.             printf("\a");  %@AB@%/* "alert" escape sequence */%@AE@%%@NL@%
  24176. }%@NL@%
  24177. %@NL@%
  24178. %@NL@%
  24179. %@2@%%@AH@%TINY.C%@AE@%%@EH@%%@NL@%
  24180. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\TINY.C%@AE@%%@NL@%
  24181. %@NL@%
  24182. %@AB@%/* tiny.c -- the smallest possible C */%@AE@%%@NL@%
  24183. %@AB@%/*           program with comments   */%@AE@%%@NL@%
  24184. %@NL@%
  24185. main() %@AB@%/* function name and argument list */%@AE@%%@NL@%
  24186. {%@NL@%
  24187.        %@AB@%/* function definition in braces */%@AE@%%@NL@%
  24188. }%@NL@%
  24189. %@NL@%
  24190. %@NL@%
  24191. %@2@%%@AH@%TODAY.C%@AE@%%@EH@%%@NL@%
  24192. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP11\TODAY.C%@AE@%%@NL@%
  24193. %@NL@%
  24194. %@AB@%/* today.c  -- demonstrates using enum  */%@AE@%%@NL@%
  24195. %@NL@%
  24196. main()%@NL@%
  24197. {%@NL@%
  24198.     enum week_days {%@NL@%
  24199.         monday = 1,    %@AB@%/* start with 1 */%@AE@%%@NL@%
  24200.         tuesday,%@NL@%
  24201.         wednesday,%@NL@%
  24202.         thursday,%@NL@%
  24203.         friday,%@NL@%
  24204.         saturday,%@NL@%
  24205.         sunday%@NL@%
  24206.     } pay_day;%@NL@%
  24207. %@NL@%
  24208.     static char *day_names[] = {%@NL@%
  24209.         "",%@NL@%
  24210.         "monday",%@NL@%
  24211.         "tuesday",%@NL@%
  24212.         "wednesday",%@NL@%
  24213.         "thursday",%@NL@%
  24214.         "friday",%@NL@%
  24215.         "saturday",%@NL@%
  24216.         "sunday"%@NL@%
  24217.     };%@NL@%
  24218. %@NL@%
  24219.     printf("What day do you want to be paid on?\n");%@NL@%
  24220. %@NL@%
  24221.     for (pay_day = monday; pay_day <= sunday; ++pay_day)%@NL@%
  24222.         {%@NL@%
  24223.         printf("%d. %s\n", pay_day, day_names[pay_day]);%@NL@%
  24224.         }%@NL@%
  24225.     %@NL@%
  24226.     printf("Which (%d-%d): ", monday, sunday);%@NL@%
  24227. %@NL@%
  24228.     do%@NL@%
  24229.         {%@NL@%
  24230.         pay_day = getch();%@NL@%
  24231.         pay_day -= '0';%@NL@%
  24232.         } while (pay_day < monday || pay_day > sunday);%@NL@%
  24233. %@NL@%
  24234.     printf("%d\n\n", pay_day);%@NL@%
  24235. %@NL@%
  24236.     printf("You selected %s\n", day_names[pay_day]);%@NL@%
  24237. %@NL@%
  24238. }%@NL@%
  24239. %@NL@%
  24240. %@NL@%
  24241. %@2@%%@AH@%TOTAL.C%@AE@%%@EH@%%@NL@%
  24242. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\TOTAL.C%@AE@%%@NL@%
  24243. %@NL@%
  24244. %@AB@%/* total.c  -- how to build an array on the fly    */%@AE@%%@NL@%
  24245. %@NL@%
  24246. <stdio.h>         %@AB@%/* for NULL   */%@AE@%%@NL@%
  24247. <malloc.h>        %@AB@%/* for size_t */%@AE@%%@NL@%
  24248. %@NL@%
  24249. main()%@NL@%
  24250. {%@NL@%
  24251.     int *iptr, count = 0, i, total;%@NL@%
  24252.     size_t bytes = sizeof(int);%@NL@%
  24253. %@NL@%
  24254.     %@AB@%/* Start the array with room for one value. */%@AE@%%@NL@%
  24255.     if ((iptr = malloc(bytes)) == NULL)%@NL@%
  24256.         {%@NL@%
  24257.         printf("Oops, malloc failed\n");%@NL@%
  24258.         exit(1);%@NL@%
  24259.         }%@NL@%
  24260. %@NL@%
  24261.     printf("Enter as many integer values as you want.\n");%@NL@%
  24262.     printf("I will build an array on the fly with them.\n");%@NL@%
  24263.     printf("(Any non-number means you are done.)\n");%@NL@%
  24264. %@NL@%
  24265.     while (scanf("%d", &iptr[count]) == 1)%@NL@%
  24266.         {%@NL@%
  24267.         ++count;%@NL@%
  24268.         %@AB@%/* Enlarge the array. */%@AE@%%@NL@%
  24269.         if ((iptr = realloc(iptr,bytes*(count+1))) == NULL)%@NL@%
  24270.             {%@NL@%
  24271.             printf("Oops, realloc failed\n");%@NL@%
  24272.             exit(1);%@NL@%
  24273.             }%@NL@%
  24274.         }%@NL@%
  24275.     total = 0;%@NL@%
  24276.     printf("You entered:\n");%@NL@%
  24277.     for (i = 0; i < count; i++)%@NL@%
  24278.         {%@NL@%
  24279.         printf("iptr[%d] = %d\n", i, iptr[i]);%@NL@%
  24280.         total += iptr[i];%@NL@%
  24281.         }%@NL@%
  24282.     printf("\nTotal: %d\n", total);%@NL@%
  24283.     return (0);%@NL@%
  24284. }%@NL@%
  24285. %@NL@%
  24286. %@NL@%
  24287. %@2@%%@AH@%TOTAL2.C%@AE@%%@EH@%%@NL@%
  24288. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP08\TOTAL2.C%@AE@%%@NL@%
  24289. %@NL@%
  24290. %@AB@%/* total2.c -- how to build an array on the fly       */%@AE@%%@NL@%
  24291. %@AB@%/*             using sbrk()                           */%@AE@%%@NL@%
  24292. %@NL@%
  24293. <stdio.h>        %@AB@%/* for NULL   */%@AE@%%@NL@%
  24294. <malloc.h>       %@AB@%/* for size_t */%@AE@%%@NL@%
  24295. %@NL@%
  24296. main()%@NL@%
  24297.     {%@NL@%
  24298.     int *iptr, count = 0, i, total;%@NL@%
  24299.     size_t bytes = sizeof(int);%@NL@%
  24300. %@NL@%
  24301.     %@AB@%/* Start the array with room for one value. */%@AE@%%@NL@%
  24302.     iptr = sbrk(0);%@NL@%
  24303.     if (sbrk(bytes) == (int *)-1)%@NL@%
  24304.         {%@NL@%
  24305.         printf("Oops, sbrk failed\n");%@NL@%
  24306.         exit(1);%@NL@%
  24307.         }%@NL@%
  24308. %@NL@%
  24309.     printf("Enter as many integer values as you want.\n");%@NL@%
  24310.     printf("I will build an array on the fly with them.\n");%@NL@%
  24311.     printf("(Any non-number means you are done.)\n");%@NL@%
  24312. %@NL@%
  24313.     while (scanf("%d", &iptr[count]) == 1)%@NL@%
  24314.         {%@NL@%
  24315.         ++count;%@NL@%
  24316.         %@AB@%/* Enlarge the array. */%@AE@%%@NL@%
  24317.         if (sbrk(bytes) == (int *)-1)%@NL@%
  24318.             {%@NL@%
  24319.             printf("Oops, sbrk failed\n");%@NL@%
  24320.             exit(1);%@NL@%
  24321.             }%@NL@%
  24322.         }%@NL@%
  24323.     total = 0;%@NL@%
  24324.     for (i = 0; i < count; i++)%@NL@%
  24325.         total += iptr[i];%@NL@%
  24326.     %@AB@%/* just print the total this time */%@AE@%%@NL@%
  24327.     printf( "%d\n", total);%@NL@%
  24328. }%@NL@%
  24329. %@NL@%
  24330. %@NL@%
  24331. %@2@%%@AH@%TRUTH.C%@AE@%%@EH@%%@NL@%
  24332. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\TRUTH.C%@AE@%%@NL@%
  24333. %@NL@%
  24334. %@AB@%/* truth.c -- shows logical operators */%@AE@%%@NL@%
  24335. %@NL@%
  24336. main()%@NL@%
  24337. {%@NL@%
  24338.     printf("1 AND 1 is %d\n", 1 && 1);%@NL@%
  24339.     printf("1 AND 0 is %d\n", 1 && 0);%@NL@%
  24340.     printf("0 AND 0 is %d\n", 0 && 0);%@NL@%
  24341.     printf("1 OR 1 is %d\n",  1 || 1);%@NL@%
  24342.     printf("1 OR 0 is %d\n",  1 || 0);%@NL@%
  24343.     printf("0 OR 0 is %d\n",  0 || 0);%@NL@%
  24344. }%@NL@%
  24345. %@NL@%
  24346. %@NL@%
  24347. %@2@%%@AH@%TTT.C%@AE@%%@EH@%%@NL@%
  24348. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\TTT.C%@AE@%%@NL@%
  24349. %@NL@%
  24350. %@AB@%/* ttt.c    --  a tic-tac-toe game demonstrates  */%@AE@%%@NL@%
  24351. %@AB@%/*              passing two-dimensional arrays   */%@AE@%%@NL@%
  24352. %@AB@%/*              to functions                     */%@AE@%%@NL@%
  24353. %@NL@%
  24354. main()%@NL@%
  24355. {%@NL@%
  24356.     static char board[3][3] = {%@NL@%
  24357.         { '-', '-', '-' },%@NL@%
  24358.         { '-', '-', '-' },%@NL@%
  24359.         { '-', '-', '-' },%@NL@%
  24360.     };%@NL@%
  24361.     int row, col, ch;%@NL@%
  24362.     extern char Check_winner();%@NL@%
  24363.     extern void Make_move(), Draw_field();%@NL@%
  24364. %@NL@%
  24365.     printf("You are X and make the first move.\n");%@NL@%
  24366.     while (1)%@NL@%
  24367.         {%@NL@%
  24368.         printf("Specify coordinate for your X.\n");%@NL@%
  24369.         printf("(eg, as a2, or Q to quit)\n");%@NL@%
  24370. %@NL@%
  24371.         %@AB@%/* Print the square. */%@AE@%%@NL@%
  24372.         Draw_field(board);%@NL@%
  24373. %@NL@%
  24374.         %@AB@%/* Input the user's coordinates. */%@AE@%%@NL@%
  24375.         if ((row = getch()) == 'Q')%@NL@%
  24376.             exit(0);%@NL@%
  24377.         row -= 'a';%@NL@%
  24378.         col = getch() - '1';%@NL@%
  24379. %@NL@%
  24380.         %@AB@%/* Check for a legal move. */%@AE@%%@NL@%
  24381.         if (row < 0 || row > 2 || col < 0 || col > 2)%@NL@%
  24382.             {%@NL@%
  24383.             printf("Bad Square Specification\n\n");%@NL@%
  24384.             continue;%@NL@%
  24385.             }%@NL@%
  24386.         if (board[row][col] != '-')%@NL@%
  24387.             {%@NL@%
  24388.             printf("Sorry, Square Occupied\n\n");%@NL@%
  24389.             continue;%@NL@%
  24390.             }%@NL@%
  24391. %@NL@%
  24392.         %@AB@%/* Make the move. */%@AE@%%@NL@%
  24393.         board[row][col] = 'X';%@NL@%
  24394.         if ((ch = Check_winner(board)) != '-' || ch == 't')%@NL@%
  24395.             break;%@NL@%
  24396.         Make_move(board);%@NL@%
  24397.         if ((ch = Check_winner(board)) != '-' || ch == 't')%@NL@%
  24398.             break;%@NL@%
  24399.         }%@NL@%
  24400.     Draw_field(board);%@NL@%
  24401.     if (ch == 't')%@NL@%
  24402.         printf("It's a tie!\n");%@NL@%
  24403.     else if (ch == 'X')%@NL@%
  24404.         printf("You win!\n");%@NL@%
  24405.     else%@NL@%
  24406.         printf("I win!\n");%@NL@%
  24407. }%@NL@%
  24408. %@NL@%
  24409. char Check_winner(char field[][3])%@NL@%
  24410. {%@NL@%
  24411.     int row, col;%@NL@%
  24412. %@NL@%
  24413.     for (row = col = 0; row < 3; ++row, ++col)%@NL@%
  24414.         {%@NL@%
  24415.         if (field[row][0] != '-'             %@AB@%/* horizontal */%@AE@%%@NL@%
  24416.                 && field[row][0] == field[row][1]%@NL@%
  24417.                 && field[row][1] == field[row][2] )%@NL@%
  24418.             {%@NL@%
  24419.             return(field[row][0]);%@NL@%
  24420.             }%@NL@%
  24421.         if (field[0][col] != '-'             %@AB@%/* vertical */%@AE@%%@NL@%
  24422.                 && field[0][col] == field[1][col]%@NL@%
  24423.                 && field[1][col] == field[2][col] )%@NL@%
  24424.             {%@NL@%
  24425.             return(field[0][col]);%@NL@%
  24426.             }%@NL@%
  24427.         }%@NL@%
  24428.     if (field[0][0] != '-'         %@AB@%/* right diagonal */%@AE@%%@NL@%
  24429.             && field[0][0] == field[1][1]%@NL@%
  24430.             && field[1][1] == field[2][2] )%@NL@%
  24431.         {%@NL@%
  24432.         return(field[0][0]);%@NL@%
  24433.         }%@NL@%
  24434.     if (field[0][2] != '-'         %@AB@%/* left diagonal */%@AE@%%@NL@%
  24435.             && field[0][2] == field[1][1]%@NL@%
  24436.             && field[1][1] == field[2][0] )%@NL@%
  24437.         {%@NL@%
  24438.         return(field[0][2]);%@NL@%
  24439.         }%@NL@%
  24440. %@NL@%
  24441.     for (row = 0; row < 3; ++row)        %@AB@%/* any moves left */%@AE@%%@NL@%
  24442.         {%@NL@%
  24443.         for (col = 0; col < 3; ++col)%@NL@%
  24444.             {%@NL@%
  24445.             if (field[row][col] == '-')%@NL@%
  24446.                 {%@NL@%
  24447.                 return('-');%@NL@%
  24448.                 }%@NL@%
  24449.             }%@NL@%
  24450.         }%@NL@%
  24451.     return ('t');%@NL@%
  24452. }%@NL@%
  24453. %@NL@%
  24454. void Make_move(char field[3][3])%@NL@%
  24455. {%@NL@%
  24456.     int row, col;%@NL@%
  24457. %@NL@%
  24458.     for (row = 2; row >= 0; --row)%@NL@%
  24459.         {%@NL@%
  24460.         for (col = 2; col >= 0; --col)%@NL@%
  24461.             {%@NL@%
  24462.             if (field[row][col] == '-')%@NL@%
  24463.                 {%@NL@%
  24464.                 field[row][col] = 'O';%@NL@%
  24465.                 return;%@NL@%
  24466.                 }%@NL@%
  24467.             }%@NL@%
  24468.         }%@NL@%
  24469. }%@NL@%
  24470. %@NL@%
  24471. void Draw_field(char field[][3])%@NL@%
  24472. {%@NL@%
  24473.     int row, col;%@NL@%
  24474. %@NL@%
  24475.     printf("\n   1  2  3\n\n");%@NL@%
  24476.     for (row = 0; row < 3; ++row)%@NL@%
  24477.         {%@NL@%
  24478.         printf("%c ", 'a'+row);%@NL@%
  24479.         for (col = 0; col < 3; ++col)%@NL@%
  24480.             {%@NL@%
  24481.             printf(" %c ", field[row][col] );%@NL@%
  24482.             }%@NL@%
  24483.         printf("\n");%@NL@%
  24484.         }%@NL@%
  24485.     printf("\n");%@NL@%
  24486. }%@NL@%
  24487. %@NL@%
  24488. %@NL@%
  24489. %@2@%%@AH@%UDEMO.C%@AE@%%@EH@%%@NL@%
  24490. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP11\UDEMO.C%@AE@%%@NL@%
  24491. %@NL@%
  24492. %@AB@%/* udemo.c  --  demonstrates a union at work */%@AE@%%@NL@%
  24493. %@NL@%
  24494. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  24495. %@NL@%
  24496. char *Strings[6] = {%@NL@%
  24497.         "Quit",%@NL@%
  24498.         "line of text",%@NL@%
  24499.         "floating point double value",%@NL@%
  24500.         "long integer value",%@NL@%
  24501.         "floating point value",%@NL@%
  24502.         "integer value"%@NL@%
  24503. };%@NL@%
  24504. %@NL@%
  24505. struct Unitstruct {%@NL@%
  24506.     union {%@NL@%
  24507.         char   wtype[BUFSIZ];%@NL@%
  24508.         double dtype;%@NL@%
  24509.         long   ltype;%@NL@%
  24510.         float  ftype;%@NL@%
  24511.         int    itype;%@NL@%
  24512.     } manyu;%@NL@%
  24513.     int type_in_union;%@NL@%
  24514. };%@NL@%
  24515. %@NL@%
  24516. main()%@NL@%
  24517. {%@NL@%
  24518.     struct Unitstruct one_of_many;%@NL@%
  24519. %@NL@%
  24520.     while ((one_of_many.type_in_union = Menu()) != 0)%@NL@%
  24521.         {%@NL@%
  24522.         Inputval(&one_of_many);%@NL@%
  24523.         Printval(&one_of_many);%@NL@%
  24524.         }%@NL@%
  24525. }%@NL@%
  24526. %@NL@%
  24527. Inputval(struct Unitstruct *one_of_many)%@NL@%
  24528.     {%@NL@%
  24529.     printf("\nEnter a %s: ", Strings[one_of_many->type_in_union]);%@NL@%
  24530.     switch(one_of_many->type_in_union)%@NL@%
  24531.         {%@NL@%
  24532.         case 1: %@NL@%
  24533.             fgets(one_of_many->manyu.wtype, BUFSIZ, stdin);%@NL@%
  24534.             break;%@NL@%
  24535.         case 2:%@NL@%
  24536.             scanf("%lf", &(one_of_many->manyu.dtype));%@NL@%
  24537.             while (getchar() != '\n');%@NL@%
  24538.             break;%@NL@%
  24539.         case 3:%@NL@%
  24540.             scanf("%ld", &(one_of_many->manyu.ltype));%@NL@%
  24541.             while (getchar() != '\n');%@NL@%
  24542.             break;%@NL@%
  24543.         case 4:%@NL@%
  24544.             scanf("%f", &(one_of_many->manyu.ftype));%@NL@%
  24545.             while (getchar() != '\n');%@NL@%
  24546.             break;%@NL@%
  24547.         case 5:%@NL@%
  24548.             scanf("%i", &(one_of_many->manyu.itype));%@NL@%
  24549.             while (getchar() != '\n');%@NL@%
  24550.             break;%@NL@%
  24551.         }%@NL@%
  24552. }%@NL@%
  24553. %@NL@%
  24554. Printval(struct Unitstruct *one_of_many)%@NL@%
  24555. {%@NL@%
  24556.     printf("The %s you entered\nwas: ", Strings[one_of_many->type_in_union]);%@NL@%
  24557.     switch (one_of_many->type_in_union)%@NL@%
  24558.         {%@NL@%
  24559.         case 1: %@NL@%
  24560.             fputs(one_of_many->manyu.wtype, stdout);%@NL@%
  24561.             break;%@NL@%
  24562.         case 2:%@NL@%
  24563.             printf("%lf", one_of_many->manyu.dtype);%@NL@%
  24564.             break;%@NL@%
  24565.         case 3:%@NL@%
  24566.             printf("%ld", one_of_many->manyu.ltype);%@NL@%
  24567.             break;%@NL@%
  24568.         case 4:%@NL@%
  24569.             printf("%f", one_of_many->manyu.ftype);%@NL@%
  24570.             break;%@NL@%
  24571.         case 5:%@NL@%
  24572.             printf("%i", one_of_many->manyu.itype);%@NL@%
  24573.             break;%@NL@%
  24574.         }%@NL@%
  24575.     printf("\n\n");%@NL@%
  24576. }%@NL@%
  24577. %@NL@%
  24578. Menu()%@NL@%
  24579. {%@NL@%
  24580.     int i;%@NL@%
  24581.     char ch;%@NL@%
  24582. %@NL@%
  24583.     for (i = 0; i < 6; ++i)%@NL@%
  24584.         {%@NL@%
  24585.         printf("%d) %s\n", i, Strings[i]);%@NL@%
  24586.         }%@NL@%
  24587.     printf("Which: ");%@NL@%
  24588.     do%@NL@%
  24589.         {%@NL@%
  24590.         ch = getch();%@NL@%
  24591.         } while (ch < '0' || ch > '5');%@NL@%
  24592.     printf("%c\n", ch);%@NL@%
  24593.     return (ch - '0');%@NL@%
  24594. }%@NL@%
  24595. %@NL@%
  24596. %@NL@%
  24597. %@2@%%@AH@%UNDOVER.C%@AE@%%@EH@%%@NL@%
  24598. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\UNDOVER.C%@AE@%%@NL@%
  24599. %@NL@%
  24600. %@AB@%/* undover.c -- illustrates the effect of underinitializing and */%@AE@%%@NL@%
  24601. %@AB@%/*              overinitializing arrays.                        */%@AE@%%@NL@%
  24602. %@NL@%
  24603. int Primes[6] = { 1, 2, 3, 5, 7, 11 };%@NL@%
  24604. %@AI@%#define %@AE@%NUMP (sizeof(Primes)/sizeof(int)) %@NL@%
  24605. %@NL@%
  24606. main()%@NL@%
  24607. {%@NL@%
  24608.     int i;%@NL@%
  24609. %@NL@%
  24610.     printf("The first %d primes are: ", NUMP);%@NL@%
  24611.     for (i = 0; i < NUMP; ++i)%@NL@%
  24612.         {%@NL@%
  24613.         printf("%d ", Primes[i]);%@NL@%
  24614.         }%@NL@%
  24615.     printf("\n");%@NL@%
  24616. }%@NL@%
  24617. %@NL@%
  24618. %@NL@%
  24619. %@2@%%@AH@%UPITY.C%@AE@%%@EH@%%@NL@%
  24620. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP10\UPITY.C%@AE@%%@NL@%
  24621. %@NL@%
  24622. %@AB@%/* upity.c --  makes an uppercase copy of a file using */%@AE@%%@NL@%
  24623. %@AB@%/*             fread() and fwrite()                    */%@AE@%%@NL@%
  24624. %@NL@%
  24625. <string.h>        %@AB@%/* for strrchr() */%@AE@%%@NL@%
  24626. <stdio.h>         %@AB@%/* for NULL      */%@AE@%%@NL@%
  24627. <malloc.h>        %@AB@%/* for malloc()  */%@AE@%%@NL@%
  24628. <ctype.h>         %@AB@%/* for isupper() */%@AE@%%@NL@%
  24629. %@NL@%
  24630. %@AI@%#define %@AE@%HUNK 512 %@NL@%
  24631. %@NL@%
  24632. main(argc, argv)%@NL@%
  24633. int argc;%@NL@%
  24634. char *argv[];%@NL@%
  24635. {%@NL@%
  24636.     char *cp, newname[128], *np;%@NL@%
  24637.     FILE *fp;%@NL@%
  24638.     int  hunks = 0, bytes = 0, totbytes = 0;%@NL@%
  24639.     int  i;%@NL@%
  24640.     if (argc != 2)%@NL@%
  24641.         {%@NL@%
  24642.         printf("usage: upity file\n");%@NL@%
  24643.         exit(1);%@NL@%
  24644.         }%@NL@%
  24645. %@NL@%
  24646.     if ((fp = fopen(argv[1], "rb")) == NULL)%@NL@%
  24647.         {%@NL@%
  24648.         printf("\"%s\": Can't open.\n", argv[1]);%@NL@%
  24649.         exit(1);%@NL@%
  24650.         }%@NL@%
  24651.     if ((cp = malloc(HUNK)) == NULL)%@NL@%
  24652.         {%@NL@%
  24653.         printf("Malloc Failed.\n");%@NL@%
  24654.         exit(1);%@NL@%
  24655.         }%@NL@%
  24656. %@NL@%
  24657.     while ((bytes = fread(cp + (HUNK * hunks), 1, HUNK, fp)) == HUNK)%@NL@%
  24658.         {%@NL@%
  24659.         totbytes += bytes;%@NL@%
  24660.         ++hunks;%@NL@%
  24661.         if ((cp = realloc(cp, HUNK + (HUNK * hunks))) == NULL)%@NL@%
  24662.             {%@NL@%
  24663.             printf("Realloc Failed.\n");%@NL@%
  24664.             exit(1);%@NL@%
  24665.             }%@NL@%
  24666.         }%@NL@%
  24667.     if (bytes < 0)%@NL@%
  24668.         {%@NL@%
  24669.         printf("\"%s\": Error Reading.\n", argv[1]);%@NL@%
  24670.         exit(1);%@NL@%
  24671.         }%@NL@%
  24672.     totbytes += bytes;%@NL@%
  24673. %@NL@%
  24674.     for (i = 0; i < totbytes; ++i)%@NL@%
  24675.         if (islower(cp[i]))%@NL@%
  24676.             cp[i] = toupper(cp[i]);%@NL@%
  24677.     %@NL@%
  24678.     (void)fclose(fp);%@NL@%
  24679. %@NL@%
  24680.     if ((np = strrchr(argv[1], '.')) != NULL)%@NL@%
  24681.         *np = '\0';%@NL@%
  24682.     strcpy(newname, argv[1]);%@NL@%
  24683.     strcat(newname, ".up");%@NL@%
  24684.     if ((fp = fopen(newname, "wb")) == NULL)%@NL@%
  24685.         {%@NL@%
  24686.         printf("\"%s\": Can't open.\n", argv[1]);%@NL@%
  24687.         exit(1);%@NL@%
  24688.         }%@NL@%
  24689. %@NL@%
  24690.     if (fwrite(cp, 1, totbytes, fp) != totbytes)%@NL@%
  24691.         {%@NL@%
  24692.         printf("\"%s\": Error writing.\n", argv[1]);%@NL@%
  24693.         exit(1);%@NL@%
  24694.         }%@NL@%
  24695. %@NL@%
  24696. }%@NL@%
  24697. %@NL@%
  24698. %@NL@%
  24699. %@2@%%@AH@%VARADDRS.C%@AE@%%@EH@%%@NL@%
  24700. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\VARADDRS.C%@AE@%%@NL@%
  24701. %@NL@%
  24702. %@AB@%/* varaddrs.c -- use & operator to get     */%@AE@%%@NL@%
  24703. %@AB@%/*               addresses of variables    */%@AE@%%@NL@%
  24704. %@NL@%
  24705. main()%@NL@%
  24706. {%@NL@%
  24707.     char c1, c2;%@NL@%
  24708.     int i;%@NL@%
  24709.     long l;%@NL@%
  24710.     float f;%@NL@%
  24711.     double d;%@NL@%
  24712. %@NL@%
  24713.     printf("Address of c1 %d\n", &c1);%@NL@%
  24714.     printf("Address of c2 %d\n", &c2);%@NL@%
  24715.     printf("Address of i  %d\n", &i);%@NL@%
  24716.     printf("Address of l  %d\n", &l);%@NL@%
  24717.     printf("Address of f  %d\n", &f);%@NL@%
  24718.     printf("Address of d  %d\n", &d);%@NL@%
  24719. }%@NL@%
  24720. %@NL@%
  24721. %@NL@%
  24722. %@2@%%@AH@%VARSIZE.C%@AE@%%@EH@%%@NL@%
  24723. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP03\VARSIZE.C%@AE@%%@NL@%
  24724. %@NL@%
  24725. %@AB@%/* varsize.c -- shows amount of memory */%@AE@%%@NL@%
  24726. %@AB@%/*              by various types       */%@AE@%%@NL@%
  24727. %@NL@%
  24728. main()%@NL@%
  24729. {%@NL@%
  24730.     printf("Size of a char is %d\n", sizeof(char));%@NL@%
  24731.     printf("Size of an int is %d\n", sizeof(int));%@NL@%
  24732.     printf("Size of a long is %d\n", sizeof(long));%@NL@%
  24733.     printf("Size of a float is %d\n", sizeof(float));%@NL@%
  24734.     printf("Size of a double is %d\n", sizeof(double));%@NL@%
  24735. }%@NL@%
  24736. %@NL@%
  24737. %@NL@%
  24738. %@2@%%@AH@%VGAMAP.C%@AE@%%@EH@%%@NL@%
  24739. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP15\VGAMAP.C%@AE@%%@NL@%
  24740. %@NL@%
  24741. %@AB@%/*  vgamap.c  -- remaps the vga mode 19 palette        */%@AE@%%@NL@%
  24742. %@AB@%/* Program list: vgamap.c (for rand())                 */%@AE@%%@NL@%
  24743. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  24744. %@AI@%#include %@AE@%<graph.h> %@NL@%
  24745. %@AI@%#include %@AE@%<conio.h> %@NL@%
  24746. %@AI@%#define %@AE@%ESC '\033' %@NL@%
  24747. %@AI@%#define %@AE@%PALSIZE 256 %@NL@%
  24748. %@AI@%#define %@AE@%ROWS 16 %@NL@%
  24749. %@AI@%#define %@AE@%COLS 16 %@NL@%
  24750. %@AI@%#define %@AE@%MIDBLUE 0x190000L %@NL@%
  24751. long newpal[PALSIZE]; %@AB@%/* array of color values */%@AE@%%@NL@%
  24752. %@NL@%
  24753. main()%@NL@%
  24754. {%@NL@%
  24755.     struct videoconfig vc;%@NL@%
  24756.     int mode = _MRES256COLOR;%@NL@%
  24757.     short xmax, ymax;%@NL@%
  24758.     short xcs[ROWS][COLS];%@NL@%
  24759.     short ycs[ROWS][COLS];%@NL@%
  24760.     short row, col;%@NL@%
  24761.     long colorval;       %@AB@%/* VGA color value */%@AE@%%@NL@%
  24762.     long index;          %@AB@%/* looping index   */%@AE@%%@NL@%
  24763.     short palval;        %@AB@%/* palette value */%@AE@%%@NL@%
  24764.     int c_base;  %@AB@%/* color base -- blue, green, or red */%@AE@%%@NL@%
  24765.     int ch;%@NL@%
  24766. %@NL@%
  24767.     if (_setvideomode(mode) == 0)%@NL@%
  24768.         {%@NL@%
  24769.         fprintf(stderr, "%d mode not supported\n", mode);%@NL@%
  24770.         exit(1);%@NL@%
  24771.         }%@NL@%
  24772.     _getvideoconfig(&vc);%@NL@%
  24773.     xmax = vc.numxpixels - 1;%@NL@%
  24774.     ymax = vc.numypixels - 1;%@NL@%
  24775.     for (col = 0; col < COLS; col++)%@NL@%
  24776.         for (row = 0; row < ROWS; row++)%@NL@%
  24777.             {%@NL@%
  24778.             xcs[row][col] =  col * xmax / COLS + 5;%@NL@%
  24779.             ycs[row][col] =  row * ymax / ROWS + 5;%@NL@%
  24780.             }%@NL@%
  24781.     _setcolor(1);%@NL@%
  24782.     _rectangle(_GBORDER, 0, 0, xmax, ymax);%@NL@%
  24783.     for (col = 1; col < COLS ; col++)%@NL@%
  24784.         {%@NL@%
  24785.         _moveto(col * (xmax + 1) / COLS, 0);%@NL@%
  24786.         _lineto(col * (xmax + 1) / COLS, ymax);%@NL@%
  24787.         }%@NL@%
  24788.     for (row = 1; row < ROWS;  row++)%@NL@%
  24789.         {%@NL@%
  24790.         _moveto(0, row * (ymax + 1) / ROWS);%@NL@%
  24791.         _lineto(xmax, row * (ymax + 1) / ROWS);%@NL@%
  24792.         }%@NL@%
  24793. %@NL@%
  24794.     for (col = 0; col < COLS; col++)%@NL@%
  24795.         for (row = 0; row < ROWS; row++)%@NL@%
  24796.             {%@NL@%
  24797.             _setcolor(row * ROWS + col);%@NL@%
  24798.             _floodfill(xcs[row][col], ycs[row][col],1);%@NL@%
  24799.              }%@NL@%
  24800.     getch();%@NL@%
  24801. %@NL@%
  24802.     %@AB@%/*  initialize newpal[] to 64 shades of blue, 64%@NL@%
  24803. %@AB@%        shades of green, 64 shades of red, and 64 shades%@NL@%
  24804. %@AB@%        of magenta */%@AE@%%@NL@%
  24805.     for (index = 0; index < 64; index++)%@NL@%
  24806.         {%@NL@%
  24807.         newpal[index] = index << 16;%@NL@%
  24808.         newpal[index + 64] = index << 8;%@NL@%
  24809.         newpal[index + 128] = index;%@NL@%
  24810.         newpal[index + 192] = index | MIDBLUE;%@NL@%
  24811.         }%@NL@%
  24812.     _remapallpalette(newpal);%@NL@%
  24813.     getch();%@NL@%
  24814. %@NL@%
  24815.     %@AB@%/* set squares and colors randomly -- ESC%@NL@%
  24816. %@AB@%       terminates loop, and other keystrokes toggle%@NL@%
  24817. %@AB@%       it on and off */%@AE@%%@NL@%
  24818.     do%@NL@%
  24819.         {%@NL@%
  24820.         while (!kbhit())%@NL@%
  24821.             {%@NL@%
  24822.             palval = rand() % PALSIZE;%@NL@%
  24823.             colorval = 0L;%@NL@%
  24824.             for (c_base = 0; c_base < 3; c_base++)%@NL@%
  24825.                 colorval += ((long) rand() % 64) <<%@NL@%
  24826.                              (c_base * 8);%@NL@%
  24827.             _remappalette (palval, colorval);%@NL@%
  24828.             }%@NL@%
  24829.         ch = getch();%@NL@%
  24830.         if (ch != ESC)%@NL@%
  24831.             ch = getch();%@NL@%
  24832.         } while (ch != ESC);%@NL@%
  24833.     _setvideomode(_DEFAULTMODE);%@NL@%
  24834. }%@NL@%
  24835. %@NL@%
  24836. %@NL@%
  24837. %@2@%%@AH@%VIEW.C%@AE@%%@EH@%%@NL@%
  24838. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP10\VIEW.C%@AE@%%@NL@%
  24839. %@NL@%
  24840. %@AB@%/* view.c  --  demonstrates lseek() by displaying  */%@AE@%%@NL@%
  24841. %@AB@%/*             a file and moving around in it      */%@AE@%%@NL@%
  24842. %@NL@%
  24843. <fcntl.h>           %@AB@%/* for open()         */%@AE@%%@NL@%
  24844. <stdio.h>           %@AB@%/* for SEEK_CUR, etc. */%@AE@%%@NL@%
  24845. %@NL@%
  24846. %@AI@%#define %@AE@%HUNK 512 %@NL@%
  24847. %@AI@%#define %@AE@%MOVE 512L %@NL@%
  24848. %@NL@%
  24849. main(argc, argv)%@NL@%
  24850. int argc;%@NL@%
  24851. char *argv[];%@NL@%
  24852. {%@NL@%
  24853.     char ch, buf[HUNK];%@NL@%
  24854.     long position = 0L;%@NL@%
  24855.     int  bytes, eofflag = 0, fd_in;%@NL@%
  24856. %@NL@%
  24857.     if (argc != 2)%@NL@%
  24858.         {%@NL@%
  24859.         fprintf(stderr, "Usage: view file\n");%@NL@%
  24860.         exit(0);%@NL@%
  24861.         }%@NL@%
  24862. %@NL@%
  24863.     if ((fd_in = open(argv[1], O_RDONLY)) < 0)%@NL@%
  24864.         {%@NL@%
  24865.         fprintf(stderr, "\"%s\": Can't open.\n", argv[1]);%@NL@%
  24866.         exit(1);%@NL@%
  24867.         }%@NL@%
  24868. %@NL@%
  24869.     for (;;)%@NL@%
  24870.         {%@NL@%
  24871.         bytes = read(fd_in, buf, HUNK);%@NL@%
  24872.         if (bytes == 0)%@NL@%
  24873.             {%@NL@%
  24874.             if (! eofflag)%@NL@%
  24875.                 {%@NL@%
  24876.                 fprintf(stderr, "\n<<at end of file>>\n");%@NL@%
  24877.                 ++eofflag;%@NL@%
  24878.                 }%@NL@%
  24879.             else%@NL@%
  24880.                 exit(0);%@NL@%
  24881.             }%@NL@%
  24882.         else if (bytes < 0)%@NL@%
  24883.             {%@NL@%
  24884.             fprintf(stderr, "\"%s\": Error Reading.\n", argv[1]);%@NL@%
  24885.             exit(1);%@NL@%
  24886.             }%@NL@%
  24887.         else%@NL@%
  24888.             {%@NL@%
  24889.             eofflag = 0;%@NL@%
  24890.             position = lseek(fd_in, 0L, SEEK_CUR);%@NL@%
  24891.             if (position == -1L)%@NL@%
  24892.                 {%@NL@%
  24893.                 fprintf(stderr, "\"%s\": Error Seeking.\n", argv[1]);%@NL@%
  24894.                 exit(1);%@NL@%
  24895.                 }%@NL@%
  24896.             Print(buf, bytes);%@NL@%
  24897.             do%@NL@%
  24898.                 {%@NL@%
  24899.                 ch = getch();%@NL@%
  24900.                 if (ch == 'q' || ch == 'Q')%@NL@%
  24901.                     exit(0);%@NL@%
  24902.                 } while (ch != '+' && ch != '-');%@NL@%
  24903. %@NL@%
  24904.             if (ch == '-')%@NL@%
  24905.                 {%@NL@%
  24906.                 position = lseek(fd_in, -2 * MOVE, SEEK_CUR);%@NL@%
  24907.                 if (position == -1L)%@NL@%
  24908.                     {%@NL@%
  24909.                     fprintf(stderr, "\"%s\": Error Seeking.\n", argv[1]);%@NL@%
  24910.                     exit(1);%@NL@%
  24911.                     }%@NL@%
  24912.                 }%@NL@%
  24913.             }%@NL@%
  24914.         }%@NL@%
  24915. }%@NL@%
  24916. %@NL@%
  24917. Print(char *buf, int cnt)%@NL@%
  24918. {%@NL@%
  24919.     int i;%@NL@%
  24920. %@NL@%
  24921.     for (i = 0; i < cnt; ++i, ++buf)%@NL@%
  24922.         {%@NL@%
  24923.         if (*buf < ' ' && *buf != '\n' && *buf != '\t')%@NL@%
  24924.             printf("^%c", *buf + '@');%@NL@%
  24925.         else%@NL@%
  24926.             putchar(*buf);%@NL@%
  24927.         }%@NL@%
  24928. }%@NL@%
  24929. %@NL@%
  24930. %@NL@%
  24931. %@2@%%@AH@%WHATCHAR.C%@AE@%%@EH@%%@NL@%
  24932. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP09\WHATCHAR.C%@AE@%%@NL@%
  24933. %@NL@%
  24934. %@AB@%/* whatchar.c  --  demonstrates the character         */%@AE@%%@NL@%
  24935. %@AB@%/*                 classification routines in ctype.h */%@AE@%%@NL@%
  24936. %@NL@%
  24937. <stdio.h>        %@AB@%/* for NULL and BUFSIZ */%@AE@%%@NL@%
  24938. <ctype.h>        %@AB@%/* for iscntl(), et al */%@AE@%%@NL@%
  24939. %@AI@%#define %@AE@%MAXL 20 %@NL@%
  24940. %@NL@%
  24941. main()%@NL@%
  24942. {%@NL@%
  24943.     char buf[BUFSIZ];%@NL@%
  24944.     int i;%@NL@%
  24945. %@NL@%
  24946.     printf("Enter a line of text (20 chars max):\n");%@NL@%
  24947.     if (gets(buf) == NULL)%@NL@%
  24948.         exit(1);%@NL@%
  24949. %@NL@%
  24950.     for (i = 0; i < MAXL; ++i)%@NL@%
  24951.         {%@NL@%
  24952.         if (buf[i] == '\0')%@NL@%
  24953.             break;%@NL@%
  24954.         printf("'%c' ->", buf[i]);%@NL@%
  24955.         if (isalpha(buf[i]))   printf(" isalpha");%@NL@%
  24956.         if (isascii(buf[i]))   printf(" isascii");%@NL@%
  24957.         if (iscntrl(buf[i]))   printf(" iscntrl");%@NL@%
  24958.         if (isgraph(buf[i]))   printf(" isgraph");%@NL@%
  24959.         if (isprint(buf[i]))   printf(" isprint");%@NL@%
  24960.         if (isdigit(buf[i]))   printf(" isdigit");%@NL@%
  24961.         if (isupper(buf[i]))   printf(" isupper");%@NL@%
  24962.         if (islower(buf[i]))   printf(" islower");%@NL@%
  24963.         if (ispunct(buf[i]))   printf(" ispunct");%@NL@%
  24964.         if (isspace(buf[i]))   printf(" isspace");%@NL@%
  24965.         if (isalnum(buf[i]))   printf(" isalnum");%@NL@%
  24966.         if (isxdigit(buf[i]))  printf(" isxdigit");%@NL@%
  24967.         printf("\n");%@NL@%
  24968.         }%@NL@%
  24969. }%@NL@%
  24970. %@NL@%
  24971. %@NL@%
  24972. %@2@%%@AH@%WHILE.C%@AE@%%@EH@%%@NL@%
  24973. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP04\WHILE.C%@AE@%%@NL@%
  24974. %@NL@%
  24975. %@NL@%
  24976. %@AB@%/* WHILE.C -- a simple while loop */%@AE@%%@NL@%
  24977. %@NL@%
  24978. main()%@NL@%
  24979. {%@NL@%
  24980.     int count = 1;%@NL@%
  24981. %@NL@%
  24982.     while (count < 11)  %@AB@%/* loop condition */%@AE@%%@NL@%
  24983.         %@AB@%/* body of loop */%@AE@%%@NL@%
  24984.         {%@NL@%
  24985.         printf("%d\n", count);%@NL@%
  24986.         count++ ;%@NL@%
  24987.         }%@NL@%
  24988.     printf("Done!\n");%@NL@%
  24989. }%@NL@%
  24990. %@NL@%
  24991. %@NL@%
  24992. %@NL@%
  24993. %@2@%%@AH@%WRITECHR.C%@AE@%%@EH@%%@NL@%
  24994. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\WRITECHR.C%@AE@%%@NL@%
  24995. %@NL@%
  24996. %@AB@%/* writechr.c -- writes char and attribute repeatedly */%@AE@%%@NL@%
  24997. %@AB@%/*               using DMA                            */%@AE@%%@NL@%
  24998. %@AB@%/* write character ch with attribute attr num times   */%@AE@%%@NL@%
  24999. %@AB@%/* starting at location pstart -- uses array notation */%@AE@%%@NL@%
  25000. %@NL@%
  25001. typedef unsigned int (far * VIDMEM); %@NL@%
  25002. %@NL@%
  25003. void Write_chars(pstart, ch, attr, num)%@NL@%
  25004. VIDMEM pstart;%@NL@%
  25005. unsigned short ch, attr, num;%@NL@%
  25006. {%@NL@%
  25007.     register count;%@NL@%
  25008.     unsigned short pair;%@NL@%
  25009. %@NL@%
  25010.     pair = (attr << 8) | (ch & 0x00FF) ;%@NL@%
  25011.     for (count = 0; count < num; count++)%@NL@%
  25012.         pstart[count] = pair;%@NL@%
  25013. }%@NL@%
  25014. %@NL@%
  25015. %@NL@%
  25016. %@2@%%@AH@%WRITESTR.C%@AE@%%@EH@%%@NL@%
  25017. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP14\WRITESTR.C%@AE@%%@NL@%
  25018. %@NL@%
  25019. %@AB@%/*  writestr.c -- writes string and attribute using DMA */%@AE@%%@NL@%
  25020. %@AB@%/*  write the string str with attribute attr at         */%@AE@%%@NL@%
  25021. %@AB@%/*  location pstart -- uses pointer notation.           */%@AE@%%@NL@%
  25022. %@NL@%
  25023. typedef unsigned int (far * VIDMEM); %@NL@%
  25024. %@NL@%
  25025. void Write_str(pstart, attr, str)%@NL@%
  25026. VIDMEM pstart;%@NL@%
  25027. unsigned short attr;%@NL@%
  25028. char *str;%@NL@%
  25029. {%@NL@%
  25030.     while (*str != '\0')%@NL@%
  25031.         *pstart++ = (attr << 8) | (*str++ & 0x00FF);%@NL@%
  25032. }%@NL@%
  25033. %@NL@%
  25034. %@NL@%
  25035. %@2@%%@AH@%XMAS.C%@AE@%%@EH@%%@NL@%
  25036. %@AS@%CD-ROM Disc Path:   \SAMPCODE\QC_PROG\CHAP07\XMAS.C%@AE@%%@NL@%
  25037. %@NL@%
  25038. %@AB@%/* xmas.c -- fills an array with values, then passes */%@AE@%%@NL@%
  25039. %@AB@%/*           each of those values to a function.     */%@AE@%%@NL@%
  25040. %@NL@%
  25041. main()%@NL@%
  25042. {%@NL@%
  25043.     int i, j, widths[20];%@NL@%
  25044.     void Center_out();%@NL@%
  25045. %@NL@%
  25046.     for (i = 0, j = 1; i < 18; ++i, j += 2)%@NL@%
  25047.         {%@NL@%
  25048.         widths[i] = j;%@NL@%
  25049.         }%@NL@%
  25050.     widths[i++] = 3;%@NL@%
  25051.     widths[i] = 3;%@NL@%
  25052. %@NL@%
  25053.     for (i = 0; i < 20; i++)%@NL@%
  25054.         {%@NL@%
  25055.         Center_out('X', widths[i]);%@NL@%
  25056.         }%@NL@%
  25057. %@NL@%
  25058. }%@NL@%
  25059. %@NL@%
  25060. void Center_out(char character, int width)%@NL@%
  25061. {%@NL@%
  25062.     int i;%@NL@%
  25063. %@NL@%
  25064.     for (i = 0; i < ((80 - width) / 2); ++i)%@NL@%
  25065.         {%@NL@%
  25066.         putch(' ');%@NL@%
  25067.         }%@NL@%
  25068.     for (i = 0; i < width; ++i)%@NL@%
  25069.         {%@NL@%
  25070.         putch(character);%@NL@%
  25071.         }%@NL@%
  25072.     putch('\r');%@NL@%
  25073.     putch('\n');%@NL@%
  25074. }%@NL@%
  25075. %@1@%%@AH@%Misc. `C' Sample Code from Microsoft%@EH@%%@AE@%
  25076. %@NL@%
  25077. %@NL@%
  25078. %@2@%%@AH@%ABS.C%@AE@%%@EH@%%@NL@%
  25079. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ABS.C%@AE@%%@NL@%
  25080. %@NL@%
  25081. %@AB@%/* ABS.C: Demonstrate macros. */%@AE@%%@NL@%
  25082. %@NL@%
  25083. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25084. %@AI@%#define %@AE@%ABS(value)  ( (value) >= 0 ? (value) : -(value) ) %@NL@%
  25085. %@NL@%
  25086. main()%@NL@%
  25087. {%@NL@%
  25088.    int val = -20;%@NL@%
  25089.    printf( "result = %d\n", ABS(val) );%@NL@%
  25090. }%@NL@%
  25091. %@NL@%
  25092. %@NL@%
  25093. %@2@%%@AH@%ALARM.C%@AE@%%@EH@%%@NL@%
  25094. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ALARM.C%@AE@%%@NL@%
  25095. %@NL@%
  25096. %@AB@%/* ALARM.C illustrates inline assembly and functions or keywords%@NL@%
  25097. %@AB@% * related to Terminate-and-Stay-Resident programs. Functions include:%@NL@%
  25098. %@AB@% *      _dos_setvect    _dos_getvect    _dos_keep%@NL@%
  25099. %@AB@% *      _chain_intr     _enable         _disable%@NL@%
  25100. %@AB@% *%@NL@%
  25101. %@AB@% * Keywords:%@NL@%
  25102. %@AB@% *      interrupt       _asm%@NL@%
  25103. %@AB@% * Directive:%@NL@%
  25104. %@AB@% *      #pragma%@NL@%
  25105. %@AB@% * Pragma:%@NL@%
  25106. %@AB@% *      check_stack     check_pointer%@NL@%
  25107. %@AB@% * Global variables:%@NL@%
  25108. %@AB@% *      _psp            _amblksiz%@NL@%
  25109. %@AB@% *%@NL@%
  25110. %@AB@% * WARNING: You must run ALARM from the DOS command line. The QC%@NL@%
  25111. %@AB@% * environment does not permit TSRs to be installed from inside it, since%@NL@%
  25112. %@AB@% * this would cause subsequent memory problems.%@NL@%
  25113. %@AB@% *%@NL@%
  25114. %@AB@% * See HARDERR.C for another example of inline assembler. See MOVEMEM.C%@NL@%
  25115. %@AB@% * for another pragma example.%@NL@%
  25116. %@AB@% */%@AE@%%@NL@%
  25117. %@NL@%
  25118. %@AI@%#include %@AE@%<dos.h> %@NL@%
  25119. %@AI@%#include %@AE@%<malloc.h> %@NL@%
  25120. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25121. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  25122. %@AI@%#include %@AE@%<time.h> %@NL@%
  25123. %@NL@%
  25124. %@AB@%/* Stack and pointer checking off */%@AE@%%@NL@%
  25125. %@AI@%#pragma %@AE@%check_stack( off ) %@NL@%
  25126. %@AI@%#pragma %@AE@%check_pointer( off ) %@NL@%
  25127. %@NL@%
  25128. %@AB@%/* Inline assembler macro to sound a bell. Note that comments in macros must%@NL@%
  25129. %@AB@% * be in the C format, not the assembler format.%@NL@%
  25130. %@AB@% */%@AE@%%@NL@%
  25131. %@AI@%#define %@AE@%BEEP() _asm { \ %@NL@%
  25132.                         _asm     push bx        %@AB@%/* Save register   */%@AE@% \%@NL@%
  25133.                         _asm     sub  bx, bx    %@AB@%/* Page 0          */%@AE@% \%@NL@%
  25134.                         _asm     mov  ax, 0E07h %@AB@%/* TTY bell        */%@AE@% \%@NL@%
  25135.                         _asm     int  10h       %@AB@%/* BIOS 10         */%@AE@% \%@NL@%
  25136.                         _asm     pop  bx        %@AB@%/* Restore         */%@AE@% \%@NL@%
  25137.                     }%@NL@%
  25138. %@NL@%
  25139. %@AI@%#define %@AE@%TICKPERMIN  1092L %@NL@%
  25140. %@AI@%#define %@AE@%MINPERHOUR  60L %@NL@%
  25141. enum BOOLEAN { FALSE, TRUE };%@NL@%
  25142. %@NL@%
  25143. %@AB@%/* Prototypes for interrupt functions */%@AE@%%@NL@%
  25144. void (interrupt far *oldtimer)( void );%@NL@%
  25145. void (interrupt far *oldvideo)( void );%@NL@%
  25146. void interrupt far newtimer( void );%@NL@%
  25147. void interrupt far newvideo( unsigned _es, unsigned _ds, unsigned _di,%@NL@%
  25148.                              unsigned _si, unsigned _bp, unsigned _sp,%@NL@%
  25149.                              unsigned _bx, unsigned _dx, unsigned _cx,%@NL@%
  25150.                              unsigned _ax, unsigned _ip, unsigned _cs,%@NL@%
  25151.                              unsigned _flags );%@NL@%
  25152. %@NL@%
  25153. %@AB@%/* Variables that will be accessed inside TSR must be global. */%@AE@%%@NL@%
  25154. int  ftimesup = FALSE, fintimer = FALSE, finvideo = FALSE;%@NL@%
  25155. long goaltick;%@NL@%
  25156. long far *pcurtick = (long far *)0x0000046cL;%@NL@%
  25157. %@NL@%
  25158. %@AB@%/* Huge pointers force compiler to do segment arithmetic for us. */%@AE@%%@NL@%
  25159. char huge *tsrstack;%@NL@%
  25160. char huge *appstack;%@NL@%
  25161. char huge *tsrbottom;%@NL@%
  25162. %@NL@%
  25163. main( int argc, char **argv )%@NL@%
  25164. {%@NL@%
  25165.     long minute, hour;%@NL@%
  25166.     unsigned tsrsize;%@NL@%
  25167. %@NL@%
  25168.     %@AB@%/* Initialize stack and bottom of program. */%@AE@%%@NL@%
  25169.     _asm mov  WORD PTR tsrstack[0], sp%@NL@%
  25170.     _asm mov  WORD PTR tsrstack[2], ss%@NL@%
  25171.     FP_SEG( tsrbottom ) = _psp;%@NL@%
  25172.     FP_OFF( tsrbottom ) = 0;%@NL@%
  25173. %@NL@%
  25174.     %@AB@%/* Use 16 paragraph heap (controlled through global in malloc.h). */%@AE@%%@NL@%
  25175.     _amblksiz = 256;%@NL@%
  25176. %@NL@%
  25177.     %@AB@%/* Program size is:%@NL@%
  25178. %@AB@%     *     top of stack%@NL@%
  25179. %@AB@%     *   - bottom of program (converted to paragraphs)%@NL@%
  25180. %@AB@%     *   + paragraphs in the heap plus%@NL@%
  25181. %@AB@%     *   + one extra paragraph just to be safe%@NL@%
  25182. %@AB@%     */%@AE@%%@NL@%
  25183.     tsrsize = ((tsrstack - tsrbottom) >> 4) + (_amblksiz >> 4) + 1;%@NL@%
  25184. %@NL@%
  25185.     %@AB@%/* If command-line, convert time to ticks past midnight. Time must%@NL@%
  25186. %@AB@%     * include 0 in first place (0930, not 930). Time must be later than%@NL@%
  25187. %@AB@%     * current time.%@NL@%
  25188. %@AB@%     */%@AE@%%@NL@%
  25189.     if( argc < 2 )%@NL@%
  25190.     {%@NL@%
  25191.         puts( "  Syntax: ALARM <hhmm> " );%@NL@%
  25192.         puts( "     where <hhmm> is time (in military format) to ring alarm" );%@NL@%
  25193.         exit( 1 );%@NL@%
  25194.     }%@NL@%
  25195. %@NL@%
  25196.     minute = atol( argv[1] + 2 );%@NL@%
  25197.     argv[1][2] = 0;%@NL@%
  25198.     hour = atol( argv[1] );%@NL@%
  25199.     goaltick = (hour * MINPERHOUR * TICKPERMIN) + (minute * TICKPERMIN);%@NL@%
  25200.     if( *pcurtick > goaltick )%@NL@%
  25201.     {%@NL@%
  25202.         puts( "It's past that time now" );%@NL@%
  25203.         exit( 1 );%@NL@%
  25204.     }%@NL@%
  25205. %@NL@%
  25206.     %@AB@%/* Replace existing timer and video routines with ours. */%@AE@%%@NL@%
  25207.     oldtimer = _dos_getvect( 0x1c );%@NL@%
  25208.     _dos_setvect( 0x1c, newtimer );%@NL@%
  25209.     oldvideo = _dos_getvect( 0x10 );%@NL@%
  25210.     _dos_setvect( 0x10, newvideo );%@NL@%
  25211. %@NL@%
  25212.     %@AB@%/* Free the PSP segment and terminate with program resident. */%@AE@%%@NL@%
  25213.     _dos_freemem( _psp );%@NL@%
  25214.     _dos_keep( 0, tsrsize );%@NL@%
  25215. }%@NL@%
  25216. %@NL@%
  25217. %@AB@%/* newtimer - Our timer interrupt compares current time to goal. If earlier%@NL@%
  25218. %@AB@% * it just continues. If later, it beeps and sets a flag to quit checking.%@NL@%
  25219. %@AB@% */%@AE@%%@NL@%
  25220. void interrupt far newtimer()%@NL@%
  25221. {%@NL@%
  25222.     if( ftimesup )%@NL@%
  25223.         _chain_intr( oldtimer );%@NL@%
  25224.     else%@NL@%
  25225.     {%@NL@%
  25226.         %@AB@%/* First execute the original timer interrupt. */%@AE@%%@NL@%
  25227.         (*oldtimer)();%@NL@%
  25228.         if( *pcurtick > goaltick )%@NL@%
  25229.         {%@NL@%
  25230.             %@AB@%/* If time is up, set flag so we'll never return. */%@AE@%%@NL@%
  25231.             ftimesup = TRUE;%@NL@%
  25232. %@NL@%
  25233.             %@AB@%/* Save current stack of application, and set old stack of TSR.%@NL@%
  25234. %@AB@%             * This is for safety since we don't know the state of the%@NL@%
  25235. %@AB@%             * application stack, but we do know the state of our own stack.%@NL@%
  25236. %@AB@%             * Turn off interrupts during the stack switch.%@NL@%
  25237. %@AB@%             */%@AE@%%@NL@%
  25238.             _disable();%@NL@%
  25239.             _asm \%@NL@%
  25240.             {%@NL@%
  25241.                 mov  WORD PTR appstack[0], sp   ; Save current stack%@NL@%
  25242.                 mov  WORD PTR appstack[2], ss%@NL@%
  25243.                 mov  sp, WORD PTR tsrstack[0]   ; Load new stack%@NL@%
  25244.                 mov  ss, WORD PTR tsrstack[2]%@NL@%
  25245.             }%@NL@%
  25246.             _enable();%@NL@%
  25247. %@NL@%
  25248.             %@AB@%/* Make sure we're not in video interrupt, then BEEP. This check%@NL@%
  25249. %@AB@%             * prevents the rare but potentially dangerous case of%@NL@%
  25250. %@AB@%             * calling INT 10 to beep while INT 10 is already running.%@NL@%
  25251. %@AB@%             */%@AE@%%@NL@%
  25252.             while( finvideo )%@NL@%
  25253.                 ;%@NL@%
  25254.             BEEP();%@NL@%
  25255.             BEEP();%@NL@%
  25256.             BEEP();%@NL@%
  25257. %@NL@%
  25258.             %@AB@%/* Restore application stack. */%@AE@%%@NL@%
  25259.             _disable();%@NL@%
  25260.             _asm \%@NL@%
  25261.             {%@NL@%
  25262.                 mov  sp, WORD PTR appstack[0]%@NL@%
  25263.                 mov  ss, WORD PTR appstack[2]%@NL@%
  25264.             }%@NL@%
  25265.             _enable();%@NL@%
  25266.         }%@NL@%
  25267.     }%@NL@%
  25268. }%@NL@%
  25269. %@NL@%
  25270. %@AB@%/* newvideo - This routine protects against reentering INT 10 while it is%@NL@%
  25271. %@AB@% * already executing. This could be disastrous if the interrupt routine was%@NL@%
  25272. %@AB@% * interrupted while it was accessing a hardware register.%@NL@%
  25273. %@AB@% */%@AE@%%@NL@%
  25274. void interrupt far newvideo( unsigned _es, unsigned _ds, unsigned _di,%@NL@%
  25275.                              unsigned _si, unsigned _bp, unsigned _sp,%@NL@%
  25276.                              unsigned _bx, unsigned _dx, unsigned _cx,%@NL@%
  25277.                              unsigned _ax, unsigned _ip, unsigned _cs,%@NL@%
  25278.                              unsigned _flags )%@NL@%
  25279. {%@NL@%
  25280.     static unsigned save_bp;%@NL@%
  25281. %@NL@%
  25282.     %@AB@%/* If not already in (most of the time), chain to original. */%@AE@%%@NL@%
  25283.     if( !finvideo )%@NL@%
  25284.         _chain_intr( oldvideo );%@NL@%
  25285.     else%@NL@%
  25286.     {%@NL@%
  25287. %@NL@%
  25288.         %@AB@%/* Set the inside flag, then make sure all the real registers%@NL@%
  25289. %@AB@%         * that might be passed to an interrupt 10h match the parameter%@NL@%
  25290. %@AB@%         * registers. Some of the real registers may be modified by the%@NL@%
  25291. %@AB@%         * preceding code. Note that BP must be saved in a static (nonstack)%@NL@%
  25292. %@AB@%         * variable so that it can be retrieved without modifying the stack.%@NL@%
  25293. %@AB@%         */%@AE@%%@NL@%
  25294.         finvideo = TRUE;%@NL@%
  25295.         _asm \%@NL@%
  25296.         {%@NL@%
  25297.             mov ax, _ax%@NL@%
  25298.             mov bx, _bx%@NL@%
  25299.             mov cx, _cx%@NL@%
  25300.             mov dx, _dx%@NL@%
  25301.             mov es, _es%@NL@%
  25302.             mov di, _di%@NL@%
  25303.             mov save_bp, bp%@NL@%
  25304.             mov bp, _bp%@NL@%
  25305.         }%@NL@%
  25306. %@NL@%
  25307.         %@AB@%/* Call the original interrupt. */%@AE@%%@NL@%
  25308.         (*oldvideo)();%@NL@%
  25309. %@NL@%
  25310.         %@AB@%/* Make sure that any values returned in real registers by the%@NL@%
  25311. %@AB@%         * interrupt are updated in the parameter registers. Reset the flag.%@NL@%
  25312. %@AB@%         */%@AE@%%@NL@%
  25313.         _asm \%@NL@%
  25314.         {%@NL@%
  25315.             mov bp, save_bp%@NL@%
  25316.             mov _bp, bp%@NL@%
  25317.             mov _di, di%@NL@%
  25318.             mov _es, es%@NL@%
  25319.             mov _dx, dx%@NL@%
  25320.             mov _cx, cx%@NL@%
  25321.             mov _bx, bx%@NL@%
  25322.             mov _ax, ax%@NL@%
  25323.         }%@NL@%
  25324.         finvideo = FALSE;%@NL@%
  25325.     }%@NL@%
  25326. }%@NL@%
  25327. %@NL@%
  25328. %@NL@%
  25329. %@2@%%@AH@%ANIMATE.C%@AE@%%@EH@%%@NL@%
  25330. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\ANIMATE.C%@AE@%%@NL@%
  25331. %@NL@%
  25332. %@AB@%/* ANIMATE.C illustrates animation functions including:%@NL@%
  25333. %@AB@% *          _imagesize     _getimage     _putimage%@NL@%
  25334. %@AB@% */%@AE@%%@NL@%
  25335. %@NL@%
  25336. %@AI@%#include %@AE@%<conio.h> %@NL@%
  25337. %@AI@%#include %@AE@%<stddef.h> %@NL@%
  25338. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  25339. %@AI@%#include %@AE@%<malloc.h> %@NL@%
  25340. %@AI@%#include %@AE@%<graph.h> %@NL@%
  25341. %@NL@%
  25342. short action[5]  = { _GPSET,   _GPRESET, _GXOR,    _GOR,     _GAND    };%@NL@%
  25343. char *descrip[5] = { "PSET  ", "PRESET", "XOR   ", "OR    ", "AND   " };%@NL@%
  25344. %@NL@%
  25345. main()%@NL@%
  25346. {%@NL@%
  25347.     char far *buffer;%@NL@%
  25348.     short i, x, y = 30;%@NL@%
  25349.     size_t imsize;%@NL@%
  25350.     short mode = _VRES16COLOR;%@NL@%
  25351. %@NL@%
  25352.     while( !_setvideomode( mode ) )         %@AB@%/* Find best graphics mode    */%@AE@%%@NL@%
  25353.         mode--;%@NL@%
  25354.     if (mode == _TEXTMONO )%@NL@%
  25355.         exit( 1 );                          %@AB@%/* No graphics available      */%@AE@%%@NL@%
  25356.     _setcolor( 3 );%@NL@%
  25357.     for ( i = 0; i < 5; i++ )%@NL@%
  25358.     {%@NL@%
  25359.         x = 50; y += 40;%@NL@%
  25360.         _settextposition( 1, 1 );           %@AB@%/* Display action type        */%@AE@%%@NL@%
  25361.         _outtext( descrip[i] );%@NL@%
  25362.                                             %@AB@%/* Draw and measure ellipse   */%@AE@%%@NL@%
  25363.         _ellipse( _GFILLINTERIOR, x - 15, y - 15, x + 15, y + 15 );%@NL@%
  25364.         imsize = (size_t)_imagesize( x - 16, y - 16, x + 16, y + 16 );%@NL@%
  25365.         buffer = (char far *)_fmalloc( imsize );%@NL@%
  25366.         if ( buffer == (char far *)NULL )%@NL@%
  25367.             exit( 1 );%@NL@%
  25368.                                             %@AB@%/* Get master copy of ellipse */%@AE@%%@NL@%
  25369.         _getimage( x - 16, y - 16, x + 16, y + 16, buffer );%@NL@%
  25370.         while( x < 260 )                    %@AB@%/* Copy row of ellipses       */%@AE@%%@NL@%
  25371.         {                                   %@AB@%/*   with specified action    */%@AE@%%@NL@%
  25372.             x += 5;%@NL@%
  25373.             _putimage( x - 16, y - 16, buffer, action[i] );%@NL@%
  25374.         }%@NL@%
  25375.         _ffree( (char far *)buffer );       %@AB@%/* Free memory                */%@AE@%%@NL@%
  25376.         getch();%@NL@%
  25377.     }%@NL@%
  25378.     exit( !_setvideomode( _DEFAULTMODE ) );%@NL@%
  25379. }%@NL@%
  25380. %@NL@%
  25381. %@NL@%
  25382. %@2@%%@AH@%ARGS.C%@AE@%%@EH@%%@NL@%
  25383. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ARGS.C%@AE@%%@NL@%
  25384. %@NL@%
  25385. %@AB@%/* ARGS.C illustrates the following variables used for accessing%@NL@%
  25386. %@AB@% * command-line arguments and environment variables:%@NL@%
  25387. %@AB@% *      argc            argv            envp%@NL@%
  25388. %@AB@% *%@NL@%
  25389. %@AB@% * Also illustrates getting a process ID with:%@NL@%
  25390. %@AB@% *      getpid%@NL@%
  25391. %@AB@% */%@AE@%%@NL@%
  25392. %@NL@%
  25393. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25394. %@AI@%#include %@AE@%<process.h> %@NL@%
  25395. %@NL@%
  25396. main( int argc,             %@AB@%/* Number of strings in array argv          */%@AE@%%@NL@%
  25397.       char *argv[],         %@AB@%/* Array of command-line argument strings   */%@AE@%%@NL@%
  25398.       char **envp )         %@AB@%/* Array of environment variable strings    */%@AE@%%@NL@%
  25399. {%@NL@%
  25400.     int count;%@NL@%
  25401. %@NL@%
  25402.     %@AB@%/* Display each command-line argument. */%@AE@%%@NL@%
  25403.     printf( "\nCommand-line arguments:\n" );%@NL@%
  25404.     for( count = 0; count < argc; count++ )%@NL@%
  25405.         printf( "  argv[%d]   %s\n", count, argv[count] );%@NL@%
  25406. %@NL@%
  25407.     %@AB@%/* Display each environment variable. */%@AE@%%@NL@%
  25408.     printf( "\nEnvironment variables:\n" );%@NL@%
  25409.     while( *envp != NULL )%@NL@%
  25410.         printf( "  %s\n", *(envp++) );%@NL@%
  25411. %@NL@%
  25412.     %@AB@%/* If run from DOS, shows different ID for DOS than for DOS shell.%@NL@%
  25413. %@AB@%     * If execed or spawned, shows ID of parent.%@NL@%
  25414. %@AB@%     */%@AE@%%@NL@%
  25415.     printf( "\nProcess id of parent: %d", getpid() );%@NL@%
  25416.     exit( 0 );%@NL@%
  25417. }%@NL@%
  25418. %@NL@%
  25419. %@NL@%
  25420. %@2@%%@AH@%ARGV.C%@AE@%%@EH@%%@NL@%
  25421. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ARGV.C%@AE@%%@NL@%
  25422. %@NL@%
  25423. %@AB@%/* ARGV.C: Demonstrate accessing command-line arguments.%@NL@%
  25424. %@AB@%*/%@AE@%%@NL@%
  25425. %@NL@%
  25426. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25427. %@NL@%
  25428. void show_args( char *argument );%@NL@%
  25429. %@NL@%
  25430. int main( int argc, char *argv[] )%@NL@%
  25431. {%@NL@%
  25432.    int count;%@NL@%
  25433.    for( count=0; count < argc; count++ )%@NL@%
  25434.       show_args( argv[count] );%@NL@%
  25435.    return 0;%@NL@%
  25436. }%@NL@%
  25437. %@NL@%
  25438. void show_args( char *argument )%@NL@%
  25439. {%@NL@%
  25440.    printf( "%s\n", argument );%@NL@%
  25441. }%@NL@%
  25442. %@NL@%
  25443. %@NL@%
  25444. %@2@%%@AH@%ARGV1.C%@AE@%%@EH@%%@NL@%
  25445. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ARGV1.C%@AE@%%@NL@%
  25446. %@NL@%
  25447. %@AB@%/* ARGV1.C: Demonstrate null pointers. */%@AE@%%@NL@%
  25448. %@NL@%
  25449. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25450. %@NL@%
  25451. void show_args( char *argument );%@NL@%
  25452. %@NL@%
  25453. int main( int argc, char **argv )%@NL@%
  25454. {%@NL@%
  25455.    while( *argv )%@NL@%
  25456.       show_args( *(argv++) );%@NL@%
  25457.    return 0;%@NL@%
  25458. }%@NL@%
  25459. %@NL@%
  25460. void show_args( char *argument )%@NL@%
  25461. {%@NL@%
  25462.    printf( "%s\n", argument );%@NL@%
  25463. }%@NL@%
  25464. %@NL@%
  25465. %@NL@%
  25466. %@2@%%@AH@%ARRAY.C%@AE@%%@EH@%%@NL@%
  25467. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ARRAY.C%@AE@%%@NL@%
  25468. %@NL@%
  25469. %@AB@%/* ARRAY.C: Demonstrate arrays. */%@AE@%%@NL@%
  25470. %@NL@%
  25471. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25472. %@NL@%
  25473. main()%@NL@%
  25474. {%@NL@%
  25475.    int j, k;%@NL@%
  25476.    static int i_array[2][3] = { { 12, 2, 444 }, { 6, 55, 777 } };%@NL@%
  25477.    static char c_array[] = "Hello";%@NL@%
  25478. %@NL@%
  25479.    printf( "--- Values --------     --- Addresses -------\n\n" );%@NL@%
  25480. %@NL@%
  25481.    for( j = 0; j < 2; j = j + 1 )%@NL@%
  25482.    {%@NL@%
  25483.       for( k = 0; k < 3; k = k + 1 )%@NL@%
  25484.       {%@NL@%
  25485.          printf( "i_array[%d][%d] = %d", j, k, i_array[j][k] );%@NL@%
  25486.          printf( "\t&i_array[%d][%d] = %u\n", j, k, &i_array[j][k] );%@NL@%
  25487.       }%@NL@%
  25488.       printf( "\n" );%@NL@%
  25489.    }%@NL@%
  25490. %@NL@%
  25491.    for( j = 0; j < 6; j = j + 1 )%@NL@%
  25492.    {%@NL@%
  25493.       printf( "c_array[%d]   = %x %c", j, c_array[j], c_array[j] );%@NL@%
  25494.       printf( "\t&c_array[%d]    = %u\n", j, &c_array[j] );%@NL@%
  25495.    }%@NL@%
  25496. }%@NL@%
  25497. %@NL@%
  25498. %@NL@%
  25499. %@2@%%@AH@%ASSERT.C%@AE@%%@EH@%%@NL@%
  25500. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ASSERT.C%@AE@%%@NL@%
  25501. %@NL@%
  25502. %@AB@%/* ASSERT.C illustrates function:%@NL@%
  25503. %@AB@% *      assert%@NL@%
  25504. %@AB@% */%@AE@%%@NL@%
  25505. %@NL@%
  25506. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25507. %@AI@%#include %@AE@%<conio.h> %@NL@%
  25508. %@AI@%#include %@AE@%<string.h> %@NL@%
  25509. %@AI@%#include %@AE@%<assert.h> %@NL@%
  25510. %@NL@%
  25511. %@AI@%#define %@AE@%MAXSTR 120 %@NL@%
  25512. %@NL@%
  25513. void chkstr( char *string );    %@AB@%/* Prototype */%@AE@%%@NL@%
  25514. %@NL@%
  25515. main()%@NL@%
  25516. {%@NL@%
  25517.     char string1[MAXSTR], string2[MAXSTR];%@NL@%
  25518. %@NL@%
  25519.     %@AB@%/* Do various processes on strings and check the results. If%@NL@%
  25520. %@AB@%     * none cause errors, force on error with empty string.%@NL@%
  25521. %@AB@%     */%@AE@%%@NL@%
  25522.     printf( "Enter a string: " );%@NL@%
  25523.     gets( string1 );%@NL@%
  25524.     chkstr( string1 );%@NL@%
  25525. %@NL@%
  25526.     printf( "Enter another string: " );%@NL@%
  25527.     gets( string2 );%@NL@%
  25528.     chkstr( string2 );%@NL@%
  25529. %@NL@%
  25530.     strcat( string1, string2 );%@NL@%
  25531.     chkstr( string1 );%@NL@%
  25532.     printf( "string1 + string2 = %s\n", string1 );%@NL@%
  25533. %@NL@%
  25534.     chkstr( "" );%@NL@%
  25535.     printf( "You'll never get here\n" );%@NL@%
  25536. }%@NL@%
  25537. %@NL@%
  25538. void chkstr( char *string )%@NL@%
  25539. {%@NL@%
  25540.     assert( string != NULL );               %@AB@%/* Cannot be NULL */%@AE@%%@NL@%
  25541.     assert( *string != '\0' );              %@AB@%/* Cannot be empty */%@AE@%%@NL@%
  25542.     assert( strlen( string ) < MAXSTR );    %@AB@%/* Length must be positive */%@AE@%%@NL@%
  25543. }%@NL@%
  25544. %@NL@%
  25545. %@NL@%
  25546. %@2@%%@AH@%ATEXIT.C%@AE@%%@EH@%%@NL@%
  25547. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ATEXIT.C%@AE@%%@NL@%
  25548. %@NL@%
  25549. %@AB@%/* ATEXIT.C illustrates:%@NL@%
  25550. %@AB@% *      atexit          onexit%@NL@%
  25551. %@AB@% */%@AE@%%@NL@%
  25552. %@NL@%
  25553. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  25554. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25555. ANSI                %@AB@%/* Comment out to try onexit     */%@AE@%%@NL@%
  25556. %@NL@%
  25557. %@AB@%/* Prototypes */%@AE@%%@NL@%
  25558. void fn1( void ), fn2( void ), fn3( void ), fn4( void );%@NL@%
  25559. %@NL@%
  25560. main()%@NL@%
  25561. {%@NL@%
  25562. %@NL@%
  25563.     %@AB@%/* atexit is ANSI standard. It returns 0 for success, nonzero%@NL@%
  25564. %@AB@%     * for fail.%@NL@%
  25565. %@AB@%     */%@AE@%%@NL@%
  25566. %@AI@%#ifdef %@AE@%ANSI %@NL@%
  25567.     atexit( fn1 );%@NL@%
  25568.     atexit( fn2 );%@NL@%
  25569.     atexit( fn3 );%@NL@%
  25570.     atexit( fn4 );%@NL@%
  25571. %@NL@%
  25572.     %@AB@%/* onexit is Microsoft extension. It returns pointer to function%@NL@%
  25573. %@AB@%     * for success, NULL for fail.%@NL@%
  25574. %@AB@%     */%@AE@%%@NL@%
  25575. %@AI@%#else %@AE@%%@NL@%
  25576.     onexit( fn1 );%@NL@%
  25577.     onexit( fn2 );%@NL@%
  25578.     onexit( fn3 );%@NL@%
  25579.     onexit( fn4 );%@NL@%
  25580. %@AI@%#endif %@AE@%%@NL@%
  25581. %@NL@%
  25582.     printf( "This is executed first.\n" );%@NL@%
  25583. }%@NL@%
  25584. %@NL@%
  25585. void fn1()%@NL@%
  25586. {%@NL@%
  25587.     printf( "next.\n" );%@NL@%
  25588. }%@NL@%
  25589. %@NL@%
  25590. void fn2()%@NL@%
  25591. {%@NL@%
  25592.     printf( "executed " );%@NL@%
  25593. }%@NL@%
  25594. %@NL@%
  25595. void fn3()%@NL@%
  25596. {%@NL@%
  25597.     printf( "is " );%@NL@%
  25598. }%@NL@%
  25599. %@NL@%
  25600. void fn4()%@NL@%
  25601. {%@NL@%
  25602.     printf( "This " );%@NL@%
  25603. }%@NL@%
  25604. %@NL@%
  25605. %@NL@%
  25606. %@2@%%@AH@%ATONUM.C%@AE@%%@EH@%%@NL@%
  25607. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ATONUM.C%@AE@%%@NL@%
  25608. %@NL@%
  25609. %@AB@%/* ATONUM.C illustrates string to number conversion functions including:%@NL@%
  25610. %@AB@% *      atof            atoi            atol            gcvt%@NL@%
  25611. %@AB@% *%@NL@%
  25612. %@AB@% * It also illustrates:%@NL@%
  25613. %@AB@% *      cgets           cputs%@NL@%
  25614. %@AB@% */%@AE@%%@NL@%
  25615. %@NL@%
  25616. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  25617. %@AI@%#include %@AE@%<string.h> %@NL@%
  25618. %@AI@%#include %@AE@%<conio.h> %@NL@%
  25619. %@NL@%
  25620. %@AI@%#define %@AE@%MAXSTR 100 %@NL@%
  25621. %@NL@%
  25622. char cnumbuf[MAXSTR] = { MAXSTR + 2, 0 };%@NL@%
  25623. numbuf cnumbuf + 2          %@AB@%/* Actual buffer starts at byte 3 */%@AE@%%@NL@%
  25624. char tmpbuf[MAXSTR];%@NL@%
  25625. %@NL@%
  25626. %@AB@%/* Numeric input and output without printf. */%@AE@%%@NL@%
  25627. main()%@NL@%
  25628. {%@NL@%
  25629.     int     integer;%@NL@%
  25630.     long    longint;%@NL@%
  25631.     float   real;%@NL@%
  25632. %@NL@%
  25633.     %@AB@%/* Using cgets (rather than gets) allows use of DOS editing keys%@NL@%
  25634. %@AB@%     * (or of editing keys from DOS command line editors).%@NL@%
  25635. %@AB@%     */%@AE@%%@NL@%
  25636.     cputs( "Enter an integer: " );%@NL@%
  25637.     cgets( cnumbuf );%@NL@%
  25638.     cputs( "\n\r" );                %@AB@%/* cputs doesn't translate \n     */%@AE@%%@NL@%
  25639.     integer = atoi( numbuf );%@NL@%
  25640.     strcpy( tmpbuf, numbuf );%@NL@%
  25641.     strcat( tmpbuf, " + " );%@NL@%
  25642. %@NL@%
  25643.     cputs( "Enter a long integer: " );%@NL@%
  25644.     cgets( cnumbuf );%@NL@%
  25645.     cputs( "\n\r" );%@NL@%
  25646.     longint = atol( numbuf );%@NL@%
  25647.     strcat( tmpbuf, numbuf );%@NL@%
  25648.     strcat( tmpbuf, " + " );%@NL@%
  25649. %@NL@%
  25650.     cputs( "Enter a floating point number: " );%@NL@%
  25651.     cgets( cnumbuf );%@NL@%
  25652.     cputs( "\n\r" );%@NL@%
  25653.     real = atof( numbuf );%@NL@%
  25654.     strcat( tmpbuf, numbuf );%@NL@%
  25655.     strcat( tmpbuf, " = " );%@NL@%
  25656. %@NL@%
  25657.     gcvt( integer + longint + real, 4, numbuf );%@NL@%
  25658.     strcat( tmpbuf, numbuf );%@NL@%
  25659.     strcat( tmpbuf, "\n\r" );%@NL@%
  25660. %@NL@%
  25661.     cputs( tmpbuf );%@NL@%
  25662. }%@NL@%
  25663. %@NL@%
  25664. %@NL@%
  25665. %@2@%%@AH@%BADSEMI.C%@AE@%%@EH@%%@NL@%
  25666. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\BADSEMI.C%@AE@%%@NL@%
  25667. %@NL@%
  25668. %@AB@%/* BADSEMI.C: Misplaced semicolon. */%@AE@%%@NL@%
  25669. %@NL@%
  25670. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25671. %@NL@%
  25672. main()%@NL@%
  25673. {%@NL@%
  25674.    int count;%@NL@%
  25675.    for( count = 0; count < 500; count++ ); %@AB@%/* Error! */%@AE@%%@NL@%
  25676.    {%@NL@%
  25677.       printf( "count = %d\n", count );%@NL@%
  25678.       printf( "And the beat goes on...\n" );%@NL@%
  25679.    }%@NL@%
  25680. }%@NL@%
  25681. %@NL@%
  25682. %@NL@%
  25683. %@2@%%@AH@%BADSEMI1.C%@AE@%%@EH@%%@NL@%
  25684. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\BADSEMI1.C%@AE@%%@NL@%
  25685. %@NL@%
  25686. %@AB@%/* BADSEMI1.C: Misplaced semicolon. */%@AE@%%@NL@%
  25687. %@NL@%
  25688. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25689. %@NL@%
  25690. main()%@NL@%
  25691. {%@NL@%
  25692.    int count;%@NL@%
  25693.    for( count = 0; count < 500; count++ )%@NL@%
  25694.       ; %@AB@%/* Null statement */%@AE@%%@NL@%
  25695.    {%@NL@%
  25696.       printf( "count = %d\n", count );%@NL@%
  25697.       printf( "And the beat goes on...\n" );%@NL@%
  25698.    }%@NL@%
  25699. }%@NL@%
  25700. %@NL@%
  25701. %@NL@%
  25702. %@2@%%@AH@%BEEP.C%@AE@%%@EH@%%@NL@%
  25703. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\UTILITY\BEEP.C%@AE@%%@NL@%
  25704. %@NL@%
  25705. %@AB@%/* BEEP.C illustrates timing and port input and output functions%@NL@%
  25706. %@AB@% * including:%@NL@%
  25707. %@AB@% *      inp             outp            clock%@NL@%
  25708. %@AB@% * Also keyword:%@NL@%
  25709. %@AB@% *      enum%@NL@%
  25710. %@AB@% *%@NL@%
  25711. %@AB@% * In addition to the delay use shown here, clock can be used as a%@NL@%
  25712. %@AB@% * timer as shown in SIEVE.C.%@NL@%
  25713. %@AB@% */%@AE@%%@NL@%
  25714. %@NL@%
  25715. %@AI@%#include %@AE@%<time.h> %@NL@%
  25716. %@AI@%#include %@AE@%<conio.h> %@NL@%
  25717. %@NL@%
  25718. void beep( unsigned duration, unsigned frequency );%@NL@%
  25719. void delay( clock_t wait );%@NL@%
  25720. %@NL@%
  25721. %@NL@%
  25722. enum notes%@NL@%
  25723. {   %@AB@%/* Enumeration of notes and frequencies     */%@AE@%%@NL@%
  25724.     C0 = 262, D0 = 296, E0 = 330, F0 = 349, G0 = 392, A0 = 440, B0 = 494,%@NL@%
  25725.     C1 = 523, D1 = 587, E1 = 659, F1 = 698, G1 = 784, A1 = 880, B1 = 988,%@NL@%
  25726.     EIGHTH = 125, QUARTER = 250, HALF = 500, WHOLE = 1000, END = 0%@NL@%
  25727. } song[] =%@NL@%
  25728. {   %@AB@%/* Array initialized to notes of song       */%@AE@%%@NL@%
  25729.     C1, HALF, G0, HALF, A0, HALF, E0, HALF, F0, HALF, E0, QUARTER,%@NL@%
  25730.     D0, QUARTER, C0, WHOLE, END%@NL@%
  25731. };%@NL@%
  25732. %@NL@%
  25733. int main ()%@NL@%
  25734. {%@NL@%
  25735.     int note = 0;%@NL@%
  25736. %@NL@%
  25737.     while( song[note] )%@NL@%
  25738.         beep( song[note++], song[note++] );%@NL@%
  25739. }%@NL@%
  25740. %@NL@%
  25741. %@AB@%/* beep - sounds the speaker for a time specified in microseconds by%@NL@%
  25742. %@AB@% * duration at a pitch specified in hertz by frequency.%@NL@%
  25743. %@AB@% */%@AE@%%@NL@%
  25744. void beep( unsigned duration, unsigned frequency )%@NL@%
  25745. {%@NL@%
  25746.     int control;%@NL@%
  25747. %@NL@%
  25748.     %@AB@%/* If frequency is 0, beep doesn't try to make a sound. It%@NL@%
  25749. %@AB@%     * just sleeps for the duration.%@NL@%
  25750. %@AB@%     */%@AE@%%@NL@%
  25751.     if (frequency)%@NL@%
  25752.     {%@NL@%
  25753.         %@AB@%/* 75 is the shortest reliable duration of a sound. */%@AE@%%@NL@%
  25754.         if( duration < 75 )%@NL@%
  25755.             duration = 75;%@NL@%
  25756. %@NL@%
  25757.         %@AB@%/* Prepare timer by sending 10111100 to port 43. */%@AE@%%@NL@%
  25758.         outp( 0x43, 0xb6 );%@NL@%
  25759. %@NL@%
  25760.         %@AB@%/* Divide input frequency by timer ticks per second and%@NL@%
  25761. %@AB@%         * write (byte by byte) to timer.%@NL@%
  25762. %@AB@%         */%@AE@%%@NL@%
  25763.         frequency = (unsigned)(1193180L / frequency);%@NL@%
  25764.         outp( 0x42, (char)frequency );%@NL@%
  25765.         outp( 0x42, (char)(frequency >> 8) );%@NL@%
  25766. %@NL@%
  25767.         %@AB@%/* Save speaker control byte. */%@AE@%%@NL@%
  25768.         control = inp( 0x61 );%@NL@%
  25769. %@NL@%
  25770.         %@AB@%/* Turn on the speaker (with bits 0 and 1). */%@AE@%%@NL@%
  25771.         outp( 0x61, control | 0x3 );%@NL@%
  25772.     }%@NL@%
  25773. %@NL@%
  25774.     delay( (clock_t)duration );%@NL@%
  25775. %@NL@%
  25776.     %@AB@%/* Turn speaker back on if necessary. */%@AE@%%@NL@%
  25777.     if( frequency )%@NL@%
  25778.         outp( 0x61, control );%@NL@%
  25779. }%@NL@%
  25780. %@NL@%
  25781. %@AB@%/* delay - Pauses for a specified number of microseconds. */%@AE@%%@NL@%
  25782. void delay( clock_t wait )%@NL@%
  25783. {%@NL@%
  25784.     clock_t goal;%@NL@%
  25785. %@NL@%
  25786.     goal = wait + clock();%@NL@%
  25787.     while( goal > clock() )%@NL@%
  25788.         ;%@NL@%
  25789. }%@NL@%
  25790. %@NL@%
  25791. %@NL@%
  25792. %@2@%%@AH@%BESSEL.C%@AE@%%@EH@%%@NL@%
  25793. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\BESSEL.C%@AE@%%@NL@%
  25794. %@NL@%
  25795. %@AB@%/* BESSEL.C illustrates Bessel functions including:%@NL@%
  25796. %@AB@% *      j0          j1          jn          y0          y1          yn%@NL@%
  25797. %@AB@% */%@AE@%%@NL@%
  25798. %@NL@%
  25799. %@AI@%#include %@AE@%<math.h> %@NL@%
  25800. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25801. %@NL@%
  25802. main()%@NL@%
  25803. {%@NL@%
  25804.     double x = 2.387;%@NL@%
  25805.     int n = 3, c;%@NL@%
  25806. %@NL@%
  25807.     printf( "Bessel functions for x = %f:\n", x );%@NL@%
  25808.     printf( "  Kind\t\tOrder\t\Function\tResult\n\n" );%@NL@%
  25809.     printf( "  First\t\t0\tj0( x )\t\t%f\n", j0( x ) );%@NL@%
  25810.     printf( "  First\t\t1\tj1( x )\t\t%f\n", j1( x ) );%@NL@%
  25811.     for( c = 2; c < 10; c++ )%@NL@%
  25812.         printf( "  First\t\t%d\tjn( n, x )\t%f\n", c, jn( c, x ) );%@NL@%
  25813. %@NL@%
  25814.     printf( "  Second\t0\ty0( x )\t\t%f\n", y0( x ) );%@NL@%
  25815.     printf( "  Second\t1\ty1( x )\t\t%f\n", y1( x ) );%@NL@%
  25816.     for( c = 2; c < 10; c++ )%@NL@%
  25817.         printf( "  Second\t%d\tyn( n, x )\t%f\n", c, yn( c, x ) );%@NL@%
  25818. }%@NL@%
  25819. %@NL@%
  25820. %@NL@%
  25821. %@2@%%@AH@%BINTEXT.C%@AE@%%@EH@%%@NL@%
  25822. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\BINTEXT.C%@AE@%%@NL@%
  25823. %@NL@%
  25824. %@AB@%/* BINTEXT.C illustrates changing between binary and text modes for%@NL@%
  25825. %@AB@% * stream I/O using function:%@NL@%
  25826. %@AB@% *      setmode%@NL@%
  25827. %@AB@% * and global variable:%@NL@%
  25828. %@AB@% *      _fmode%@NL@%
  25829. %@AB@% */%@AE@%%@NL@%
  25830. %@NL@%
  25831. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25832. %@AI@%#include %@AE@%<fcntl.h> %@NL@%
  25833. %@AI@%#include %@AE@%<io.h> %@NL@%
  25834. %@AI@%#include %@AE@%<string.h> %@NL@%
  25835. <stdlib.h>         %@AB@%/* For _fmode and exit */%@AE@%%@NL@%
  25836. %@NL@%
  25837. main()%@NL@%
  25838. {%@NL@%
  25839.     FILE *htmp;%@NL@%
  25840.     char name[13];%@NL@%
  25841. %@NL@%
  25842.     %@AB@%/* Set default mode to binary and open file. */%@AE@%%@NL@%
  25843.     _fmode = O_BINARY;%@NL@%
  25844.     if( (htmp = fopen( tmpnam( name ), "w+" )) == NULL )%@NL@%
  25845.         exit( 1 );%@NL@%
  25846.     fprintf( htmp, "\nThis\nis\nsome\nbinary\ntext.\n\n" );%@NL@%
  25847. %@NL@%
  25848.     %@AB@%/* Flush buffer and change mode to text. */%@AE@%%@NL@%
  25849.     fflush( htmp );%@NL@%
  25850.     setmode( fileno( htmp ), O_TEXT );%@NL@%
  25851.     fprintf( htmp, "\nThis\nis\nsome\ntext\ntext.\n\n" );%@NL@%
  25852.     fclose( htmp );%@NL@%
  25853. %@NL@%
  25854.     system( strcat( "TYPE ", name ) );%@NL@%
  25855.     remove( name );%@NL@%
  25856.     exit( 0 );%@NL@%
  25857. }%@NL@%
  25858. %@NL@%
  25859. %@NL@%
  25860. %@2@%%@AH@%BREAKER.C%@AE@%%@EH@%%@NL@%
  25861. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\BREAKER.C%@AE@%%@NL@%
  25862. %@NL@%
  25863. %@AB@%/* BREAKER.C: Demonstrate break statement. */%@AE@%%@NL@%
  25864. %@NL@%
  25865. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25866. %@AI@%#include %@AE@%<conio.h> %@NL@%
  25867. %@NL@%
  25868. main()%@NL@%
  25869. {%@NL@%
  25870.    char ch;%@NL@%
  25871.    printf( "Press any key. Press TAB to quit.\n" );%@NL@%
  25872.    while( 1 )%@NL@%
  25873.    {%@NL@%
  25874.       ch = getche();%@NL@%
  25875.       if( ch == '\t' )%@NL@%
  25876.       {%@NL@%
  25877.          printf( "\a\nYou pressed TAB.\n" );%@NL@%
  25878.          break;%@NL@%
  25879.       }%@NL@%
  25880.    }%@NL@%
  25881. }%@NL@%
  25882. %@NL@%
  25883. %@NL@%
  25884. %@2@%%@AH@%BREAKER1.C%@AE@%%@EH@%%@NL@%
  25885. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\BREAKER1.C%@AE@%%@NL@%
  25886. %@NL@%
  25887. %@AB@%/* BREAKER1.C: Break only exits one loop. */%@AE@%%@NL@%
  25888. %@NL@%
  25889. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25890. %@AI@%#include %@AE@%<conio.h> %@NL@%
  25891. %@NL@%
  25892. main()%@NL@%
  25893. {%@NL@%
  25894.    char ch;%@NL@%
  25895.    printf( "Press any key. Press RETURN to quit.\n" );%@NL@%
  25896.    do%@NL@%
  25897.    {%@NL@%
  25898.       while( ( ch = getche() ) != '\r' )%@NL@%
  25899.       {%@NL@%
  25900.          if( ch == '\t' )%@NL@%
  25901.          {%@NL@%
  25902.             printf( "\a\nYou pressed TAB.\n" );%@NL@%
  25903.             break;%@NL@%
  25904.          }%@NL@%
  25905.       }%@NL@%
  25906.    } while( ch != '\r' );%@NL@%
  25907.    printf( "\nBye bye." );%@NL@%
  25908. }%@NL@%
  25909. %@NL@%
  25910. %@NL@%
  25911. %@2@%%@AH@%BUFTEST.C%@AE@%%@EH@%%@NL@%
  25912. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\BUFTEST.C%@AE@%%@NL@%
  25913. %@NL@%
  25914. %@AB@%/* BUFTEST.C illustrates buffer control for stream I/O using functions:%@NL@%
  25915. %@AB@% *      setbuf              setvbuf%@NL@%
  25916. %@AB@% */%@AE@%%@NL@%
  25917. %@NL@%
  25918. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25919. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  25920. %@AI@%#include %@AE@%<time.h> %@NL@%
  25921. %@AI@%#define %@AE@%BUF2SIZ 2048 %@NL@%
  25922. %@NL@%
  25923. long countln( FILE *stream );           %@AB@%/* Prototype        */%@AE@%%@NL@%
  25924. char buf1[BUFSIZ], buf2[BUF2SIZ];       %@AB@%/* File buffers     */%@AE@%%@NL@%
  25925. %@NL@%
  25926. main( int argc, char *argv[] )%@NL@%
  25927. {%@NL@%
  25928.     time_t start;%@NL@%
  25929.     FILE *stream;%@NL@%
  25930.     int  c;%@NL@%
  25931. %@NL@%
  25932.     %@AB@%/* Use our buffer with the default size. This gives us the option%@NL@%
  25933. %@AB@%     * of examining and/or modifying the buffer during I/0 (though the%@NL@%
  25934. %@AB@%     * example doesn't illustrate this).%@NL@%
  25935. %@AB@%     */%@AE@%%@NL@%
  25936.     if( (stream = fopen( argv[1], "rt" )) == NULL )%@NL@%
  25937.         exit( 1 );%@NL@%
  25938.     setbuf( stream, buf1 );%@NL@%
  25939.     start = clock();%@NL@%
  25940.     c = countln( stream );%@NL@%
  25941.     printf( "Time: %5.2f\tBuffering: Normal\tBuffer size: %d\n",%@NL@%
  25942.              ((float)clock() - start) / CLK_TCK, BUFSIZ );%@NL@%
  25943. %@NL@%
  25944.     %@AB@%/* Use a larger buffer. */%@AE@%%@NL@%
  25945.     if( (stream = fopen( argv[1], "rt" )) == NULL )%@NL@%
  25946.         exit( 1 );%@NL@%
  25947.     setvbuf( stream, buf2, _IOFBF, sizeof( buf2 ) );%@NL@%
  25948.     start = clock();%@NL@%
  25949.     c = countln( stream );%@NL@%
  25950.     printf( "Time: %5.2f\tBuffering: Normal\tBuffer size: %d\n",%@NL@%
  25951.              ((float)clock() - start) / CLK_TCK, BUF2SIZ );%@NL@%
  25952. %@NL@%
  25953.     %@AB@%/* Try it with no buffering. */%@AE@%%@NL@%
  25954.     if( (stream = fopen( argv[1], "rt" )) == NULL )%@NL@%
  25955.         exit( 1 );%@NL@%
  25956.     setvbuf( stream, NULL, _IONBF, 0 );%@NL@%
  25957.     start = clock();%@NL@%
  25958.     c = countln( stream );%@NL@%
  25959.     printf( "Time: %5.2f\tBuffering: Normal\tBuffer size: %d\n",%@NL@%
  25960.              ((float)clock() - start) / CLK_TCK, 0 );%@NL@%
  25961. %@NL@%
  25962.     printf( "File %s has %d lines", argv[1], c );%@NL@%
  25963.     exit( 0 );%@NL@%
  25964. }%@NL@%
  25965. %@NL@%
  25966. %@AB@%/* Count lines in text files and close file. */%@AE@%%@NL@%
  25967. long countln( FILE *stream )%@NL@%
  25968. {%@NL@%
  25969.     char linebuf[120];%@NL@%
  25970.     long c = 0;%@NL@%
  25971. %@NL@%
  25972.     while( !feof( stream ) )%@NL@%
  25973.     {%@NL@%
  25974.         fgets( linebuf, 121, stream );%@NL@%
  25975.         ++c;%@NL@%
  25976.     }%@NL@%
  25977.     fclose( stream );%@NL@%
  25978.     return c;%@NL@%
  25979. }%@NL@%
  25980. %@NL@%
  25981. %@NL@%
  25982. %@2@%%@AH@%CABS.C%@AE@%%@EH@%%@NL@%
  25983. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\MATH\CABS.C%@AE@%%@NL@%
  25984. %@NL@%
  25985. %@AB@%/* CABS.C illustrates functions:%@NL@%
  25986. %@AB@% *      cabs            hypot%@NL@%
  25987. %@AB@% */%@AE@%%@NL@%
  25988. %@NL@%
  25989. %@AI@%#include %@AE@%<math.h> %@NL@%
  25990. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  25991. %@NL@%
  25992. main()%@NL@%
  25993. {%@NL@%
  25994.     static struct complex ne = {  3.0,  4.0 }, se = { -3.0, -4.0 },%@NL@%
  25995.                           sw = { -3.0, -4.0 }, nw = { -3.0,  4.0 };%@NL@%
  25996. %@NL@%
  25997.     printf( "Absolute %4.1lf + %4.1lfi:\t\t%4.1f\n",%@NL@%
  25998.             ne.x, ne.y, cabs( ne ) );%@NL@%
  25999.     printf( "Absolute %4.1lf + %4.1lfi:\t\t%4.1f\n",%@NL@%
  26000.             sw.x, sw.y, cabs( sw ) );%@NL@%
  26001. %@NL@%
  26002.     printf( "Hypotenuse of %4.1lf and %4.1lf:\t%4.1f\n",%@NL@%
  26003.             se.x, se.y, hypot( se.x, se.y ) );%@NL@%
  26004.     printf( "Hypotenuse of %4.1lf and %4.1lf:\t%4.1f\n",%@NL@%
  26005.             nw.x, nw.y, hypot( nw.x, nw.y ) );%@NL@%
  26006. }%@NL@%
  26007. %@NL@%
  26008. %@NL@%
  26009. %@NL@%
  26010. %@2@%%@AH@%CASE.C%@AE@%%@EH@%%@NL@%
  26011. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\CASE.C%@AE@%%@NL@%
  26012. %@NL@%
  26013. %@AB@%/* CASE.C illustrates case conversion and other conversions.%@NL@%
  26014. %@AB@% * Functions illustrated include:%@NL@%
  26015. %@AB@% *      strupr          toupper         _toupper%@NL@%
  26016. %@AB@% *      strlwr          tolower         _tolower%@NL@%
  26017. %@AB@% *      strrev          toascii%@NL@%
  26018. %@AB@% */%@AE@%%@NL@%
  26019. %@NL@%
  26020. %@AI@%#include %@AE@%<string.h> %@NL@%
  26021. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26022. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  26023. %@NL@%
  26024. char mstring[] = "Dog Saw Dad Live On";%@NL@%
  26025. char *ustring, *tstring, *estring;%@NL@%
  26026. char *p;%@NL@%
  26027. %@NL@%
  26028. main()%@NL@%
  26029. {%@NL@%
  26030.     printf( "Original:\t%s\n", mstring );%@NL@%
  26031. %@NL@%
  26032.     %@AB@%/* Upper and lower case. */%@AE@%%@NL@%
  26033.     ustring = strupr( strdup( mstring ) );%@NL@%
  26034.     printf( "Upper case:\t%s\n", ustring );%@NL@%
  26035. %@NL@%
  26036.     printf( "Lower case:\t%s\n", strlwr( ustring ) );%@NL@%
  26037. %@NL@%
  26038.     %@AB@%/* Reverse case of each character. */%@AE@%%@NL@%
  26039.     tstring = strdup( mstring );%@NL@%
  26040.     for( p = tstring; *p; p++ )%@NL@%
  26041.     {%@NL@%
  26042.         if( isupper( *p ) )%@NL@%
  26043.             *p = tolower( *p );%@NL@%
  26044.         else%@NL@%
  26045.             *p = toupper( *p );%@NL@%
  26046. %@NL@%
  26047.         %@AB@%/* This alternate code (commented out) shows how to use _tolower%@NL@%
  26048. %@AB@%         * and _toupper for the same purpose.%@NL@%
  26049. %@AB@%        if( isupper( *p ) )%@NL@%
  26050. %@AB@%            *p = _tolower( *p );%@NL@%
  26051. %@AB@%        else if( islower( *p ) )%@NL@%
  26052. %@AB@%            *p = _toupper( *p );%@NL@%
  26053. %@AB@%         */%@AE@%%@NL@%
  26054.     }%@NL@%
  26055.     printf( "Toggle case:\t%s\n", tstring );%@NL@%
  26056. %@NL@%
  26057.     %@AB@%/* Encode and decode string. The decoding technique will convert%@NL@%
  26058. %@AB@%     * WordStar-style strings, which have some high bits set.%@NL@%
  26059. %@AB@%     */%@AE@%%@NL@%
  26060.     estring = strdup( mstring );%@NL@%
  26061.     for( p = estring; *p; p++ )%@NL@%
  26062.         *p = *p | 0x80;%@NL@%
  26063.     printf( "Encoded:\t%s\n", estring );%@NL@%
  26064. %@NL@%
  26065.     for( p = estring; *p; p++ )%@NL@%
  26066.         *p = toascii( *p );%@NL@%
  26067.     printf( "Decoded:\t%s\n", estring );%@NL@%
  26068. %@NL@%
  26069.     printf( "Reversed:\t%s\n", strrev( ustring ) );%@NL@%
  26070. }%@NL@%
  26071. %@NL@%
  26072. %@NL@%
  26073. %@2@%%@AH@%CGA.C%@AE@%%@EH@%%@NL@%
  26074. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\CGA.C%@AE@%%@NL@%
  26075. %@NL@%
  26076. %@AB@%/* CGA.C: Demonstrates CGA colors */%@AE@%%@NL@%
  26077. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26078. %@AI@%#include %@AE@%<graph.h> %@NL@%
  26079. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26080. %@NL@%
  26081. long bkcolor[8] = %@NL@%
  26082.    {_BLACK, _BLUE, _GREEN, _CYAN,%@NL@%
  26083.     _RED, _MAGENTA, _BROWN, _WHITE};%@NL@%
  26084. %@NL@%
  26085. char *bkcolor_name [] =%@NL@%
  26086.    {"_BLACK", "_BLUE", "_GREEN", "_CYAN",%@NL@%
  26087.    "_RED", "_MAGENTA", "_BROWN", "_WHITE"};%@NL@%
  26088. %@NL@%
  26089. main()%@NL@%
  26090. {%@NL@%
  26091.    int i, j, k;%@NL@%
  26092.    _setvideomode( _MRES4COLOR );%@NL@%
  26093.    for( i=0; i<= 3; i++ )%@NL@%
  26094.    {%@NL@%
  26095.       _selectpalette( i );%@NL@%
  26096.       for( k=0; k <= 7; k++ )%@NL@%
  26097.       {%@NL@%
  26098.          _setbkcolor( bkcolor[k] );%@NL@%
  26099.          for( j=0; j<=3; j++ )%@NL@%
  26100.          {%@NL@%
  26101.             _settextposition( 1, 1 );%@NL@%
  26102.             printf( "background color: %8s\n", bkcolor_name[k] );%@NL@%
  26103.             printf( "palette: %d\ncolor: %d\n", i, j );%@NL@%
  26104.             _setcolor( j );%@NL@%
  26105.             _rectangle( _GFILLINTERIOR, 160, 100, 320, 200 );%@NL@%
  26106.                getch();%@NL@%
  26107.          }%@NL@%
  26108.       }%@NL@%
  26109.    }%@NL@%
  26110.    _setvideomode( _DEFAULTMODE );%@NL@%
  26111. }%@NL@%
  26112. %@NL@%
  26113. %@NL@%
  26114. %@2@%%@AH@%CGAPAL.C%@AE@%%@EH@%%@NL@%
  26115. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\CGAPAL.C%@AE@%%@NL@%
  26116. %@NL@%
  26117. %@AB@%/* CGAPAL.C illustrates CGA palettes using:%@NL@%
  26118. %@AB@% *      _selectpalette%@NL@%
  26119. %@AB@% */%@AE@%%@NL@%
  26120. %@NL@%
  26121. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26122. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  26123. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26124. %@AI@%#include %@AE@%<graph.h> %@NL@%
  26125. %@NL@%
  26126. long bkcolor[8] = { _BLACK,  _BLUE,     _GREEN,  _CYAN,%@NL@%
  26127.                     _RED,    _MAGENTA,  _BROWN,  _WHITE };%@NL@%
  26128. char *bkname [] = { "BLACK", "BLUE",    "GREEN", "CYAN",%@NL@%
  26129.                     "RED",   "MAGENTA", "BROWN", "WHITE" };%@NL@%
  26130. main()%@NL@%
  26131. {%@NL@%
  26132.     int i, j, k;%@NL@%
  26133. %@NL@%
  26134.     if ( !_setvideomode( _MRES4COLOR ) )%@NL@%
  26135.         exit( 1 );%@NL@%
  26136.     for( i = 0; i < 4; i++ )%@NL@%
  26137.     {%@NL@%
  26138.         _selectpalette( i );%@NL@%
  26139.         for( k = 0; k < 8; k++ )%@NL@%
  26140.         {%@NL@%
  26141.             _setbkcolor( bkcolor[k] );%@NL@%
  26142.             for( j = 0; j < 4; j++ )%@NL@%
  26143.             {%@NL@%
  26144.                 _settextposition( 1, 1 );%@NL@%
  26145.                 printf( "Background color: %8s\n", bkname[k] );%@NL@%
  26146.                 printf( "Palette: %d\nColor: %d\n", i, j );%@NL@%
  26147.                 _setcolor( j );%@NL@%
  26148.                 _rectangle( _GFILLINTERIOR, 160, 100, 320, 200 );%@NL@%
  26149.                 getch();%@NL@%
  26150.             }%@NL@%
  26151.         }%@NL@%
  26152.     }%@NL@%
  26153.     exit( !_setvideomode( _DEFAULTMODE ) );%@NL@%
  26154. }%@NL@%
  26155. %@NL@%
  26156. %@NL@%
  26157. %@2@%%@AH@%CHMOD1.C%@AE@%%@EH@%%@NL@%
  26158. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\CHMOD1.C%@AE@%%@NL@%
  26159. %@NL@%
  26160. %@AB@%/* CHMOD1.C illustrates reading and changing file attribute and time using:%@NL@%
  26161. %@AB@% *      access          chmod           utime%@NL@%
  26162. %@AB@% *%@NL@%
  26163. %@AB@% * See CHMOD2.C for a more powerful variation of this program using%@NL@%
  26164. %@AB@% * _dos functions.%@NL@%
  26165. %@AB@% */%@AE@%%@NL@%
  26166. %@NL@%
  26167. %@AI@%#include %@AE@%<io.h> %@NL@%
  26168. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  26169. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  26170. %@AI@%#include %@AE@%<sys\utime.h> %@NL@%
  26171. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26172. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26173. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  26174. %@NL@%
  26175. enum FILEATTRIB { EXIST, WRITE = 2, READ = 4, READWRITE = 6 };%@NL@%
  26176. %@NL@%
  26177. %@AB@%/* Exist macro uses access */%@AE@%%@NL@%
  26178. %@AI@%#define %@AE@%EXIST( name ) !access( name, EXIST ) %@NL@%
  26179. %@NL@%
  26180. main( int argc, char *argv[] )%@NL@%
  26181. {%@NL@%
  26182.     if( !EXIST( argv[1] ) )%@NL@%
  26183.     {%@NL@%
  26184.         printf( "Syntax:  CHMOD1 <filename>" );%@NL@%
  26185.         exit( 1 );%@NL@%
  26186.     }%@NL@%
  26187. %@NL@%
  26188.     if( !access( argv[1], WRITE ) )%@NL@%
  26189.     {%@NL@%
  26190.         printf( "File %s is read/write. Change to read only? ", argv[1] );%@NL@%
  26191. %@NL@%
  26192.         %@AB@%/* Note: Use stdlib.h for function definition of toupper rather%@NL@%
  26193. %@AB@%         * than macro version in ctype.h. Macro side effects would cause%@NL@%
  26194. %@AB@%         * macro version to read two keys.%@NL@%
  26195. %@AB@%         */%@AE@%%@NL@%
  26196.         if( toupper( getch() ) == 'Y' )%@NL@%
  26197.             chmod( argv[1], S_IREAD );%@NL@%
  26198.     }%@NL@%
  26199.     else%@NL@%
  26200.     {%@NL@%
  26201.         printf( "File %s is read only. Change to read/write? ", argv[1] );%@NL@%
  26202.         if( toupper( getch() ) == 'Y' )%@NL@%
  26203.             chmod( argv[1], S_IREAD | S_IWRITE );%@NL@%
  26204.     }%@NL@%
  26205. %@NL@%
  26206.     printf( "\nUpdate file time to current time? " );%@NL@%
  26207.     if( toupper( getch() ) == 'Y' )%@NL@%
  26208.         utime( argv[1], NULL );%@NL@%
  26209.     exit( 0 );%@NL@%
  26210. }%@NL@%
  26211. %@NL@%
  26212. %@NL@%
  26213. %@2@%%@AH@%CHMOD2.C%@AE@%%@EH@%%@NL@%
  26214. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\CHMOD2.C%@AE@%%@NL@%
  26215. %@NL@%
  26216. %@AB@%/* CHMOD2.C illustrates reading and changing file attributes and file%@NL@%
  26217. %@AB@% * time using:%@NL@%
  26218. %@AB@% *      _dos_getftime       _dos_setftime%@NL@%
  26219. %@AB@% *      _dos_getfileattr    _dos_setfileattr%@NL@%
  26220. %@AB@% *%@NL@%
  26221. %@AB@% * See CHMOD1.C for a simpler variation of this program using the utime,%@NL@%
  26222. %@AB@% * access, and chmod functions.%@NL@%
  26223. %@AB@% */%@AE@%%@NL@%
  26224. %@NL@%
  26225. %@AI@%#include %@AE@%<dos.h> %@NL@%
  26226. %@AI@%#include %@AE@%<fcntl.h> %@NL@%
  26227. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26228. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26229. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  26230. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  26231. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  26232. %@NL@%
  26233. char *datestr( unsigned d, char *buf );     %@AB@%/* Prototypes */%@AE@%%@NL@%
  26234. char *timestr( unsigned t, char *buf );%@NL@%
  26235. %@NL@%
  26236. main( int argc, char *argv[] )%@NL@%
  26237. {%@NL@%
  26238.     unsigned fdate, ftime, fattr;%@NL@%
  26239.     struct dosdate_t ddate;%@NL@%
  26240.     struct dostime_t dtime;%@NL@%
  26241.     int hsource;%@NL@%
  26242.     char timebuf[10], datebuf[10], *pkind;%@NL@%
  26243. %@NL@%
  26244.     %@AB@%/* Open to get file handle and test for errors (such as nonexistence). */%@AE@%%@NL@%
  26245.     if( _dos_open( argv[1], O_RDONLY, &hsource ) )%@NL@%
  26246.     {%@NL@%
  26247.         printf( "Can't open file\n" );%@NL@%
  26248.         exit( 1 );%@NL@%
  26249.     }%@NL@%
  26250. %@NL@%
  26251.     %@AB@%/* Get time, date, and attribute of file. */%@AE@%%@NL@%
  26252.     _dos_getftime( hsource, &fdate, &ftime );%@NL@%
  26253.     _dos_getfileattr( argv[1], &fattr );%@NL@%
  26254. %@NL@%
  26255.     %@AB@%/* Convert information into formatted strings. */%@AE@%%@NL@%
  26256.     datestr( fdate, datebuf );%@NL@%
  26257.     timestr( ftime, timebuf );%@NL@%
  26258. %@NL@%
  26259.     if( fattr & _A_SUBDIR )%@NL@%
  26260.         pkind = "Directory";%@NL@%
  26261.     else if( fattr & _A_VOLID )%@NL@%
  26262.         pkind = "Label";%@NL@%
  26263.     else%@NL@%
  26264.         pkind = "File";%@NL@%
  26265. %@NL@%
  26266.     printf( "%-12s   %-8s   %-9s   %-9s    %s %s %s\n",%@NL@%
  26267.             "FILE", "TIME", "DATE", "KIND", "RDO", "HID", "SYS" );%@NL@%
  26268.     printf( "%-12s   %8s   %8s    %-9s     %c   %c   %c\n",%@NL@%
  26269.             argv[1], timebuf, datebuf, pkind,%@NL@%
  26270.             (fattr & _A_RDONLY) ? 'Y' : 'N',%@NL@%
  26271.             (fattr & _A_HIDDEN) ? 'Y' : 'N',%@NL@%
  26272.             (fattr & _A_SYSTEM) ? 'Y' : 'N' );%@NL@%
  26273. %@NL@%
  26274.     %@AB@%/* Update file time or attribute. */%@AE@%%@NL@%
  26275.     printf( "Change: (T)ime  (R)ead only  (H)idden  (S)ystem\n" );%@NL@%
  26276.     switch( toupper( getch() ) )    %@AB@%/* Use stdlib.h, not ctype.h */%@AE@%%@NL@%
  26277.     {%@NL@%
  26278.         case 'T':               %@AB@%/* Set to current time */%@AE@%%@NL@%
  26279.             _dos_gettime( &dtime );%@NL@%
  26280.             _dos_getdate( &ddate );%@NL@%
  26281.             ftime = (dtime.hour << 11) | (dtime.minute << 5);%@NL@%
  26282.             fdate = ((ddate.year - 1980) << 9) | (ddate.month << 5) |%@NL@%
  26283.                       ddate.day;%@NL@%
  26284.             _dos_setftime( hsource, fdate, ftime );%@NL@%
  26285.             break;%@NL@%
  26286.         case 'R':               %@AB@%/* Toggle read only */%@AE@%%@NL@%
  26287.             _dos_setfileattr( argv[1], fattr ^ _A_RDONLY );%@NL@%
  26288.             break;%@NL@%
  26289.         case 'H':               %@AB@%/* Toggle hidden    */%@AE@%%@NL@%
  26290.             _dos_setfileattr( argv[1], fattr ^ _A_HIDDEN );%@NL@%
  26291.             break;%@NL@%
  26292.         case 'S':               %@AB@%/* Toggle system    */%@AE@%%@NL@%
  26293.             _dos_setfileattr( argv[1], fattr ^ _A_SYSTEM );%@NL@%
  26294.             break;%@NL@%
  26295.     }%@NL@%
  26296.     _dos_close( hsource );%@NL@%
  26297.     exit( 1 );%@NL@%
  26298. }%@NL@%
  26299. %@NL@%
  26300. %@AB@%/* Take unsigned time in the format:                fedcba9876543210%@NL@%
  26301. %@AB@% * s=2 sec incr, m=0-59, h=23                       hhhhhmmmmmmsssss%@NL@%
  26302. %@AB@% * Change to a 9-byte string (ignore seconds):      hh:mm ?m%@NL@%
  26303. %@AB@% */%@AE@%%@NL@%
  26304. char *timestr( unsigned t, char *buf )%@NL@%
  26305. {%@NL@%
  26306.     int h = (t >> 11) & 0x1f, m = (t >> 5) & 0x3f;%@NL@%
  26307. %@NL@%
  26308.     sprintf( buf, "%2.2d:%02.2d %cm", h % 12, m,  h > 11 ? 'p' : 'a' );%@NL@%
  26309.     return buf;%@NL@%
  26310. }%@NL@%
  26311. %@NL@%
  26312. %@AB@%/* Take unsigned date in the format:    fedcba9876543210%@NL@%
  26313. %@AB@% * d=1-31, m=1-12, y=0-119 (1980-2099)  yyyyyyymmmmddddd%@NL@%
  26314. %@AB@% * Change to a 9-byte string:           mm/dd/yy%@NL@%
  26315. %@AB@% */%@AE@%%@NL@%
  26316. char *datestr( unsigned d, char *buf )%@NL@%
  26317. {%@NL@%
  26318.     sprintf( buf, "%2.2d/%02.2d/%02.2d",%@NL@%
  26319.              (d >> 5) & 0x0f, d & 0x1f, (d >> 9) + 80 );%@NL@%
  26320.     return buf;%@NL@%
  26321. }%@NL@%
  26322. %@NL@%
  26323. %@NL@%
  26324. %@2@%%@AH@%CMPSTR.C%@AE@%%@EH@%%@NL@%
  26325. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\CMPSTR.C%@AE@%%@NL@%
  26326. %@NL@%
  26327. %@AB@%/* CMPSTR.C illustrates string and memory comparison functions including:%@NL@%
  26328. %@AB@% *       memcmp        memicmp%@NL@%
  26329. %@AB@% *       strncmp       strnicmp%@NL@%
  26330. %@AB@% *       strcmp        stricmp          strcmpi%@NL@%
  26331. %@AB@% */%@AE@%%@NL@%
  26332. %@NL@%
  26333. %@AI@%#include %@AE@%<memory.h> %@NL@%
  26334. %@AI@%#include %@AE@%<string.h> %@NL@%
  26335. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26336. %@NL@%
  26337. char string1[] = "The quick brown dog jumps over the lazy fox";%@NL@%
  26338. char string2[] = "The QUICK brown fox jumps over the lazy dog";%@NL@%
  26339. %@NL@%
  26340. main()%@NL@%
  26341. {%@NL@%
  26342.     char tmp[20];%@NL@%
  26343.     int result;%@NL@%
  26344. %@NL@%
  26345.     printf( "Compare strings:\n\t\t%s\n\t\t%s\n\n", string1, string2 );%@NL@%
  26346. %@NL@%
  26347.     printf( "Function:\tmemcmp\n" );%@NL@%
  26348.     result = memcmp( string1, string2 , 42 );%@NL@%
  26349.     if( result > 0 )%@NL@%
  26350.         strcpy( tmp, "greater than" );%@NL@%
  26351.     else if( result < 0 )%@NL@%
  26352.         strcpy( tmp, "less than" );%@NL@%
  26353.     else%@NL@%
  26354.         strcpy( tmp, "equal to" );%@NL@%
  26355.     printf( "Result:\t\tString 1 is %s than string 2\n\n", tmp );%@NL@%
  26356. %@NL@%
  26357.     printf( "Function:\tmemicmp\n" );%@NL@%
  26358.     result = memicmp( string1, string2, 42 );%@NL@%
  26359.     if( result > 0 )%@NL@%
  26360.         strcpy( tmp, "greater than" );%@NL@%
  26361.     else if( result < 0 )%@NL@%
  26362.         strcpy( tmp, "less than" );%@NL@%
  26363.     else%@NL@%
  26364.         strcpy( tmp, "equal to" );%@NL@%
  26365.     printf( "Result:\t\tString 1 is %s than string 2\n\n", tmp );%@NL@%
  26366. %@NL@%
  26367.     printf( "Function:\tstrncmp\n" );%@NL@%
  26368.     result = strncmp( string1, string2 , 42 );%@NL@%
  26369.     if( result > 0 )%@NL@%
  26370.         strcpy( tmp, "greater than" );%@NL@%
  26371.     else if( result < 0 )%@NL@%
  26372.         strcpy( tmp, "less than" );%@NL@%
  26373.     else%@NL@%
  26374.         strcpy( tmp, "equal to" );%@NL@%
  26375.     printf( "Result:\t\tString 1 is %s than string 2\n\n", tmp );%@NL@%
  26376. %@NL@%
  26377.     printf( "Function:\tstrnicmp\n" );%@NL@%
  26378.     result = strnicmp( string1, string2, 42 );%@NL@%
  26379.     if( result > 0 )%@NL@%
  26380.         strcpy( tmp, "greater than" );%@NL@%
  26381.     else if( result < 0 )%@NL@%
  26382.         strcpy( tmp, "less than" );%@NL@%
  26383.     else%@NL@%
  26384.         strcpy( tmp, "equal to" );%@NL@%
  26385.     printf( "Result:\t\tString 1 is %s than string 2\n\n", tmp );%@NL@%
  26386. %@NL@%
  26387.     printf( "Function:\tstrcmp\n" );%@NL@%
  26388.     result = strcmp( string1, string2 );%@NL@%
  26389.     if( result > 0 )%@NL@%
  26390.         strcpy( tmp, "greater than" );%@NL@%
  26391.     else if( result < 0 )%@NL@%
  26392.         strcpy( tmp, "less than" );%@NL@%
  26393.     else%@NL@%
  26394.         strcpy( tmp, "equal to" );%@NL@%
  26395.     printf( "Result:\t\tString 1 is %s than string 2\n\n", tmp );%@NL@%
  26396. %@NL@%
  26397.     printf( "Function:\tstricmp or strcmpi\n" );%@NL@%
  26398.     result = stricmp( string1, string2 );%@NL@%
  26399.     %@AB@%/* strcmpi (commented out) is the same as stricmp.%@NL@%
  26400. %@AB@%    result = strcmpi( string1, string2 );%@NL@%
  26401. %@AB@%     */%@AE@%%@NL@%
  26402.     if( result > 0 )%@NL@%
  26403.         strcpy( tmp, "greater than" );%@NL@%
  26404.     else if( result < 0 )%@NL@%
  26405.         strcpy( tmp, "less than" );%@NL@%
  26406.     else%@NL@%
  26407.         strcpy( tmp, "equal to" );%@NL@%
  26408.     printf( "Result:\t\tString 1 is %s than string 2\n", tmp );%@NL@%
  26409. }%@NL@%
  26410. %@NL@%
  26411. %@NL@%
  26412. %@2@%%@AH@%COLOR.C%@AE@%%@EH@%%@NL@%
  26413. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\COLOR.C%@AE@%%@NL@%
  26414. %@NL@%
  26415.  %@AB@%/* COLOR.C: Sets a medium resolution mode with maximum color choices */%@AE@%%@NL@%
  26416. %@NL@%
  26417. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26418. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  26419. %@AI@%#include %@AE@%<graph.h> %@NL@%
  26420. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26421. struct videoconfig vc;%@NL@%
  26422. %@NL@%
  26423. main()%@NL@%
  26424. {%@NL@%
  26425.    if( _setvideomode( _MRES256COLOR ) )%@NL@%
  26426.       ;%@NL@%
  26427.    else if( _setvideomode( _MRES16COLOR ) )%@NL@%
  26428.       ;%@NL@%
  26429.    else if( _setvideomode( _MRES4COLOR ) )%@NL@%
  26430.       ;%@NL@%
  26431.    else   {%@NL@%
  26432.       printf( "Error: No color graphics capability\n" );%@NL@%
  26433.       exit( 0 );%@NL@%
  26434.    }%@NL@%
  26435. %@NL@%
  26436.    _getvideoconfig( &vc );%@NL@%
  26437. %@NL@%
  26438.    printf( "%d available colors\n", vc.numcolors );%@NL@%
  26439.    printf( "%d horizontal pixels\n", vc.numxpixels );%@NL@%
  26440.    printf( "%d vertical pixels\n", vc.numypixels );%@NL@%
  26441. %@NL@%
  26442.    getch();%@NL@%
  26443.    _clearscreen( _GCLEARSCREEN );%@NL@%
  26444.    _setvideomode( _DEFAULTMODE );%@NL@%
  26445. }%@NL@%
  26446. %@NL@%
  26447. %@NL@%
  26448. %@2@%%@AH@%COLTEXT.C%@AE@%%@EH@%%@NL@%
  26449. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\COLTEXT.C%@AE@%%@NL@%
  26450. %@NL@%
  26451. %@AB@%/* COLTEXT.C: Displays text in color */%@AE@%%@NL@%
  26452. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26453. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26454. %@AI@%#include %@AE@%<graph.h> %@NL@%
  26455. %@NL@%
  26456. char buffer [80];%@NL@%
  26457. %@NL@%
  26458. main()%@NL@%
  26459. {%@NL@%
  26460.    int blink,fgd;%@NL@%
  26461.    long bgd;%@NL@%
  26462. %@NL@%
  26463.    _clearscreen( _GCLEARSCREEN );%@NL@%
  26464.    printf( "Text color attributes:\n" );%@NL@%
  26465. %@NL@%
  26466.    for( blink=0; blink<=16; blink+=16 ) %@NL@%
  26467.    {%@NL@%
  26468.       for( bgd=0; bgd<8; bgd++ ) %@NL@%
  26469.       {%@NL@%
  26470.          _setbkcolor( bgd );%@NL@%
  26471.          _settextposition( bgd + ((blink / 16) * 9) + 3, 1 );%@NL@%
  26472.          _settextcolor( 7 );%@NL@%
  26473.          sprintf( buffer, "Bgd: %d Fgd:", bgd );%@NL@%
  26474.          _outtext( buffer );%@NL@%
  26475.          %@NL@%
  26476.          for( fgd=0; fgd<16; fgd++ ) %@NL@%
  26477.          {%@NL@%
  26478.             _settextcolor( fgd+blink );%@NL@%
  26479.             sprintf( buffer, " %2d ", fgd+blink );%@NL@%
  26480.             _outtext( buffer );%@NL@%
  26481.          }%@NL@%
  26482.       }%@NL@%
  26483.    }%@NL@%
  26484.    getch();%@NL@%
  26485.    _setvideomode( _DEFAULTMODE );%@NL@%
  26486. }%@NL@%
  26487. %@NL@%
  26488. %@NL@%
  26489. %@2@%%@AH@%COM.C%@AE@%%@EH@%%@NL@%
  26490. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\COM.C%@AE@%%@NL@%
  26491. %@NL@%
  26492. %@AB@%/* COM.C illustrates serial port access using function:%@NL@%
  26493. %@AB@% *      _bios_serialcom%@NL@%
  26494. %@AB@% */%@AE@%%@NL@%
  26495. %@NL@%
  26496. %@AI@%#include %@AE@%<bios.h> %@NL@%
  26497. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26498. %@NL@%
  26499. main()%@NL@%
  26500. {%@NL@%
  26501.     unsigned status, port;%@NL@%
  26502. %@NL@%
  26503.     for( port = 0; port < 3; port++ )%@NL@%
  26504.     {%@NL@%
  26505.         status = _bios_serialcom( _COM_STATUS, port, 0 );%@NL@%
  26506. %@NL@%
  26507.         %@AB@%/* Report status of each serial port and test whether there is a%@NL@%
  26508. %@AB@%         * modem for each. If data-set-ready and clear-to-send, modem exists.%@NL@%
  26509. %@AB@%         */%@AE@%%@NL@%
  26510.         printf( "COM%c status: %.4X\tModem: %s\n",%@NL@%
  26511.                 (char)port + '1', status,%@NL@%
  26512.                 (status & 0x0030) ? "YES" : "NO" );%@NL@%
  26513.     }%@NL@%
  26514. }%@NL@%
  26515. %@NL@%
  26516. %@NL@%
  26517. %@2@%%@AH@%COMMA.C%@AE@%%@EH@%%@NL@%
  26518. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\COMMA.C%@AE@%%@NL@%
  26519. %@NL@%
  26520. %@AB@%/* COMMA.C: Demonstrate comma operator. */%@AE@%%@NL@%
  26521. %@NL@%
  26522. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26523. %@NL@%
  26524. main()%@NL@%
  26525. {%@NL@%
  26526.    int val = 5, val1 = 666, temp;%@NL@%
  26527.    temp = val, val = val1, val1 = temp;%@NL@%
  26528.    printf( "val = %d  val1 = %d\n", val, val1 );%@NL@%
  26529. }%@NL@%
  26530. %@NL@%
  26531. %@NL@%
  26532. %@2@%%@AH@%CONT.C%@AE@%%@EH@%%@NL@%
  26533. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\CONT.C%@AE@%%@NL@%
  26534. %@NL@%
  26535. %@AB@%/* CONT.C: Demonstrate continue statement. */%@AE@%%@NL@%
  26536. %@NL@%
  26537. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26538. %@NL@%
  26539. main()%@NL@%
  26540. {%@NL@%
  26541.    int count;%@NL@%
  26542.    for( count = 0; count < 10; count++ )%@NL@%
  26543.    {%@NL@%
  26544.       if( count > 3 )%@NL@%
  26545.          continue;%@NL@%
  26546.       printf( "count = %d\n", count );%@NL@%
  26547.    }%@NL@%
  26548.    printf( "Done!\n" );%@NL@%
  26549. }%@NL@%
  26550. %@NL@%
  26551. %@NL@%
  26552. %@2@%%@AH@%CONT1.C%@AE@%%@EH@%%@NL@%
  26553. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\CONT1.C%@AE@%%@NL@%
  26554. %@NL@%
  26555. %@AB@%/* CONT1.C: Demonstrate alternative to continue. */%@AE@%%@NL@%
  26556. %@NL@%
  26557. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26558. %@NL@%
  26559. main()%@NL@%
  26560. {%@NL@%
  26561.    int count;%@NL@%
  26562.    for( count = 0; count < 10; count++ )%@NL@%
  26563.    {%@NL@%
  26564.       if( count < 4 )%@NL@%
  26565.          printf( "count = %d\n", count );%@NL@%
  26566.    }%@NL@%
  26567.    printf( "Done!\n" );%@NL@%
  26568. }%@NL@%
  26569. %@NL@%
  26570. %@NL@%
  26571. %@2@%%@AH@%CONVERT.C%@AE@%%@EH@%%@NL@%
  26572. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\CONVERT.C%@AE@%%@NL@%
  26573. %@NL@%
  26574. %@AB@%/* CONVERT.C: Demonstrate type conversions. */%@AE@%%@NL@%
  26575. %@NL@%
  26576. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26577. %@NL@%
  26578. main()%@NL@%
  26579. {%@NL@%
  26580.    char c_val = 10;%@NL@%
  26581.    int i_val = 20;%@NL@%
  26582.    long l_val = 64000;%@NL@%
  26583.    float f_val = 3.1;%@NL@%
  26584.    int result;%@NL@%
  26585. %@NL@%
  26586.    result = c_val + i_val + l_val + f_val;  %@AB@%/* Error! */%@AE@%%@NL@%
  26587. %@NL@%
  26588.    printf( "%d\n", result );%@NL@%
  26589. }%@NL@%
  26590. %@NL@%
  26591. %@NL@%
  26592. %@2@%%@AH@%COPROC.C%@AE@%%@EH@%%@NL@%
  26593. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\MATH\COPROC.C%@AE@%%@NL@%
  26594. %@NL@%
  26595. %@AB@%/* COPROC.C illustrates use of the status and control words of a floating%@NL@%
  26596. %@AB@% * point coprocessor (or emulator). Functions illustrated include:%@NL@%
  26597. %@AB@% *      _clear87            _status87           _control87%@NL@%
  26598. %@AB@% */%@AE@%%@NL@%
  26599. %@NL@%
  26600. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26601. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26602. %@AI@%#include %@AE@%<float.h> %@NL@%
  26603. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  26604. %@AI@%#include %@AE@%<string.h> %@NL@%
  26605. %@NL@%
  26606. double dx = 1e-40, dy;%@NL@%
  26607. float fx, fy;%@NL@%
  26608. unsigned status, control;%@NL@%
  26609. char tmpstr[20];%@NL@%
  26610. char *binstr( int num, char *buffer );%@NL@%
  26611. %@NL@%
  26612. main()%@NL@%
  26613. {%@NL@%
  26614.     printf( "Status Word Key:\n" );%@NL@%
  26615.     printf( "B\tBusy flag\n0-3\tCondition codes\nS\tStack top pointer\n" );%@NL@%
  26616.     printf( "E\tError summary\nF\tStack flag\nP\tPrecision exception\n" );%@NL@%
  26617.     printf( "U\tUnderflow exception\nO\tOverflow exception\n" );%@NL@%
  26618.     printf( "Z\tZero divide exception\nD\tDenormalized exception\n" );%@NL@%
  26619.     printf( "I\tInvalid operation exception\n\n" );%@NL@%
  26620. %@NL@%
  26621.     binstr(  _clear87(), tmpstr );%@NL@%
  26622.     printf( "B3SSS210EFPUOZDI  Function\tCondition\n\n" );%@NL@%
  26623.     printf( "%16s  _clear87\tAfter clearing\n", tmpstr );%@NL@%
  26624. %@NL@%
  26625.     %@AB@%/* Storing double to float that hasn't enough precision for it%@NL@%
  26626. %@AB@%     * causes underflow and precision exceptions.%@NL@%
  26627. %@AB@%     */%@AE@%%@NL@%
  26628.     fx = dx;%@NL@%
  26629.     binstr(  _status87(), tmpstr );%@NL@%
  26630.     printf( "%16s  _status87\tAfter moving double to float\n", tmpstr );%@NL@%
  26631. %@NL@%
  26632.     %@AB@%/* Storing float with lost precision back to double adds denormalized%@NL@%
  26633. %@AB@%     * exception (previous exceptions remain).%@NL@%
  26634. %@AB@%     */%@AE@%%@NL@%
  26635.     dy = fx;%@NL@%
  26636.     binstr(  _clear87(), tmpstr );%@NL@%
  26637.     printf( "%16s  _clear87\tAfter moving float to double\n", tmpstr );%@NL@%
  26638. %@NL@%
  26639.     %@AB@%/* Using clear87() erases previous exceptions. */%@AE@%%@NL@%
  26640.     fy = dy;%@NL@%
  26641.     binstr(  _status87(), tmpstr );%@NL@%
  26642.     printf( "%16s  _status87\tAfter moving double to float\n\n", tmpstr );%@NL@%
  26643. %@NL@%
  26644.     getch();%@NL@%
  26645.     printf( "Control Word Key:\n" );%@NL@%
  26646.     printf( "i\tInfinity control\nr\tRounding control\n" );%@NL@%
  26647.     printf( "p\tPrecision control\ne\tInterrupt enable mask\n" );%@NL@%
  26648.     printf( "U\tUnderflow mask\nO\tOverflow mask\n" );%@NL@%
  26649.     printf( "Z\tZero divide mask\nD\tDenormalized mask\n" );%@NL@%
  26650.     printf( "I\tInvalid operation mask\n\n" );%@NL@%
  26651.     printf( "???irrppe?PUOZDI  Result\n" );%@NL@%
  26652.     fy = .1;%@NL@%
  26653. %@NL@%
  26654.     %@AB@%/* Show current control word. */%@AE@%%@NL@%
  26655.     binstr( _control87( 0, 0 ), tmpstr );%@NL@%
  26656.     printf( "%16s  %.1f * %.1f = %.15e with initial precision\n",%@NL@%
  26657.             tmpstr, fy, fy, fy * fy );%@NL@%
  26658. %@NL@%
  26659.     %@AB@%/* Set precision to 24 bits. */%@AE@%%@NL@%
  26660.     binstr( _control87( PC_24, MCW_PC ), tmpstr );%@NL@%
  26661.     printf( "%16s  %.1f * %.1f = %.15e with 24 bit precision\n",%@NL@%
  26662.             tmpstr, fy, fy, fy * fy );%@NL@%
  26663. %@NL@%
  26664.     %@AB@%/* Restore default. */%@AE@%%@NL@%
  26665.     binstr( _control87( CW_DEFAULT, 0xffff ), tmpstr );%@NL@%
  26666.     printf( "%16s  %.1f * %.1f = %.15e with default precision\n",%@NL@%
  26667.             tmpstr, fy, fy, fy * fy );%@NL@%
  26668. }%@NL@%
  26669. %@NL@%
  26670. %@AB@%/* Convert integer to string of 16 binary characters. */%@AE@%%@NL@%
  26671. char *binstr( int num, char *buffer )%@NL@%
  26672. {%@NL@%
  26673.     char tmp[17];%@NL@%
  26674.     int  len;%@NL@%
  26675. %@NL@%
  26676.     memset( buffer, '0', 16 );%@NL@%
  26677.     len = strlen( itoa( num, tmp, 2 ) );%@NL@%
  26678.     strcpy( buffer + 16 - len, tmp );%@NL@%
  26679.     return buffer;%@NL@%
  26680. }%@NL@%
  26681. %@NL@%
  26682. %@NL@%
  26683. %@2@%%@AH@%COPY2.C%@AE@%%@EH@%%@NL@%
  26684. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\COPY2.C%@AE@%%@NL@%
  26685. %@NL@%
  26686. %@AB@%/* COPY2.C illustrates DOS file I/O and DOS memory allocation functions%@NL@%
  26687. %@AB@% * including:%@NL@%
  26688. %@AB@% *      _dos_open       _dos_close      _dos_creatnew   _dos_creat%@NL@%
  26689. %@AB@% *      _dos_read       _dos_write      _dos_allocmem   _dos_free%@NL@%
  26690. %@AB@% *%@NL@%
  26691. %@AB@% * See COPY1.C for another version of the copyfile function.%@NL@%
  26692. %@AB@% */%@AE@%%@NL@%
  26693. %@NL@%
  26694. %@AI@%#include %@AE@%<dos.h> %@NL@%
  26695. %@AI@%#include %@AE@%<fcntl.h> %@NL@%
  26696. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26697. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26698. %@NL@%
  26699. int copyfile( char *source, char *destin );     %@AB@%/* Prototype */%@AE@%%@NL@%
  26700. %@NL@%
  26701. main( int argc, char *argv[] )%@NL@%
  26702. {%@NL@%
  26703.     if( argc == 3 )%@NL@%
  26704.         if( copyfile( argv[1], argv[2] ) )%@NL@%
  26705.             printf( "Copy failed\n" );%@NL@%
  26706.         else%@NL@%
  26707.             printf( "Copy successful\n" );%@NL@%
  26708.     else%@NL@%
  26709.         printf( "  SYNTAX: COPY2 <source> <target>\n" );%@NL@%
  26710. }%@NL@%
  26711. %@NL@%
  26712. %@AB@%/* Function to copy one file to another (both specified by path).%@NL@%
  26713. %@AB@% * Dynamically allocates memory for the file buffer. Prompts before%@NL@%
  26714. %@AB@% * overwriting existing file. Returns 0 if successful, or an error%@NL@%
  26715. %@AB@% * number if unsuccessful. This function uses dos functions only; no%@NL@%
  26716. %@AB@% * standard C library functions are used.%@NL@%
  26717. %@AB@% */%@AE@%%@NL@%
  26718. %@AI@%#define %@AE@%EXIST 80 %@NL@%
  26719. enum ATTRIB { NORMAL, RDONLY, HIDDEN, SYSTEM = 4 };%@NL@%
  26720. int copyfile( char *source, char *target )%@NL@%
  26721. {%@NL@%
  26722.     char far *buf = NULL;%@NL@%
  26723.     static char prompt[] = "Target exists. Overwrite? ", newline[] = "\n\r";%@NL@%
  26724.     int hsource, htarget, ch;%@NL@%
  26725.     unsigned ret, segbuf, count;%@NL@%
  26726. %@NL@%
  26727.     %@AB@%/* Attempt to dynamically allocate all of memory (0xffff paragraphs).%@NL@%
  26728. %@AB@%     * This will fail, but will return the amount actually available%@NL@%
  26729. %@AB@%     * in segbuf. Then allocate this amount.%@NL@%
  26730. %@AB@%     */%@AE@%%@NL@%
  26731.     _dos_allocmem( 0xffff, &segbuf );%@NL@%
  26732.     count = segbuf;%@NL@%
  26733.     if( ret = _dos_allocmem( count, &segbuf ) )%@NL@%
  26734.         return ret;%@NL@%
  26735.     FP_SEG( buf ) = segbuf;%@NL@%
  26736. %@NL@%
  26737.     %@AB@%/* Open source file and create target, overwriting if necessary. */%@AE@%%@NL@%
  26738.     if( ret = _dos_open( source, O_RDONLY, &hsource ) )%@NL@%
  26739.         return ret;%@NL@%
  26740.     ret = _dos_creatnew( target, NORMAL, &htarget );%@NL@%
  26741.     if( ret == EXIST )%@NL@%
  26742.     {%@NL@%
  26743.         %@AB@%/* Use _dos_write to display prompts. Use bdos to call%@NL@%
  26744. %@AB@%         * function 1 to get and echo keystroke.%@NL@%
  26745. %@AB@%         */%@AE@%%@NL@%
  26746.         _dos_write( 1, prompt, sizeof( prompt ) - 1, &ch );%@NL@%
  26747.         ch = bdos( 1, 0, 0 ) & 0x00ff;%@NL@%
  26748.         if( (ch == 'y') || (ch == 'Y') )%@NL@%
  26749.             ret = _dos_creat( target, NORMAL, &htarget );%@NL@%
  26750.         _dos_write( 1, newline, sizeof( newline ) - 1, &ch );%@NL@%
  26751.     }%@NL@%
  26752.     if( ret )%@NL@%
  26753.         return ret;%@NL@%
  26754. %@NL@%
  26755.     %@AB@%/* Read and write until there is nothing left. */%@AE@%%@NL@%
  26756.     while( count )%@NL@%
  26757.     {%@NL@%
  26758.         %@AB@%/* Read and write input. */%@AE@%%@NL@%
  26759.         if( (ret = _dos_read( hsource, buf, count, &count )) )%@NL@%
  26760.             return ret;%@NL@%
  26761.         if( (ret = _dos_write( htarget, buf, count, &count )) )%@NL@%
  26762.             return ret;%@NL@%
  26763.     }%@NL@%
  26764. %@NL@%
  26765.     %@AB@%/* Close files and free memory. */%@AE@%%@NL@%
  26766.     _dos_close( hsource );%@NL@%
  26767.     _dos_close( htarget );%@NL@%
  26768.     _dos_freemem( segbuf );%@NL@%
  26769.     return 0;%@NL@%
  26770. }%@NL@%
  26771. %@NL@%
  26772. %@NL@%
  26773. %@NL@%
  26774. %@2@%%@AH@%CPYSTR.C%@AE@%%@EH@%%@NL@%
  26775. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\CPYSTR.C%@AE@%%@NL@%
  26776. %@NL@%
  26777. %@AB@%/* CPYSTR.C illustrate memory and string copy and move functions including:%@NL@%
  26778. %@AB@% *      memccpy         memcpy          memmove%@NL@%
  26779. %@AB@% *      strncpy         strcpy          strdup          strlen%@NL@%
  26780. %@AB@% */%@AE@%%@NL@%
  26781. %@NL@%
  26782. %@AI@%#include %@AE@%<memory.h> %@NL@%
  26783. %@AI@%#include %@AE@%<string.h> %@NL@%
  26784. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26785. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26786. %@AI@%#include %@AE@%<dos.h> %@NL@%
  26787. %@NL@%
  26788. char string1[60] = "The quick brown dog jumps over the lazy fox";%@NL@%
  26789. char string2[60] = "The quick brown fox jumps over the lazy dog";%@NL@%
  26790. %@AB@%/*                           1         2         3         4         5 *%@NL@%
  26791. %@AB@% *                  12345678901234567890123456789012345678901234567890 */%@AE@%%@NL@%
  26792. main()%@NL@%
  26793. {%@NL@%
  26794.     char buffer[61];%@NL@%
  26795.     char *pdest, *newstring;%@NL@%
  26796.     int  pos;%@NL@%
  26797. %@NL@%
  26798.     printf( "Function:\tmemccpy 60 characters or to character 's'\n" );%@NL@%
  26799.     printf( "Source:\t\t%s\n", string1 );%@NL@%
  26800.     pdest = memccpy( buffer, string1, 's', 60 );%@NL@%
  26801.     *pdest = '\0';%@NL@%
  26802.     printf( "Result:\t\t%s\n", buffer );%@NL@%
  26803.     printf( "Length:\t\t%d characters\n\n", strlen( buffer ) );%@NL@%
  26804. %@NL@%
  26805.     pos = pdest - buffer;%@NL@%
  26806.     printf( "Function:\tstrcpy\n" );%@NL@%
  26807.     printf( "Source:\t\t%s\n", string2 + pos );%@NL@%
  26808.     pdest = strcpy( buffer + pos, string2 + pos );%@NL@%
  26809.     printf( "Result:\t\t%s\n", buffer );%@NL@%
  26810.     printf( "Length:\t\t%d characters\n\n", strlen( buffer ) );%@NL@%
  26811. %@NL@%
  26812.     printf( "Function:\tmemcpy 20 characters\n" );%@NL@%
  26813.     printf( "Source:\t\t%s\n", string2 );%@NL@%
  26814.     memcpy( buffer, string2, 20 );%@NL@%
  26815.     printf( "Result:\t\t%s\n", buffer );%@NL@%
  26816.     printf( "Length:\t\t%d characters\n\n", strlen( buffer ) );%@NL@%
  26817. %@NL@%
  26818.     printf( "Function:\tstrncpy 30 characters\n" );%@NL@%
  26819.     printf( "Source:\t\t%s\n", string1 + 20 );%@NL@%
  26820.     pdest = strncpy( buffer + 20, string1 + 20, 30 );%@NL@%
  26821.     printf( "Result:\t\t%s\n", buffer );%@NL@%
  26822.     printf( "Length:\t\t%d characters\n\n", strlen( buffer ) );%@NL@%
  26823. %@NL@%
  26824.     getch();%@NL@%
  26825. %@NL@%
  26826.     printf( "Function:\tstrdup\n" );%@NL@%
  26827.     printf( "Source:\t\t%s\n", buffer );%@NL@%
  26828.     newstring = strdup( buffer );%@NL@%
  26829.     printf( "Result:\t\t%s\n", newstring );%@NL@%
  26830.     printf( "Length:\t\t%d characters\n\n", strlen( newstring ) );%@NL@%
  26831. %@NL@%
  26832.     %@AB@%/* Illustrate overlapping copy: memmove handles it correctly;%@NL@%
  26833. %@AB@%     * memcpy does not.%@NL@%
  26834. %@AB@%     */%@AE@%%@NL@%
  26835.     printf( "Function:\tmemcpy with overlap\n" );%@NL@%
  26836.     printf( "Source:\t\t%s\n", string1 + 4 );%@NL@%
  26837.     printf( "Destination:\t%s\n", string1 + 10 );%@NL@%
  26838.     memcpy( string1 + 10, string1 + 4, 40 );%@NL@%
  26839.     printf( "Result:\t\t%s\n", string1 );%@NL@%
  26840.     printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );%@NL@%
  26841. %@NL@%
  26842.     printf( "Function:\tmemmove with overlap\n" );%@NL@%
  26843.     printf( "Source:\t\t%s\n", string2 + 4 );%@NL@%
  26844.     printf( "Destination:\t%s\n", string2 + 10 );%@NL@%
  26845.     memmove( string2 + 10, string2 + 4, 40 );%@NL@%
  26846.     printf( "Result:\t\t%s\n", string2 );%@NL@%
  26847.     printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );%@NL@%
  26848. }%@NL@%
  26849. %@NL@%
  26850. %@NL@%
  26851. %@2@%%@AH@%DECRMENT.C%@AE@%%@EH@%%@NL@%
  26852. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\DECRMENT.C%@AE@%%@NL@%
  26853. %@NL@%
  26854. %@AB@%/* DECRMENT.C: Demonstrate prefix and postfix operators.%@NL@%
  26855. %@AB@%*/%@AE@%%@NL@%
  26856. %@NL@%
  26857. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26858. main()%@NL@%
  26859. {%@NL@%
  26860.    int val, sample = 3, proton = 3;%@NL@%
  26861.    val = sample--;%@NL@%
  26862.    printf( "val = %d  sample = %d\n", val, sample );%@NL@%
  26863.    val = --proton;%@NL@%
  26864.    printf( "val = %d  proton = %d\n", val, proton );%@NL@%
  26865. }%@NL@%
  26866. %@NL@%
  26867. %@NL@%
  26868. %@2@%%@AH@%DIRECT.C%@AE@%%@EH@%%@NL@%
  26869. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\DIRECT.C%@AE@%%@NL@%
  26870. %@NL@%
  26871. %@AB@%/* DIRECT.C illustrates directory functions and miscellaneous file%@NL@%
  26872. %@AB@% * and other functions including:%@NL@%
  26873. %@AB@% *      getcwd          mkdir           chdir           rmdir%@NL@%
  26874. %@AB@% *      system          mktemp          remove          unlink%@NL@%
  26875. %@AB@% *      stat%@NL@%
  26876. %@AB@% *%@NL@%
  26877. %@AB@% * See NULLFILE.C for an example of fstat, which is similar to stat.%@NL@%
  26878. %@AB@% */%@AE@%%@NL@%
  26879. %@NL@%
  26880. %@AI@%#include %@AE@%<direct.h> %@NL@%
  26881. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  26882. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26883. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26884. %@AI@%#include %@AE@%<io.h> %@NL@%
  26885. %@AI@%#include %@AE@%<time.h> %@NL@%
  26886. %@AI@%#include %@AE@%<sys/types.h> %@NL@%
  26887. %@AI@%#include %@AE@%<sys/stat.h> %@NL@%
  26888. %@NL@%
  26889. main()%@NL@%
  26890. {%@NL@%
  26891.     char cwd[_MAX_DIR];%@NL@%
  26892.     static char tmpdir[] = "DRXXXXXX";%@NL@%
  26893.     struct stat filestat;%@NL@%
  26894. %@NL@%
  26895.     %@AB@%/* Get the current working directory. */%@AE@%%@NL@%
  26896.     getcwd( cwd, _MAX_DIR );%@NL@%
  26897. %@NL@%
  26898.     %@AB@%/* Try to make temporary name for directory. */%@AE@%%@NL@%
  26899.     if( mktemp( tmpdir ) == NULL )%@NL@%
  26900.     {%@NL@%
  26901.         perror( "Can't make temporary directory" );%@NL@%
  26902.         exit( 1 );%@NL@%
  26903.     }%@NL@%
  26904. %@NL@%
  26905.     %@AB@%/* Try to create a new directory, and if successful, change to it. */%@AE@%%@NL@%
  26906.     if( !mkdir( tmpdir ) )%@NL@%
  26907.     {%@NL@%
  26908.         chdir( tmpdir );%@NL@%
  26909. %@NL@%
  26910.         %@AB@%/* Create and display a file to prove it. */%@AE@%%@NL@%
  26911.         system( "echo This is a test. > TEST.TXT" );%@NL@%
  26912.         system( "type test.txt" );%@NL@%
  26913. %@NL@%
  26914.         %@AB@%/* Display some file statistics. */%@AE@%%@NL@%
  26915.         if( !stat( "TEST.TXT", &filestat ) )%@NL@%
  26916.         {%@NL@%
  26917.             printf( "File: TEST.TXT\n" );%@NL@%
  26918.             printf( "Drive %c:\n", filestat.st_dev + 'A' );%@NL@%
  26919.             printf( "Directory: %s\\%s\n", cwd + 2, tmpdir );%@NL@%
  26920.             printf( "Size: %ld\n", filestat.st_size );%@NL@%
  26921.             printf( "Created: %s", ctime( &filestat.st_atime ) );%@NL@%
  26922.         }%@NL@%
  26923.         getch();%@NL@%
  26924. %@NL@%
  26925.         %@AB@%/* Delete file, go back to original directory, and remove%@NL@%
  26926. %@AB@%         * directory. Note that under DOS, remove is equivalent to unlink,%@NL@%
  26927. %@AB@%         * so this line could be used instead.%@NL@%
  26928. %@AB@%        unlink( "TEST.TXT" );%@NL@%
  26929. %@AB@%         */%@AE@%%@NL@%
  26930.         remove( "TEST.TXT" );%@NL@%
  26931.         chdir( cwd );%@NL@%
  26932.         rmdir( tmpdir );%@NL@%
  26933.     }%@NL@%
  26934. }%@NL@%
  26935. %@NL@%
  26936. %@NL@%
  26937. %@2@%%@AH@%DISK.C%@AE@%%@EH@%%@NL@%
  26938. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\DISK.C%@AE@%%@NL@%
  26939. %@NL@%
  26940. %@AB@%/* DISK.C illustrates low-level disk access using functions:%@NL@%
  26941. %@AB@% *      _bios_disk          _dos_getdiskfree%@NL@%
  26942. %@AB@% */%@AE@%%@NL@%
  26943. %@NL@%
  26944. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  26945. %@AI@%#include %@AE@%<conio.h> %@NL@%
  26946. %@AI@%#include %@AE@%<bios.h> %@NL@%
  26947. %@AI@%#include %@AE@%<dos.h> %@NL@%
  26948. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  26949. %@NL@%
  26950. char far diskbuf[512];%@NL@%
  26951. %@NL@%
  26952. main( int argc, char *argv[] )%@NL@%
  26953. {%@NL@%
  26954.     unsigned status = 0, i;%@NL@%
  26955.     struct diskinfo_t di;%@NL@%
  26956.     struct diskfree_t df;%@NL@%
  26957.     unsigned char far *p, linebuf[17];%@NL@%
  26958. %@NL@%
  26959.     if( argc != 5 )%@NL@%
  26960.     {%@NL@%
  26961.         printf( "  SYNTAX: DISK <driveletter> <head> <track> <sector>" );%@NL@%
  26962.         exit( 1 );%@NL@%
  26963.     }%@NL@%
  26964. %@NL@%
  26965.     if( (di.drive = toupper( argv[1][0] ) - 'A' ) > 1 )%@NL@%
  26966.     {%@NL@%
  26967.         printf( "Must be floppy drive" );%@NL@%
  26968.         exit( 1 );%@NL@%
  26969.     }%@NL@%
  26970.     di.head     = atoi( argv[2] );%@NL@%
  26971.     di.track    = atoi( argv[3] );%@NL@%
  26972.     di.sector   = atoi( argv[4] );%@NL@%
  26973.     di.nsectors = 1;%@NL@%
  26974.     di.buffer   = diskbuf;%@NL@%
  26975. %@NL@%
  26976.     %@AB@%/* Get information about disk size. */%@AE@%%@NL@%
  26977.     if( _dos_getdiskfree( di.drive + 1, &df ) )%@NL@%
  26978.         exit( 1 );%@NL@%
  26979. %@NL@%
  26980.     %@AB@%/* Try reading disk three times before giving up. */%@AE@%%@NL@%
  26981.     for( i = 0; i < 3; i++ )%@NL@%
  26982.     {%@NL@%
  26983.         status = _bios_disk( _DISK_READ, &di ) >> 8;%@NL@%
  26984.         if( !status )%@NL@%
  26985.             break;%@NL@%
  26986.     }%@NL@%
  26987. %@NL@%
  26988.     %@AB@%/* Display one sector. */%@AE@%%@NL@%
  26989.     if( status )%@NL@%
  26990.         printf( "Error: 0x%.2x\n", status );%@NL@%
  26991.     else%@NL@%
  26992.     {%@NL@%
  26993.         for( p = diskbuf, i = 0; p < (diskbuf + df.bytes_per_sector); p++ )%@NL@%
  26994.         {%@NL@%
  26995.             linebuf[i++] = (*p > 32) ? *p : '.';%@NL@%
  26996.             printf( "%.2x ", *p );%@NL@%
  26997.             if( i == 16 )%@NL@%
  26998.             {%@NL@%
  26999.                 linebuf[i] = '\0';%@NL@%
  27000.                 printf( " %16s\n", linebuf );%@NL@%
  27001.                 i = 0;%@NL@%
  27002.             }%@NL@%
  27003.         }%@NL@%
  27004.     }%@NL@%
  27005.     exit( 1 );%@NL@%
  27006. }%@NL@%
  27007. %@NL@%
  27008. %@NL@%
  27009. %@2@%%@AH@%DO.C%@AE@%%@EH@%%@NL@%
  27010. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\DO.C%@AE@%%@NL@%
  27011. %@NL@%
  27012. %@AB@%/* DO.C: Demonstrate do loop. */%@AE@%%@NL@%
  27013. %@NL@%
  27014. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27015. %@NL@%
  27016. main()%@NL@%
  27017. {%@NL@%
  27018.    int test = 10;%@NL@%
  27019.    do%@NL@%
  27020.    {%@NL@%
  27021.       printf( "test = %d\n", test );%@NL@%
  27022.       test -= 2;%@NL@%
  27023.    }  while( test > 0 );%@NL@%
  27024. }%@NL@%
  27025. %@NL@%
  27026. %@NL@%
  27027. %@2@%%@AH@%DOSMEM.C%@AE@%%@EH@%%@NL@%
  27028. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\MEMORY\DOSMEM.C%@AE@%%@NL@%
  27029. %@NL@%
  27030. %@AB@%/* DOSMEM.C illustrates functions:%@NL@%
  27031. %@AB@% *      _dos_allocmem       _dos_setblock       _dos_freemem%@NL@%
  27032. %@AB@% *%@NL@%
  27033. %@AB@% * See COPY2.C for another example of _dos_allocmem and _dos_freemem.%@NL@%
  27034. %@AB@% */%@AE@%%@NL@%
  27035. %@NL@%
  27036. %@AI@%#include %@AE@%<dos.h> %@NL@%
  27037. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27038. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  27039. %@NL@%
  27040. main()%@NL@%
  27041. {%@NL@%
  27042.     char far *buf = NULL, far *p;%@NL@%
  27043.     unsigned segbuf, maxbuf, size = 512;%@NL@%
  27044. %@NL@%
  27045.     %@AB@%/* Allocate 512-byte buffer. Convert the size to paragraphs).%@NL@%
  27046. %@AB@%     * Assign the segment to the buffer. Fill with A's.%@NL@%
  27047. %@AB@%     */%@AE@%%@NL@%
  27048.     if( _dos_allocmem( size >> 4, &segbuf ) )%@NL@%
  27049.         exit( 1 );%@NL@%
  27050.     FP_SEG( buf ) = segbuf;%@NL@%
  27051.     for( p = buf; p < (buf + size); p++ )%@NL@%
  27052.         *p = 'A';%@NL@%
  27053. %@NL@%
  27054.     %@AB@%/* Double the allocation. Fill the second half with B's. */%@AE@%%@NL@%
  27055.     size *= 2;%@NL@%
  27056.     if( _dos_setblock( size >> 4, segbuf, &maxbuf ) )%@NL@%
  27057.         exit( 1 );%@NL@%
  27058.     FP_SEG( buf ) = segbuf;%@NL@%
  27059.     for( p = buf + (size / 2); p < (buf + size); p++ )%@NL@%
  27060.         *p = 'B';%@NL@%
  27061.     *(--p) = '\0';%@NL@%
  27062. %@NL@%
  27063.     printf( "Memory available: %u paragraphs\n", maxbuf );%@NL@%
  27064.     printf( "Buffer at %Fp contains:\n%Fs", (int far *)buf, buf );%@NL@%
  27065. %@NL@%
  27066.     %@AB@%/* Free memory */%@AE@%%@NL@%
  27067.     exit( !_dos_freemem( segbuf ) );%@NL@%
  27068. }%@NL@%
  27069. %@NL@%
  27070. %@NL@%
  27071. %@2@%%@AH@%EGA.C%@AE@%%@EH@%%@NL@%
  27072. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\EGA.C%@AE@%%@NL@%
  27073. %@NL@%
  27074. %@AB@%/* EGA.C: Demonstrates EGA palettes */%@AE@%%@NL@%
  27075. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27076. %@AI@%#include %@AE@%<conio.h> %@NL@%
  27077. %@AI@%#include %@AE@%<graph.h> %@NL@%
  27078. %@NL@%
  27079. main()%@NL@%
  27080. {%@NL@%
  27081.    _setvideomode( _ERESCOLOR );%@NL@%
  27082.    _setcolor( 4 );%@NL@%
  27083.    _rectangle( _GFILLINTERIOR, 50, 50, 200, 200 );%@NL@%
  27084. %@NL@%
  27085.    _settextposition( 1, 1 );%@NL@%
  27086.    printf( "Normal palette\n" );%@NL@%
  27087.    printf( "Press a key" );%@NL@%
  27088.    getch();%@NL@%
  27089. %@NL@%
  27090.    _remappalette( 4, _BLUE );%@NL@%
  27091. %@NL@%
  27092.    _settextposition( 1, 1 );%@NL@%
  27093.    printf( "Remapped palette\n" );%@NL@%
  27094.    printf( "Press a key" );%@NL@%
  27095.    getch();%@NL@%
  27096. %@NL@%
  27097.    _remappalette( 4, _RED );%@NL@%
  27098. %@NL@%
  27099.    _settextposition( 1, 1 );%@NL@%
  27100.    printf( "Restored palette\n" );%@NL@%
  27101.    printf( "Press a key to clear the screen" );%@NL@%
  27102.    getch();%@NL@%
  27103. %@NL@%
  27104.    _clearscreen( _GCLEARSCREEN );%@NL@%
  27105.    _setvideomode( _DEFAULTMODE );%@NL@%
  27106. }%@NL@%
  27107. %@NL@%
  27108. %@NL@%
  27109. %@2@%%@AH@%ELSE.C%@AE@%%@EH@%%@NL@%
  27110. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ELSE.C%@AE@%%@NL@%
  27111. %@NL@%
  27112. %@AB@%/* ELSE.C: Demonstrate else clause. */%@AE@%%@NL@%
  27113. %@NL@%
  27114. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27115. %@AI@%#include %@AE@%<conio.h> %@NL@%
  27116. %@AI@%#define %@AE@% B_KEY  'b' %@NL@%
  27117. %@NL@%
  27118. main()%@NL@%
  27119. {%@NL@%
  27120.    char ch;%@NL@%
  27121.    printf( "Press the b key to hear a bell.\n" );%@NL@%
  27122.    ch = getch();%@NL@%
  27123.    if( ch == B_KEY )%@NL@%
  27124.       printf( "Beep!\a\n" );%@NL@%
  27125.    else%@NL@%
  27126.       printf( "Bye bye.\n" );%@NL@%
  27127. }%@NL@%
  27128. %@NL@%
  27129. %@NL@%
  27130. %@2@%%@AH@%ELSE1.C%@AE@%%@EH@%%@NL@%
  27131. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ELSE1.C%@AE@%%@NL@%
  27132. %@NL@%
  27133. %@AB@%/* ELSE1.C: Demonstrate else-if construct. */%@AE@%%@NL@%
  27134. %@NL@%
  27135. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27136. %@AI@%#include %@AE@%<conio.h> %@NL@%
  27137. %@AI@%#define %@AE@% B_KEY  'b' %@NL@%
  27138. %@AI@%#define %@AE@% ENTER_KEY '\r' %@NL@%
  27139. %@NL@%
  27140. main()%@NL@%
  27141. {%@NL@%
  27142.    char ch;%@NL@%
  27143.    printf( "Press the b key to hear a bell.\n" );%@NL@%
  27144.    ch = getch();%@NL@%
  27145.    if( ch == B_KEY )%@NL@%
  27146.       printf( "Beep!\a\n" );%@NL@%
  27147.    else%@NL@%
  27148.       if( ch == ENTER_KEY )%@NL@%
  27149.          printf( "What a boring selection...\n" );%@NL@%
  27150.    else%@NL@%
  27151.       printf( "Bye bye.\n" );%@NL@%
  27152. }%@NL@%
  27153. %@NL@%
  27154. %@NL@%
  27155. %@2@%%@AH@%EMPLOY1.C%@AE@%%@EH@%%@NL@%
  27156. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\EMPLOY1.C%@AE@%%@NL@%
  27157. %@NL@%
  27158. %@AB@%/* EMPLOY1.C: Demonstrate structure pointers. */%@AE@%%@NL@%
  27159. %@NL@%
  27160. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27161. %@NL@%
  27162. %@NL@%
  27163. struct employee%@NL@%
  27164. {%@NL@%
  27165.    char name[10];%@NL@%
  27166.    int months;%@NL@%
  27167.    float wage;%@NL@%
  27168. };%@NL@%
  27169. %@NL@%
  27170. void display( struct employee *e_ptr  );%@NL@%
  27171. %@NL@%
  27172. main()%@NL@%
  27173. {%@NL@%
  27174.    static struct employee jones =%@NL@%
  27175.       {%@NL@%
  27176.       "Jones, J",%@NL@%
  27177.       77,%@NL@%
  27178.       13.68%@NL@%
  27179.       };%@NL@%
  27180. %@NL@%
  27181.    display( &jones );%@NL@%
  27182. }%@NL@%
  27183. %@NL@%
  27184. void display( struct employee *e_ptr )%@NL@%
  27185. {%@NL@%
  27186.    printf( "Name: %s\n", e_ptr->name );%@NL@%
  27187.    printf( "Months of service: %d\n", e_ptr->months );%@NL@%
  27188.    printf( "Hourly wage: %6.2f\n", e_ptr->wage );%@NL@%
  27189. }%@NL@%
  27190. %@NL@%
  27191. %@NL@%
  27192. %@2@%%@AH@%EMPLOYEE.C%@AE@%%@EH@%%@NL@%
  27193. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\EMPLOYEE.C%@AE@%%@NL@%
  27194. %@NL@%
  27195. %@AB@%/* EMPLOYEE.C: Demonstrate structures. */%@AE@%%@NL@%
  27196. %@NL@%
  27197. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27198. %@AI@%#include %@AE@%<string.h> %@NL@%
  27199. %@NL@%
  27200. struct employee%@NL@%
  27201. {%@NL@%
  27202.    char name[10];%@NL@%
  27203.    int months;%@NL@%
  27204.    float wage;%@NL@%
  27205. };%@NL@%
  27206. %@NL@%
  27207. void display( struct employee show );%@NL@%
  27208. %@NL@%
  27209. main()%@NL@%
  27210. {%@NL@%
  27211.    struct employee jones;%@NL@%
  27212. %@NL@%
  27213.    strcpy( jones.name, "Jones, J" );%@NL@%
  27214.    jones.months = 77;%@NL@%
  27215.    jones.wage = 13.68;%@NL@%
  27216. %@NL@%
  27217.    display( jones );%@NL@%
  27218. }%@NL@%
  27219. %@NL@%
  27220. void display( struct employee show )%@NL@%
  27221. {%@NL@%
  27222.    printf( "Name: %s\n", show.name );%@NL@%
  27223.    printf( "Months of service: %d\n", show.months );%@NL@%
  27224.    printf( "Hourly wage: %6.2f\n", show.wage );%@NL@%
  27225. }%@NL@%
  27226. %@NL@%
  27227. %@NL@%
  27228. %@2@%%@AH@%ENVIRON1.C%@AE@%%@EH@%%@NL@%
  27229. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\ENVIRON1.C%@AE@%%@NL@%
  27230. %@NL@%
  27231. %@AB@%/* ENVIRON.C illustrates environment variable functions including:%@NL@%
  27232. %@AB@% *      getenv          putenv          _searchenv%@NL@%
  27233. %@AB@% */%@AE@%%@NL@%
  27234. %@NL@%
  27235. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  27236. %@AI@%#include %@AE@%<string.h> %@NL@%
  27237. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27238. %@NL@%
  27239. main()%@NL@%
  27240. {%@NL@%
  27241.     char *pathvar, pathbuf[128], filebuf[128];%@NL@%
  27242. %@NL@%
  27243.     %@AB@%/* Get the PATH environment variable and save a copy of it. */%@AE@%%@NL@%
  27244.     pathvar = getenv( "PATH" );%@NL@%
  27245.     strcpy( pathbuf, pathvar );%@NL@%
  27246.     printf( "Old PATH: %s\n", pathvar ? pathvar : "variable not set");%@NL@%
  27247. %@NL@%
  27248.     %@AB@%/* Add a new directory to the path. */%@AE@%%@NL@%
  27249.     strcat( pathbuf, ";\\QC;" );%@NL@%
  27250.     if( putenv( pathbuf ) == -1 )%@NL@%
  27251.     {%@NL@%
  27252.         printf( "Failed\n");%@NL@%
  27253.         return 1;%@NL@%
  27254.     }%@NL@%
  27255.     else%@NL@%
  27256.         printf( "New PATH: %s\n", pathbuf );%@NL@%
  27257. %@NL@%
  27258.     %@AB@%/* Search for file in the new path. */%@AE@%%@NL@%
  27259.     _searchenv( "QC.INI", "PATH", filebuf );%@NL@%
  27260.     if( *filebuf )%@NL@%
  27261.         printf( "QC.INI found at %s\n", filebuf );%@NL@%
  27262.     else%@NL@%
  27263.         printf( "QC.INI not found\n" );%@NL@%
  27264. %@NL@%
  27265.     %@AB@%/* Restore original path. */%@AE@%%@NL@%
  27266.     if( putenv( pathvar ) == -1 )%@NL@%
  27267.         printf( "Failed\n");%@NL@%
  27268.     else%@NL@%
  27269.         printf( "Old PATH: %s\n", pathvar );%@NL@%
  27270.     return 0;%@NL@%
  27271. }%@NL@%
  27272. %@NL@%
  27273. %@NL@%
  27274. %@2@%%@AH@%ERROR.C%@AE@%%@EH@%%@NL@%
  27275. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\ERROR.C%@AE@%%@NL@%
  27276. %@NL@%
  27277. %@AB@%/* ERROR.C illustrates stream file error handling. Functions illustrated%@NL@%
  27278. %@AB@% * include:%@NL@%
  27279. %@AB@% *      ferror          clearerr        exit            _exit%@NL@%
  27280. %@AB@% *      perror          strerror        _strerror%@NL@%
  27281. %@AB@% *%@NL@%
  27282. %@AB@% * The _exit routine is not specifically illustrated, but it is the same%@NL@%
  27283. %@AB@% * as exit except that file buffers are not flushed and atexit and onexit%@NL@%
  27284. %@AB@% * are not called.%@NL@%
  27285. %@AB@% */%@AE@%%@NL@%
  27286. %@NL@%
  27287. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27288. %@AI@%#include %@AE@%<string.h> %@NL@%
  27289. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  27290. %@AI@%#include %@AE@%<errno.h> %@NL@%
  27291. enum BOOL { FALSE, TRUE };%@NL@%
  27292. %@NL@%
  27293. FILE *stream;%@NL@%
  27294. char string[] = "This should never be written";%@NL@%
  27295. void errortest( FILE *stream, char *msg, int fterm );%@NL@%
  27296. %@NL@%
  27297. main( int argc, char *argv[] )%@NL@%
  27298. {%@NL@%
  27299.     %@AB@%/* Open file and test to see if open caused error. If so, terminate. */%@AE@%%@NL@%
  27300.     stream = fopen( argv[1], "r" );%@NL@%
  27301.     errortest( stream, "Can't open file", TRUE );%@NL@%
  27302. %@NL@%
  27303.     %@AB@%/* Try to write to a read-only file, then test to see if write%@NL@%
  27304. %@AB@%     * caused error. If so, clear error, but don't terminate.%@NL@%
  27305. %@AB@%     */%@AE@%%@NL@%
  27306.     fprintf( stream, "%s\n", string );%@NL@%
  27307.     errortest( stream, "Can't write file", FALSE );%@NL@%
  27308.     exit( 0 );%@NL@%
  27309. }%@NL@%
  27310. %@NL@%
  27311. %@AB@%/* Error test routine takes a stream, a message, and a flag telling whether%@NL@%
  27312. %@AB@% * to terminate if there is an error.%@NL@%
  27313. %@AB@% */%@AE@%%@NL@%
  27314. void errortest( FILE *stream, char *msg, int fterm )%@NL@%
  27315. {%@NL@%
  27316.     %@AB@%/* If stream doesn't exist (failed fopen) or if there is an error%@NL@%
  27317. %@AB@%     * on the stream, handle error.%@NL@%
  27318. %@AB@%     */%@AE@%%@NL@%
  27319.     if( (stream == NULL) || (ferror( stream )) )%@NL@%
  27320.     {%@NL@%
  27321.         perror( msg );%@NL@%
  27322.         %@AB@%/* _strerror and strerror can be used to get the same result%@NL@%
  27323. %@AB@%         * as perror, as illustrated by these lines (commented out).%@NL@%
  27324. %@AB@%        printf( "%s: %s\n", msg, strerror( errno ) );%@NL@%
  27325. %@AB@%        printf( _strerror( msg ) );%@NL@%
  27326. %@AB@%         */%@AE@%%@NL@%
  27327. %@NL@%
  27328.         %@AB@%/* Terminate or clear error, depending on terminate flag. */%@AE@%%@NL@%
  27329.         if( fterm )%@NL@%
  27330.             exit( errno );%@NL@%
  27331.         else%@NL@%
  27332.             clearerr( stream );%@NL@%
  27333.     }%@NL@%
  27334. }%@NL@%
  27335. %@NL@%
  27336. %@NL@%
  27337. %@2@%%@AH@%EXEC.C%@AE@%%@EH@%%@NL@%
  27338. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\EXEC.C%@AE@%%@NL@%
  27339. %@NL@%
  27340. %@AB@%/* EXEC.C illustrates the different versions of exec including:%@NL@%
  27341. %@AB@% *      execl           execle          execlp          execlpe%@NL@%
  27342. %@AB@% *      execv           execve          execvp          execvpe%@NL@%
  27343. %@AB@% *%@NL@%
  27344. %@AB@% * Although EXEC.C can exec any program, you can verify how different%@NL@%
  27345. %@AB@% * versions handle arguments and environment by compiling and%@NL@%
  27346. %@AB@% * specifying the sample program ARGS.C. See SPAWN.C for examples%@NL@%
  27347. %@AB@% * of the similar spawn functions.%@NL@%
  27348. %@AB@% */%@AE@%%@NL@%
  27349. %@NL@%
  27350. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27351. %@AI@%#include %@AE@%<conio.h> %@NL@%
  27352. %@AI@%#include %@AE@%<process.h> %@NL@%
  27353. %@NL@%
  27354. char *my_env[] =                %@AB@%/* Environment for exec?e */%@AE@%%@NL@%
  27355. {%@NL@%
  27356.     "THIS=environment will be",%@NL@%
  27357.     "PASSED=to child by the",%@NL@%
  27358.     "SPAWN=functions",%@NL@%
  27359.     NULL%@NL@%
  27360. };%@NL@%
  27361. %@NL@%
  27362. main()%@NL@%
  27363. {%@NL@%
  27364.     char *args[4], prog[80];%@NL@%
  27365.     int ch;%@NL@%
  27366. %@NL@%
  27367.     printf( "Enter name of program to exec: " );%@NL@%
  27368.     gets( prog );%@NL@%
  27369.     printf( " 1. execl   2. execle   3. execlp   4. execlpe\n" );%@NL@%
  27370.     printf( " 5. execv   6. execve   7. execvp   8. execvpe\n" );%@NL@%
  27371.     printf( "Type a number from 1 to 8 (or 0 to quit): " );%@NL@%
  27372.     ch = getche();%@NL@%
  27373.     if( (ch < '1') || (ch > '8') )%@NL@%
  27374.         exit( 1 );%@NL@%
  27375.     printf( "\n\n" );%@NL@%
  27376. %@NL@%
  27377.     %@AB@%/* Arguments for execv? */%@AE@%%@NL@%
  27378.     args[0] = prog;             %@AB@%/* First argument ignored under most */%@AE@%%@NL@%
  27379.     args[1] = "exec??";         %@AB@%/*   recent versions of DOS          */%@AE@%%@NL@%
  27380.     args[2] = "two";%@NL@%
  27381.     args[3] = NULL;%@NL@%
  27382. %@NL@%
  27383.     switch( ch )%@NL@%
  27384.     {%@NL@%
  27385.         case '1':%@NL@%
  27386.             execl( prog, prog, "execl", "two", NULL );%@NL@%
  27387.             break;%@NL@%
  27388.         case '2':%@NL@%
  27389.             execle( prog, prog, "execle", "two", NULL, my_env );%@NL@%
  27390.             break;%@NL@%
  27391.         case '3':%@NL@%
  27392.             execlp( prog, prog, "execlp", "two", NULL );%@NL@%
  27393.             break;%@NL@%
  27394.         case '4':%@NL@%
  27395.             execlpe( prog, prog, "execlpe", "two", NULL, my_env );%@NL@%
  27396.             break;%@NL@%
  27397.         case '5':%@NL@%
  27398.             execv( prog, args );%@NL@%
  27399.             break;%@NL@%
  27400.         case '6':%@NL@%
  27401.             execve( prog, args, my_env );%@NL@%
  27402.             break;%@NL@%
  27403.         case '7':%@NL@%
  27404.             execvp( prog, args );%@NL@%
  27405.             break;%@NL@%
  27406.         case '8':%@NL@%
  27407.             execvpe( prog, args, my_env );%@NL@%
  27408.             break;%@NL@%
  27409.         default:%@NL@%
  27410.             break;%@NL@%
  27411.     }%@NL@%
  27412. %@NL@%
  27413.     %@AB@%/* This point is only reached if exec fails. */%@AE@%%@NL@%
  27414.     printf( "\nProcess was not execed." );%@NL@%
  27415.     exit( 0 );%@NL@%
  27416. }%@NL@%
  27417. %@NL@%
  27418. %@NL@%
  27419. %@2@%%@AH@%EXTDIR.C%@AE@%%@EH@%%@NL@%
  27420. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\EXTDIR.C%@AE@%%@NL@%
  27421. %@NL@%
  27422. %@AB@%/* EXTDIR.C illustrates wild card handling using functions:%@NL@%
  27423. %@AB@% *      _dos_findfirst      _dos_findnext       sprintf%@NL@%
  27424. %@AB@% */%@AE@%%@NL@%
  27425. %@NL@%
  27426. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  27427. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27428. %@AI@%#include %@AE@%<conio.h> %@NL@%
  27429. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  27430. %@AI@%#include %@AE@%<dos.h> %@NL@%
  27431. %@AI@%#include %@AE@%<io.h> %@NL@%
  27432. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  27433. %@AI@%#include %@AE@%<sys\utime.h> %@NL@%
  27434. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  27435. %@NL@%
  27436. long fileinfo( struct find_t *find );       %@AB@%/* Prototypes */%@AE@%%@NL@%
  27437. char *timestr( unsigned d, char *buf );%@NL@%
  27438. char *datestr( unsigned d, char *buf );%@NL@%
  27439. %@NL@%
  27440. main( int argc, char *argv[] )%@NL@%
  27441. {%@NL@%
  27442.     struct find_t find;%@NL@%
  27443.     long size;%@NL@%
  27444. %@NL@%
  27445.     %@AB@%/* Find first matching file, then find additional matches. */%@AE@%%@NL@%
  27446.     if( !_dos_findfirst( argv[1], 0xffff, &find ) )%@NL@%
  27447.     {%@NL@%
  27448.         printf( "%-12s   %-8s    %-8s   %-8s   %-9s   %s %s %s %s\n",%@NL@%
  27449.                 "FILE", "SIZE", "TIME", "DATE", "KIND",%@NL@%
  27450.                 "RDO", "HID", "SYS", "ARC" );%@NL@%
  27451.         size = fileinfo( &find );%@NL@%
  27452.     }%@NL@%
  27453.     else%@NL@%
  27454.     {%@NL@%
  27455.         printf( "  SYNTAX: TOUCH <wildfilespec>" );%@NL@%
  27456.         exit( 1 );%@NL@%
  27457.     }%@NL@%
  27458.     while( !_dos_findnext( &find ) )%@NL@%
  27459.         size += fileinfo( &find );%@NL@%
  27460.     printf( "%-12s   %8ld\n\n", "Total", size );%@NL@%
  27461.     exit( 0 );%@NL@%
  27462. }%@NL@%
  27463. %@NL@%
  27464. long fileinfo( struct find_t *pfind )%@NL@%
  27465. {%@NL@%
  27466.     char timebuf[10], datebuf[10], *pkind;%@NL@%
  27467. %@NL@%
  27468.     datestr( pfind->wr_date, datebuf );%@NL@%
  27469.     timestr( pfind->wr_time, timebuf );%@NL@%
  27470. %@NL@%
  27471.     if( pfind->attrib & _A_SUBDIR )%@NL@%
  27472.         pkind = "Directory";%@NL@%
  27473.     else if( pfind->attrib & _A_VOLID )%@NL@%
  27474.         pkind = "Label";%@NL@%
  27475.     else%@NL@%
  27476.         pkind = "File";%@NL@%
  27477. %@NL@%
  27478.     printf( "%-12s   %8ld    %8s   %8s   %-9s    %c   %c   %c   %c\n",%@NL@%
  27479.             pfind->name, pfind->size, timebuf, datebuf, pkind,%@NL@%
  27480.             (pfind->attrib & _A_RDONLY) ? 'Y' : 'N',%@NL@%
  27481.             (pfind->attrib & _A_HIDDEN) ? 'Y' : 'N',%@NL@%
  27482.             (pfind->attrib & _A_SYSTEM) ? 'Y' : 'N',%@NL@%
  27483.             (pfind->attrib & _A_ARCH)   ? 'Y' : 'N' );%@NL@%
  27484.     return pfind->size;%@NL@%
  27485. }%@NL@%
  27486. %@NL@%
  27487. %@AB@%/* Take unsigned time in the format:                fedcba9876543210%@NL@%
  27488. %@AB@% * s=2 sec incr, m=0-59, h=23                       hhhhhmmmmmmsssss%@NL@%
  27489. %@AB@% * Change to a 9-byte string (ignore seconds):      hh:mm ?m%@NL@%
  27490. %@AB@% */%@AE@%%@NL@%
  27491. char *timestr( unsigned t, char *buf )%@NL@%
  27492. {%@NL@%
  27493.     int h = (t >> 11) & 0x1f, m = (t >> 5) & 0x3f;%@NL@%
  27494. %@NL@%
  27495.     sprintf( buf, "%2.2d:%02.2d %cm", h % 12, m,  h > 11 ? 'p' : 'a' );%@NL@%
  27496.     return buf;%@NL@%
  27497. }%@NL@%
  27498. %@NL@%
  27499. %@AB@%/* Take unsigned date in the format:    fedcba9876543210%@NL@%
  27500. %@AB@% * d=1-31, m=1-12, y=0-119 (1980-2099)  yyyyyyymmmmddddd%@NL@%
  27501. %@AB@% * Change to a 9-byte string:           mm/dd/yy%@NL@%
  27502. %@AB@% */%@AE@%%@NL@%
  27503. char *datestr( unsigned d, char *buf )%@NL@%
  27504. {%@NL@%
  27505.     sprintf( buf, "%2.2d/%02.2d/%02.2d",%@NL@%
  27506.              (d >> 5) & 0x0f, d & 0x1f, (d >> 9) + 80 );%@NL@%
  27507.     return buf;%@NL@%
  27508. }%@NL@%
  27509. %@NL@%
  27510. %@NL@%
  27511. %@2@%%@AH@%EXTERR.C%@AE@%%@EH@%%@NL@%
  27512. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\EXTERR.C%@AE@%%@NL@%
  27513. %@NL@%
  27514. %@AB@%/* EXTERR.C illustrates function:%@NL@%
  27515. %@AB@% *      dosexterr%@NL@%
  27516. %@AB@% */%@AE@%%@NL@%
  27517. %@NL@%
  27518. %@AI@%#include %@AE@%<dos.h> %@NL@%
  27519. %@AI@%#include %@AE@%<fcntl.h> %@NL@%
  27520. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27521. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  27522. %@NL@%
  27523. void errorinfo( void );     %@AB@%/* Prototype */%@AE@%%@NL@%
  27524. %@NL@%
  27525. main( int argc, char *argv[] )%@NL@%
  27526. {%@NL@%
  27527.     int hsource;%@NL@%
  27528. %@NL@%
  27529.     %@AB@%/* Open to get file handle and test for errors. Try specifying%@NL@%
  27530. %@AB@%     * invalid files to show different errors.%@NL@%
  27531. %@AB@%     */%@AE@%%@NL@%
  27532.     if( _dos_open( argv[1], O_RDWR, &hsource ) )%@NL@%
  27533.         errorinfo();%@NL@%
  27534.     printf( "No error\n" );%@NL@%
  27535.     _dos_close( hsource );%@NL@%
  27536.     exit( 0 );%@NL@%
  27537. }%@NL@%
  27538. %@NL@%
  27539. void errorinfo()%@NL@%
  27540. {%@NL@%
  27541.     struct DOSERROR err;%@NL@%
  27542.     static char *eclass[] =%@NL@%
  27543.     {%@NL@%
  27544.         "", "Out of Resource", "Temporary Situation", "Authorization",%@NL@%
  27545.         "Internal", "Hardware Failure", "System Failure", "Application Error",%@NL@%
  27546.         "Not Found", "Bad Format", "Locked", "Media", "Already Exists",%@NL@%
  27547.         "Unknown"%@NL@%
  27548.     };%@NL@%
  27549.     static char *eaction[] =%@NL@%
  27550.     {%@NL@%
  27551.         "", "Retry", "Delay Retry", "User", "Abort", "Immediate Exit",%@NL@%
  27552.         "Ignore", "Retry After User Intervention"%@NL@%
  27553.     };%@NL@%
  27554.     static char *elocus[] =%@NL@%
  27555.     {%@NL@%
  27556.         "", "Unknown", "Block Device", "Net", "Serial Device", "Memory"%@NL@%
  27557.     };%@NL@%
  27558. %@NL@%
  27559. %@NL@%
  27560.     %@AB@%/* Get error information and display class, action, and locus. */%@AE@%%@NL@%
  27561.     dosexterr( &err );%@NL@%
  27562.     printf( "Class:\t%s\nAction:\t%s\nLocus:\t%s\nAction\t",%@NL@%
  27563.             eclass[err.class], eaction[err.action], elocus[err.locus] );%@NL@%
  27564. %@NL@%
  27565.     %@AB@%/* Errors that could be caused by sample _dos_open. You can expand%@NL@%
  27566. %@AB@%     * this list to handle others.%@NL@%
  27567. %@AB@%     */%@AE@%%@NL@%
  27568.     switch( err.exterror )%@NL@%
  27569.     {%@NL@%
  27570.         case 2:%@NL@%
  27571.             printf( "File not found\n" );%@NL@%
  27572.             break;%@NL@%
  27573.         case 3:%@NL@%
  27574.             printf( "Path not found\n" );%@NL@%
  27575.             break;%@NL@%
  27576.         case 5:%@NL@%
  27577.             printf( "Access denied\n" );%@NL@%
  27578.             break;%@NL@%
  27579.     }%@NL@%
  27580.     exit( err.exterror );%@NL@%
  27581. }%@NL@%
  27582. %@NL@%
  27583. %@NL@%
  27584. %@2@%%@AH@%FACTOR.C%@AE@%%@EH@%%@NL@%
  27585. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FACTOR.C%@AE@%%@NL@%
  27586. %@NL@%
  27587. %@AB@%/* FACTOR.C: Demonstrate functions.%@NL@%
  27588. %@AB@% *  NOTE:  a value > 16 will overflow a long integer.%@NL@%
  27589. %@AB@% */%@AE@%%@NL@%
  27590. %@NL@%
  27591. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27592. %@NL@%
  27593. long factor( int param );%@NL@%
  27594. %@NL@%
  27595. main()%@NL@%
  27596. {%@NL@%
  27597.    int num;%@NL@%
  27598.    long result;%@NL@%
  27599.    %@AB@%/* Display a prompt */%@AE@%%@NL@%
  27600.    printf( "Type a number: " );%@NL@%
  27601.    %@AB@%/* Input a numeric value, assign to num */%@AE@%%@NL@%
  27602.    scanf( "%d", &num );%@NL@%
  27603.    result = factor( num );%@NL@%
  27604.    printf( "Factorial of %d is %ld\n", num, result );%@NL@%
  27605. }%@NL@%
  27606. %@NL@%
  27607. long factor( int param )%@NL@%
  27608. {%@NL@%
  27609.    long value = 1;%@NL@%
  27610.    while( param > 1 )%@NL@%
  27611.    {%@NL@%
  27612.       value = value * param;%@NL@%
  27613.       param = param - 1;%@NL@%
  27614.    }%@NL@%
  27615.    return value;%@NL@%
  27616. }%@NL@%
  27617. %@NL@%
  27618. %@NL@%
  27619. %@2@%%@AH@%FCVT.C%@AE@%%@EH@%%@NL@%
  27620. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FCVT.C%@AE@%%@NL@%
  27621. %@NL@%
  27622. %@AB@%/* FCVT.C illustrates floating point to string conversion functions:%@NL@%
  27623. %@AB@% *      gcvt            ecvt            fcvt%@NL@%
  27624. %@AB@% *%@NL@%
  27625. %@AB@% * See MKFPSTR.C for an example of using the data returned by fcvt%@NL@%
  27626. %@AB@% * to build a formatted string. See ATONUM.C for an example of using%@NL@%
  27627. %@AB@% * the string returned by gcvt.%@NL@%
  27628. %@AB@% */%@AE@%%@NL@%
  27629. %@NL@%
  27630. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  27631. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27632. %@AI@%#include %@AE@%<string.h> %@NL@%
  27633. %@NL@%
  27634. main()%@NL@%
  27635. {%@NL@%
  27636.     int decimal, sign;%@NL@%
  27637.     char *pnumstr;%@NL@%
  27638.     int precision = 7;%@NL@%
  27639.     char numbuf[50];%@NL@%
  27640.     double number1, number2;%@NL@%
  27641. %@NL@%
  27642.     printf( "Enter two floating point numbers: " );%@NL@%
  27643.     scanf( "%lf %lf", &number1, &number2 );%@NL@%
  27644. %@NL@%
  27645.     %@AB@%/* With gcvt, precision specifies total number of digits.%@NL@%
  27646. %@AB@%     * The decimal place and sign are inserted in the string.%@NL@%
  27647. %@AB@%     */%@AE@%%@NL@%
  27648.     gcvt( number1 + number2, precision, numbuf );%@NL@%
  27649.     printf( "\nString produced by gcvt: %s\n", numbuf );%@NL@%
  27650.     printf( "Total digits: %d\n", precision );%@NL@%
  27651. %@NL@%
  27652.     %@AB@%/* With ecvt, precision specifies total number of digits.%@NL@%
  27653. %@AB@%     * The decimal place and sign are provided for use in formatting.%@NL@%
  27654. %@AB@%     */%@AE@%%@NL@%
  27655.     pnumstr = ecvt( number1 + number2, precision, &decimal, &sign );%@NL@%
  27656.     printf( "\nString produced by ecvt: %s\nSign: %s\n",%@NL@%
  27657.              pnumstr, sign ? "-" : "+" );%@NL@%
  27658.     printf( "Digits left of decimal: %d\nTotal digits: %d\n",%@NL@%
  27659.              decimal, precision );%@NL@%
  27660. %@NL@%
  27661.     %@AB@%/* With fcvt, precision specifies digits after decimal place.%@NL@%
  27662. %@AB@%     * The decimal place and sign are provided for use in formatting.%@NL@%
  27663. %@AB@%     */%@AE@%%@NL@%
  27664.     pnumstr = fcvt( number1 + number2, precision, &decimal, &sign );%@NL@%
  27665.     printf( "\nString produced by fcvt: %s\nSign: %s\n",%@NL@%
  27666.              pnumstr, sign ? "-" : "+"  );%@NL@%
  27667.     printf( "Digits left of decimal: %d\nDigits after decimal: %d\n",%@NL@%
  27668.              decimal, precision );%@NL@%
  27669. }%@NL@%
  27670. %@NL@%
  27671. %@NL@%
  27672. %@2@%%@AH@%FIGURE.C%@AE@%%@EH@%%@NL@%
  27673. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\FIGURE.C%@AE@%%@NL@%
  27674. %@NL@%
  27675. %@AB@%/* FIGURE.C illustrates graphics drawing functions including:%@NL@%
  27676. %@AB@% *      _setpixel   _lineto     _moveto     _rectangle      _ellipse%@NL@%
  27677. %@AB@% *      _arc        _pie%@NL@%
  27678. %@AB@% *%@NL@%
  27679. %@AB@% * Window versions of graphics drawing functions (such as _rectangle_w,%@NL@%
  27680. %@AB@% * _ellipse_wxy, and _lineto_w) are illustrated in WINDOW.C and GEDIT.C.%@NL@%
  27681. %@AB@% */%@AE@%%@NL@%
  27682. %@NL@%
  27683. %@AI@%#include %@AE@%<conio.h> %@NL@%
  27684. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  27685. %@AI@%#include %@AE@%<graph.h> %@NL@%
  27686. %@NL@%
  27687. main()%@NL@%
  27688. {%@NL@%
  27689.     short x, y;%@NL@%
  27690.     short mode = _VRES16COLOR;%@NL@%
  27691. %@NL@%
  27692.     while( !_setvideomode( mode ) )     %@AB@%/* Find best graphics mode   */%@AE@%%@NL@%
  27693.         mode--;%@NL@%
  27694.     if( mode == _TEXTMONO )%@NL@%
  27695.         exit( 1 );                      %@AB@%/* No graphics available     */%@AE@%%@NL@%
  27696. %@NL@%
  27697.     for( x = 10, y = 50; y < 90; x += 2, y += 3 )%@AB@%/* Draw pixels      */%@AE@%%@NL@%
  27698.         _setpixel( x, y );%@NL@%
  27699.     getch();%@NL@%
  27700. %@NL@%
  27701.     for( x = 60, y = 50; y < 90; y += 3 )        %@AB@%/* Draw lines       */%@AE@%%@NL@%
  27702.     {%@NL@%
  27703.         _moveto( x, y );%@NL@%
  27704.         _lineto( x + 20, y );%@NL@%
  27705.     }%@NL@%
  27706.     getch();%@NL@%
  27707. %@NL@%
  27708.     x = 110; y = 70;                             %@AB@%/* Draw rectangles  */%@AE@%%@NL@%
  27709.     _rectangle( _GBORDER,       x - 20, y - 20, x, y );%@NL@%
  27710.     _rectangle( _GFILLINTERIOR, x + 20, y + 20, x, y );%@NL@%
  27711.     getch();%@NL@%
  27712. %@NL@%
  27713.     x = 160;                                     %@AB@%/* Draw ellipses    */%@AE@%%@NL@%
  27714.     _ellipse( _GBORDER,       x - 20, y - 20, x, y );%@NL@%
  27715.     _ellipse( _GFILLINTERIOR, x + 20, y + 20, x, y );%@NL@%
  27716.     getch();%@NL@%
  27717. %@NL@%
  27718.     x = 210;                                     %@AB@%/* Draw arcs        */%@AE@%%@NL@%
  27719.     _arc( x - 20, y - 20, x, y, x, y - 10, x - 10, y );%@NL@%
  27720.     _arc( x + 20, y + 20, x, y, x + 10, y + 20, x + 20, y + 10 );%@NL@%
  27721.     getch();%@NL@%
  27722. %@NL@%
  27723.     x = 260;                                    %@AB@%/* Draw pies        */%@AE@%%@NL@%
  27724.     _pie( _GBORDER,       x - 20, y - 20, x, y, x, y - 10, x - 10, y );%@NL@%
  27725.     _pie( _GFILLINTERIOR, x + 20, y + 20, x, y,%@NL@%
  27726.                           x + 10, y + 20, x + 20, y + 10 );%@NL@%
  27727.     getch();%@NL@%
  27728. %@NL@%
  27729.     exit( !_setvideomode( _DEFAULTMODE ) );%@NL@%
  27730. }%@NL@%
  27731. %@NL@%
  27732. %@NL@%
  27733. %@2@%%@AH@%FILEONE.C%@AE@%%@EH@%%@NL@%
  27734. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FILEONE.C%@AE@%%@NL@%
  27735. %@NL@%
  27736. %@AB@%/* FILEONE.C: Visibility in multiple source files.%@NL@%
  27737. %@AB@%*/%@AE@%%@NL@%
  27738. %@NL@%
  27739. int chico = 20, harpo = 30;%@NL@%
  27740. extern void yonder( void );%@NL@%
  27741. %@NL@%
  27742. main()%@NL@%
  27743. {%@NL@%
  27744.    yonder();%@NL@%
  27745. }%@NL@%
  27746. %@NL@%
  27747. %@NL@%
  27748. %@2@%%@AH@%FILETWO.C%@AE@%%@EH@%%@NL@%
  27749. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FILETWO.C%@AE@%%@NL@%
  27750. %@NL@%
  27751. %@AB@%/* FILETWO.C: Visibility in multiple source files.%@NL@%
  27752. %@AB@%*/%@AE@%%@NL@%
  27753. %@NL@%
  27754. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27755. %@NL@%
  27756. void yonder( void )%@NL@%
  27757. {%@NL@%
  27758.    extern int chico, harpo;%@NL@%
  27759.    printf( "chico = %d, harpo = %d\n", chico, harpo );%@NL@%
  27760. }%@NL@%
  27761. %@NL@%
  27762. %@NL@%
  27763. %@2@%%@AH@%FILL.C%@AE@%%@EH@%%@NL@%
  27764. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\FILL.C%@AE@%%@NL@%
  27765. %@NL@%
  27766. %@AB@%/* FILL.C illustrates color, filling, and linestyle functions including:%@NL@%
  27767. %@AB@% *    _setlinestyle      _setfillmask       _setcolor%@NL@%
  27768. %@AB@% *    _getlinestyle      _floodfill%@NL@%
  27769. %@AB@% *%@NL@%
  27770. %@AB@% * The _getfillmask function is not shown, but its use is similar to%@NL@%
  27771. %@AB@% * _getlinestyle.%@NL@%
  27772. %@AB@% */%@AE@%%@NL@%
  27773. %@NL@%
  27774. %@AI@%#include %@AE@%<conio.h> %@NL@%
  27775. %@AI@%#include %@AE@%<graph.h> %@NL@%
  27776. %@AI@%#include %@AE@%<time.h> %@NL@%
  27777. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  27778. %@NL@%
  27779. main()%@NL@%
  27780. {%@NL@%
  27781.     short x, y, xinc, yinc, xwid, ywid;%@NL@%
  27782.     unsigned char fill[8];%@NL@%
  27783.     struct videoconfig vc;%@NL@%
  27784.     unsigned seed = (unsigned)time( 0L );   %@AB@%/* Different seed each time */%@AE@%%@NL@%
  27785.     short i, color, mode = _VRES16COLOR;%@NL@%
  27786. %@NL@%
  27787.     while( !_setvideomode( mode ) )         %@AB@%/* Find best graphics mode  */%@AE@%%@NL@%
  27788.         mode--;%@NL@%
  27789.     if (mode == _TEXTMONO )%@NL@%
  27790.         exit( 1 );                          %@AB@%/* No graphics available    */%@AE@%%@NL@%
  27791.     _getvideoconfig( &vc );%@NL@%
  27792. %@NL@%
  27793.     xinc = vc.numxpixels / 8;               %@AB@%/* Size variables to mode   */%@AE@%%@NL@%
  27794.     yinc = vc.numxpixels / 8;%@NL@%
  27795.     xwid = (xinc / 2) - 4;%@NL@%
  27796.     ywid = (yinc / 2) - 4;%@NL@%
  27797. %@NL@%
  27798.     %@AB@%/* Draw circles and lines with different patterns. */%@AE@%%@NL@%
  27799.     for( x = xinc; x <= (vc.numxpixels - xinc); x += xinc )%@NL@%
  27800.     {%@NL@%
  27801.         for( y = yinc; y <= (vc.numypixels - yinc); y += yinc )%@NL@%
  27802.         {%@NL@%
  27803.             %@AB@%/* Vary random seed, randomize fill and color. */%@AE@%%@NL@%
  27804.             srand( seed = (seed + 431) * 5 );%@NL@%
  27805.             for( i = 0; i < 8; i++ )%@NL@%
  27806.                 fill[i] = rand();%@NL@%
  27807.             _setfillmask( fill );%@NL@%
  27808.             color = (rand() % vc.numcolors) + 1;%@NL@%
  27809.             _setcolor( color );%@NL@%
  27810. %@NL@%
  27811.             %@AB@%/* Draw ellipse and fill with random color. */%@AE@%%@NL@%
  27812.             _ellipse( _GBORDER, x - xwid, y - ywid, x + xwid, y + ywid );%@NL@%
  27813.             _setcolor( (rand() % vc.numcolors) + 1 );%@NL@%
  27814.             _floodfill( x, y, color );%@NL@%
  27815. %@NL@%
  27816.             %@AB@%/* Draw vertical and horizontal lines. Vertical line style%@NL@%
  27817. %@AB@%             * is opposite of (not) horizontal style. Since lines are%@NL@%
  27818. %@AB@%             * overdrawn with several linestyles, this has the effect of%@NL@%
  27819. %@AB@%             * combining colors and styles.%@NL@%
  27820. %@AB@%             */%@AE@%%@NL@%
  27821.             _setlinestyle( rand() );%@NL@%
  27822.             _moveto( 0, y + ywid + 4 );%@NL@%
  27823.             _lineto( vc.numxpixels - 1, y + ywid + 4 );%@NL@%
  27824.             _setlinestyle( ~_getlinestyle() );%@NL@%
  27825.             _moveto( x + xwid + 4, 0 );%@NL@%
  27826.             _lineto( x + xwid + 4, vc.numypixels - 1 );%@NL@%
  27827.         }%@NL@%
  27828.     }%@NL@%
  27829.     getch();%@NL@%
  27830.     exit( !_setvideomode( _DEFAULTMODE ) );%@NL@%
  27831. }%@NL@%
  27832. %@NL@%
  27833. %@NL@%
  27834. %@2@%%@AH@%FINDSTR.C%@AE@%%@EH@%%@NL@%
  27835. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FINDSTR.C%@AE@%%@NL@%
  27836. %@NL@%
  27837. %@AB@%/* FINDSTR.C illustrates memory and string search functions including:%@NL@%
  27838. %@AB@% *       memchr        strchr        strrchr            strstr%@NL@%
  27839. %@AB@% */%@AE@%%@NL@%
  27840. %@NL@%
  27841. %@AI@%#include %@AE@%<memory.h> %@NL@%
  27842. %@AI@%#include %@AE@%<string.h> %@NL@%
  27843. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27844. %@NL@%
  27845. int  ch = 'r';%@NL@%
  27846. char str[] =    "lazy";%@NL@%
  27847. char string[] = "The quick brown dog jumps over the lazy fox";%@NL@%
  27848. char fmt1[] =   "         1         2         3         4         5";%@NL@%
  27849. char fmt2[] =   "12345678901234567890123456789012345678901234567890";%@NL@%
  27850. %@NL@%
  27851. main()%@NL@%
  27852. {%@NL@%
  27853.     char *pdest;%@NL@%
  27854.     int result;%@NL@%
  27855. %@NL@%
  27856.     printf( "String to be searched:\n\t\t%s\n", string );%@NL@%
  27857.     printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );%@NL@%
  27858. %@NL@%
  27859.     printf( "Function:\tmemchr\n" );%@NL@%
  27860.     printf( "Search char:\t%c\n", ch );%@NL@%
  27861.     pdest = memchr( string, ch, sizeof( string ) );%@NL@%
  27862.     result = pdest - string + 1;%@NL@%
  27863.     if( pdest != NULL )%@NL@%
  27864.         printf( "Result:\t\t%c found at position %d\n\n", ch, result );%@NL@%
  27865.     else%@NL@%
  27866.         printf( "Result:\t\t%c not found\n" );%@NL@%
  27867. %@NL@%
  27868.     printf( "Function:\tstrchr\n" );%@NL@%
  27869.     printf( "Search char:\t%c\n", ch );%@NL@%
  27870.     pdest = strchr( string, ch );%@NL@%
  27871.     result = pdest - string + 1;%@NL@%
  27872.     if( pdest != NULL )%@NL@%
  27873.         printf( "Result:\t\t%c found at position %d\n\n", ch, result );%@NL@%
  27874.     else%@NL@%
  27875.         printf( "Result:\t\t%c not found\n" );%@NL@%
  27876. %@NL@%
  27877.     printf( "Function:\tstrrchr\n" );%@NL@%
  27878.     printf( "Search char:\t%c\n", ch );%@NL@%
  27879.     pdest = strrchr( string, ch );%@NL@%
  27880.     result = pdest - string + 1;%@NL@%
  27881.     if( pdest != NULL )%@NL@%
  27882.         printf( "Result:\t\t%c found at position %d\n\n", ch, result );%@NL@%
  27883.     else%@NL@%
  27884.         printf( "Result:\t\t%c not found\n" );%@NL@%
  27885. %@NL@%
  27886.     printf( "Function:\tstrstr\n" );%@NL@%
  27887.     printf( "Search string:\t%s\n", str );%@NL@%
  27888.     pdest = strstr( string, str );%@NL@%
  27889.     result = pdest - string + 1;%@NL@%
  27890.     if( pdest != NULL )%@NL@%
  27891.         printf( "Result:\t\t%s found at position %d\n\n", str, result );%@NL@%
  27892.     else%@NL@%
  27893.         printf( "Result:\t\t%c not found\n" );%@NL@%
  27894. }%@NL@%
  27895. %@NL@%
  27896. %@NL@%
  27897. %@2@%%@AH@%FOR1.C%@AE@%%@EH@%%@NL@%
  27898. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FOR1.C%@AE@%%@NL@%
  27899. %@NL@%
  27900. %@AB@%/* FOR1.C: Demonstrate multiple expressions. */%@AE@%%@NL@%
  27901. %@NL@%
  27902. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27903. %@NL@%
  27904. main()%@NL@%
  27905. {%@NL@%
  27906.    int a, b;%@NL@%
  27907.    for( a = 256, b = 1; b < 512; a /= 2, b *= 2 )%@NL@%
  27908.       printf( "a = %d  b = %d\n", a, b );%@NL@%
  27909. }%@NL@%
  27910. %@NL@%
  27911. %@NL@%
  27912. %@2@%%@AH@%FOR2.C%@AE@%%@EH@%%@NL@%
  27913. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FOR2.C%@AE@%%@NL@%
  27914. %@NL@%
  27915. %@AB@%/* FOR2.C: Demonstrate variations on for loop. */%@AE@%%@NL@%
  27916. %@NL@%
  27917. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27918. %@AI@%#include %@AE@%<conio.h> %@NL@%
  27919. %@NL@%
  27920. main()%@NL@%
  27921. {%@NL@%
  27922.    for( printf( "Type something\n" ); getche() != '\r'; )%@NL@%
  27923.       ; %@AB@%/* Null statement */%@AE@%%@NL@%
  27924. }%@NL@%
  27925. %@NL@%
  27926. %@NL@%
  27927. %@2@%%@AH@%FOR3.C%@AE@%%@EH@%%@NL@%
  27928. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FOR3.C%@AE@%%@NL@%
  27929. %@NL@%
  27930. %@AB@%/* FOR3.C: Demonstrate similarity of for and while.%@NL@%
  27931. %@AB@%*/%@AE@%%@NL@%
  27932. %@NL@%
  27933. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27934. %@NL@%
  27935. main()%@NL@%
  27936. {%@NL@%
  27937.    int count;%@NL@%
  27938. %@NL@%
  27939.    for( count = 0; count < 10; count++ )%@NL@%
  27940.       printf( "count = %d\n", count );%@NL@%
  27941. %@NL@%
  27942.    count = 0;%@NL@%
  27943.    while( count < 10 )%@NL@%
  27944.    {%@NL@%
  27945.       printf( "count = %d\n", count );%@NL@%
  27946.       count++;%@NL@%
  27947.    }%@NL@%
  27948. %@NL@%
  27949. }%@NL@%
  27950. %@NL@%
  27951. %@NL@%
  27952. %@2@%%@AH@%FOR4.C%@AE@%%@EH@%%@NL@%
  27953. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FOR4.C%@AE@%%@NL@%
  27954. %@NL@%
  27955. %@AB@%/* FOR.C: Demonstrate for loop. */%@AE@%%@NL@%
  27956. %@NL@%
  27957. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27958. %@NL@%
  27959. main()%@NL@%
  27960. {%@NL@%
  27961.    int test;%@NL@%
  27962.    for( test = 10; test > 0; test -= 2 )%@NL@%
  27963.       printf( "test = %d\n", test );%@NL@%
  27964. }%@NL@%
  27965. %@NL@%
  27966. %@NL@%
  27967. %@2@%%@AH@%FREECT.C%@AE@%%@EH@%%@NL@%
  27968. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FREECT.C%@AE@%%@NL@%
  27969. %@NL@%
  27970. %@AB@%/* FREECT.C illustrates the following heap functions:%@NL@%
  27971. %@AB@% *      _freect         _memavl%@NL@%
  27972. %@AB@% */%@AE@%%@NL@%
  27973. %@NL@%
  27974. %@AI@%#include %@AE@%<malloc.h> %@NL@%
  27975. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  27976. %@NL@%
  27977. main()%@NL@%
  27978. {%@NL@%
  27979.     char near *bufs[64];%@NL@%
  27980.     unsigned request, avail, i;%@NL@%
  27981. %@NL@%
  27982.     printf( "Near heap bytes free: %u\n\n", _memavl() );%@NL@%
  27983.     printf( "How many 1K buffers do you want from the near heap? " );%@NL@%
  27984.     scanf( "%d", &request );%@NL@%
  27985.     if( request > 64 )%@NL@%
  27986.     {%@NL@%
  27987.         printf( "There are only 64K in a segment.\n" );%@NL@%
  27988.         request = 64;%@NL@%
  27989.     }%@NL@%
  27990. %@NL@%
  27991.     avail = _freect( 1024 );%@NL@%
  27992.     request = (avail > request) ? request : avail;%@NL@%
  27993.     printf( "You can have %d buffers\n", request );%@NL@%
  27994. %@NL@%
  27995.     printf( "They are available at:\n");%@NL@%
  27996.     for( i = 0; i < request; i++ )%@NL@%
  27997.     {%@NL@%
  27998.         bufs[i] = (char near *)_nmalloc( 1024 );%@NL@%
  27999.         printf( "%2d %Fp   ", i + 1, (char far *)bufs[i] );%@NL@%
  28000.         if( (i % 5) == 4 )%@NL@%
  28001.             printf( "\n" );%@NL@%
  28002.     }%@NL@%
  28003.     printf( "\n\nNear heap bytes free: %u\n\n", _memavl() );%@NL@%
  28004.     printf( "Freeing buffers . . ." );%@NL@%
  28005.     for( i = request; i; i-- )%@NL@%
  28006.         _nfree( bufs[i] );%@NL@%
  28007.     printf( "\n\nNear heap bytes free: %u", _memavl() );%@NL@%
  28008. }%@NL@%
  28009. %@NL@%
  28010. %@NL@%
  28011. %@2@%%@AH@%FSAMPLER.C%@AE@%%@EH@%%@NL@%
  28012. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\FSAMPLER.C%@AE@%%@NL@%
  28013. %@NL@%
  28014. %@AB@%/* SAMPLER.C: Display sample text in various fonts.%@NL@%
  28015. %@AB@%*/%@AE@%%@NL@%
  28016. %@NL@%
  28017. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28018. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28019. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  28020. %@AI@%#include %@AE@%<graph.h> %@NL@%
  28021. %@AI@%#include %@AE@%<string.h> %@NL@%
  28022. %@NL@%
  28023. %@AI@%#define %@AE@%NFONTS 6 %@NL@%
  28024. %@NL@%
  28025. main()%@NL@%
  28026. %@NL@%
  28027. {%@NL@%
  28028.   static unsigned char *text[2*NFONTS] =%@NL@%
  28029.   {%@NL@%
  28030.       "COURIER",        "courier",%@NL@%
  28031.       "HELV",           "helv",%@NL@%
  28032.       "TMS RMN",        "tms rmn",%@NL@%
  28033.       "MODERN",         "modern",%@NL@%
  28034.       "SCRIPT",         "script",%@NL@%
  28035.       "ROMAN",          "roman"%@NL@%
  28036.   };%@NL@%
  28037.   static unsigned char *face[NFONTS] =%@NL@%
  28038.   {%@NL@%
  28039.       "t'courier'",%@NL@%
  28040.       "t'helv'",%@NL@%
  28041.       "t'tms rmn'"%@NL@%
  28042.       "t'modern'"%@NL@%
  28043.       "t'script'"%@NL@%
  28044.       "t'roman'"%@NL@%
  28045.   };%@NL@%
  28046.   static unsigned char list[20];%@NL@%
  28047.   struct videoconfig vc;%@NL@%
  28048.   int mode = _VRES16COLOR;%@NL@%
  28049.   register i;%@NL@%
  28050. %@NL@%
  28051.   %@AB@%/*  Read header info from all .FON files in%@NL@%
  28052. %@AB@%   *  current directory   */%@AE@%%@NL@%
  28053. %@NL@%
  28054.   if(_registerfonts( "*.FON" )<0 )%@NL@%
  28055.   {%@NL@%
  28056.      _outtext("Error:  can't register fonts");%@NL@%
  28057.      exit( 0 );%@NL@%
  28058.   }%@NL@%
  28059. %@NL@%
  28060.   %@AB@%/*   Set highest available video mode */%@AE@%%@NL@%
  28061. %@NL@%
  28062.   while( !_setvideomode( mode ) )%@NL@%
  28063.      mode--;%@NL@%
  28064.   if( mode == _TEXTMONO )%@NL@%
  28065.      exit ( 0 );%@NL@%
  28066. %@NL@%
  28067.   %@AB@%/*   Copy video configuration into structure vc */%@AE@%%@NL@%
  28068. %@NL@%
  28069.   _getvideoconfig( &vc );%@NL@%
  28070. %@NL@%
  28071.   %@AB@%/*   Display six lines of sample text */%@AE@%%@NL@%
  28072. %@NL@%
  28073.   for( i = 0; i<NFONTS; i++ )%@NL@%
  28074.   {%@NL@%
  28075.      strcpy( list, face[i] );%@NL@%
  28076.      strcat( list, "h30w24b" );%@NL@%
  28077. %@NL@%
  28078.      if( !_setfont( list ) )%@NL@%
  28079.      {%@NL@%
  28080.          _setcolor( i + 1 );%@NL@%
  28081.          _moveto( 0, (i * vc.numypixels) / NFONTS );%@NL@%
  28082.          _outgtext( text[i * 2] );%@NL@%
  28083.          _moveto( vc.numxpixels / 2,%@NL@%
  28084.                      (i * vc.numypixels) / NFONTS );%@NL@%
  28085.          _outgtext( text[(i * 2) + 1] );%@NL@%
  28086.      }%@NL@%
  28087.      else%@NL@%
  28088.      {%@NL@%
  28089.          _setvideomode( _DEFAULTMODE );%@NL@%
  28090.          _outtext( "Error:  can't set font" );%@NL@%
  28091.          exit( 0 );%@NL@%
  28092.      }%@NL@%
  28093.   }%@NL@%
  28094.   getch();%@NL@%
  28095.   _setvideomode( _DEFAULTMODE );%@NL@%
  28096. %@NL@%
  28097.   %@AB@%/* Return memory when finished with fonts */%@AE@%%@NL@%
  28098. %@NL@%
  28099.   _unregisterfonts();%@NL@%
  28100.   exit( 0 );%@NL@%
  28101. }%@NL@%
  28102. %@NL@%
  28103. %@NL@%
  28104. %@2@%%@AH@%FUNCPTR.C%@AE@%%@EH@%%@NL@%
  28105. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FUNCPTR.C%@AE@%%@NL@%
  28106. %@NL@%
  28107. %@AB@%/* FUNCPTR.C: Demonstrate function pointers. */%@AE@%%@NL@%
  28108. %@NL@%
  28109. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28110. %@NL@%
  28111. main()%@NL@%
  28112. {%@NL@%
  28113.    int (*func_ptr) ();%@NL@%
  28114.    func_ptr = printf;%@NL@%
  28115.    (*func_ptr) ( "Curiouser and curiouser...\n" );%@NL@%
  28116. }%@NL@%
  28117. %@NL@%
  28118. %@NL@%
  28119. %@2@%%@AH@%FUNCPTR1.C%@AE@%%@EH@%%@NL@%
  28120. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\FUNCPTR1.C%@AE@%%@NL@%
  28121. %@NL@%
  28122. %@AB@%/* FUNCPTR1.C: Passing function pointers as arguments.%@NL@%
  28123. %@AB@%*/%@AE@%%@NL@%
  28124. %@NL@%
  28125. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28126. %@NL@%
  28127. void gimme_func( void (*func_ptr) () );%@NL@%
  28128. %@NL@%
  28129. main()%@NL@%
  28130. {%@NL@%
  28131.    gimme_func( puts );%@NL@%
  28132.    gimme_func( printf );%@NL@%
  28133. }%@NL@%
  28134. %@NL@%
  28135. void gimme_func( void (*func_ptr) () )%@NL@%
  28136. {%@NL@%
  28137.    (*func_ptr) ( "Ausgezeichnet!" );%@NL@%
  28138. }%@NL@%
  28139. %@NL@%
  28140. %@NL@%
  28141. %@2@%%@AH@%FUNGET.C%@AE@%%@EH@%%@NL@%
  28142. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\FUNGET.C%@AE@%%@NL@%
  28143. %@NL@%
  28144. %@AB@%/* FUNGET.C illustrates getting and ungetting characters from a file.%@NL@%
  28145. %@AB@% * Functions illustrated include:%@NL@%
  28146. %@AB@% *      getc            getchar         ungetc%@NL@%
  28147. %@AB@% *      fgetc           fgetchar%@NL@%
  28148. %@AB@% *%@NL@%
  28149. %@AB@% * Although getchar and fgetchar are not specifically used in the example,%@NL@%
  28150. %@AB@% * they are equivalent to using getc or fgetc with stdin. See HEXDUMP.C%@NL@%
  28151. %@AB@% * for another example of getc and fgetc.%@NL@%
  28152. %@AB@% */%@AE@%%@NL@%
  28153. %@NL@%
  28154. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28155. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28156. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  28157. %@AI@%#include %@AE@%<string.h> %@NL@%
  28158. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  28159. %@NL@%
  28160. void getword( FILE *stream, char *buf );%@NL@%
  28161. void skiptoword( FILE *stream );%@NL@%
  28162. %@NL@%
  28163. main()%@NL@%
  28164. {%@NL@%
  28165.     char buffer[128];%@NL@%
  28166.     FILE *infile;%@NL@%
  28167. %@NL@%
  28168.     printf( "Enter file name: " );%@NL@%
  28169.     gets( buffer );%@NL@%
  28170.     if( (infile = fopen( buffer, "rb" )) == NULL )%@NL@%
  28171.     {%@NL@%
  28172.         perror( "Can't open file" );%@NL@%
  28173.         exit( 1 );%@NL@%
  28174.     }%@NL@%
  28175. %@NL@%
  28176.     %@AB@%/* Read each word and print reversed version. */%@AE@%%@NL@%
  28177.     while( 1 )%@NL@%
  28178.     {%@NL@%
  28179.         skiptoword( infile );%@NL@%
  28180.         getword( infile, buffer );%@NL@%
  28181.         puts( strrev( buffer ) );%@NL@%
  28182.     }%@NL@%
  28183. }%@NL@%
  28184. %@NL@%
  28185. %@AB@%/* Read one word (defined as a string of alphanumeric characters). */%@AE@%%@NL@%
  28186. void getword( FILE *stream, char *p )%@NL@%
  28187. {%@NL@%
  28188.     int  ch;%@NL@%
  28189. %@NL@%
  28190.     do%@NL@%
  28191.     {%@NL@%
  28192.         %@AB@%/* Macro version used here, but function version could be used:%@NL@%
  28193. %@AB@%        ch = fgetc( stream );%@NL@%
  28194. %@AB@%         */%@AE@%%@NL@%
  28195.         ch = getc( stream );        %@AB@%/* Get characters until EOF  */%@AE@%%@NL@%
  28196.         if( ch == EOF )             %@AB@%/*   or non-alphanumeric     */%@AE@%%@NL@%
  28197.             exit( 0 );%@NL@%
  28198.         *(p++) = (char)ch;%@NL@%
  28199.     } while( isalnum( ch ) );%@NL@%
  28200.     ungetc( ch, stream );           %@AB@%/* Put non-alphanumeric back */%@AE@%%@NL@%
  28201.     *(--p) = '\0';                  %@AB@%/* Null-terminate            */%@AE@%%@NL@%
  28202. }%@NL@%
  28203. %@NL@%
  28204. %@AB@%/* Throw away non-digit characters. */%@AE@%%@NL@%
  28205. void skiptoword( FILE *stream )%@NL@%
  28206. {%@NL@%
  28207.     int  ch;%@NL@%
  28208. %@NL@%
  28209.     do%@NL@%
  28210.     {%@NL@%
  28211.         ch = getc( stream );%@NL@%
  28212.         if( ch == EOF )%@NL@%
  28213.             exit( 0 );%@NL@%
  28214.     } while( !isalnum( ch ) );%@NL@%
  28215.     ungetc( ch, stream );%@NL@%
  28216. }%@NL@%
  28217. %@NL@%
  28218. %@NL@%
  28219. %@2@%%@AH@%GETCH.C%@AE@%%@EH@%%@NL@%
  28220. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\GETCH.C%@AE@%%@NL@%
  28221. %@NL@%
  28222. %@AB@%/* GETCH.C illustrates how to process ASCII or extended keys.%@NL@%
  28223. %@AB@% * Functions illustrated include:%@NL@%
  28224. %@AB@% *      getch           getche%@NL@%
  28225. %@AB@% */%@AE@%%@NL@%
  28226. %@NL@%
  28227. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28228. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  28229. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28230. %@NL@%
  28231. main()%@NL@%
  28232. {%@NL@%
  28233.     int key;%@NL@%
  28234. %@NL@%
  28235.     %@AB@%/* Read and display keys until ESC is pressed. */%@AE@%%@NL@%
  28236.     while( 1 )%@NL@%
  28237.     {%@NL@%
  28238.         %@AB@%/* If first key is 0, then get second extended. */%@AE@%%@NL@%
  28239.         if( !(key = getch()) )%@NL@%
  28240.         {%@NL@%
  28241.             key = getch();%@NL@%
  28242.             printf( "ASCII: no\tChar: NA\t" );%@NL@%
  28243.         }%@NL@%
  28244. %@NL@%
  28245.         %@AB@%/* Otherwise there's only one key. */%@AE@%%@NL@%
  28246.         else%@NL@%
  28247.             printf( "ASCII: yes\tChar: %c \t", isgraph( key ) ? key : ' ' );%@NL@%
  28248. %@NL@%
  28249.         printf( "Decimal: %d\tHex: %X\n", key, key );%@NL@%
  28250. %@NL@%
  28251.         %@AB@%/* Echo character response to prompt. */%@AE@%%@NL@%
  28252.         if( key == 27)%@NL@%
  28253.         {%@NL@%
  28254.             printf( "Do you really want to quit? (Y/n) " );%@NL@%
  28255.             key = getche();%@NL@%
  28256.             printf( "\n" );%@NL@%
  28257.             if( (toupper( key ) == 'Y') || (key == 13) )%@NL@%
  28258.                 break;%@NL@%
  28259.         }%@NL@%
  28260.     }%@NL@%
  28261. }%@NL@%
  28262. %@NL@%
  28263. %@NL@%
  28264. %@2@%%@AH@%GOODMAC.C%@AE@%%@EH@%%@NL@%
  28265. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\GOODMAC.C%@AE@%%@NL@%
  28266. %@NL@%
  28267. %@AB@%/* GOODMAC.C: Parentheses in macro arguments. */%@AE@%%@NL@%
  28268. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28269. %@NL@%
  28270. %@AI@%#define %@AE@%FOURX(arg)  ( (arg) * 4 ) %@NL@%
  28271. %@NL@%
  28272. main()%@NL@%
  28273. {%@NL@%
  28274.    int val;%@NL@%
  28275.    val = FOURX( 2 + 3 );%@NL@%
  28276.    printf( "val = %d\n", val );%@NL@%
  28277. }%@NL@%
  28278. %@NL@%
  28279. %@NL@%
  28280. %@2@%%@AH@%GOODSEMI.C%@AE@%%@EH@%%@NL@%
  28281. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\GOODSEMI.C%@AE@%%@NL@%
  28282. %@NL@%
  28283. %@AB@%/* GOODSEMI.C */%@AE@%%@NL@%
  28284. %@NL@%
  28285. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28286. %@NL@%
  28287. main()%@NL@%
  28288. {%@NL@%
  28289.    int count;%@NL@%
  28290.    for( count = 0; count < 500; count++ )%@NL@%
  28291.    {%@NL@%
  28292.       printf( "count = %d\n", count );%@NL@%
  28293.       printf( "And the beat goes on...\n" );%@NL@%
  28294.    }%@NL@%
  28295. }%@NL@%
  28296. %@NL@%
  28297. %@NL@%
  28298. %@2@%%@AH@%GRAPHIC.C%@AE@%%@EH@%%@NL@%
  28299. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\GRAPHIC.C%@AE@%%@NL@%
  28300. %@NL@%
  28301. %@AB@%/* GRAPHIC.C: Displays every graphics mode */%@AE@%%@NL@%
  28302. %@NL@%
  28303. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28304. %@AI@%#include %@AE@%<graph.h> %@NL@%
  28305. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28306. %@NL@%
  28307. struct videoconfig screen;%@NL@%
  28308. int modes[12] =%@NL@%
  28309. {%@NL@%
  28310.    _MRES4COLOR, _MRESNOCOLOR, _HRESBW,%@NL@%
  28311.    _HERCMONO,%@NL@%
  28312.    _MRES16COLOR, _HRES16COLOR, _ERESNOCOLOR, _ERESCOLOR,%@NL@%
  28313.    _VRES2COLOR, _VRES16COLOR, _MRES256COLOR%@NL@%
  28314. };%@NL@%
  28315. %@NL@%
  28316. void print_menu( void );%@NL@%
  28317. void show_mode( char );%@NL@%
  28318. %@NL@%
  28319. main()%@NL@%
  28320. {%@NL@%
  28321.    char key;%@NL@%
  28322.    print_menu();%@NL@%
  28323.    while( (key = getch()) != 'x' )%@NL@%
  28324.       show_mode( key );%@NL@%
  28325. }%@NL@%
  28326. %@NL@%
  28327. void print_menu( void )%@NL@%
  28328. {%@NL@%
  28329.    _setvideomode( _DEFAULTMODE );%@NL@%
  28330.    _clearscreen( _GCLEARSCREEN );%@NL@%
  28331.    printf( "Please choose a graphics mode\nType 'x' to exit.\n\n" );%@NL@%
  28332.    printf( "0 _MRES4COLOR\n1 _MRESNOCOLOR\n2 _HRESBW\n" );%@NL@%
  28333.    printf( "3 _HERCMONO\n4 _MRES16COLOR\n5 _HRES16COLOR\n" );%@NL@%
  28334.    printf( "6 _ERESNOCOLOR\n7 _ERESCOLOR\n" );%@NL@%
  28335.    printf( "8 _VRES2COLOR\n9 _VRES16COLOR\na _MRES256COLOR\n" );%@NL@%
  28336. }%@NL@%
  28337. %@NL@%
  28338. void show_mode( char which )%@NL@%
  28339. {%@NL@%
  28340.    int nc, i;%@NL@%
  28341.    int height, width;%@NL@%
  28342.    int mode = which;%@NL@%
  28343. %@NL@%
  28344.    if( mode < '0' || mode > '9' )%@NL@%
  28345.       if( mode == 'a' )%@NL@%
  28346.          mode = '9' + 1;%@NL@%
  28347.       else if( mode == 'b' )%@NL@%
  28348.          mode = '9' + 2;%@NL@%
  28349.       else%@NL@%
  28350.          return;%@NL@%
  28351. %@NL@%
  28352.    if( _setvideomode( modes[mode - '0'] ) )%@NL@%
  28353.    {%@NL@%
  28354.       _getvideoconfig( &screen );%@NL@%
  28355.       nc = screen.numcolors;%@NL@%
  28356.       width = screen.numxpixels/nc;%@NL@%
  28357.       height = screen.numypixels/2;%@NL@%
  28358.       for( i = 0; i < nc; i++ )%@NL@%
  28359.       {%@NL@%
  28360.          _setcolor( i );%@NL@%
  28361.          _rectangle( _GFILLINTERIOR, i * width, 0, (i + 1) * width, height );%@NL@%
  28362.       }%@NL@%
  28363.    }%@NL@%
  28364.    else%@NL@%
  28365.    {%@NL@%
  28366.       printf( " \nVideo mode %c is not available.\n", which);%@NL@%
  28367.       printf( "Please press a key.\n" );%@NL@%
  28368.    }%@NL@%
  28369.    getch();%@NL@%
  28370.    _setvideomode( _DEFAULTMODE );%@NL@%
  28371.    print_menu();%@NL@%
  28372. }%@NL@%
  28373. %@NL@%
  28374. %@NL@%
  28375. %@2@%%@AH@%HALLOC.C%@AE@%%@EH@%%@NL@%
  28376. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\MEMORY\HALLOC.C%@AE@%%@NL@%
  28377. %@NL@%
  28378. %@AB@%/* HALLOC.C illustrates dynamic allocation of huge memory using functions:%@NL@%
  28379. %@AB@% *      halloc          hfree%@NL@%
  28380. %@AB@% */%@AE@%%@NL@%
  28381. %@NL@%
  28382. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28383. %@AI@%#include %@AE@%<malloc.h> %@NL@%
  28384. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  28385. %@NL@%
  28386. main()%@NL@%
  28387. {%@NL@%
  28388.     char huge *bigbuf, huge *p;%@NL@%
  28389.     long count = 100000L;%@NL@%
  28390. %@NL@%
  28391.     %@AB@%/* Allocate huge buffer. */%@AE@%%@NL@%
  28392.     bigbuf = (char huge *)halloc( count, sizeof( char ) );%@NL@%
  28393.     if( bigbuf == NULL )%@NL@%
  28394.     {%@NL@%
  28395.         printf( "Insufficient memory" );%@NL@%
  28396.         exit( 1 );%@NL@%
  28397.     }%@NL@%
  28398. %@NL@%
  28399.     %@AB@%/* Fill the buffer with characters. */%@AE@%%@NL@%
  28400.     for( p = bigbuf; count; count--, p++ )%@NL@%
  28401.         *p = (char)(count % 10) + '0';%@NL@%
  28402. %@NL@%
  28403.     %@AB@%/* Free huge buffer. */%@AE@%%@NL@%
  28404.     hfree( bigbuf );%@NL@%
  28405.     exit( 0 );%@NL@%
  28406. }%@NL@%
  28407. %@NL@%
  28408. %@NL@%
  28409. %@2@%%@AH@%HARDERR.C%@AE@%%@EH@%%@NL@%
  28410. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\HARDERR.C%@AE@%%@NL@%
  28411. %@NL@%
  28412. %@AB@%/* HARDERR.C illustrates handling of hardware errors using functions:%@NL@%
  28413. %@AB@% *      _harderr            _hardresume         _hardretn%@NL@%
  28414. %@AB@% */%@AE@%%@NL@%
  28415. %@NL@%
  28416. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28417. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28418. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  28419. %@AI@%#include %@AE@%<direct.h> %@NL@%
  28420. %@AI@%#include %@AE@%<string.h> %@NL@%
  28421. %@AI@%#include %@AE@%<dos.h> %@NL@%
  28422. %@AI@%#include %@AE@%<bios.h> %@NL@%
  28423. %@NL@%
  28424. void far hardhandler( unsigned deverr, unsigned doserr, unsigned far *hdr );%@NL@%
  28425. int _bios_str( char *p );%@NL@%
  28426. %@NL@%
  28427. main() %@NL@%
  28428. {%@NL@%
  28429.     %@AB@%/* Install our hard error handler. */%@AE@%%@NL@%
  28430.     _harderr( hardhandler );%@NL@%
  28431. %@NL@%
  28432.     %@AB@%/* Test it. */%@AE@%%@NL@%
  28433.     printf( "Make sure there is no disk in drive A:\n" );%@NL@%
  28434.     printf( "Press a key when ready...\n" );%@NL@%
  28435.     getch();%@NL@%
  28436.     if( mkdir( "a:\test" ) )%@NL@%
  28437.     {%@NL@%
  28438.         printf( "Failed" );%@NL@%
  28439.         exit( 1 );%@NL@%
  28440.     }%@NL@%
  28441.     else%@NL@%
  28442.     {%@NL@%
  28443.         printf( "Succeeded" );%@NL@%
  28444.         rmdir( "a:test" );%@NL@%
  28445.         exit( 0 );%@NL@%
  28446.     }%@NL@%
  28447. }%@NL@%
  28448. %@NL@%
  28449. %@AB@%/* Handler to deal with hard error codes. Since DOS is not reentrant,%@NL@%
  28450. %@AB@% * it is not safe use DOS calls to do I/O within the DOS Critical Error%@NL@%
  28451. %@AB@% * Handler (int 24h) used by _harderr. Therefore, screen output and%@NL@%
  28452. %@AB@% * keyboard input must be done through the BIOS.%@NL@%
  28453. %@AB@% */%@AE@%%@NL@%
  28454. void far hardhandler( unsigned deverr, unsigned doserr, unsigned far *hdr )%@NL@%
  28455. {%@NL@%
  28456.     int ch;%@NL@%
  28457.     static char buf[200], tmpbuf[10];%@NL@%
  28458. %@NL@%
  28459.     %@AB@%/* Copy message to buffer, then use BIOS to print it. */%@AE@%%@NL@%
  28460.     strcpy( buf, "\n\rDevice error code: " );%@NL@%
  28461.     strcat( buf, itoa( deverr, tmpbuf, 10 ) );%@NL@%
  28462.     strcat( buf, "\n\rDOS error code:    " );%@NL@%
  28463.     strcat( buf, itoa( doserr, tmpbuf, 10 ) );%@NL@%
  28464.     strcat( buf, "\n\r(R)etry, (F)ail, or (Q)uit? " );%@NL@%
  28465. %@NL@%
  28466.     %@AB@%/* Use BIOS to write strings and get a key. */%@AE@%%@NL@%
  28467.     _bios_str( buf );%@NL@%
  28468.     ch = _bios_keybrd( _KEYBRD_READ ) & 0x00ff;%@NL@%
  28469.     _bios_str( "\n\r" );%@NL@%
  28470. %@NL@%
  28471.     switch( ch )%@NL@%
  28472.     {%@NL@%
  28473.         case 'R':%@NL@%
  28474.         case 'r':       %@AB@%/* Try again */%@AE@%%@NL@%
  28475.         default:%@NL@%
  28476.             _hardresume( _HARDERR_RETRY );%@NL@%
  28477.         case 'F':%@NL@%
  28478.         case 'f':       %@AB@%/* Return to DOS with error code */%@AE@%%@NL@%
  28479.             _hardretn( doserr );%@NL@%
  28480.         case 'Q':%@NL@%
  28481.         case 'q':       %@AB@%/* Quit program */%@AE@%%@NL@%
  28482.             _hardresume( _HARDERR_ABORT );%@NL@%
  28483. %@NL@%
  28484.     }%@NL@%
  28485. }%@NL@%
  28486. %@NL@%
  28487. %@AB@%/* Display a string using BIOS interrupt 0x0e (Write TTY). Return length%@NL@%
  28488. %@AB@% * of string displayed.%@NL@%
  28489. %@AB@% */%@AE@%%@NL@%
  28490. int _bios_str( char *p )%@NL@%
  28491. {%@NL@%
  28492.     union REGS inregs, outregs;%@NL@%
  28493.     char *start = p;%@NL@%
  28494. %@NL@%
  28495.     inregs.h.ah = 0x0e;%@NL@%
  28496.     for( ; *p; p++ )%@NL@%
  28497.     {%@NL@%
  28498.         inregs.h.al = *p;%@NL@%
  28499.         int86( 0x10, &inregs, &outregs );%@NL@%
  28500.     }%@NL@%
  28501.     return p - start;%@NL@%
  28502. }%@NL@%
  28503. %@NL@%
  28504. %@NL@%
  28505. %@2@%%@AH@%HEAPWALK.C%@AE@%%@EH@%%@NL@%
  28506. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\MEMORY\HEAPWALK.C%@AE@%%@NL@%
  28507. %@NL@%
  28508. %@AB@%/* HEAPWALK.C illustrates heap testing functions including:%@NL@%
  28509. %@AB@% *      _heapchk        _fheapchk       _nheapchk%@NL@%
  28510. %@AB@% *      _heapset        _fheapset       _nheapset%@NL@%
  28511. %@AB@% *      _heapwalk       _fheapwalk      _nheapwalk%@NL@%
  28512. %@AB@% *      _msize          _fmsize         _nmsize%@NL@%
  28513. %@AB@% *%@NL@%
  28514. %@AB@% * Only the model independent versions are shown. They map to the model%@NL@%
  28515. %@AB@% * dependent versions, depending on the memory model.%@NL@%
  28516. %@AB@% */%@AE@%%@NL@%
  28517. %@NL@%
  28518. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28519. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28520. %@AI@%#include %@AE@%<malloc.h> %@NL@%
  28521. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  28522. %@AI@%#include %@AE@%<time.h> %@NL@%
  28523. %@NL@%
  28524. %@AB@%/* Macro to get a random integer within a specified range */%@AE@%%@NL@%
  28525. %@AI@%#define %@AE@%getrandom( min, max ) ((rand() % (int)((max) - (min))) + (min) + 1) %@NL@%
  28526. %@NL@%
  28527. void heapdump( char fill );%@NL@%
  28528. void heapstat( int status );%@NL@%
  28529. %@NL@%
  28530. main()%@NL@%
  28531. {%@NL@%
  28532.     int *p[10], i;%@NL@%
  28533. %@NL@%
  28534.     srand( (unsigned)time( 0L ) );  %@AB@%/* Seed with current time. */%@AE@%%@NL@%
  28535. %@NL@%
  28536.     %@AB@%/* Check heap status. Should be OK at start of heap. */%@AE@%%@NL@%
  28537.     heapstat( _heapchk() );%@NL@%
  28538. %@NL@%
  28539.     %@AB@%/* Now do some operations that affect the heap. In this example,%@NL@%
  28540. %@AB@%     * allocate random-size blocks.%@NL@%
  28541. %@AB@%     */%@AE@%%@NL@%
  28542.     for( i = 0; i < 10; i++ )%@NL@%
  28543.     {%@NL@%
  28544.         if( (p[i] = (int *)calloc( getrandom( 1, 10000 ),%@NL@%
  28545.                                    sizeof( int ) )) == NULL )%@NL@%
  28546.         {%@NL@%
  28547.             --i;%@NL@%
  28548.             break;%@NL@%
  28549.         }%@NL@%
  28550.         printf( "Allocated %u at %p\n", _msize( p[i] ), (void far *)p[i] );%@NL@%
  28551.     }%@NL@%
  28552. %@NL@%
  28553.     %@AB@%/* Fill all free blocks with the test character. */%@AE@%%@NL@%
  28554.     heapstat( _heapset( 254 ) );%@NL@%
  28555. %@NL@%
  28556.     %@AB@%/* In a real program, you might do operations here on the allocated%@NL@%
  28557. %@AB@%     * buffers. Then do heapdump to make sure none of the operations wrote%@NL@%
  28558. %@AB@%     * to free blocks.%@NL@%
  28559. %@AB@%     */%@AE@%%@NL@%
  28560.     heapdump( 254 );%@NL@%
  28561. %@NL@%
  28562.     %@AB@%/* Do some more heap operations. */%@AE@%%@NL@%
  28563.     for( ; i >= 0; i-- )%@NL@%
  28564.     {%@NL@%
  28565.         free( p[i] );%@NL@%
  28566.         printf( "Deallocating %u at %p\n", _msize( p[i] ), (void far *)p[i] );%@NL@%
  28567.     }%@NL@%
  28568. %@NL@%
  28569.     %@AB@%/* Check heap again. */%@AE@%%@NL@%
  28570.     heapdump( 254 );%@NL@%
  28571. }%@NL@%
  28572. %@NL@%
  28573. %@AB@%/* Test routine to check each block in the heap. */%@AE@%%@NL@%
  28574. void heapdump( char fill )%@NL@%
  28575. {%@NL@%
  28576.     struct _heapinfo hi;%@NL@%
  28577.     int heapstatus, i;%@NL@%
  28578.     char far *p;%@NL@%
  28579. %@NL@%
  28580.     %@AB@%/* Walk through entries, displaying results and checking free blocks. */%@AE@%%@NL@%
  28581.     printf( "\nHeap dump:\n" );%@NL@%
  28582.     hi._pentry = NULL;%@NL@%
  28583.     while( (heapstatus = _heapwalk( &hi )) == _HEAPOK )%@NL@%
  28584.     {%@NL@%
  28585.         printf( "\n\t%s block at %p of size %u\t",%@NL@%
  28586.                 hi._useflag == _USEDENTRY ? "USED" : "FREE",%@NL@%
  28587.                 hi._pentry, hi._size );%@NL@%
  28588. %@NL@%
  28589.         %@AB@%/* For free entries, check each byte to see that it still has%@NL@%
  28590. %@AB@%         * only the fill character.%@NL@%
  28591. %@AB@%         */%@AE@%%@NL@%
  28592.         if( hi._useflag != _USEDENTRY )%@NL@%
  28593.         {%@NL@%
  28594.             for( p = (char far *)hi._pentry, i = 0; i < hi._size; p++, i++ )%@NL@%
  28595.                 if( (char)*p != fill )%@NL@%
  28596.                     break;%@NL@%
  28597.             if( i == hi._size )%@NL@%
  28598.                 printf( "Not changed" );%@NL@%
  28599.             else%@NL@%
  28600.                 printf( "Changed" );%@NL@%
  28601.         }%@NL@%
  28602.     }%@NL@%
  28603.     heapstat( heapstatus );%@NL@%
  28604. }%@NL@%
  28605. %@NL@%
  28606. %@AB@%/* Report on the status returned by _heapwalk, _heapset, or _heapchk. */%@AE@%%@NL@%
  28607. void heapstat( int status )%@NL@%
  28608. {%@NL@%
  28609.     printf( "\nHeap status: " );%@NL@%
  28610.     switch( status )%@NL@%
  28611.     {%@NL@%
  28612.         case _HEAPOK:%@NL@%
  28613.             printf( "OK - heap is fine" );%@NL@%
  28614.             break;%@NL@%
  28615.         case _HEAPEMPTY:%@NL@%
  28616.             printf( "OK - empty heap" );%@NL@%
  28617.             break;%@NL@%
  28618.         case _HEAPEND:%@NL@%
  28619.             printf( "OK - end of heap" );%@NL@%
  28620.             break;%@NL@%
  28621.         case _HEAPBADPTR:%@NL@%
  28622.             printf( "ERROR - bad pointer to heap" );%@NL@%
  28623.             break;%@NL@%
  28624.         case _HEAPBADBEGIN:%@NL@%
  28625.             printf( "ERROR - bad start of heap" );%@NL@%
  28626.             break;%@NL@%
  28627.         case _HEAPBADNODE:%@NL@%
  28628.             printf( "ERROR - bad node in heap" );%@NL@%
  28629.             break;%@NL@%
  28630.     }%@NL@%
  28631.     printf( "\n\n" );%@NL@%
  28632. }%@NL@%
  28633. %@NL@%
  28634. %@NL@%
  28635. %@2@%%@AH@%HEXDUMP1.C%@AE@%%@EH@%%@NL@%
  28636. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\HEXDUMP1.C%@AE@%%@NL@%
  28637. %@NL@%
  28638. %@AB@%/* HEXDUMP.C illustrates directory splitting and character stream I/O.%@NL@%
  28639. %@AB@% * Functions illustrated include:%@NL@%
  28640. %@AB@% *      _splitpath      _makepath%@NL@%
  28641. %@AB@% *      fgetc           fputc           fgetchar        fputchar%@NL@%
  28642. %@AB@% *      getc            putc            getchar         putchar%@NL@%
  28643. %@AB@% *%@NL@%
  28644. %@AB@% * While fgetchar, getchar, fputchar, and putchar are not specifically%@NL@%
  28645. %@AB@% * used in the example, they are equivalent to using fgetc or getc with%@NL@%
  28646. %@AB@% * stdin, or to using fputc or putc with stdout. See FUNGET.C for another%@NL@%
  28647. %@AB@% * example of fgetc and getc.%@NL@%
  28648. %@AB@% */%@AE@%%@NL@%
  28649. %@NL@%
  28650. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28651. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28652. %@AI@%#include %@AE@%<io.h> %@NL@%
  28653. %@AI@%#include %@AE@%<dos.h> %@NL@%
  28654. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  28655. %@AI@%#include %@AE@%<string.h> %@NL@%
  28656. %@NL@%
  28657. main()%@NL@%
  28658. {%@NL@%
  28659.     FILE *infile, *outfile;%@NL@%
  28660.     char inpath[_MAX_PATH], outpath[_MAX_PATH];%@NL@%
  28661.     char drive[_MAX_DRIVE], dir[_MAX_DIR];%@NL@%
  28662.     char fname[_MAX_FNAME], ext[_MAX_EXT];%@NL@%
  28663.     int  in, size;%@NL@%
  28664.     long i = 0L;%@NL@%
  28665. %@NL@%
  28666.     %@AB@%/* Query for and open input file. */%@AE@%%@NL@%
  28667.     printf( "Enter input file name: " );%@NL@%
  28668.     gets( inpath );%@NL@%
  28669.     strcpy( outpath, inpath );%@NL@%
  28670.     if( (infile = fopen( inpath, "rb" )) == NULL )%@NL@%
  28671.     {%@NL@%
  28672.         printf( "Can't open input file" );%@NL@%
  28673.         exit( 1 );%@NL@%
  28674.     }%@NL@%
  28675. %@NL@%
  28676.     %@AB@%/* Build output file by splitting path and rebuilding with%@NL@%
  28677. %@AB@%     * new extension.%@NL@%
  28678. %@AB@%     */%@AE@%%@NL@%
  28679.     _splitpath( outpath, drive, dir, fname, ext );%@NL@%
  28680.     strcpy( ext, "hx" );%@NL@%
  28681.     _makepath( outpath, drive, dir, fname, ext );%@NL@%
  28682. %@NL@%
  28683.     %@AB@%/* If file does not exist, open it */%@AE@%%@NL@%
  28684.     if( access( outpath, 0 ) )%@NL@%
  28685.     {%@NL@%
  28686.         outfile = fopen( outpath, "wb" );%@NL@%
  28687.         printf( "Creating %s from %s . . .\n", outpath, inpath );%@NL@%
  28688.     }%@NL@%
  28689.     else%@NL@%
  28690.     {%@NL@%
  28691.         printf( "Output file already exists" );%@NL@%
  28692.         exit( 1 );%@NL@%
  28693.     }%@NL@%
  28694. %@NL@%
  28695.     printf( "(B)yte or (W)ord: " );%@NL@%
  28696.     size = getche();%@NL@%
  28697. %@NL@%
  28698.     %@AB@%/* Get each character from input and write to output. */%@AE@%%@NL@%
  28699.     while( !feof( infile ) )%@NL@%
  28700.     {%@NL@%
  28701.         if( (size == 'W') || (size == 'w') )%@NL@%
  28702.         {%@NL@%
  28703.             in = getw( infile );%@NL@%
  28704.             fprintf( outfile, " %.4X", in );%@NL@%
  28705.             if( !(++i % 8) )%@NL@%
  28706.                 putw( 0x0D0A, outfile );        %@AB@%/* New line      */%@AE@%%@NL@%
  28707.         }%@NL@%
  28708.         else%@NL@%
  28709.         {%@NL@%
  28710.             %@AB@%/* This example uses the fgetc and fputc functions. You%@NL@%
  28711. %@AB@%             * could also use the macro versions:%@NL@%
  28712. %@AB@%            in = getc( infile );%@NL@%
  28713. %@AB@%             */%@AE@%%@NL@%
  28714.             in = fgetc( infile );%@NL@%
  28715.             fprintf( outfile, " %.2X", in );%@NL@%
  28716.             if( !(++i % 16) )%@NL@%
  28717.             {%@NL@%
  28718.                 %@AB@%/* Macro version:%@NL@%
  28719. %@AB@%                putc( 13, outfile );%@NL@%
  28720. %@AB@%                 */%@AE@%%@NL@%
  28721.                 fputc( 13, outfile );           %@AB@%/* New line      */%@AE@%%@NL@%
  28722.                 fputc( 10, outfile );%@NL@%
  28723.             }%@NL@%
  28724.         }%@NL@%
  28725.     }%@NL@%
  28726.     fclose( infile );%@NL@%
  28727.     fclose( outfile );%@NL@%
  28728.     exit( 0 );%@NL@%
  28729. }%@NL@%
  28730. %@NL@%
  28731. %@NL@%
  28732. %@2@%%@AH@%HORIZON.C%@AE@%%@EH@%%@NL@%
  28733. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\HORIZON.C%@AE@%%@NL@%
  28734. %@NL@%
  28735. %@AB@%/* HORIZON.C: VGA graphics with cycling of 256 colors */%@AE@%%@NL@%
  28736. %@NL@%
  28737. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28738. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  28739. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28740. %@AI@%#include %@AE@%<graph.h> %@NL@%
  28741. %@NL@%
  28742. %@AI@%#define %@AE@%RED 0x0000003FL %@NL@%
  28743. %@AI@%#define %@AE@%GRN 0x00003F00L %@NL@%
  28744. %@AI@%#define %@AE@%BLU 0x003F0000L %@NL@%
  28745. %@AI@%#define %@AE@%WHT 0x003F3F3FL %@NL@%
  28746. %@AI@%#define %@AE@%STEP 21 %@NL@%
  28747. %@NL@%
  28748. struct videoconfig screen;%@NL@%
  28749. long int rainbow[512];%@NL@%
  28750. %@NL@%
  28751. main()%@NL@%
  28752. {%@NL@%
  28753.    int i;%@NL@%
  28754.    long int col, gray;%@NL@%
  28755. %@NL@%
  28756.    if( _setvideomode( _MRES256COLOR ) == 0 )%@NL@%
  28757.    {%@NL@%
  28758.       printf("This program requires a VGA card.\n" );%@NL@%
  28759.       exit( 0 );%@NL@%
  28760.    }%@NL@%
  28761.    for( col = 0; col < 64; col++ )%@NL@%
  28762.    {%@NL@%
  28763.       gray = col | (col << 8) | (col << 16);%@NL@%
  28764.       rainbow[col] = rainbow[col + 256] = BLU & gray;%@NL@%
  28765.       rainbow[col + 64] = rainbow[col + 64 + 256] = BLU | gray;%@NL@%
  28766.       rainbow[col + 128] = rainbow[col + 128 + 256] = RED | (WHT & ~gray);%@NL@%
  28767.       rainbow[col + 192] = rainbow[col + 192 + 256] = RED & ~gray;%@NL@%
  28768.    }%@NL@%
  28769.    _setvieworg( 160, 85 );%@NL@%
  28770. %@NL@%
  28771.    for( i = 0; i < 255; i++ )%@NL@%
  28772.    {%@NL@%
  28773.       _setcolor( 255 - i );%@NL@%
  28774.       _moveto( i, i - 255 );%@NL@%
  28775.       _lineto( -i, 255 - i );%@NL@%
  28776.       _moveto( -i, i - 255 );%@NL@%
  28777.       _lineto( i, 255 - i );%@NL@%
  28778.       _ellipse( _GBORDER, -i, -i / 2, i, i / 2 );%@NL@%
  28779.    }%@NL@%
  28780.    for( i = 0; !kbhit(); i += STEP, i %= 256 )%@NL@%
  28781.       _remapallpalette( &(rainbow[i]) );%@NL@%
  28782. %@NL@%
  28783.    _setvideomode( _DEFAULTMODE );%@NL@%
  28784. }%@NL@%
  28785. %@NL@%
  28786. %@NL@%
  28787. %@2@%%@AH@%IF1.C%@AE@%%@EH@%%@NL@%
  28788. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\IF1.C%@AE@%%@NL@%
  28789. %@NL@%
  28790. %@AB@%/* IF.C: Demonstrate if statement. */%@AE@%%@NL@%
  28791. %@NL@%
  28792. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28793. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28794. %@AI@%#define %@AE@% B_KEY  'b' %@NL@%
  28795. %@NL@%
  28796. main()%@NL@%
  28797. {%@NL@%
  28798.    char ch;%@NL@%
  28799.    printf( "Press the b key to hear a bell.\n" );%@NL@%
  28800.    ch = getche();%@NL@%
  28801.    if( ch == B_KEY )%@NL@%
  28802.       printf( "Beep!\a\n" );%@NL@%
  28803. }%@NL@%
  28804. %@NL@%
  28805. %@NL@%
  28806. %@2@%%@AH@%INCMAC.C%@AE@%%@EH@%%@NL@%
  28807. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\INCMAC.C%@AE@%%@NL@%
  28808. %@NL@%
  28809. %@AB@%/* INCMAC.C: Increment operator in macro argument.%@NL@%
  28810. %@AB@%*/%@AE@%%@NL@%
  28811. %@NL@%
  28812. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28813. %@AI@%#define %@AE@%ABS(value)  ( (value) >= 0 ? (value) : -(value) ) %@NL@%
  28814. %@NL@%
  28815. main()%@NL@%
  28816. {%@NL@%
  28817.    static int array[4] = {3, -20, -555, 6};%@NL@%
  28818.    int *ptr = array;%@NL@%
  28819.    int val, count;%@NL@%
  28820.    for( count = 0; count < 4; count++ )%@NL@%
  28821.    {%@NL@%
  28822.       val = ABS(*ptr++); %@AB@%/* Error! */%@AE@%%@NL@%
  28823.       printf( "abs of array[%d] = %d\n", count, val );%@NL@%
  28824.    }%@NL@%
  28825. }%@NL@%
  28826. %@NL@%
  28827. %@NL@%
  28828. %@2@%%@AH@%INCMAC1.C%@AE@%%@EH@%%@NL@%
  28829. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\INCMAC1.C%@AE@%%@NL@%
  28830. %@NL@%
  28831. %@AB@%/* INCMAC1.C: Increment operator in the for statement.%@NL@%
  28832. %@AB@%*/%@AE@%%@NL@%
  28833. %@NL@%
  28834. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28835. %@AI@%#define %@AE@%ABS(value)  ( (value) >= 0 ? (value) : -(value) ) %@NL@%
  28836. %@NL@%
  28837. main()%@NL@%
  28838. {%@NL@%
  28839.    static int array[4] = {3, -20, -555, 6};%@NL@%
  28840.    int *ptr = array;%@NL@%
  28841.    int val, count;%@NL@%
  28842.    for( count = 0; count < 4; count++, ptr++ )%@NL@%
  28843.    {%@NL@%
  28844.       val = ABS(*ptr);%@NL@%
  28845.       printf( "abs of array[%d] = %d\n", count, val );%@NL@%
  28846.    }%@NL@%
  28847. }%@NL@%
  28848. %@NL@%
  28849. %@NL@%
  28850. %@2@%%@AH@%INTMATH.C%@AE@%%@EH@%%@NL@%
  28851. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\MATH\INTMATH.C%@AE@%%@NL@%
  28852. %@NL@%
  28853. %@AB@%/* INTMATH.C illustrates integer math functions including:%@NL@%
  28854. %@AB@% *      abs         labs        min         max         div         ldiv%@NL@%
  28855. %@AB@% *%@NL@%
  28856. %@AB@% * See MATH.C for an example of the similar fabs function.%@NL@%
  28857. %@AB@% */%@AE@%%@NL@%
  28858. %@NL@%
  28859. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  28860. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28861. %@AI@%#include %@AE@%<math.h> %@NL@%
  28862. %@NL@%
  28863. main()%@NL@%
  28864. {%@NL@%
  28865.     int x, y;%@NL@%
  28866.     long lx, ly;%@NL@%
  28867.     div_t divres;%@NL@%
  28868.     ldiv_t ldivres;%@NL@%
  28869. %@NL@%
  28870.     printf( "Enter two integers: " );%@NL@%
  28871.     scanf( "%d %d", &x, &y );%@NL@%
  28872. %@NL@%
  28873.     printf("Function\tResult\n\n" );%@NL@%
  28874.     printf( "abs\t\tThe absolute value of %d is %d\n", x, abs( x ) );%@NL@%
  28875.     printf( "min\t\tThe lesser of %d and %d is %d\n", x, y, min( x, y ) );%@NL@%
  28876.     printf( "max\t\tThe greater of %d and %d is %d\n", x, y, max( x, y ) );%@NL@%
  28877.     divres = div( x, y );%@NL@%
  28878.     printf( "div\t\tFor %d / %d, quotient is %d and remainder is %d\n\n",%@NL@%
  28879.             x, y, divres.quot, divres.rem );%@NL@%
  28880. %@NL@%
  28881.     printf( "Enter two long integers: " );%@NL@%
  28882.     scanf( "%ld %ld", &lx, &ly );%@NL@%
  28883. %@NL@%
  28884.     printf("Function\tResult\n\n" );%@NL@%
  28885.     ldivres = ldiv( lx, ly );%@NL@%
  28886.     printf( "labs\t\tThe absolute value of %ld is %ld\n", lx, labs( lx ) );%@NL@%
  28887.     printf( "ldiv\t\tFor %ld / %ld, quotient is %ld and remainder is %ld\n",%@NL@%
  28888.             lx, ly, ldivres.quot, ldivres.rem );%@NL@%
  28889. }%@NL@%
  28890. %@NL@%
  28891. %@NL@%
  28892. %@2@%%@AH@%IS.C%@AE@%%@EH@%%@NL@%
  28893. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\IS.C%@AE@%%@NL@%
  28894. %@NL@%
  28895. %@AB@%/* IS.C illustrates character classification functions including:%@NL@%
  28896. %@AB@% *      isprint         isascii         isalpha         isalnum%@NL@%
  28897. %@AB@% *      isupper         islower         isdigit         isxdigit%@NL@%
  28898. %@AB@% *      ispunct         isspace         iscntrl         isgraph%@NL@%
  28899. %@AB@% *%@NL@%
  28900. %@AB@% * Console output is also shown using:%@NL@%
  28901. %@AB@% *      cprintf%@NL@%
  28902. %@AB@% *%@NL@%
  28903. %@AB@% * See PRINTF.C for additional examples of formatting for cprintf.%@NL@%
  28904. %@AB@% */%@AE@%%@NL@%
  28905. %@NL@%
  28906. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  28907. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28908. %@NL@%
  28909. main()%@NL@%
  28910. {%@NL@%
  28911.     int ch;%@NL@%
  28912. %@NL@%
  28913.     %@AB@%/* Display each ASCII character with character type table. */%@AE@%%@NL@%
  28914.     for( ch = 0; ch < 256; ch++ )%@NL@%
  28915.     {%@NL@%
  28916.         if( ch % 22 == 0 )%@NL@%
  28917.         {%@NL@%
  28918.             if( ch )%@NL@%
  28919.                 getch();%@NL@%
  28920. %@NL@%
  28921.             %@AB@%/* Note that cprintf does not convert "\n" to a CR/LF sequence.%@NL@%
  28922. %@AB@%             * You can specify this sequence with "\n\r".%@NL@%
  28923. %@AB@%             */%@AE@%%@NL@%
  28924.             cprintf( "\n\rNum Char ASCII Alpha AlNum Cap Low Digit " );%@NL@%
  28925.             cprintf( "XDigit Punct White CTRL Graph Print \n\r" );%@NL@%
  28926.         }%@NL@%
  28927.         cprintf( "%3d  ", ch );%@NL@%
  28928. %@NL@%
  28929.         %@AB@%/* Console output functions (cprint, cputs, and putch) display%@NL@%
  28930. %@AB@%         * graphic characters for all values except 7 (bell), 8 (backspace),%@NL@%
  28931. %@AB@%         * 10 (line feed), 13 (carriage return), and 255.%@NL@%
  28932. %@AB@%         */%@AE@%%@NL@%
  28933.         if( ch == 7 || ch == 8 || ch == 10 || ch == 13 || ch == 255 )%@NL@%
  28934.             cprintf("NV" );%@NL@%
  28935.         else%@NL@%
  28936.             cprintf("%c ", ch );%@NL@%
  28937.         cprintf( "%5s", isascii( ch )  ? "Y" : "N" );%@NL@%
  28938.         cprintf( "%6s", isalpha( ch )  ? "Y" : "N" );%@NL@%
  28939.         cprintf( "%6s", isalnum( ch )  ? "Y" : "N" );%@NL@%
  28940.         cprintf( "%5s", isupper( ch )  ? "Y" : "N" );%@NL@%
  28941.         cprintf( "%4s", islower( ch )  ? "Y" : "N" );%@NL@%
  28942.         cprintf( "%5s", isdigit( ch )  ? "Y" : "N" );%@NL@%
  28943.         cprintf( "%7s", isxdigit( ch ) ? "Y" : "N" );%@NL@%
  28944.         cprintf( "%6s", ispunct( ch )  ? "Y" : "N" );%@NL@%
  28945.         cprintf( "%6s", isspace( ch )  ? "Y" : "N" );%@NL@%
  28946.         cprintf( "%5s", iscntrl( ch )  ? "Y" : "N" );%@NL@%
  28947.         cprintf( "%6s", isprint( ch )  ? "Y" : "N" );%@NL@%
  28948.         cprintf( "%6s\n\r", isgraph( ch )  ? "Y" : "N" );%@NL@%
  28949.     }%@NL@%
  28950. }%@NL@%
  28951. %@NL@%
  28952. %@NL@%
  28953. %@2@%%@AH@%KBHIT.C%@AE@%%@EH@%%@NL@%
  28954. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\KBHIT.C%@AE@%%@NL@%
  28955. %@NL@%
  28956. %@AB@%/* KBHIT.C illustrates:%@NL@%
  28957. %@AB@% *      kbhit%@NL@%
  28958. %@AB@% */%@AE@%%@NL@%
  28959. %@NL@%
  28960. %@AI@%#include %@AE@%<conio.h> %@NL@%
  28961. %@NL@%
  28962. main()%@NL@%
  28963. {%@NL@%
  28964.     %@AB@%/* Display message until key is pressed. Use getch to throw key away. */%@AE@%%@NL@%
  28965.     while( !kbhit() )%@NL@%
  28966.         cputs( "Hit me!! " );%@NL@%
  28967.     getch();%@NL@%
  28968. }%@NL@%
  28969. %@NL@%
  28970. %@NL@%
  28971. %@2@%%@AH@%KEYBRD.C%@AE@%%@EH@%%@NL@%
  28972. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\UTILITY\KEYBRD.C%@AE@%%@NL@%
  28973. %@NL@%
  28974. %@AB@%/* KEYBRD.C illustrates:%@NL@%
  28975. %@AB@% *      _bios_keybrd%@NL@%
  28976. %@AB@% */%@AE@%%@NL@%
  28977. %@NL@%
  28978. %@AI@%#include %@AE@%<bios.h> %@NL@%
  28979. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  28980. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  28981. %@NL@%
  28982. main()%@NL@%
  28983. {%@NL@%
  28984.     unsigned key, shift;%@NL@%
  28985.     unsigned char scan, ascii = 0;%@NL@%
  28986. %@NL@%
  28987.     %@AB@%/* Read and display keys until ESC is pressed. */%@AE@%%@NL@%
  28988.     while( ascii != 27 )%@NL@%
  28989.     {%@NL@%
  28990.         %@AB@%/* Drain any keys in the keyboard type-ahead buffer, then get%@NL@%
  28991. %@AB@%         * the current key. If you want the last key typed rather than%@NL@%
  28992. %@AB@%         * the key currently being typed, omit the initial loop.%@NL@%
  28993. %@AB@%         */%@AE@%%@NL@%
  28994.         while( _bios_keybrd( _KEYBRD_READY ) )%@NL@%
  28995.             _bios_keybrd( _KEYBRD_READ );%@NL@%
  28996.         key = _bios_keybrd( _KEYBRD_READ );%@NL@%
  28997. %@NL@%
  28998.         %@AB@%/* Get shift state. */%@AE@%%@NL@%
  28999.         shift = _bios_keybrd( _KEYBRD_SHIFTSTATUS );%@NL@%
  29000. %@NL@%
  29001.         %@AB@%/* Split key into scan and ascii parts. */%@AE@%%@NL@%
  29002.         scan = key >> 8;%@NL@%
  29003.         ascii = key & 0x00ff;%@NL@%
  29004. %@NL@%
  29005.         %@AB@%/* Categorize key. */%@AE@%%@NL@%
  29006.         if( ascii )%@NL@%
  29007.             printf( "ASCII: yes\tChar: %c \t",%@NL@%
  29008.                     isgraph( ascii ) ? ascii : ' ' );%@NL@%
  29009.         else%@NL@%
  29010.             printf( "ASCII: no\tChar: NA\t" );%@NL@%
  29011.         printf( "Code: %.2X\tScan: %.2X\t Shift: %.2X\n",%@NL@%
  29012.                 ascii, scan, shift );%@NL@%
  29013.     }%@NL@%
  29014. }%@NL@%
  29015. %@NL@%
  29016. %@NL@%
  29017. %@2@%%@AH@%LOCK.C%@AE@%%@EH@%%@NL@%
  29018. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\LOCK.C%@AE@%%@NL@%
  29019. %@NL@%
  29020. %@AB@%/* LOCK.C illustrates network file sharing functions:%@NL@%
  29021. %@AB@% *      sopen       locking%@NL@%
  29022. %@AB@% *%@NL@%
  29023. %@AB@% * Also the global variable:%@NL@%
  29024. %@AB@% *      _osmajor%@NL@%
  29025. %@AB@% *%@NL@%
  29026. %@AB@% * The program requires DOS 3.0 or higher. The DOS SHARE command must%@NL@%
  29027. %@AB@% * be loaded. The locking mechanism will only work if the program is%@NL@%
  29028. %@AB@% * run from a network drive.%@NL@%
  29029. %@AB@% */%@AE@%%@NL@%
  29030. %@NL@%
  29031. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  29032. %@AI@%#include %@AE@%<conio.h> %@NL@%
  29033. %@AI@%#include %@AE@%<io.h> %@NL@%
  29034. %@AI@%#include %@AE@%<fcntl.h> %@NL@%
  29035. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  29036. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  29037. %@AI@%#include %@AE@%<sys\locking.h> %@NL@%
  29038. %@AI@%#include %@AE@%<share.h> %@NL@%
  29039. <stdlib.h>         %@AB@%/* For _osmajor and exit */%@AE@%%@NL@%
  29040. %@NL@%
  29041. void error( char *msg );%@NL@%
  29042. %@NL@%
  29043. main( int argc, char *argv[] )%@NL@%
  29044. {%@NL@%
  29045.     int handle, i;%@NL@%
  29046.     static char msg[] = "Are any of these bytes locked?";%@NL@%
  29047.     char buf[1];%@NL@%
  29048. %@NL@%
  29049.     %@AB@%/* Check for DOS version >= 3.0 */%@AE@%%@NL@%
  29050.     if( _osmajor < 3 )%@NL@%
  29051.         error( "Must be DOS 3.0 or higher" );%@NL@%
  29052. %@NL@%
  29053.     %@AB@%/* If no argument, write file and lock some bytes in it. */%@AE@%%@NL@%
  29054.     if( argc == 1 )%@NL@%
  29055.     {%@NL@%
  29056.         %@AB@%/* Open file with deny none sharing. */%@AE@%%@NL@%
  29057.         handle = sopen( "tmpfil", O_BINARY | O_RDWR | O_CREAT,%@NL@%
  29058.                                   SH_DENYNO, S_IREAD | S_IWRITE );%@NL@%
  29059.         if( handle == -1 )%@NL@%
  29060.             error( "Can't open file\n" );%@NL@%
  29061. %@NL@%
  29062.         write( handle, msg, sizeof( msg ) - 1 );%@NL@%
  29063. %@NL@%
  29064.         %@AB@%/* Lock 10 bytes starting at byte 10. */%@AE@%%@NL@%
  29065.         lseek( handle, 10L, SEEK_SET );%@NL@%
  29066.         if( locking( handle, LK_LOCK, 10L ) )%@NL@%
  29067.             error( "Locking failed\n" );%@NL@%
  29068.         printf( "Locked 10 bytes starting at byte 10\n" );%@NL@%
  29069. %@NL@%
  29070.         system( "LOCK read" );%@NL@%
  29071.         getch();%@NL@%
  29072. %@NL@%
  29073.         %@AB@%/* Unlock. */%@AE@%%@NL@%
  29074.         lseek( handle, 10L, SEEK_SET );%@NL@%
  29075.         locking( handle, LK_UNLCK, 10L );%@NL@%
  29076.         printf( "\nUnlocked 10 bytes starting at byte 10\n" );%@NL@%
  29077. %@NL@%
  29078.         system( "LOCK read" );%@NL@%
  29079.         getch();%@NL@%
  29080.     }%@NL@%
  29081. %@NL@%
  29082.     %@AB@%/* If argument, Try to read some locked bytes. */%@AE@%%@NL@%
  29083.     else%@NL@%
  29084.     {%@NL@%
  29085.         %@AB@%/* Open file with deny none sharing. */%@AE@%%@NL@%
  29086.         handle = sopen( "tmpfil", O_BINARY | O_RDWR,%@NL@%
  29087.                                   SH_DENYNO, S_IREAD | S_IWRITE );%@NL@%
  29088.         if( handle == -1 )%@NL@%
  29089.             error( "Can't open file\n" );%@NL@%
  29090. %@NL@%
  29091.         for( i = 0; i < sizeof( msg ) - 1; i++ )%@NL@%
  29092.         {%@NL@%
  29093.             %@AB@%/* Print characters until locked bytes are reached. */%@AE@%%@NL@%
  29094.             if( read( handle, buf, 1 ) == -1 )%@NL@%
  29095.                 break;%@NL@%
  29096.             else%@NL@%
  29097.                 putchar( *buf );%@NL@%
  29098.         }%@NL@%
  29099.     }%@NL@%
  29100.     close( handle );%@NL@%
  29101.     exit( 0 );%@NL@%
  29102. }%@NL@%
  29103. %@NL@%
  29104. void error( char *errmsg )%@NL@%
  29105. {%@NL@%
  29106.     printf( errmsg );%@NL@%
  29107.     exit( 1 );%@NL@%
  29108. }%@NL@%
  29109. %@NL@%
  29110. %@NL@%
  29111. %@2@%%@AH@%MATH.C%@AE@%%@EH@%%@NL@%
  29112. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\MATH\MATH.C%@AE@%%@NL@%
  29113. %@NL@%
  29114. %@AB@%/* MATH.C illustrates floating point math functions including:%@NL@%
  29115. %@AB@% *      exp             pow             sqrt            frexp%@NL@%
  29116. %@AB@% *      log             log10           ldexp           modf%@NL@%
  29117. %@AB@% *      ceil            floor           fabs            fmod%@NL@%
  29118. %@AB@% */%@AE@%%@NL@%
  29119. %@NL@%
  29120. %@AI@%#include %@AE@%<math.h> %@NL@%
  29121. %@AI@%#include %@AE@%<float.h> %@NL@%
  29122. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  29123. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  29124. %@NL@%
  29125. main()%@NL@%
  29126. {%@NL@%
  29127.     double x, rx, y;%@NL@%
  29128.     int n;%@NL@%
  29129. %@NL@%
  29130.     printf( "\nEnter a real number: " );%@NL@%
  29131.     scanf( "%lf", &x );%@NL@%
  29132. %@NL@%
  29133.     printf( "Mantissa: %2.2lf\tExponent: %d\n", frexp( x, &n ), n );%@NL@%
  29134.     printf( "Fraction: %2.2lf\tInteger: %lf\n", modf( x, &y ), y );%@NL@%
  29135. %@NL@%
  29136.     printf("\nFunction\tResult for %2.2f\n\n", x );%@NL@%
  29137.     if( (rx = exp( x )) && (errno != ERANGE) )%@NL@%
  29138.         printf( "exp\t\t%2.2f\n", rx );%@NL@%
  29139.     else%@NL@%
  29140.         errno = 0;%@NL@%
  29141.     if( x > 0.0 )%@NL@%
  29142.         printf( "log\t\t%2.2f\n", log( x ) );%@NL@%
  29143.     if( x > 0.0 )%@NL@%
  29144.         printf( "log10\t\t%2.2f\n", log10( x ) );%@NL@%
  29145.     if( x >= 0.0 )%@NL@%
  29146.         printf( "sqrt\t\t%2.2f\n", sqrt( x ) );%@NL@%
  29147.     printf( "ceil\t\t%2.2f\n", ceil( x ) );%@NL@%
  29148.     printf( "floor\t\t%2.2f\n", floor( x ) );%@NL@%
  29149.     printf( "fabs\t\t%2.2f\n", fabs( x ) );%@NL@%
  29150. %@NL@%
  29151.     printf( "\nEnter another real number: " );%@NL@%
  29152.     scanf( "%lf", &y );%@NL@%
  29153.     printf("\nFunction\tResult for %2.2f and %2.2f\n\n", x, y );%@NL@%
  29154.     printf( "fmod\t\t%2.2f\n", fmod( x, y ) );%@NL@%
  29155.     rx = pow( x, y );%@NL@%
  29156.     if( (errno != ERANGE) && (errno != EDOM) )%@NL@%
  29157.         printf( "pow\t\t%2.2f\n", rx );%@NL@%
  29158.     else%@NL@%
  29159.         errno = 0;%@NL@%
  29160.     rx = hypot( x, y );%@NL@%
  29161.     if( errno != ERANGE )%@NL@%
  29162.         printf( "hypot\t\t%2.2f\n", hypot( x, y ) );%@NL@%
  29163.     else%@NL@%
  29164.         errno = 0;%@NL@%
  29165. %@NL@%
  29166.     printf( "\nEnter an integer exponent: " );%@NL@%
  29167.     scanf( "%d", &n );%@NL@%
  29168.     rx = ldexp( x, n );%@NL@%
  29169.     if( errno != ERANGE )%@NL@%
  29170.     {%@NL@%
  29171.         printf("\nFunction\tResult for %2.2f to power %d\n\n", x, n );%@NL@%
  29172.         printf( "ldexp\t\t%2.2f\n", ldexp( x, n ) );%@NL@%
  29173.     }%@NL@%
  29174. }%@NL@%
  29175. %@NL@%
  29176. %@NL@%
  29177. %@2@%%@AH@%MATHERR.C%@AE@%%@EH@%%@NL@%
  29178. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\MATH\MATHERR.C%@AE@%%@NL@%
  29179. %@NL@%
  29180. %@AB@%/* MATHERR.C illustrates writing an error routine for math functions.%@NL@%
  29181. %@AB@% * The error function must be:%@NL@%
  29182. %@AB@% *      matherr%@NL@%
  29183. %@AB@% *%@NL@%
  29184. %@AB@% * To use matherr, you must turn off the Extended Dictionary flag within%@NL@%
  29185. %@AB@% * the QC environment (Options-Make-Linker Flags) or use the /NOE linker%@NL@%
  29186. %@AB@% * option outside the environment. For example:%@NL@%
  29187. %@AB@% *      QCL matherr.c /link /NOE%@NL@%
  29188. %@AB@% */%@AE@%%@NL@%
  29189. %@NL@%
  29190. %@AI@%#include %@AE@%<math.h> %@NL@%
  29191. %@AI@%#include %@AE@%<string.h> %@NL@%
  29192. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  29193. %@NL@%
  29194. main()%@NL@%
  29195. {%@NL@%
  29196.     %@AB@%/* Do several math operations that cause errors. The matherr%@NL@%
  29197. %@AB@%     * routine handles DOMAIN errors, but lets the system handle%@NL@%
  29198. %@AB@%     * other errors normally.%@NL@%
  29199. %@AB@%     */%@AE@%%@NL@%
  29200.     printf( "log( -2.0 ) = %e\n", log( -2.0 ) );%@NL@%
  29201.     printf( "log10( -5.0 ) = %e\n", log10( -5.0 ) );%@NL@%
  29202.     printf( "log( 0.0 ) = %e\n", log( 0.0 ) );%@NL@%
  29203. }%@NL@%
  29204. %@NL@%
  29205. %@AB@%/* Routine to handle several math errors caused by passing a negative%@NL@%
  29206. %@AB@% * argument to log or log10 (DOMAIN errors). If this happens, matherr%@NL@%
  29207. %@AB@% * returns the natural or base-10 logarithm of the absolute value of%@NL@%
  29208. %@AB@% * the argument and suppresses the usual error message.%@NL@%
  29209. %@AB@% */%@AE@%%@NL@%
  29210. int matherr( struct exception *except )%@NL@%
  29211. {%@NL@%
  29212.     %@AB@%/* Handle DOMAIN errors for log or log10. */%@AE@%%@NL@%
  29213.     if( except->type == DOMAIN )%@NL@%
  29214.     {%@NL@%
  29215.         if( strcmp( except->name, "log" ) == 0 )%@NL@%
  29216.         {%@NL@%
  29217.             except->retval = log( -(except->arg1) );%@NL@%
  29218.             printf( "Special: using absolute value: %s: DOMAIN error\n",%@NL@%
  29219.                     except->name );%@NL@%
  29220.             return 1;%@NL@%
  29221.         }%@NL@%
  29222.         else if( strcmp( except->name, "log10" ) == 0 )%@NL@%
  29223.         {%@NL@%
  29224.             except->retval = log10( -(except->arg1) );%@NL@%
  29225.             printf( "Special: using absolute value: %s: DOMAIN error\n",%@NL@%
  29226.                     except->name );%@NL@%
  29227.             return 1;%@NL@%
  29228.         }%@NL@%
  29229.     }%@NL@%
  29230.     else%@NL@%
  29231.     {%@NL@%
  29232.         printf( "Normal: " );%@NL@%
  29233.         return 0;    %@AB@%/* Else use the default actions */%@AE@%%@NL@%
  29234.     }%@NL@%
  29235. }%@NL@%
  29236. %@NL@%
  29237. %@NL@%
  29238. %@2@%%@AH@%MENU.C%@AE@%%@EH@%%@NL@%
  29239. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\UTILITY\MENU.C%@AE@%%@NL@%
  29240. %@NL@%
  29241. %@AB@%/* MENU - Module of functions to put menus on the screen and handle keyboard%@NL@%
  29242. %@AB@% * input. To use it, include the MENU.H file in your program. The following%@NL@%
  29243. %@AB@% * functions are public:%@NL@%
  29244. %@AB@% *%@NL@%
  29245. %@AB@% *   Menu       -   Puts a menu on screen and reads input for it%@NL@%
  29246. %@AB@% *   Box        -   Puts a box on screen (fill it yourself)%@NL@%
  29247. %@AB@% *   GetKey     -   Gets ASCII or function key%@NL@%
  29248. %@AB@% *   _outchar   -   Displays character using current text position and color%@NL@%
  29249. %@AB@% *%@NL@%
  29250. %@AB@% * The following structures are defined:%@NL@%
  29251. %@AB@% *%@NL@%
  29252. %@AB@% *   MENU       -   Defines menu colors, box type, and centering%@NL@%
  29253. %@AB@% *   ITEM       -   Defines text of menu item and index of highlight character%@NL@%
  29254. %@AB@% *%@NL@%
  29255. %@AB@% * The global variable "mnuAtrib" has type MENU. Change this variable to%@NL@%
  29256. %@AB@% * change menu appearance.%@NL@%
  29257. %@AB@% */%@AE@%%@NL@%
  29258. %@NL@%
  29259. %@AI@%#include %@AE@%<string.h> %@NL@%
  29260. %@AI@%#include %@AE@%<stddef.h> %@NL@%
  29261. %@AI@%#include %@AE@%<ctype.h> %@NL@%
  29262. %@AI@%#include %@AE@%<graph.h> %@NL@%
  29263. %@AI@%#include %@AE@%<bios.h> %@NL@%
  29264. %@AI@%#include %@AE@%"menu.h" %@NL@%
  29265. %@NL@%
  29266. %@AB@%/* Default menu attribute. The default works for color or B&W. You can%@NL@%
  29267. %@AB@% * override the default value by defining your own MENU variable and%@NL@%
  29268. %@AB@% * assigning it to mnuAtrib. Or you can modify specific fields at%@NL@%
  29269. %@AB@% * run time. For example, you could use a different attribute for color%@NL@%
  29270. %@AB@% * than for black and white.%@NL@%
  29271. %@AB@% */%@AE@%%@NL@%
  29272. struct MENU mnuAtrib =%@NL@%
  29273. {%@NL@%
  29274.     _TBLACK, _TBLACK, _TWHITE, _TBRIGHTWHITE, _TBRIGHTWHITE,%@NL@%
  29275.     _TWHITE, _TWHITE, _TBLACK, _TWHITE, _TBLACK,%@NL@%
  29276.     TRUE,%@NL@%
  29277.     '┌', '┐', '┘', '└', '│', '─'%@NL@%
  29278. };%@NL@%
  29279. %@NL@%
  29280. %@AB@%/* Menu - Puts menu on screen and reads menu input from keyboard. When a%@NL@%
  29281. %@AB@% * highlighted hot key or ENTER is pressed, returns the index of the%@NL@%
  29282. %@AB@% * selected menu item.%@NL@%
  29283. %@AB@% *%@NL@%
  29284. %@AB@% * Params: row and col - If "fCentered" attribute of "mnuAtrib" is true,%@NL@%
  29285. %@AB@% *           center row and column of menu; otherwise top left of menu%@NL@%
  29286. %@AB@% *         aItem - array of structure containing the text of each item%@NL@%
  29287. %@AB@% *           and the index of the highlighted hot key%@NL@%
  29288. %@AB@% *         iCur - index of the current selection--pass 0 for first item,%@NL@%
  29289. %@AB@% *           or maintain a static value%@NL@%
  29290. %@AB@% *%@NL@%
  29291. %@AB@% * Return: The index of the selected item%@NL@%
  29292. %@AB@% *%@NL@%
  29293. %@AB@% * Uses:   mnuAtrib%@NL@%
  29294. %@AB@% */%@AE@%%@NL@%
  29295. int Menu( int row, int col, struct ITEM aItem[], int iCur )%@NL@%
  29296. {%@NL@%
  29297.     int cItem, cchItem = 2; %@AB@%/* Counts of items and chars per item       */%@AE@%%@NL@%
  29298.     int i, iPrev;           %@AB@%/* Indexes - temporary and previous         */%@AE@%%@NL@%
  29299.     int acchItem[MAXITEM];  %@AB@%/* Array of counts of character in items    */%@AE@%%@NL@%
  29300.     char *pchT;             %@AB@%/* Temporary character pointer              */%@AE@%%@NL@%
  29301.     char achHilite[36];     %@AB@%/* Array for highlight characters           */%@AE@%%@NL@%
  29302.     unsigned uKey;          %@AB@%/* Unsigned key code                        */%@AE@%%@NL@%
  29303.     long bgColor;           %@AB@%/* Screen color, position, and cursor       */%@AE@%%@NL@%
  29304.     short fgColor;%@NL@%
  29305.     struct rccoord rc;%@NL@%
  29306.     unsigned fCursor;%@NL@%
  29307. %@NL@%
  29308.     %@AB@%/* Save screen information. */%@AE@%%@NL@%
  29309.     fCursor = _displaycursor( _GCURSOROFF );%@NL@%
  29310.     bgColor = _getbkcolor();%@NL@%
  29311.     fgColor = _gettextcolor();%@NL@%
  29312.     rc = _gettextposition();%@NL@%
  29313. %@NL@%
  29314.     %@AB@%/* Count items, find longest, and put count of each in array. Also,%@NL@%
  29315. %@AB@%     * put the highlighted character from each in a string.%@NL@%
  29316. %@AB@%     */%@AE@%%@NL@%
  29317.     for( cItem = 0; aItem[cItem].achItem[0]; cItem++ )%@NL@%
  29318.     {%@NL@%
  29319.         acchItem[cItem] = strlen( aItem[cItem].achItem );%@NL@%
  29320.         cchItem = (acchItem[cItem] > cchItem) ? acchItem[cItem] : cchItem;%@NL@%
  29321.         i = aItem[cItem].iHilite;%@NL@%
  29322.         achHilite[cItem] = aItem[cItem].achItem[i];%@NL@%
  29323.     }%@NL@%
  29324.     cchItem += 2;%@NL@%
  29325.     achHilite[cItem] = 0;          %@AB@%/* Null-terminate and lowercase string  */%@AE@%%@NL@%
  29326.     strlwr( achHilite );%@NL@%
  29327. %@NL@%
  29328.     %@AB@%/* Adjust if centered, and draw menu box. */%@AE@%%@NL@%
  29329.     if( mnuAtrib.fCentered )%@NL@%
  29330.     {%@NL@%
  29331.         row -= cItem / 2;%@NL@%
  29332.         col -= cchItem / 2;%@NL@%
  29333.     }%@NL@%
  29334.     Box( row++, col++, cItem, cchItem );%@NL@%
  29335. %@NL@%
  29336.     %@AB@%/* Put items on menu. */%@AE@%%@NL@%
  29337.     for( i = 0; i < cItem; i++ )%@NL@%
  29338.     {%@NL@%
  29339.         if( i == iCur )%@NL@%
  29340.             Itemize( row + i, col, TRUE, aItem[i], cchItem - acchItem[i] );%@NL@%
  29341.         else%@NL@%
  29342.             Itemize( row + i, col, FALSE, aItem[i], cchItem - acchItem[i] );%@NL@%
  29343.     }%@NL@%
  29344. %@NL@%
  29345.     while( TRUE )%@NL@%
  29346.     {%@NL@%
  29347.         %@AB@%/* Wait until a uKey is pressed, then evaluate it. */%@AE@%%@NL@%
  29348.         uKey = GetKey( WAIT );%@NL@%
  29349.         switch( uKey )%@NL@%
  29350.         {%@NL@%
  29351.             case U_UP:                      %@AB@%/* Up key       */%@AE@%%@NL@%
  29352.                 iPrev = iCur;%@NL@%
  29353.                 iCur = (iCur > 0) ? (--iCur % cItem) : cItem - 1;%@NL@%
  29354.                 break;%@NL@%
  29355.             case U_DN:                      %@AB@%/* Down key     */%@AE@%%@NL@%
  29356.                 iPrev = iCur;%@NL@%
  29357.                 iCur = (iCur < cItem) ? (++iCur % cItem) : 0;%@NL@%
  29358.                 break;%@NL@%
  29359.             default:%@NL@%
  29360.                 if( uKey > 256 )             %@AB@%/* Ignore unknown function key  */%@AE@%%@NL@%
  29361.                     continue;%@NL@%
  29362.                 pchT = strchr( achHilite, (char)tolower( uKey ) );%@NL@%
  29363.                 if( pchT != NULL )          %@AB@%/* If in highlight string,      */%@AE@%%@NL@%
  29364.                     iCur = pchT - achHilite;%@AB@%/*   evaluate and fall through  */%@AE@%%@NL@%
  29365.                 else%@NL@%
  29366.                     continue;               %@AB@%/* Ignore unkown ASCII key      */%@AE@%%@NL@%
  29367.             case ENTER:%@NL@%
  29368.                 _setbkcolor( bgColor );%@NL@%
  29369.                 _settextcolor( fgColor );%@NL@%
  29370.                 _settextposition( rc.row, rc.col );%@NL@%
  29371.                 _displaycursor( fCursor );%@NL@%
  29372.                 return iCur;%@NL@%
  29373.         }%@NL@%
  29374.         %@AB@%/* Redisplay current and previous. */%@AE@%%@NL@%
  29375.         Itemize( row + iCur, col, TRUE, aItem[iCur], cchItem - acchItem[iCur] );%@NL@%
  29376.         Itemize( row + iPrev, col, FALSE, aItem[iPrev], cchItem - acchItem[iPrev] );%@NL@%
  29377.     }%@NL@%
  29378. }%@NL@%
  29379. %@NL@%
  29380. %@AB@%/* Box - Draw menu box, filling interior with blanks of the border color.%@NL@%
  29381. %@AB@% *%@NL@%
  29382. %@AB@% * Params: row and col - upper left of box%@NL@%
  29383. %@AB@% *         rowLast and colLast - height and width%@NL@%
  29384. %@AB@% *%@NL@%
  29385. %@AB@% * Return: None%@NL@%
  29386. %@AB@% *%@NL@%
  29387. %@AB@% * Uses:   mnuAtrib%@NL@%
  29388. %@AB@% */%@AE@%%@NL@%
  29389. void Box( int row, int col, int rowLast, int colLast )%@NL@%
  29390. {%@NL@%
  29391.     int i;%@NL@%
  29392.     char achT[MAXITEM + 2];         %@AB@%/* Temporary array of characters */%@AE@%%@NL@%
  29393. %@NL@%
  29394.     %@AB@%/* Set color and position. */%@AE@%%@NL@%
  29395.     _settextposition( row, col );%@NL@%
  29396.     _settextcolor( mnuAtrib.fgBorder );%@NL@%
  29397.     _setbkcolor( mnuAtrib.bgBorder );%@NL@%
  29398. %@NL@%
  29399.     %@AB@%/* Draw box top. */%@AE@%%@NL@%
  29400.     achT[0] = mnuAtrib.chNW;%@NL@%
  29401.     memset( achT + 1, mnuAtrib.chEW, colLast );%@NL@%
  29402.     achT[colLast + 1] = mnuAtrib.chNE;%@NL@%
  29403.     achT[colLast + 2] = 0;%@NL@%
  29404.     _outtext( achT );%@NL@%
  29405. %@NL@%
  29406.     %@AB@%/* Draw box sides and center. */%@AE@%%@NL@%
  29407.     achT[0] = mnuAtrib.chNS;%@NL@%
  29408.     memset( achT + 1, ' ', colLast );%@NL@%
  29409.     achT[colLast + 1] = mnuAtrib.chNS;%@NL@%
  29410.     achT[colLast + 2] = 0;%@NL@%
  29411.     for( i = 1; i <= rowLast; ++i )%@NL@%
  29412.     {%@NL@%
  29413.         _settextposition( row + i, col );%@NL@%
  29414.         _outtext( achT );%@NL@%
  29415.     }%@NL@%
  29416. %@NL@%
  29417.     %@AB@%/* Draw box bottom. */%@AE@%%@NL@%
  29418.     _settextposition( row + rowLast + 1, col );%@NL@%
  29419.     achT[0] = mnuAtrib.chSW;%@NL@%
  29420.     memset( achT + 1, mnuAtrib.chEW, colLast );%@NL@%
  29421.     achT[colLast + 1] = mnuAtrib.chSE;%@NL@%
  29422.     achT[colLast + 2] = 0;%@NL@%
  29423.     _outtext( achT );%@NL@%
  29424. }%@NL@%
  29425. %@NL@%
  29426. %@AB@%/* Itemize - Display one (item) selection of a menu. This function%@NL@%
  29427. %@AB@% * is normally only used internally by Menu.%@NL@%
  29428. %@AB@% *%@NL@%
  29429. %@AB@% * Params: row and col - top left of menu%@NL@%
  29430. %@AB@% *         fCur - flag set if item is current selection%@NL@%
  29431. %@AB@% *         itm - structure containing item text and index of highlight%@NL@%
  29432. %@AB@% *         cBlank - count of blanks to fill%@NL@%
  29433. %@AB@% *%@NL@%
  29434. %@AB@% * Return: none%@NL@%
  29435. %@AB@% *%@NL@%
  29436. %@AB@% * Uses:   mnuAtrib%@NL@%
  29437. %@AB@% */%@AE@%%@NL@%
  29438. void Itemize( int row, int col, int fCur, struct ITEM itm, int cBlank )%@NL@%
  29439. {%@NL@%
  29440.     int i;%@NL@%
  29441.     char achT[MAXITEM];             %@AB@%/* Temporary array of characters */%@AE@%%@NL@%
  29442. %@NL@%
  29443.     %@AB@%/* Set text position and color. */%@AE@%%@NL@%
  29444.     _settextposition( row, col );%@NL@%
  29445.     if( fCur )%@NL@%
  29446.     {%@NL@%
  29447.         _settextcolor( mnuAtrib.fgSelect );%@NL@%
  29448.         _setbkcolor( mnuAtrib.bgSelect );%@NL@%
  29449.     }%@NL@%
  29450.     else%@NL@%
  29451.     {%@NL@%
  29452.         _settextcolor( mnuAtrib.fgNormal );%@NL@%
  29453.         _setbkcolor( mnuAtrib.bgNormal );%@NL@%
  29454.     }%@NL@%
  29455. %@NL@%
  29456.     %@AB@%/* Display item and fill blanks. */%@AE@%%@NL@%
  29457.     strcat( strcpy( achT, " " ), itm.achItem );%@NL@%
  29458.     _outtext( achT );%@NL@%
  29459.     memset( achT, ' ', cBlank-- );%@NL@%
  29460.     achT[cBlank] = 0;%@NL@%
  29461.     _outtext( achT );%@NL@%
  29462. %@NL@%
  29463.     %@AB@%/* Set position and color of highlight character, then display it. */%@AE@%%@NL@%
  29464.     i = itm.iHilite;%@NL@%
  29465.     _settextposition( row, col + i + 1 );%@NL@%
  29466.     if( fCur )%@NL@%
  29467.     {%@NL@%
  29468.         _settextcolor( mnuAtrib.fgSelHilite );%@NL@%
  29469.         _setbkcolor( mnuAtrib.bgSelHilite );%@NL@%
  29470.     }%@NL@%
  29471.     else%@NL@%
  29472.     {%@NL@%
  29473.         _settextcolor( mnuAtrib.fgNormHilite );%@NL@%
  29474.         _setbkcolor( mnuAtrib.bgNormHilite );%@NL@%
  29475.     }%@NL@%
  29476.     _outchar( itm.achItem[i] );%@NL@%
  29477. }%@NL@%
  29478. %@NL@%
  29479. %@AB@%/* GetKey - Gets a key from the keyboard. This routine distinguishes%@NL@%
  29480. %@AB@% * between ASCII keys and function or control keys with different shift%@NL@%
  29481. %@AB@% * states. It also accepts a flag to return immediately if no key is%@NL@%
  29482. %@AB@% * available.%@NL@%
  29483. %@AB@% *%@NL@%
  29484. %@AB@% * Params: fWait - Code to indicate how to handle keyboard buffer:%@NL@%
  29485. %@AB@% *   NO_WAIT     Return 0 if no key in buffer, else return key%@NL@%
  29486. %@AB@% *   WAIT        Return first key if available, else wait for key%@NL@%
  29487. %@AB@% *   CLEAR_WAIT  Throw away any key in buffer and wait for new key%@NL@%
  29488. %@AB@% *%@NL@%
  29489. %@AB@% * Return: One of the following:%@NL@%
  29490. %@AB@% *%@NL@%
  29491. %@AB@% *   Keytype                                High Byte    Low Byte%@NL@%
  29492. %@AB@% *   -------                                ---------    --------%@NL@%
  29493. %@AB@% *   No key available (only with NO_WAIT)       0           0%@NL@%
  29494. %@AB@% *   ASCII value                                0        ASCII code%@NL@%
  29495. %@AB@% *   Unshifted function or keypad               1        scan code%@NL@%
  29496. %@AB@% *   Shifted function or keypad                 2        scan code%@NL@%
  29497. %@AB@% *   CTRL function or keypad                    3        scan code%@NL@%
  29498. %@AB@% *   ALT function or keypad                     4        scan code%@NL@%
  29499. %@AB@% *%@NL@%
  29500. %@AB@% * Note:   getkey cannot return codes for keys not recognized by BIOS%@NL@%
  29501. %@AB@% *         int 16, such as the CTRL-UP or the 5 key on the numeric keypad.%@NL@%
  29502. %@AB@% */%@AE@%%@NL@%
  29503. unsigned GetKey( int fWait )%@NL@%
  29504. {%@NL@%
  29505.     unsigned uKey, uShift;%@NL@%
  29506. %@NL@%
  29507.     %@AB@%/* If CLEAR_WAIT, drain the keyboard buffer. */%@AE@%%@NL@%
  29508.     if( fWait == CLEAR_WAIT )%@NL@%
  29509.         while( _bios_keybrd( _KEYBRD_READY ) )%@NL@%
  29510.             _bios_keybrd( _KEYBRD_READ );%@NL@%
  29511. %@NL@%
  29512.     %@AB@%/* If NO_WAIT, return 0 if there is no key ready. */%@AE@%%@NL@%
  29513.     if( !fWait && !_bios_keybrd( _KEYBRD_READY ) )%@NL@%
  29514.         return FALSE;%@NL@%
  29515. %@NL@%
  29516.     %@AB@%/* Get key code. */%@AE@%%@NL@%
  29517.     uKey = _bios_keybrd( _KEYBRD_READ );%@NL@%
  29518. %@NL@%
  29519.     %@AB@%/* If low byte is not zero, its an ASCII key. Check scan code to see%@NL@%
  29520. %@AB@%     * if it's on the numeric keypad. If not, clear high byte and return.%@NL@%
  29521. %@AB@%     */%@AE@%%@NL@%
  29522.     if( uKey & 0x00ff )%@NL@%
  29523.         if( (uKey >> 8) < 69 )%@NL@%
  29524.             return( uKey & 0x00ff );%@NL@%
  29525. %@NL@%
  29526.     %@AB@%/* For function keys and numeric keypad, put scan code in low byte%@NL@%
  29527. %@AB@%     * and shift state codes in high byte.%@NL@%
  29528. %@AB@%     */%@AE@%%@NL@%
  29529.     uKey >>= 8;%@NL@%
  29530.     uShift = _bios_keybrd( _KEYBRD_SHIFTSTATUS ) & 0x000f;%@NL@%
  29531.     switch( uShift )%@NL@%
  29532.     {%@NL@%
  29533.         case 0:%@NL@%
  29534.             return( 0x0100 | uKey );  %@AB@%/* None (1)    */%@AE@%%@NL@%
  29535.         case 1:%@NL@%
  29536.         case 2:%@NL@%
  29537.         case 3:%@NL@%
  29538.             return( 0x0200 | uKey );  %@AB@%/* Shift (2)   */%@AE@%%@NL@%
  29539.         case 4:%@NL@%
  29540.             return( 0x0300 | uKey );  %@AB@%/* Control (3) */%@AE@%%@NL@%
  29541.         case 8:%@NL@%
  29542.             return( 0x0400 | uKey );  %@AB@%/* Alt (4)     */%@AE@%%@NL@%
  29543.     }%@NL@%
  29544. }%@NL@%
  29545. %@NL@%
  29546. %@AB@%/* _outchar - Display a character. This is the character equivalent of%@NL@%
  29547. %@AB@% * _outtext. It is affected by _settextposition, _settextcolor, and%@NL@%
  29548. %@AB@% * _setbkcolor. It should not be used in loops. Build strings and then%@NL@%
  29549. %@AB@% * _outtext to show multiple characters.%@NL@%
  29550. %@AB@% *%@NL@%
  29551. %@AB@% * Params: out - character to be displayed%@NL@%
  29552. %@AB@% *%@NL@%
  29553. %@AB@% * Return: none%@NL@%
  29554. %@AB@% */%@AE@%%@NL@%
  29555. void _outchar( char out )%@NL@%
  29556. {%@NL@%
  29557.     static char achT[2] = " ";      %@AB@%/* Temporary array of characters */%@AE@%%@NL@%
  29558. %@NL@%
  29559.     achT[0] = out;%@NL@%
  29560.     _outtext( achT );%@NL@%
  29561. }%@NL@%
  29562. %@NL@%
  29563. %@NL@%
  29564. %@2@%%@AH@%MKFPSTR.C%@AE@%%@EH@%%@NL@%
  29565. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\MKFPSTR.C%@AE@%%@NL@%
  29566. %@NL@%
  29567. %@AB@%/* MKFPSTR.C illustrates building and displaying floating point strings%@NL@%
  29568. %@AB@% * without printf. Functions illustrated include:%@NL@%
  29569. %@AB@% *      strcat          strncat         cscanf          fcvt%@NL@%
  29570. %@AB@% */%@AE@%%@NL@%
  29571. %@NL@%
  29572. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  29573. %@AI@%#include %@AE@%<conio.h> %@NL@%
  29574. %@AI@%#include %@AE@%<string.h> %@NL@%
  29575. %@NL@%
  29576. main()%@NL@%
  29577. {%@NL@%
  29578.     int decimal, sign;%@NL@%
  29579.     int precision = 7;%@NL@%
  29580.     static char numbuf[50] = ""; %@NL@%
  29581.     char *pnumstr, tmpbuf[50];%@NL@%
  29582.     double number1, number2;%@NL@%
  29583. %@NL@%
  29584.     cputs( "Enter two floating point numbers: " );%@NL@%
  29585.     cscanf( "%lf %lf", &number1, &number2 );%@NL@%
  29586.     putch( '\n' );%@NL@%
  29587. %@NL@%
  29588.     %@AB@%/* Use information from fcvt to format number string. */%@AE@%%@NL@%
  29589.     pnumstr = fcvt( number1 + number2, precision, &decimal, &sign );%@NL@%
  29590. %@NL@%
  29591.     %@AB@%/* Start with sign if negative. */%@AE@%%@NL@%
  29592.     if( sign )%@NL@%
  29593.         strcat( numbuf, "-" );%@NL@%
  29594. %@NL@%
  29595.     if( decimal <= 0 )%@NL@%
  29596.     {%@NL@%
  29597.         %@AB@%/* If decimal is left of first digit (decimal negative), put%@NL@%
  29598. %@AB@%         * in leading zeros, then add digits.%@NL@%
  29599. %@AB@%         */%@AE@%%@NL@%
  29600.         strcat( numbuf, "0." );%@NL@%
  29601.         memset( tmpbuf, '0', (size_t)abs( decimal ) );%@NL@%
  29602.         tmpbuf[abs( decimal )] = 0;%@NL@%
  29603.         strcat( numbuf, tmpbuf );%@NL@%
  29604.         strcat( numbuf, pnumstr );%@NL@%
  29605.     }%@NL@%
  29606.     else%@NL@%
  29607.     {%@NL@%
  29608.         %@AB@%/* If decimal is right of first digit, put in leading digits.%@NL@%
  29609. %@AB@%         * then add decimal and trailing digits.%@NL@%
  29610. %@AB@%         */%@AE@%%@NL@%
  29611.         strncat( numbuf, pnumstr, (size_t)decimal );%@NL@%
  29612.         strcat( numbuf, "." );%@NL@%
  29613.         strcat( numbuf, pnumstr + decimal );%@NL@%
  29614.     }%@NL@%
  29615.     cputs( strcat( "Total is: ", numbuf ) );%@NL@%
  29616. }%@NL@%
  29617. %@NL@%
  29618. %@NL@%
  29619. %@2@%%@AH@%MORE.C%@AE@%%@EH@%%@NL@%
  29620. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\MORE.C%@AE@%%@NL@%
  29621. %@NL@%
  29622. %@AB@%/* MORE.C illustrates line input and output using:%@NL@%
  29623. %@AB@% *      gets            puts            isatty          fileno%@NL@%
  29624. %@AB@% *%@NL@%
  29625. %@AB@% * Like the DOS MORE command, it is a filter whose input and output can%@NL@%
  29626. %@AB@% * be redirected.%@NL@%
  29627. %@AB@% */%@AE@%%@NL@%
  29628. %@NL@%
  29629. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  29630. %@AI@%#include %@AE@%<bios.h> %@NL@%
  29631. %@AI@%#include %@AE@%<io.h> %@NL@%
  29632. %@NL@%
  29633. main()%@NL@%
  29634. {%@NL@%
  29635.     long line = 0, page = 0;%@NL@%
  29636.     char tmp[128];%@NL@%
  29637. %@NL@%
  29638.     %@AB@%/* Get each line from standard input and write to standard output. */%@AE@%%@NL@%
  29639.     while( 1 )%@NL@%
  29640.     {%@NL@%
  29641.         %@AB@%/* If standard out is the screen, wait for key after each screen. */%@AE@%%@NL@%
  29642.         if( isatty( fileno( stdout ) ) )%@NL@%
  29643.         {%@NL@%
  29644.             %@AB@%/* Wait for key every 23 lines. You must get the key directly%@NL@%
  29645. %@AB@%             * from the BIOS, since input is usually redirected.%@NL@%
  29646. %@AB@%             */%@AE@%%@NL@%
  29647.             if( !(++line % 23 ) )%@NL@%
  29648.                 _bios_keybrd( _KEYBRD_READ );%@NL@%
  29649.         }%@NL@%
  29650.         %@AB@%/* Must be redirected to file, so send a header every 58 lines. */%@AE@%%@NL@%
  29651.         else%@NL@%
  29652.         {%@NL@%
  29653.             if( !(line++ % 58) )%@NL@%
  29654.                 printf( "\f\nPage: %d\n\n", ++page );%@NL@%
  29655.         }%@NL@%
  29656. %@NL@%
  29657.         %@AB@%/* Get and put a line of text. */%@AE@%%@NL@%
  29658.         if( gets( tmp ) == NULL )%@NL@%
  29659.             break;%@NL@%
  29660.         puts( tmp );%@NL@%
  29661. %@NL@%
  29662.     }%@NL@%
  29663. }%@NL@%
  29664. %@NL@%
  29665. %@NL@%
  29666. %@2@%%@AH@%MOVEMEM.C%@AE@%%@EH@%%@NL@%
  29667. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\MEMORY\MOVEMEM.C%@AE@%%@NL@%
  29668. %@NL@%
  29669. %@AB@%/* MOVEMEM.C illustrate direct memory access using functions:%@NL@%
  29670. %@AB@% *      movedata        FP_SEG          FP_OFF%@NL@%
  29671. %@AB@% *%@NL@%
  29672. %@AB@% * Also illustrated:%@NL@%
  29673. %@AB@% *      pack pragma%@NL@%
  29674. %@AB@% *%@NL@%
  29675. %@AB@% * See COPY2.C for another example of FP_SEG.%@NL@%
  29676. %@AB@% */%@AE@%%@NL@%
  29677. %@NL@%
  29678. %@AI@%#include %@AE@%<memory.h> %@NL@%
  29679. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  29680. %@AI@%#include %@AE@%<dos.h> %@NL@%
  29681. %@NL@%
  29682. pack(1)     %@AB@%/* Use pragma to force packing on byte boundaries. */%@AE@%%@NL@%
  29683. %@NL@%
  29684. struct LOWMEMVID%@NL@%
  29685. {%@NL@%
  29686.     char     vidmode;       %@AB@%/* 0x449 */%@AE@%%@NL@%
  29687.     unsigned scrwid;        %@AB@%/* 0x44A */%@AE@%%@NL@%
  29688.     unsigned scrlen;        %@AB@%/* 0x44C */%@AE@%%@NL@%
  29689.     unsigned scroff;        %@AB@%/* 0x44E */%@AE@%%@NL@%
  29690.     struct   LOCATE%@NL@%
  29691.     {%@NL@%
  29692.         unsigned char col;%@NL@%
  29693.         unsigned char row;%@NL@%
  29694.     } csrpos[8];            %@AB@%/* 0x450 */%@AE@%%@NL@%
  29695.     struct   CURSIZE%@NL@%
  29696.     {%@NL@%
  29697.         unsigned char end;%@NL@%
  29698.         unsigned char start;%@NL@%
  29699.     } csrsize;              %@AB@%/* 0x460 */%@AE@%%@NL@%
  29700.     char     page;          %@AB@%/* 0x462 */%@AE@%%@NL@%
  29701. } vid;%@NL@%
  29702. struct LOWMEMVID far *pvid = &vid;%@NL@%
  29703. %@NL@%
  29704. main()%@NL@%
  29705. {%@NL@%
  29706.     int page;%@NL@%
  29707. %@NL@%
  29708.     %@AB@%/* Move system information into uninitialized structure variable. */%@AE@%%@NL@%
  29709.     movedata( 0, 0x449, FP_SEG( pvid ), FP_OFF( pvid ), sizeof( vid ) );%@NL@%
  29710. %@NL@%
  29711.     printf( "Move data from low memory 0000:0449 to structure at %Fp\n\n",%@NL@%
  29712.              (void far *)&vid );%@NL@%
  29713. %@NL@%
  29714.     printf( "Mode:\t\t\t%u\n", vid.vidmode );%@NL@%
  29715.     printf( "Page:\t\t\t%u\n", vid.page );%@NL@%
  29716.     printf( "Screen width:\t\t%u\n", vid.scrwid );%@NL@%
  29717.     printf( "Screen length:\t\t%u\n", vid.scrlen );%@NL@%
  29718.     printf( "Cursor size:\t\tstart: %u\tend: %u\n",%@NL@%
  29719.              vid.csrsize.start, vid.csrsize.end );%@NL@%
  29720.     printf( "Cursor location:\t" );%@NL@%
  29721.     for( page = 0; page < 8; page++ )%@NL@%
  29722.         printf( "page:\t%u\tcolumn: %u\trow: %u\n\t\t\t",%@NL@%
  29723.                 page, vid.csrpos[page].col, vid.csrpos[page].row );%@NL@%
  29724. }%@NL@%
  29725. %@NL@%
  29726. %@NL@%
  29727. %@2@%%@AH@%NFORMAT.C%@AE@%%@EH@%%@NL@%
  29728. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\NFORMAT.C%@AE@%%@NL@%
  29729. %@NL@%
  29730. %@AB@%/* NFORMAT.C: Prints numbers and a string. */%@AE@%%@NL@%
  29731. %@NL@%
  29732. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  29733. %@AI@%#include %@AE@%<string.h> %@NL@%
  29734. %@NL@%
  29735. main()%@NL@%
  29736. {%@NL@%
  29737.    int    a = -765,%@NL@%
  29738.           b = 1,%@NL@%
  29739.           c = 44000,%@NL@%
  29740.           d = 33;%@NL@%
  29741.    float  e = 1.33E8,%@NL@%
  29742.           f = -0.1234567,%@NL@%
  29743.           g = 12345.6789,%@NL@%
  29744.           h = 1.0;%@NL@%
  29745.    char   i[80];%@NL@%
  29746. %@NL@%
  29747.    strcpy( i, "word 1, word 2, word 3, word 4, word 5" );%@NL@%
  29748. %@NL@%
  29749.    printf( "Unformatted:\n%d %d %d %d\n", a, b, c, d );%@NL@%
  29750.    printf( "%f %f %f %f\n", e, f, g, h );%@NL@%
  29751.    printf( "%s\n", i );%@NL@%
  29752. }%@NL@%
  29753. %@NL@%
  29754. %@NL@%
  29755. %@2@%%@AH@%NOT.C%@AE@%%@EH@%%@NL@%
  29756. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\NOT.C%@AE@%%@NL@%
  29757. %@NL@%
  29758. %@AB@%/* NOT.C: Demonstrate logical NOT operator. */%@AE@%%@NL@%
  29759. %@NL@%
  29760. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  29761. %@NL@%
  29762. main()%@NL@%
  29763. {%@NL@%
  29764.   int val = 0;%@NL@%
  29765.   if( !val )%@NL@%
  29766.      printf( "val is zero" );%@NL@%
  29767. }%@NL@%
  29768. %@NL@%
  29769. %@NL@%
  29770. %@2@%%@AH@%NULLFILE.C%@AE@%%@EH@%%@NL@%
  29771. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\NULLFILE.C%@AE@%%@NL@%
  29772. %@NL@%
  29773. %@AB@%/* NULLFILE.C illustrates these functions:%@NL@%
  29774. %@AB@% *      chsize          umask           setmode%@NL@%
  29775. %@AB@% *      creat           fstat%@NL@%
  29776. %@AB@% */%@AE@%%@NL@%
  29777. %@NL@%
  29778. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  29779. %@AI@%#include %@AE@%<conio.h> %@NL@%
  29780. %@AI@%#include %@AE@%<io.h> %@NL@%
  29781. %@AI@%#include %@AE@%<fcntl.h> %@NL@%
  29782. %@AI@%#include %@AE@%<sys\types.h> %@NL@%
  29783. %@AI@%#include %@AE@%<sys\stat.h> %@NL@%
  29784. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  29785. %@AI@%#include %@AE@%<time.h> %@NL@%
  29786. %@NL@%
  29787. main()%@NL@%
  29788. {%@NL@%
  29789.     int fhandle;%@NL@%
  29790.     long fsize;%@NL@%
  29791.     struct stat fstatus;%@NL@%
  29792.     char fname[80];%@NL@%
  29793. %@NL@%
  29794.     %@AB@%/* Create a file of a specified length. */%@AE@%%@NL@%
  29795.     printf( "What dummy file do you want to create: " );%@NL@%
  29796.     gets( fname );%@NL@%
  29797.     if( !access( fname, 0 ) )%@NL@%
  29798.     {%@NL@%
  29799.         printf( "File exists" );%@NL@%
  29800.         exit( 1 );%@NL@%
  29801.     }%@NL@%
  29802. %@NL@%
  29803.     %@AB@%/* Mask out write permission. This means that a later call to open%@NL@%
  29804. %@AB@%     * will not be able to set write permission. This is not particularly%@NL@%
  29805. %@AB@%     * useful in DOS, but umask is provided primarily for compatibility%@NL@%
  29806. %@AB@%     * with systems (such as Unix) that allow multiple permission levels.%@NL@%
  29807. %@AB@%     */%@AE@%%@NL@%
  29808.     umask( S_IWRITE );%@NL@%
  29809. %@NL@%
  29810.     %@AB@%/* Despite write request, file is read only because of mask. */%@AE@%%@NL@%
  29811.     if( (fhandle = creat( fname, S_IREAD | S_IWRITE )) == -1 )%@NL@%
  29812.     {%@NL@%
  29813.         printf( "File can't be created" );%@NL@%
  29814.         exit( 1 );%@NL@%
  29815.     }%@NL@%
  29816. %@NL@%
  29817.     %@AB@%/* Since creat uses the default mode (usually text), you must%@NL@%
  29818. %@AB@%     * use setmode to make sure the mode is binary.%@NL@%
  29819. %@AB@%     */%@AE@%%@NL@%
  29820.     setmode( fhandle, O_BINARY );%@NL@%
  29821. %@NL@%
  29822.     printf( "How long do you want the file to be? " );%@NL@%
  29823.     scanf( "%ld", &fsize );%@NL@%
  29824.     chsize( fhandle, fsize );%@NL@%
  29825. %@NL@%
  29826.     %@AB@%/* Display statistics. */%@AE@%%@NL@%
  29827.     fstat( fhandle, &fstatus );%@NL@%
  29828.     printf( "File: %s\n", fname );%@NL@%
  29829.     printf( "Size: %ld\n", fstatus.st_size );%@NL@%
  29830.     printf( "Drive %c:\n", fstatus.st_dev + 'A' );%@NL@%
  29831.     printf( "Permission: %s\n",%@NL@%
  29832.             (fstatus.st_mode & S_IWRITE) ? "Read/Write" : "Read Only" );%@NL@%
  29833.     printf( "Created: %s", ctime( &fstatus.st_atime ) );%@NL@%
  29834. %@NL@%
  29835.     close( fhandle );%@NL@%
  29836.     exit( 0 );%@NL@%
  29837. }%@NL@%
  29838. %@NL@%
  29839. %@NL@%
  29840. %@2@%%@AH@%NUMTOA.C%@AE@%%@EH@%%@NL@%
  29841. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\NUMTOA.C%@AE@%%@NL@%
  29842. %@NL@%
  29843. %@AB@%/* NUMTOA.C illustrates number to string conversion functions including:%@NL@%
  29844. %@AB@% *      itoa            ltoa            ultoa%@NL@%
  29845. %@AB@% */%@AE@%%@NL@%
  29846. %@NL@%
  29847. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  29848. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  29849. %@NL@%
  29850. main()%@NL@%
  29851. {%@NL@%
  29852.     int  base, i;%@NL@%
  29853.     long l;%@NL@%
  29854.     unsigned long ul;%@NL@%
  29855.     char buffer[60];%@NL@%
  29856. %@NL@%
  29857.     printf( "Enter an integer: " );%@NL@%
  29858.     scanf( "%d", &i );%@NL@%
  29859.     for( base =  2; base <= 16; base += 2 )%@NL@%
  29860.     {%@NL@%
  29861.         itoa( i, buffer, base );%@NL@%
  29862.         if( base != 10 )%@NL@%
  29863.             printf( "%d in base %d is: %s\n", i, base, buffer );%@NL@%
  29864.     }%@NL@%
  29865. %@NL@%
  29866.     printf( "Enter a long integer: " );%@NL@%
  29867.     scanf( "%ld", &l );%@NL@%
  29868.     for( base =  2; base <= 16; base += 2 )%@NL@%
  29869.     {%@NL@%
  29870.         ltoa( l, buffer, base );%@NL@%
  29871.         if( base != 10 )%@NL@%
  29872.             printf( "%ld in base %d is: %s\n", l, base, buffer );%@NL@%
  29873.     }%@NL@%
  29874. %@NL@%
  29875.     printf( "Enter an unsigned long integer: " );%@NL@%
  29876.     scanf( "%lu", &ul );%@NL@%
  29877.     for( base =  2; base <= 16; base += 2 )%@NL@%
  29878.     {%@NL@%
  29879.         ultoa( ul, buffer, base );%@NL@%
  29880.         if( base != 10 )%@NL@%
  29881.             printf( "%lu in base %d is: %s\n", ul, base, buffer );%@NL@%
  29882.     }%@NL@%
  29883. }%@NL@%
  29884. %@NL@%
  29885. %@NL@%
  29886. %@2@%%@AH@%PAGE.C%@AE@%%@EH@%%@NL@%
  29887. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\PAGE.C%@AE@%%@NL@%
  29888. %@NL@%
  29889. %@AB@%/* PAGE.C illustrates video page functions including:%@NL@%
  29890. %@AB@% *      _getactivepage  _getvisualpage  _setactivepage  _setvisualpage%@NL@%
  29891. %@AB@% */%@AE@%%@NL@%
  29892. %@NL@%
  29893. %@AI@%#include %@AE@%<conio.h> %@NL@%
  29894. %@AI@%#include %@AE@%<graph.h> %@NL@%
  29895. %@AI@%#include %@AE@%<time.h> %@NL@%
  29896. %@NL@%
  29897. void delay( clock_t wait );         %@AB@%/* Prototype */%@AE@%%@NL@%
  29898. char *jumper[] = { { "\\o/\n O \n/ \\" },%@NL@%
  29899.                    { "_o_\n O \n( )"   },%@NL@%
  29900.                    { " o \n/O\\\n/ \\" },%@NL@%
  29901.                    { " o \n O \n( )"   }%@NL@%
  29902.                  };%@NL@%
  29903. main()%@NL@%
  29904. {%@NL@%
  29905.     int i, oldvpage, oldapage, oldcursor;%@NL@%
  29906. %@NL@%
  29907.     _clearscreen( _GCLEARSCREEN );%@NL@%
  29908.     oldcursor = _displaycursor( _GCURSOROFF );%@NL@%
  29909.     oldapage  = _getactivepage();%@NL@%
  29910.     oldvpage  = _getvisualpage();%@NL@%
  29911. %@NL@%
  29912.     %@AB@%/* Draw image on each page */%@AE@%%@NL@%
  29913.     for( i = 0; i < 4; i++ )%@NL@%
  29914.     {%@NL@%
  29915.         _setactivepage( i );%@NL@%
  29916.         _settextposition( 1, 1 );%@NL@%
  29917.         _outtext( jumper[i] );%@NL@%
  29918.     }%@NL@%
  29919. %@NL@%
  29920.     while( !kbhit() )%@NL@%
  29921.         %@AB@%/* Cycle through pages 0 to 3 */%@AE@%%@NL@%
  29922.         for( i = 0; i < 4; i++ )%@NL@%
  29923.         {%@NL@%
  29924.             _setvisualpage( i );%@NL@%
  29925.             delay( 100L );%@NL@%
  29926.         }%@NL@%
  29927.     getch();%@NL@%
  29928. %@NL@%
  29929.     %@AB@%/* Restore original page and cursor. */%@AE@%%@NL@%
  29930.     _displaycursor( oldcursor );%@NL@%
  29931.     _setactivepage( oldapage );%@NL@%
  29932.     _setvisualpage( oldvpage );%@NL@%
  29933. }%@NL@%
  29934. %@NL@%
  29935. %@AB@%/* delay - Pauses for a specified number of microseconds. */%@AE@%%@NL@%
  29936. void delay( clock_t wait )%@NL@%
  29937. {%@NL@%
  29938.     clock_t goal;%@NL@%
  29939. %@NL@%
  29940.     goal = wait + clock();%@NL@%
  29941.     while( goal > clock() )%@NL@%
  29942.         ;%@NL@%
  29943. }%@NL@%
  29944. %@NL@%
  29945. %@NL@%
  29946. %@2@%%@AH@%PAGER.C%@AE@%%@EH@%%@NL@%
  29947. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\FILE\PAGER.C%@AE@%%@NL@%
  29948. %@NL@%
  29949. %@AB@%/* PAGER.C illustrates line input and output on stream text files.%@NL@%
  29950. %@AB@% * Functions illustrated include:%@NL@%
  29951. %@AB@% *      fopen           fclose          fcloseall       feof%@NL@%
  29952. %@AB@% *      fgets           fputs           rename%@NL@%
  29953. %@AB@% */%@AE@%%@NL@%
  29954. %@NL@%
  29955. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  29956. %@AI@%#include %@AE@%<io.h> %@NL@%
  29957. %@AI@%#include %@AE@%<dos.h> %@NL@%
  29958. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  29959. %@AI@%#include %@AE@%<string.h> %@NL@%
  29960. %@NL@%
  29961. %@AI@%#define %@AE@%MAXSTRING 128 %@NL@%
  29962. %@AI@%#define %@AE@%PAGESIZE 55 %@NL@%
  29963. enum BOOL { FALSE, TRUE };%@NL@%
  29964. %@NL@%
  29965. void quit( int error );%@NL@%
  29966. FILE *infile, *outfile;%@NL@%
  29967. char outpath[] = "tmXXXXXX";%@NL@%
  29968. %@NL@%
  29969. main( int argc, char *argv[] )%@NL@%
  29970. {%@NL@%
  29971.     char tmp[MAXSTRING];%@NL@%
  29972.     long line = 0, page = 1;%@NL@%
  29973.     int  endflag = FALSE;%@NL@%
  29974. %@NL@%
  29975.     %@AB@%/* Open file (from command line) and output (temporary) files. */%@AE@%%@NL@%
  29976.     if( (infile = fopen( argv[1], "rt" )) == NULL )%@NL@%
  29977.         quit( 1 );%@NL@%
  29978.     mktemp( outpath );%@NL@%
  29979.     if( (outfile = fopen( outpath, "wt" )) == NULL )%@NL@%
  29980.         quit( 2 );%@NL@%
  29981. %@NL@%
  29982.     %@AB@%/* Get each line from input and write to output. */%@AE@%%@NL@%
  29983.     while( 1 )%@NL@%
  29984.     {%@NL@%
  29985.         %@AB@%/* Insert form feed and page header at the top of each page. */%@AE@%%@NL@%
  29986.         if( !(line++ % PAGESIZE) )%@NL@%
  29987.             fprintf( outfile, "\f\nPage %d\n\n", page++ );%@NL@%
  29988. %@NL@%
  29989.         if( fgets( tmp, MAXSTRING - 1, infile ) == NULL )%@NL@%
  29990.             if( feof( infile ) )%@NL@%
  29991.                 break;%@NL@%
  29992.             else%@NL@%
  29993.                 quit( 3 );%@NL@%
  29994.         if( fputs( tmp, outfile ) == EOF )%@NL@%
  29995.             quit( 3 );%@NL@%
  29996.     }%@NL@%
  29997. %@NL@%
  29998.     %@AB@%/* Close files and move temporary file to original by deleting%@NL@%
  29999. %@AB@%     * original and renaming temporary.%@NL@%
  30000. %@AB@%     */%@AE@%%@NL@%
  30001.     fcloseall();%@NL@%
  30002.     remove( argv[1] );%@NL@%
  30003.     rename( outpath, argv[1] );%@NL@%
  30004.     exit( 0 );%@NL@%
  30005. }%@NL@%
  30006. %@NL@%
  30007. %@AB@%/* Handle errors. */%@AE@%%@NL@%
  30008. void quit( int error )%@NL@%
  30009. {%@NL@%
  30010.     switch( error )%@NL@%
  30011.     {%@NL@%
  30012.         case 1:%@NL@%
  30013.             perror( "Can't open input file" );%@NL@%
  30014.             break;%@NL@%
  30015.         case 2:%@NL@%
  30016.             perror( "Can't open output file" );%@NL@%
  30017.             fclose( infile );%@NL@%
  30018.             break;%@NL@%
  30019.         case 3:%@NL@%
  30020.             perror( "Error processing file" );%@NL@%
  30021.             fclose( infile );%@NL@%
  30022.             fclose( outfile );%@NL@%
  30023.             remove( outpath );%@NL@%
  30024.             break;%@NL@%
  30025.     }%@NL@%
  30026.     exit( error );%@NL@%
  30027. }%@NL@%
  30028. %@NL@%
  30029. %@NL@%
  30030. %@2@%%@AH@%PALETTE1.C%@AE@%%@EH@%%@NL@%
  30031. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\GRAPHICS\PALETTE1.C%@AE@%%@NL@%
  30032. %@NL@%
  30033. %@AB@%/* PALETTE.C illustrates functions for assigning color values to%@NL@%
  30034. %@AB@% * color indexes. Functions illustrated include:%@NL@%
  30035. %@AB@% *      _remappalette       _remapallpalette%@NL@%
  30036. %@AB@% */%@AE@%%@NL@%
  30037. %@NL@%
  30038. %@AI@%#include %@AE@%<graph.h> %@NL@%
  30039. %@AI@%#include %@AE@%<conio.h> %@NL@%
  30040. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  30041. %@AI@%#include %@AE@%<stdlib.h> %@NL@%
  30042. %@NL@%
  30043. %@AB@%/* Macro for mixing Red, Green, and Blue elements of color */%@AE@%%@NL@%
  30044. %@AI@%#define %@AE@%RGB(r,g,b) (((long) ((b) << 8 | (g)) << 8) | (r)) %@NL@%
  30045. %@NL@%
  30046. main()%@NL@%
  30047. {%@NL@%
  30048.     short red, blue, green;%@NL@%
  30049.     short inc, i, mode, cells, x, y, xinc, yinc;%@NL@%
  30050.     long tmp, pal[256];%@NL@%
  30051.     char buf[40];%@NL@%
  30052.     struct videoconfig vc;%@NL@%
  30053. %@NL@%
  30054.     %@AB@%/* Make sure all palette numbers are valid */%@AE@%%@NL@%
  30055.     for( i = 0; i < 256; i++ )%@NL@%
  30056.         pal[i] = _BLACK;%@NL@%
  30057. %@NL@%
  30058.     %@AB@%/* Loop through each graphics mode that supports palettes. */%@AE@%%@NL@%
  30059.     for( mode = _MRES4COLOR; mode <= _MRES256COLOR; mode++ )%@NL@%
  30060.     {%@NL@%
  30061.         if( mode == _ERESNOCOLOR )%@NL@%
  30062.             mode++;%@NL@%
  30063.         if( !_setvideomode( mode ) )%@NL@%
  30064.             continue;%@NL@%
  30065. %@NL@%
  30066.         %@AB@%/* Set variables for each mode. */%@AE@%%@NL@%
  30067.         _getvideoconfig( &vc );%@NL@%
  30068.         switch( vc.numcolors )%@NL@%
  30069.         {%@NL@%
  30070.             case 256:           %@AB@%/* Active bits in this order:           */%@AE@%%@NL@%
  30071.                 cells = 13;%@NL@%
  30072.                 inc = 12;       %@AB@%/* ???????? ??bbbbbb ??gggggg ??rrrrrr  */%@AE@%%@NL@%
  30073.                 break;%@NL@%
  30074.             case  16:%@NL@%
  30075.                 cells = 4;%@NL@%
  30076.                 if( (vc.mode == _ERESCOLOR) || (vc.mode == _VRES16COLOR) )%@NL@%
  30077.                     inc = 16;   %@AB@%/* ???????? ??????bb ??????gg ??????rr  */%@AE@%%@NL@%
  30078.                 else%@NL@%
  30079.                     inc = 32;   %@AB@%/* ???????? ??????Bb ??????Gg ??????Rr  */%@AE@%%@NL@%
  30080.                 break;%@NL@%
  30081.             case   4:%@NL@%
  30082.                 cells = 2;%@NL@%
  30083.                 inc = 32;       %@AB@%/* ???????? ??????Bb ??????Gg ??????Rr  */%@AE@%%@NL@%
  30084.                 break;%@NL@%
  30085.             default:%@NL@%
  30086.                 continue;%@NL@%
  30087.         }%@NL@%
  30088.         xinc = vc.numxpixels / cells;%@NL@%
  30089.         yinc = vc.numypixels / cells;%@NL@%
  30090. %@NL@%
  30091.         %@AB@%/* Fill palette arrays in BGR order */%@AE@%%@NL@%
  30092.         for( i = 0, blue = 0; blue < 64; blue += inc )%@NL@%
  30093.             for( green = 0; green < 64; green += inc )%@NL@%
  30094.                 for( red = 0; red < 64; red += inc )%@NL@%
  30095.                 {%@NL@%
  30096.                     pal[i] = RGB( red, green, blue );%@NL@%
  30097.                     %@AB@%/* Special case of using 6 bits to represent 16 colors.%@NL@%
  30098. %@AB@%                     * If both bits are on for any color, intensity is set.%@NL@%
  30099. %@AB@%                     * If one bit is set for a color, the color is on.%@NL@%
  30100. %@AB@%                     */%@AE@%%@NL@%
  30101.                     if( inc == 32 )%@NL@%
  30102.                         pal[i + 8] = pal[i] | (pal[i] >> 1);%@NL@%
  30103.                     i++;%@NL@%
  30104.                 }%@NL@%
  30105. %@NL@%
  30106.         %@AB@%/* If palettes available, remap all palettes at once. */%@AE@%%@NL@%
  30107.         if( !_remapallpalette( pal ) )%@NL@%
  30108.         {%@NL@%
  30109.             _setvideomode( _DEFAULTMODE );%@NL@%
  30110.             _outtext( "Palettes not available with this adapter" );%@NL@%
  30111.             exit( 1 );%@NL@%
  30112.         }%@NL@%
  30113. %@NL@%
  30114.         %@AB@%/* Draw colored squares */%@AE@%%@NL@%
  30115.         for( i = 0, x = 0; x < vc.numxpixels; x += xinc )%@NL@%
  30116.             for( y = 0; y < vc.numypixels; y += yinc )%@NL@%
  30117.             {%@NL@%
  30118.                 _setcolor( i++ );%@NL@%
  30119.                 _rectangle( _GFILLINTERIOR, x, y, x + xinc, y + yinc );%@NL@%
  30120.             }%@NL@%
  30121.         sprintf( buf, "Mode %d has %d colors", vc.mode, vc.numcolors );%@NL@%
  30122.         _setcolor( vc.numcolors / 2 );%@NL@%
  30123.         _outtext( buf );%@NL@%
  30124.         getch();%@NL@%
  30125. %@NL@%
  30126.         %@AB@%/* Change each palette entry separately in GRB order. */%@AE@%%@NL@%
  30127.         for( i = 0, green = 0; green < 64; green += inc )%@NL@%
  30128.             for( red = 0; red < 64; red += inc )%@NL@%
  30129.                 for(blue = 0; blue < 64; blue += inc )%@NL@%
  30130.                 {%@NL@%
  30131.                     tmp = RGB( red, green, blue );%@NL@%
  30132.                     _remappalette( i, tmp );%@NL@%
  30133.                     if( inc == 32 )%@NL@%
  30134.                         _remappalette( i + 8, tmp | (tmp >> 1) );%@NL@%
  30135.                     i++;%@NL@%
  30136.                 }%@NL@%
  30137.         getch();%@NL@%
  30138.     }%@NL@%
  30139.     exit( !_setvideomode( _DEFAULTMODE ) );%@NL@%
  30140. }%@NL@%
  30141. %@NL@%
  30142. %@NL@%
  30143. %@2@%%@AH@%PARRAY.C%@AE@%%@EH@%%@NL@%
  30144. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\PARRAY.C%@AE@%%@NL@%
  30145. %@NL@%
  30146. %@AB@%/* PARRAY.C: Demonstrate pointer to array. */%@AE@%%@NL@%
  30147. %@NL@%
  30148. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  30149. %@NL@%
  30150. int i_array[] = { 25, 300, 2, 12 };%@NL@%
  30151. %@NL@%
  30152. main()%@NL@%
  30153. {%@NL@%
  30154.    int *ptr;%@NL@%
  30155.    int count;%@NL@%
  30156.    ptr = &i_array[0];%@NL@%
  30157.    for( count = 0; count < 4 ; count++ )%@NL@%
  30158.    {%@NL@%
  30159.       printf( "array[%d] = %d\n", count, *ptr );%@NL@%
  30160.       ptr++;%@NL@%
  30161.    }%@NL@%
  30162. }%@NL@%
  30163. %@NL@%
  30164. %@NL@%
  30165. %@2@%%@AH@%PARRAY1.C%@AE@%%@EH@%%@NL@%
  30166. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\PARRAY1.C%@AE@%%@NL@%
  30167. %@NL@%
  30168. %@AB@%/* PARRAY1.C: Compact version of PARRAY.C. */%@AE@%%@NL@%
  30169. %@NL@%
  30170. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  30171. %@NL@%
  30172. int i_array[] = { 25, 300, 2, 12 };%@NL@%
  30173. %@NL@%
  30174. %@NL@%
  30175. main()%@NL@%
  30176. {%@NL@%
  30177.    int count;%@NL@%
  30178.    int *ptr = i_array;%@NL@%
  30179.    for( count = 0; count < 4 ; count++ )%@NL@%
  30180.       printf( "array[%d] = %d\n", count, *ptr++ );%@NL@%
  30181. }%@NL@%
  30182. %@NL@%
  30183. %@NL@%
  30184. %@2@%%@AH@%PFUNC.C%@AE@%%@EH@%%@NL@%
  30185. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\PFUNC.C%@AE@%%@NL@%
  30186. %@NL@%
  30187. %@AB@%/* PFUNC.C: Passing pointers to a function. */%@AE@%%@NL@%
  30188. %@NL@%
  30189. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  30190. %@NL@%
  30191. void swap( int *ptr1, int *ptr2 );%@NL@%
  30192. %@NL@%
  30193. main()%@NL@%
  30194. {%@NL@%
  30195.    int first = 1, second = 3;%@NL@%
  30196.    int *ptr = &second;%@NL@%
  30197.    printf( "first: %d  second: %d\n", first, *ptr );%@NL@%
  30198.    swap( &first, ptr );%@NL@%
  30199.    printf( "first: %d  second: %d\n", first, *ptr );%@NL@%
  30200. }%@NL@%
  30201. %@NL@%
  30202. void swap( int *ptr1, int *ptr2 )%@NL@%
  30203. {%@NL@%
  30204.    int temp;%@NL@%
  30205.    temp = *ptr1;%@NL@%
  30206.    *ptr1 = *ptr2;%@NL@%
  30207.    *ptr2 = temp;%@NL@%
  30208. }%@NL@%
  30209. %@NL@%
  30210. %@NL@%
  30211. %@2@%%@AH@%POINTER.C%@AE@%%@EH@%%@NL@%
  30212. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\POINTER.C%@AE@%%@NL@%
  30213. %@NL@%
  30214. %@AB@%/* POINTER.C: Demonstrate pointer basics. */%@AE@%%@NL@%
  30215. %@NL@%
  30216. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  30217. %@NL@%
  30218. main()%@NL@%
  30219. {%@NL@%
  30220.    int val = 25;%@NL@%
  30221.    int *ptr;%@NL@%
  30222.    ptr = &val;%@NL@%
  30223.    printf( " val = %d\n", val );%@NL@%
  30224.    printf( "*ptr = %d\n\n", *ptr );%@NL@%
  30225.    printf( "&val = %u\n", &val );%@NL@%
  30226.    printf( " ptr = %d\n", ptr );%@NL@%
  30227. }%@NL@%
  30228. %@NL@%
  30229. %@NL@%
  30230. %@2@%%@AH@%PRINTF.C%@AE@%%@EH@%%@NL@%
  30231. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\PRINTF.C%@AE@%%@NL@%
  30232. %@NL@%
  30233. %@AB@%/* PRINTF.C illustrates output formatting with:%@NL@%
  30234. %@AB@% *      printf%@NL@%
  30235. %@AB@% *%@NL@%
  30236. %@AB@% * The rules for formatting also apply to cprintf, sprintf, vfprintf,%@NL@%
  30237. %@AB@% * vprintf, and vsprintf. For other examples of printf formatting,%@NL@%
  30238. %@AB@% * see EXTDIR.C (sprintf), VPRINTF (vprintf), TABLE.C (fprintf),%@NL@%
  30239. %@AB@% * ROTATE.C (printf), and IS.C (cprintf).%@NL@%
  30240. %@AB@% */%@AE@%%@NL@%
  30241. %@NL@%
  30242. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  30243. %@NL@%
  30244. main()%@NL@%
  30245. {%@NL@%
  30246.     char ch = 'h', *string = "computer";%@NL@%
  30247.     int count = 234, *ptr, hex = 0x10, oct = 010, dec = 10;%@NL@%
  30248.     double fp = 251.7366;%@NL@%
  30249. %@NL@%
  30250.     %@AB@%/* Display integers. */%@AE@%%@NL@%
  30251.     printf("%d    %+d    %06d    %X    %x    %o\n\n",%@NL@%
  30252.             count, count, count, count, count, count );%@NL@%
  30253. %@NL@%
  30254.     %@AB@%/* Count characters printed. */%@AE@%%@NL@%
  30255.     printf( "            V\n" );%@NL@%
  30256.     printf( "1234567890123%n45678901234567890\n", &count );%@NL@%
  30257.     printf( "Number of characters printed: %d\n\n", count );%@NL@%
  30258. %@NL@%
  30259.     %@AB@%/* Display characters. */%@AE@%%@NL@%
  30260.     printf( "%10c%5c\n\n", ch, ch );%@NL@%
  30261. %@NL@%
  30262.     %@AB@%/* Display strings. */%@AE@%%@NL@%
  30263.     printf( "%25s\n%25.4s\n\n", string, string );%@NL@%
  30264. %@NL@%
  30265.     %@AB@%/* Display real numbers. */%@AE@%%@NL@%
  30266.     printf( "%f    %.2f    %e    %E\n\n", fp, fp, fp, fp );%@NL@%
  30267. %@NL@%
  30268.     %@AB@%/* Display in different radixes. */%@AE@%%@NL@%
  30269.     printf( "%i    %i    %i\n\n", hex, oct, dec );%@NL@%
  30270. %@NL@%
  30271.     %@AB@%/* Display pointers. */%@AE@%%@NL@%
  30272.     ptr = &count;%@NL@%
  30273.     printf( "%Np    %p    %Fp\n", ptr, (int far *)ptr, (int far *)ptr );%@NL@%
  30274. }%@NL@%
  30275. %@NL@%
  30276. %@NL@%
  30277. %@2@%%@AH@%PRTESC.C%@AE@%%@EH@%%@NL@%
  30278. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\PRTESC.C%@AE@%%@NL@%
  30279. %@NL@%
  30280. %@AB@%/* PRTESC.C: Prints escape characters \",\n, and \t.%@NL@%
  30281. %@AB@%*/%@AE@%%@NL@%
  30282. %@NL@%
  30283. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  30284. %@AI@%#include %@AE@%<string.h> %@NL@%
  30285. %@NL@%
  30286. main()%@NL@%
  30287. {%@NL@%
  30288.    char b[80];%@NL@%
  30289.    int i,j;%@NL@%
  30290. %@NL@%
  30291.    strcpy( b, "and seven years ago\n" );%@NL@%
  30292.    printf( "\"Four score\n" );%@NL@%
  30293.    printf( b );%@NL@%
  30294.    printf( "\tone tab\n\t\ttwo tabs\n\t\t\tthree tabs\n" );%@NL@%
  30295.    i = sizeof( b );%@NL@%
  30296.    j = strlen( b );%@NL@%
  30297.    printf( "Size is %d\nLength is %d.\n", i, j );%@NL@%
  30298. }%@NL@%
  30299. %@NL@%
  30300. %@NL@%
  30301. %@2@%%@AH@%PSTRING.C%@AE@%%@EH@%%@NL@%
  30302. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\PSTRING.C%@AE@%%@NL@%
  30303. %@NL@%
  30304. %@AB@%/* PSTRING.C: Demonstrate pointer to a string. */%@AE@%%@NL@%
  30305. %@NL@%
  30306. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  30307. %@NL@%
  30308. main()%@NL@%
  30309. {%@NL@%
  30310.    int count;%@NL@%
  30311.    static char name[] = "john";%@NL@%
  30312.    char *ptr = name;%@NL@%
  30313.    for( count = 0; count < 4; count++ )%@NL@%
  30314.    {%@NL@%
  30315.       printf( "name[%d]: %c\n", count, *ptr++ );%@NL@%
  30316.    }%@NL@%
  30317. }%@NL@%
  30318. %@NL@%
  30319. %@NL@%
  30320. %@2@%%@AH@%PSTRING1.C%@AE@%%@EH@%%@NL@%
  30321. %@AS@%CD-ROM Disc Path:   \SAMPCODE\C\OTHER\LEARN\PSTRING1.C%@AE@%%@NL@%
  30322. %@NL@%
  30323. %@AB@%/* PSTRING1.C: Look for null at string's end.  */%@AE@%%@NL@%
  30324. %@NL@%
  30325. %@AI@%#include %@AE@%<stdio.h> %@NL@%
  30326. %@