home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap3 / 3_2 / testgrid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  3.8 KB  |  155 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* function prototypes */
  6. int GetValue(char *src, char *target);
  7. void CgiError(char *msg);
  8.  
  9. int main(int argc, char** argv)
  10. {
  11.     char* path_info;
  12.     char* t;
  13.     int x, y, row, col;
  14.     int cellsize, numcols, cellnum;
  15.  
  16.     /* We expect the first and only argument to be the image coordinates
  17.      * the user selected in the form "x,y".
  18.      */
  19.  
  20.     if (argc != 2) {
  21.         CgiError("Wrong number of arguments, client may not support ISMAP.");
  22.     }
  23.  
  24.     /*
  25.      * Check the selected region
  26.      */
  27.  
  28.         if (!(t = strchr(argv[1], ',')))
  29.     {
  30.         CgiError("Your client doesn't support image mapping properly.");
  31.     }
  32.  
  33.     /* Terminate the string at the comma and then
  34.      * convert the strings into numbers.
  35.      */
  36.  
  37.     *t++ = '\0';
  38.     x = atoi(argv[1]);
  39.     y = atoi(t);
  40.  
  41.     /* Some browsers actually return negative numbers for coordinates,
  42.      * which is a bug in the browser software. Check for this case and
  43.      * reject the input. The only valid range of both x and y values for
  44.      * this example is from 0 to 399 since the image has a 400 x 400 dimension.
  45.      */
  46.  
  47.     if (x < 0 || y < 0)
  48.     {
  49.         CgiError("Your browser has returned negative coordinates, which is an error. Try upgrading your browser to the latest version.");
  50.     }
  51.  
  52.     /* Store the environment variable PATH_INFO in a local variable */
  53.  
  54.     path_info = getenv("PATH_INFO");
  55.  
  56.     /* Parse out the cellsize and numcols parameters from the path info
  57.      * otherwise we cannot do anything.
  58.      */
  59.  
  60.     if (path_info == NULL
  61.          || (cellsize = GetValue(path_info, "/cellsize=")) < 1
  62.          || (numcols = GetValue(path_info, "/numcols=")) < 1)
  63.     {
  64.         CgiError("Unable to determine image size from request");
  65.     }
  66.  
  67.     /* The input is correct so output what the user clicked on */
  68.  
  69.     printf("Content-type: text/html\n\n");
  70.  
  71.     printf("<HTML>\n");
  72.     printf("<HEAD><TITLE>CGI Script How-To: Test Script</TITLE></HEAD>\n");
  73.     printf("<BODY>\n");
  74.     printf("<H1>CGI Script How-to: Test Script</H1>\n");
  75.  
  76.     printf("You clicked on the region: <b>x=%d, y=%d</b><P>\n", x, y);
  77.  
  78.     /* Calculate the row and column numbers from the (x,y) coordinates
  79.      * where column 1, row 1 is the top-left cell and column 4, row 4
  80.      * is the bottom-right cell.
  81.      */
  82.  
  83.     col = (x / cellsize) + 1;
  84.     row = (y / cellsize) + 1;
  85.  
  86.     printf("This maps to row <b>#%d</b> and column <b>#%d</b><P>\n", row, col);
  87.  
  88.     /* Given the row and column numbers just computed and the number
  89.      * of columns we can figure out the cell number.
  90.      */
  91.  
  92.     cellnum = (row - 1) * numcols + col;
  93.  
  94.     printf("The number of this cell is <b>%d</b>\n", cellnum);
  95.     printf("</BODY></HTML>\n");
  96.     exit(0);
  97. }
  98.  
  99.  
  100. /* function CgiError
  101.  *
  102.  * Displays an error message to the client and aborts the program.
  103.  *
  104.  * where msg = the error message to display.
  105.  */
  106.  
  107. void CgiError(char *msg)
  108. {
  109.     printf("Content-type: text/html\n\n");
  110.     printf("<HTML>\n");
  111.     printf("<HEAD><TITLE>Image Mapping Error</TITLE></HEAD>\n");
  112.     printf("<BODY>\n");
  113.     printf("<H1>Image Mapping Error</H1>\n");
  114.     printf("This CGI program encountered an error:\n");
  115.     printf("<P>%s\n", msg);
  116.     printf("</BODY></HTML>\n");
  117.     exit(1);
  118. }
  119.  
  120.  
  121. /* function GetValue
  122.  *
  123.  * Finds pattern within string and returns
  124.  * the corresponding "value=xxx" numeric value.
  125.  *
  126.  * where src    = string to check with extra path information
  127.  *       target = search pattern (e.g., "/cellsize=")
  128.  *
  129.  * Returns: value (non-negative number) if successful
  130.  *          -1 if target string not found or number is invalid
  131.  */
  132.  
  133. int GetValue(char *src, char *target)
  134. {
  135.     int value;
  136.     char *s = strstr(src, target);
  137.  
  138.     if (s != NULL && sscanf(s + strlen(target), "%d", &value) == 1)
  139.     {
  140.            return value;
  141.     }
  142.  
  143.     /* Note that negative values are not valid in this context so we can just
  144.      * return the value as is. Whether the value is negative or the value is
  145.      * not a number is an indication of an error. Only a positive non-zero
  146.      * value will be accepted as a successful value.
  147.      */
  148.  
  149.     return -1;        /* target not found */
  150. }
  151.  
  152. /*
  153.  * end of testgrid.c
  154.  */
  155.