home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / ST_USER / 1990 / USERAU90.MSA / TEXT_CLINIC.DOC < prev    next >
Text File  |  1990-06-24  |  12KB  |  308 lines

  1.                          PROGRAMMER'S CLINIC
  2.  
  3.  
  4.                     Mathew Lodge chairs the popular
  5.                   programming problem solving clinic
  6.  
  7.  
  8. A quiet month at the clinic, but some interesting and useful letters were sent
  9. in. In the April clinic (in ST World!) I asked what the difference was between
  10. TOS 1.6 and TOS 1.4. Shortly after I'd written that my question was answered in
  11. a message on the USENET Atari ST newsgroup by staff from Atari USA. The
  12. differences are minimal - TOS 1.6 is just TOS 1.4 modified to work with the
  13. STE's extra hardware.
  14.  
  15. This month also sees the first MINIX question to grace the pages of this
  16. column. MINIX is a remarkable piece of software, being a complete UNIX clone
  17. for the ST written by Dr Andrew Tanenbaum. If you're at all interested in
  18. operating systems design, or if you just want to turn your ST into a UNIX
  19. machine, then MINIX is the way to do it.
  20.     I was recently told that a 1040ST with a large hard drive running MINIX is
  21. more powerful than some DEC mainframes were five years ago! Enough operating
  22. systems talk, let's get on with the clinic.
  23.  
  24.  
  25. A WORD IN YOUR SHELL-LIKE
  26. --------------------------
  27. Mark T. White of Toronto, Canada, gave the solution to Ian McCall's shel_write
  28. problem detailed in the June (ST World) clinic, but he also suggested another
  29. method not given.
  30.     "The solution to his problem is to use a command line processor, and
  31. several PD and commercial ones are available. If he installs one using 'Install
  32. application' for the extension .BAT, then creates the following file with a
  33. .BAT extension, double-clicking on the file will accomplish the desired
  34. results:
  35.  
  36. \AUTO\INSTALL.PRG
  37. SIGNUM2.PRG
  38. EXIT
  39.  
  40. EXIT may need to be changed to BYE for some CLIs.
  41.  
  42.  
  43.  
  44. RAGING Cs
  45. ----------
  46. The Atari ST is a computer designed to be programmed in C, and this is
  47. reflected in the fact that most of the code that is sent to the clinic is in
  48. this language. This month I've had a few queries about C, the first of which is
  49. from Ian McCall in Sheffield, Yorkshire and concerns the switch from K&R (UNIX)
  50. C to ANSI standard C, and dialog buttons.
  51.     "Since all a compiler/linker does is convert routines into object code and
  52. then link them together, is it possible to switch from K&R to ANSI simply by
  53. changing which library you use? And if so, where can I obtain a copy of the
  54. ANSI libraries without buying a new compiler (I'm using Laser C).
  55.     "Secondly, how can you change the text inside an object of type G_BUTTON
  56. from Laser C? The manual states that the ob_spec field of the object structure
  57. for this type is a pointer to text to be output, but if I set this to point to
  58. anything, I get the familiar two bombs. Incidentally, the object structure of
  59. Laser C is different to the object structure listed in issue 46. In Laser C,
  60. ob_spec is defined as char *ob_spec. Is it this that's causing me problems?"
  61.  
  62.                      *             *            *
  63.  
  64. The next C query comes from Waikei Chung, who has been experimenting with
  65. Lattice C 3.04.
  66.     "I have had a copy of Lattice C for some time now, but I have not used it
  67. until now, and I found that Metacomco has apparently left the ST market. I'm
  68. beginning the long and difficult path of learning C, and I'm interested by a
  69. section in the Lattice manual stating that we may 'write assembly language
  70. modules for inclusion in C programs.'
  71.     "Unfortunately, I am rather confused by the sparse details, so I wonder if
  72. anyone can elaborate. How does you include assembler code into the final
  73. program, and, above all, do I require a separate assembler package?"
  74.  
  75.                      *             *            *
  76.  
  77.     The final C query is about compiling a C program under MINIX, and is from
  78. David Hoggan.
  79.     "Does anyone have a working source copy of AWK, BAWK or any incarnation of
  80. this utility? The versions I've downloaded and tried to compile won't, causing
  81. a file system panic in the process. What I have managed to download and compile
  82. (nearly everything works, as the source was for PCs) has brought MINIX in line
  83. with V1.5.0 for the PC. Also, has anyone figured out how to get MINIX to read a
  84. real time, battery backed clock? I know it can read the clocks in the Megas,
  85. but does anyone know how to do it on an ST(E)?"
  86.  
  87.  
  88.  
  89.  
  90. IMG decompression
  91.  
  92. In a clinic special last year (in ST World again), Dimitri Koveos described and
  93. explained the IMG file format. However, he did not include any code for reading
  94. and writing IMG files, which is what the redoubtable Lloyd Patton has provided
  95. for the clinic this month. The IMG reading routine is written in C.
  96.     "The code is contained in the file IMGREAD. The function decompress() does
  97. the actual work and is fairly self-explanatory. All the complications are in
  98. the interface function read_image_file(). Which is simply concerned with error
  99. checking and initialising the data structures. Images that are less than a
  100. screen width or height size are placed in a memory form block that is at least
  101. a 640 pixels wide and 400 pixels high as this will allow easier manipulation of
  102. the picture area at a later date
  103.  
  104. ------------------------------------------------------------------------------
  105. /* IMG file decompression */
  106. /* Author : Lloyd Patton */
  107.  
  108. #include <local.h>
  109. #include <gemdefs.h>
  110. #include <obdefs.h>
  111.  
  112. typedef struct {
  113.   WORD version, header_length, number_of_planes
  114.   WORD pattern_length, micron_width, micron_height,
  115.   WORD line_length, number_of_lines;
  116.   WORD flag, palette[16];
  117.   } XIMGHDR;  /* this typedef should be in a header file */
  118.  
  119. #define NoFile  2
  120. #define NoLoadMemory  4
  121.  
  122. static BYTE
  123.   MONOonly[] = "[3][Only monochrome IMG|files supported!][Sorry ]",
  124.   BadFile[] = "[3][IMG file format error|encountered!][ Sorry ]";
  125.  
  126. /* decompress an image file - returns 0 if error occurs */
  127. static decompress_image_file(fp, lines, pixels, llength,picture)
  128.   register FILE *fp;  /* inout file pointer /*
  129.   UWORD lines, pixels;  /* no. scan lines && pixels */
  130.   LONG llength;
  131.   BYTE *picture;
  132.   {
  133.  
  134.   WORD repcnt, this;
  135.   register WORD first, second, dtype, count;
  136.   register BYTE *buffer, *pptr, *bptr;
  137.  
  138.   if ((buffer = lcalloc(llength, 1L)) == NULL)
  139.     {
  140.     form_error(NoLoadMemory);
  141.     return (0);
  142.     }
  143.   wind_update(BEG_MCTRL);
  144.   graf_mouse(HOURGLASS, NULL);
  145.   for (this = dtype = 0; this < lines && dtype != EOF; this += repcnt)
  146.     {
  147.     repcnt = 1;
  148.     bptr = buffer;
  149.     while (((bptr - buffer) << 3) < pixels && (dtype = getc(fp)) != EOF)
  150.       {
  151.       switch (dtype)
  152.         {
  153.         case 0x80:  /* BIT STRING */
  154.           count = getc(fp); /* copy count no. of bytes */
  155.           while (count--)
  156.             *bptr++ = getc(fp);
  157.           break;
  158.         case 0: /* either VERT REPL count || PATTERN RUN */
  159.           count = getc(fp);
  160.           first = getc(fp);
  161.           second = getc(fp);
  162.           if (count == 0) /* VERTICAL REPLICATION */
  163.             {
  164.             if (first == 255 && bptr == buffer)
  165.               repcnt = second;
  166.             else
  167.               lines = this; /* error reading file */
  168.               continue;
  169.             }
  170.           else {  /* PATTERN RUN */
  171.             while (count--)
  172.               {
  173.               *bptr++ =  first;
  174.               *bptr++ = second;
  175.               }
  176.             }
  177.           break;
  178.         default:  /* SOLID RUN */
  179.           first = (dtype & 0x80) ? -1: 0;
  180.           count = dtype & 0x7F;
  181.           while (count--)
  182.             *bptr++ = first;
  183.           break;
  184.         }
  185.       }
  186.     for (first = 0; first < repcnt; ++first)
  187.       { /* copy scan line to picture buffer */
  188.       pptr = picture + (this + first) * llength;
  189.       bcopy(buffer, pptr, (WORD)llength);
  190.       }
  191.     }
  192.   graf_mouse(ARROW, NULL);
  193.   wind_update(END_MCTRL);
  194.   return (lines == this && dtype != EOF);
  195.   }
  196.  
  197.  
  198. /* read an image file - returns pointer to picture data or
  199.  * NULL if error.
  200.  * Your program should not be within a BEG_MCTRL loop
  201.  * when this function is called.
  202. */
  203.  
  204. BYTE *read_image_file(vdi, img, mform, path)
  205.   WORD vdi;       /* vdi handle */
  206.   register MFDB *mform; /* memory form descriptor for pic*/
  207.   register XIMGHDR *img;  /* IMG file header */
  208.   BYTE *path;       /* full pathname for file to read */
  209.   {
  210.  
  211.   register FILE *fp;
  212.   register LONG psize;
  213.   register UWORD lines, pwidth, wwidth;
  214.   register WORD pxy[8];
  215.   MFDB tform;
  216.  
  217.   if ((fp = fopen(path, "br")) != NULL)
  218.     {
  219.     fread(img, sizeof(XIMGHDR), 1, fp);
  220.     fseek(fp, (LONG)(img -> header_length * 2), 0);
  221.     if (img -> number_of_planes != 1)
  222.       {
  223.       form_alert(1, MONOonly);
  224.       return (NULL);
  225.       }
  226.     mform -> fd_nplanes = 1;
  227.     mform -> fd_stand = 0;
  228.     psize = (pwidth = mform -> fd_w = img -> line_length) / 16;
  229.     mform -> fd_wdwidth = wwidth = psize += (img ->line_length % 16) ? 1: 0;
  230.     psize *= lines = mform -> fd_h = img -> number_of_lines;
  231.     tform.fd_addr = mform -> fd_addr;
  232.     if (mform -> fd_addr)
  233.       mform -> fd_addr = (LONG)lrealloc(mform -> fd_addr, psize * 2L);
  234.     else
  235.       mform -> fd_addr = (LONG)lcalloc(mform -> fd_addr, psize * 2L, 1L);
  236.     if (mform -> fd_addr == NULL)
  237.       {
  238.       fclose(fp);
  239.       form_error(NoLoadMemory);
  240.       return ((BYTE *)(mform -> fd_addr = tform.fd_addr));
  241.       }
  242.     if (decompress_image_file(fp, lines, pwidth, (ULONG)wwidth*2L,
  243. mform -> fd_addr))
  244.       {
  245.       fclose(fp);
  246.       if (pwidth < 640 || lines < 400)
  247.         { /* ensure a minimum screen size form */
  248.         tform = *mform;
  249.         tform.fd_w = img -> line_length = max(img -> line_length, 640);
  250.         tform.fd_h=img -> number_of_lines=max(img -> number_of_lines, 400);
  251.         tform.fd_wdwidth = max(mform -> fd_wdwidth, 40);
  252.         if (tform.fd_addr=(LONG)lcalloc(tform.fd_wdwidth*2L,(LONG)tform.fd_h))
  253.           {
  254.           pxy[0] = pxy[1] = pxy[4] = pxy[5] = 0;
  255.           pxy[2] = pxy[6] = mform -> fd_w - 1;
  256.           pxy[3] = pxy[7] = mform -> fd_h - 1;
  257.           vro_cpyfm(vdi, S_ONLY, pxy, mform, &tform);
  258.           free(mform -> fd_addr);
  259.           *mform = tform;
  260.           }
  261.         else {
  262.           form_error(NoLoadMemory);
  263.           free(mform);
  264.           return (NULL);
  265.           }
  266.         }
  267.       return ((BYTE *)mform -> fd_addr);
  268.       }
  269.     else {
  270.       fclose(fp);
  271.       form_alert(1, BadFile);
  272.       free(mform -> fd_addr);
  273.       }
  274.     }
  275.   else
  276.     form_error(NoFile);
  277.   return (NULL);
  278.   }
  279. ."
  280. ------------------------------------------------------------------------------
  281.  
  282.  
  283. WINDING DOWN
  284. -------------
  285. Clinic is more yours than it is mine, so if you know the answer to a problem
  286. then write in. Keep the letters coming - especially the solutions and comments
  287. on other people's problems and ideas, but don't forget that the clinic depends
  288. on your problems too! Remember to include your full name and give your phone
  289. number if possible.
  290.     If you have a listing longer than about 25 lines then please include it on
  291. a disk. Putting the text of your letter on disk is helpful, too, 1st Word
  292. format if possible, but straight ASCII is fine.
  293.     If you are sending a complete program then I also like to see it running
  294. before putting it into the column, so please include a double-clickable version
  295. of your program if at all possible. If you want the disk returning include a
  296. stamped addressed envelope.
  297.  
  298. Mathew Lodge
  299. "Programmer's Clinic"
  300. "Maen Melin"
  301. Holmes Chapel Road
  302. Lach Dennis
  303. Northwich
  304. Cheshire
  305. CW9 7SZ
  306.  
  307. JANET : SOCS18 @ uk.ac.york.vaxa
  308.