home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctutor1.zip / CHAP14.TXT < prev    next >
Text File  |  1989-11-10  |  13KB  |  294 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                    Chapter 14
  6.                                              EXAMPLE PROGRAMS
  7.  
  8.  
  9. WHY THIS CHAPTER?
  10. ____________________________________________________________
  11.  
  12. Although every program in this tutorial has been a complete
  13. program, each one has also been a very small program intended
  14. to teach you some principle of programming in C.  It would do
  15. you a disservice to leave you at that point without
  16. introducing you to a few larger programs to illustrate how to
  17. put together the constructs you have learned to create a major
  18. program.  This chapter contains four programs of increasing
  19. complexity, each designed to take you into a higher plateau
  20. of programming, and each designed to be useful to you in some
  21. way.
  22.  
  23. This course was originally written for use on an IBM-PC or
  24. compatible running the PC-DOS operating system.  Some of the
  25. example programs in this chapter will not compile and execute
  26. properly if you are using some other computer or operating
  27. system, but the techniques may still be useful to you.  It
  28. would be advantageous to you to spend some time studying these
  29. programs regardless of what computer you are using.
  30.  
  31. DOSEX will illustrate how to make DOS system calls and will
  32. teach you, through self-study, how the system responds to the
  33. keyboard.  WHATNEXT reads commands input on the command line
  34. and will aid you in setting up a variable batch file, one that
  35. requests an operator input and responds to the input by
  36. branching to a different part of the batch file.
  37.  
  38. LIST is the C source code for a program that operates
  39. similarly to the listing program you used to print out the C
  40. source files when you began studying C with the aid of this
  41. tutorial.  Finally we come to VC, the Visual Calculator, which
  42. you should find to be a useful program even if you don't study
  43. its source code.  VC uses most of the programming techniques
  44. we have studied in this course and a few that we never even
  45. mentioned such as separately compiled subroutines.
  46.  
  47. We will take a look at the example programs one at a time but
  48. without a complete explanation of any of them because you have
  49. been studying C for some time now and should be able to read
  50. and understand most of these programs on your own.
  51.  
  52.  
  53. DOSEX.C - The DOS Example Program
  54. ____________________________________________________________
  55.  
  56. The copy of DOS that you received with your IBM-PC or
  57. compatible has about 80 internal DOS calls that you can use
  58.  
  59.                                                     Page 14-1
  60.  
  61.                                 Chapter 14 - Example Programs
  62.  
  63. as a programmer to control your peripheral devices and read
  64. information or status from them.  Some of the earlier IBM DOS
  65. manuals, DOS 2.0 and earlier, have these calls listed in the
  66. back of the manual along with how to use them.  Most of the
  67. manuals supplied with compatible computers make no mention of
  68. these calls even though they are extremely useful.  These
  69. calls can be accessed from nearly any programming language but
  70. they do require some initial study to learn how to use them. 
  71. This program is intended to aid you in this study.
  72.  
  73. Display the program on your monitor or print    =============
  74. it out for reference.  It is merely a loop         DOSEX.C
  75. watching for a keyboard input or a change in    =============
  76. the time.  If either happens, it reacts
  77. accordingly.  In line 32, the function
  78. kbhit() returns a value of 1 if a key has been hit but not yet
  79. read from the input buffer by the program. 
  80.  
  81. Look at the function named get_time() for an example of a DOS
  82. call.  An interrupt 21(hex) is called after setting the AH
  83. register to 2C(hex) = 44(decimal).  The time is returned in
  84. the CH, CL, and DH registers.  Refer to the DOS call
  85. definitions in your copy of DOS.  If the definitions are not
  86. included there, Peter Norton's book, "Programmers Guide to the
  87. IBM PC" is recommended as a good reference manual for these
  88. calls and many other programming techniques.  Your compiler
  89. may have a built in function to do this.  If you read your
  90. documentation, you will probably find many useful functions
  91. available with your compiler that are included as a
  92. convenience for you by your compiler writer. 
  93.  
  94. Another useful function is the pos_cursor() function that
  95. positions the cursor anywhere on the monitor that you desire
  96. by using a DOS interrupt.  In this case, the interrupt used
  97. is 10(hex) which is the general monitor interrupt.  This
  98. particular service is number 2 of about 10 different monitor
  99. services available.  This function is included here as another
  100. example to you.
  101.  
  102. The next function, service number 6 of interrupt 10(hex) is
  103. the window scroll service.  It should be self explanatory.
  104.  
  105. In this program, the cursor is positioned and some data is
  106. output to the monitor, then the cursor is hidden by moving it
  107. to line 26 which is not displayed.  After you compile and run
  108. the program, you will notice that the cursor is not visible
  109. on the monitor.  This is possible in any program, but be sure
  110. to put the cursor in view before returning to DOS because DOS
  111. does not like to have a hidden cursor and may do some strange
  112. things. 
  113.  
  114. Some time spent studying this program will be valuable to you
  115. as it will reveal how the keyboard data is input to the
  116. computer.  Especially of importance is how the special keys
  117.  
  118.                                                     Page 14-2
  119.  
  120.                                 Chapter 14 - Example Programs
  121.  
  122. such as function keys, arrows, etc. are handled.  Also note
  123. that this program uses full prototype checking and is a good
  124. example of how to use it.  Since it also uses the modern
  125. method of function definitions, it is a good example of that
  126. also.
  127.  
  128.  
  129. WHATNEXT.C - The Batch File Interrogator
  130. ____________________________________________________________
  131.  
  132. This is an example of how to read the data on  ==============
  133. the command line following the function call.    WHATNEXT.C
  134. Notice that there are two variables listed     ==============
  135. within the parentheses following the main()
  136. call.  The first variable is a count of words
  137. in the entire command line including the command itself and
  138. the second variable is a pointer to an array of pointers
  139. defining the actual words on the command line.
  140.  
  141. First the question on the command line, made up of some number
  142. of words, is displayed on the monitor and the program waits
  143. for the operator to hit a key.  If the key hit is one of those
  144. in the last word of the group of words on the command line,
  145. the number of the character within the group is returned to
  146. the program where it can be tested with the ERRORLEVEL command
  147. in the batch file.  You could use this technique to create a
  148. variable AUTOEXEC.BAT file or any other batch file can use
  149. this for a many way branch.  Compile and run this file with
  150. TEST.BAT for an example of how it works in practice.  You may
  151. find this technique useful in one of your batch files and you
  152. will almost certainly need to read in the command line
  153. parameters someday.
  154.  
  155. An interesting alternative would be for you to write a program
  156. named WOULD.C that would return a 1 if a Y or y were typed and
  157. a zero if any other key were hit.  Then your batch file could
  158. have a line such as;
  159.  
  160. WOULD YOU LIKE TO USE THE ALTERNATIVE METHOD (Y/N)
  161.  
  162. Dos would use WOULD as the program name, ignore the rest of
  163. the statement except for displaying it on the screen.  You
  164. would then respond to the question on the monitor with a
  165. single keyhit.  Your batch file would then respond to the 1
  166. or 0 returned and either run the alternative part of the batch
  167. file or the primary part whatever each part was.
  168.  
  169. WOULD YOU LIKE PRIMARY (Y/N)
  170. IF ERRORLEVEL 1 GOTO PRIMARY
  171. (secondary commands)
  172. GOTO DONE
  173. :PRIMARY
  174. (primary commands)
  175. :DONE
  176.  
  177.                                                     Page 14-3
  178.  
  179.                                 Chapter 14 - Example Programs
  180.  
  181.  
  182.  
  183. LIST.C - The Program Lister
  184. ____________________________________________________________
  185.  
  186. This program is actually composed of two       ==============
  187. files, LIST.C and LISTF.C that must be             LIST.C
  188. separately compiled and linked together with   ==============
  189. your linker.  There is nothing new here and
  190. you should have no trouble compiling and
  191. linking this program by reading the documentation supplied
  192. with your C compiler.
  193.  
  194. The only thing that is new in this program is the inclusion
  195. of three extern variables in the LISTF.C listing.  The only
  196. purpose for this is to tie these global variables to the main
  197. program and tell the compiler that these are not new
  198. variables.  The compiler will therefore not generate any new
  199. storage space for them but simply use their names during the
  200. compile process.  At link time, the linker will get their
  201. actual storage locations from the LIST.OBJ file and use those
  202. locations for the variables in the LISTF part of the memory
  203. map also.  The variables of those names in both files are
  204. therefore the same identical variables and can be used just
  205. as any other global variables could be used if both parts of
  206. the program were in one file.
  207.  
  208. There is no reason why the variables couldn't have been
  209. defined in the LISTF.C part of the program and declared as
  210. extern in the LIST.C part.  Some of the variables could have
  211. been defined in one and some in the other.  It is merely a
  212. matter of personal taste.  Carried to an extreme, all of the
  213. variables could have been defined in a third file and named
  214. extern in both of these files.  The third file would then be
  215. compiled and included in the linking process.
  216.  
  217. It would be to your advantage to compile, link, and run this
  218. program to prepare you for the next program which is composed
  219. of 6 separate files which must all work together.
  220.  
  221.  
  222. VC.C - The Visual Calculator
  223. ____________________________________________________________
  224.  
  225. This program finally ties nearly everything    ==============
  226. together because it uses nearly every concept       VC.C
  227. covered in the entire tutorial.  It is so big  ==============
  228. that I will not even try to cover the finer
  229. points of its operation.  Only a few of the
  230. more important points will be discussed.
  231.  
  232. The first thing you should do is go through the tutorial for
  233. VC included in the file VC.DOC.  There are several dozen steps
  234. for you to execute, with each step illustrating some aspect
  235.  
  236.                                                     Page 14-4
  237.  
  238.                                 Chapter 14 - Example Programs
  239.  
  240. of the Visual Calculator.  You will get a
  241. good feel for what it is capable of doing and  ==============
  242. make your study of the source code very            VC.DOC
  243. profitable.  In addition, you will probably    ==============
  244. find many ways to use the Visual Calculator
  245. to solve problems involving calculations where the simplicity
  246. of the problem at hand does not warrant writing a program.
  247.  
  248. Notice that the structure definitions, used in all of the
  249. separate parts of the program, are defined in the file
  250. STRUCT.DEF.  During program development, when it became
  251. necessary to change one of the structures slightly, it was not
  252. necessary to change it in all of the files, only one file
  253. required modification which was then included in the source
  254. files.  Notice that the transcript data is stored in a doubly
  255. linked list with the data itself being stored in a separate
  256. dynamically allocated character string.  This line is pointed
  257. to by the pointer lineloc.
  258.  
  259. For ease of development, the similar functions were grouped
  260. together and compiled separately.  Thus, all of the functions
  261. involving the monitor were included in the file named VIDEO.C,
  262. and all of the functions involving the data storage were
  263. grouped into the FILE.C collection.  Dividing your program in
  264. a way similar to this should simplify debugging and future
  265. modifications. 
  266.  
  267. Of special interest is the function named monitor().  This
  268. function examines the video mode through use of a DOS command
  269. and if it is a 7, it assumes it is a monochrome monitor,
  270. otherwise it assumes a color monitor.  The colors of the
  271. various fields are established at this time and used
  272. throughout the program.  Most of the data is written directly
  273. to the video memory, but some is written through the standard
  274. BIOS routines.
  275.  
  276. The file DEFIN.H is a catalogue of the functions to aid in
  277. finding the functions.  This file was generated as one of the
  278. first files and was maintained and updated for use during the
  279. entire design and coding lifetime.  It also contains all of
  280. the prototype definitions for the functions in all of the
  281. source files, and is included in every source file to do
  282. prototype checking.
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.                                                     Page 14-5
  294.