home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 343_01 / cips2.c < prev    next >
C/C++ Source or Header  |  1991-05-24  |  18KB  |  665 lines

  1.  
  2.         /*****************************************************
  3.         *
  4.         *       file d:\cips\rstring.c
  5.         *
  6.         *       Functions: This file contains
  7.         *            read_string
  8.         *            clear_buffer
  9.         *            long_clear_buffer
  10.         *
  11.         *       Purpose: This function reads a string of input
  12.         *            from the keyboard.
  13.         *
  14.         *******************************************************/
  15.  
  16. #include "d:\cips\cips.h"
  17.  
  18.  
  19. read_string(string)
  20.         char *string;
  21. {
  22.         int     eof,
  23.                 letter,
  24.                 no_error;
  25.  
  26.         eof = -1;
  27.         no_error = 0;
  28.  
  29.         while((letter = getchar()) != '\n' &&
  30.                letter !=  eof)
  31.            *string++ = letter;
  32.  
  33.         *string = '\0';
  34.  
  35.         return((letter == eof) ? eof : no_error);
  36.  
  37. }       /* ends read_string */
  38.  
  39.  
  40.  
  41. clear_buffer(string)
  42.    char string[];
  43. {
  44.    int i;
  45.    for(i=0; i<MAX_NAME_LENGTH; i++)
  46.       string[i] = ' ';
  47. }
  48.  
  49.  
  50.  
  51. long_clear_buffer(string)
  52.    char string[];
  53. {
  54.    int i;
  55.    for(i=0; i<300; i++)
  56.       string[i] = ' ';
  57. }
  58.  
  59.  
  60.            /******************************************************
  61.            *
  62.            *       file d:\cips\mof.c
  63.            *
  64.            *       Functions: This file contains
  65.            *          my_open
  66.            *
  67.            *       Purpose: This function opens a file. Borland's
  68.            *          Turbo C opens files a little different from
  69.            *          the standard UNIX C. Instead of using this
  70.            *          different method in a number of various files,
  71.            *          the method is placed in this one file. If the
  72.            *          programs are moved to another system, all changes
  73.            *          will be located in this one place.
  74.            *
  75.            *       External Calls:
  76.            *          none
  77.            *
  78.            *       Modifications:
  79.            *          18 June 1987 - created
  80.            *
  81.            ****************************************************/
  82.  
  83.  
  84.  
  85. my_open(file_name)
  86.    char file_name[];
  87. {
  88.    int file_descriptor;
  89.  
  90.    file_descriptor = open(file_name, O_RDWR | O_CREAT | O_BINARY, 
  91.                           S_IWRITE);
  92.  
  93.    return(file_descriptor);
  94.  
  95.  
  96. }  /* ends my_open  */
  97.  
  98.        /*****************************************************
  99.        *
  100.        *       file c:\lsu\mrw.c
  101.        *
  102.        *       Functions: This file contains
  103.        *           my_read
  104.        *           my_write
  105.        *
  106.        *       Purpose: These two functions call the Turbo C
  107.        *           functions _read and _write. All software
  108.        *           will use my_read and my_write so that if
  109.        *           the software is ported to another system
  110.        *           that uses read and write changing the
  111.        *           two functions in this file will take care
  112.        *           of the move.
  113.        *
  114.        *       External Call:
  115.        *           read
  116.        *           _write
  117.        *
  118.        *       Modifications:
  119.        *           10 June 1987 - created
  120.        *
  121.        *
  122.        ************************************************************/
  123.  
  124.  
  125. my_read(file_descriptor, buffer, number_of_bytes)
  126.    int  file_descriptor, number_of_bytes;
  127.    char *buffer;
  128. {
  129.    int bytes_read;
  130.    int read();
  131.    bytes_read = read(file_descriptor, buffer, number_of_bytes);
  132.    return(bytes_read);
  133. }
  134.  
  135.  
  136. my_write(file_descriptor, buffer, number_of_bytes)
  137.    int  file_descriptor, number_of_bytes;
  138.    char *buffer;
  139. {
  140.    int bytes_written;
  141.    int write();
  142.    bytes_written = write(file_descriptor, buffer, number_of_bytes);
  143.    return(bytes_written);
  144. }
  145.  
  146.         /******************************************************
  147.         *
  148.         *        file d:\cips\gpcips.c
  149.         *
  150.         *       Functions: This file contains
  151.         *           get_parameters
  152.         *           show_parameters
  153.         *
  154.         *       Purpose - These functions get image parameters.
  155.         *
  156.         *       External Calls:
  157.         *           rstring.c - read_string
  158.         *           intcvrt.c - get_integer
  159.         *
  160.         *       Modifications:
  161.         *           19 February 1987 - These functions were taken out
  162.         *                of the file ip.c.
  163.         *           28 June 1990 - changed to gpcips and the channel
  164.         *                parameter was removed.
  165.         *
  166.         *******************************************************/
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. get_parameters(il, ie, ll, le)
  174.         int *il, *ie, *le, *ll;
  175. {
  176.         int choice, not_finished;
  177.  
  178.         not_finished = 1;
  179.         while(not_finished){
  180.            show_parameters(il, ie, ll, le);
  181.            printf("\n\nEnter choice to change (enter 0 for no changes) __\b\b");
  182.            get_integer(&choice);
  183.  
  184.            switch (choice){
  185.               case 0:
  186.                  not_finished = 0;
  187.                  break;
  188.  
  189.               case 1:
  190.                  break;
  191.  
  192.               case 2:
  193.                  printf("\nEnter initial line\n___\b\b\b");
  194.                  get_integer(il);
  195.                  break;
  196.  
  197.               case 3:
  198.                  printf("\nEnter initial element\n___\b\b\b");
  199.                  get_integer(ie);
  200.                  break;
  201.  
  202.               case 4:
  203.                  printf("\nEnter last line\n___\b\b\b");
  204.                  get_integer(ll);
  205.                  break;
  206.  
  207.               case 5:
  208.                  printf("\nEnter last element\n___\b\b\b");
  209.                  get_integer(le);
  210.                  break;
  211.  
  212.            }  /* ends switch choice       */
  213.         }     /* ends while not_finished  */
  214. }             /* ends get_parameters      */
  215.  
  216.  
  217. show_parameters(il, ie, ll, le)
  218.         int *il, *ie, *le, *ll;
  219. {
  220.    printf("\n\nThe image parameters are:");
  221.    printf("\n\t2.      il = %4d", *il);
  222.    printf("\n\t3.      ie = %4d", *ie);
  223.    printf("\n\t4.      ll = %4d", *ll);
  224.    printf("\n\t5.      le = %4d", *le);
  225. }  /* ends show_parameters  */
  226.  
  227.        /******************************************************
  228.        *
  229.        *       file d:\cips\intcvrt.c
  230.        *
  231.        *       Functions: This file contains
  232.        *           get_integer
  233.        *           int_convert
  234.        *
  235.        *       Purpose: These functions convert a string of
  236.        *                characters to their number value.
  237.        *
  238.        *       Modifications:
  239.        *            Taken from Jamsa's software package
  240.        *            and altered to fit into the computer
  241.        *            vision programming 22 August 1986.
  242.        *
  243.        *******************************************************/
  244.  
  245.  
  246. #include "d:\cips\numdefs.h"
  247.  
  248.  
  249. get_integer(n)
  250.    int *n;
  251. {
  252.    char string[80];
  253.  
  254.    read_string(string);
  255.    int_convert(string, n);
  256. }
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  int_convert (ascii_val, result)
  263.     char *ascii_val;
  264.     int *result;
  265.   {
  266.     int sign = 1;  /* -1 if negative */
  267.  
  268.     *result = 0;   /* value returned to the calling routine */
  269.  
  270.     /* read passed blanks */
  271.  
  272.     while (is_blank(*ascii_val))
  273.        ascii_val++;              /* get next letter */
  274.  
  275.     /* check for sign */
  276.  
  277.     if (*ascii_val == '-' || *ascii_val == '+')
  278.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  279.  
  280.    /*
  281.     * convert the ASCII representation to the actual
  282.     * decimal value by subtracting '0' from each character.
  283.     *
  284.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  285.     * by subtracting '0' (or 48 in decimal) we get the desired
  286.     * value.
  287.     *
  288.     * if we have already converted '9' to 9 and the next character
  289.     * is '3', we must first multiply 9 by 10 and then convert '3'
  290.     * to decimal and add it to the previous total yielding 93.
  291.     *
  292.     */
  293.  
  294.     while (*ascii_val)
  295.      if (is_digit(*ascii_val))
  296.        *result = *result * 10 + to_decimal(*ascii_val++);
  297.  
  298.      else
  299.        return (IO_ERROR);
  300.  
  301.  
  302.     *result = *result * sign;
  303.  
  304.     return (NO_ERROR);
  305.   }
  306.  
  307.  
  308.  
  309.  
  310.  
  311.          /************************************************
  312.          *
  313.          *         Functions: This file contains
  314.          *           get_short
  315.          *             short_convert
  316.          *
  317.          *         Purpose: These functions convert a string of
  318.          *                characters to their number value.
  319.          *
  320.          *         Modifications:
  321.          *            Taken from Jamsa's software package
  322.          *            and altered to fit into the computer
  323.          *            vision programming 22 August 1986.
  324.          *
  325.          *************************************************/
  326.  
  327.  
  328. get_short(n)
  329.    short *n;
  330. {
  331.    char string[80];
  332.  
  333.    read_string(string);
  334.    short_convert(string, n);
  335. }
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  short_convert (ascii_val, result)
  342.     char *ascii_val;
  343.     short *result;
  344.   {
  345.     int sign = 1;  /* -1 if negative */
  346.  
  347.     *result = 0;   /* value returned to the calling routine */
  348.  
  349.     /* read passed blanks */
  350.  
  351.     while (is_blank(*ascii_val))
  352.        ascii_val++;              /* get next letter */
  353.  
  354.     /* check for sign */
  355.  
  356.     if (*ascii_val == '-' || *ascii_val == '+')
  357.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  358.  
  359.    /*
  360.     * convert the ASCII representation to the actual
  361.     * decimal value by subtracting '0' from each character.
  362.     *
  363.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  364.     * by subtracting '0' (or 48 in decimal) we get the desired
  365.     * value.
  366.     *
  367.     * if we have already converted '9' to 9 and the next character
  368.     * is '3', we must first multiply 9 by 10 and then convert '3'
  369.     * to decimal and add it to the previous total yielding 93.
  370.     *
  371.     */
  372.  
  373.     while (*ascii_val){
  374.      if (is_digit(*ascii_val)){
  375.        *result = *result * 10 + to_decimal(*ascii_val++);
  376.        if( (sign == -1) && (*result > 0)) *result = *result * -1;
  377.      }
  378.      else
  379.        return (IO_ERROR);
  380.     }  /* ends while ascii_val  */
  381.  
  382.     return (NO_ERROR);
  383.   }
  384.  
  385.  
  386.  
  387.  
  388.  
  389.          /***********************************************
  390.          *
  391.          *         file d:\cips\locvrt.c
  392.          *
  393.          *         Functions: This file contains
  394.          *           get_long
  395.          *             long_convert
  396.          *
  397.          *         Purpose: These functions convert a string of
  398.          *                characters to their number value.
  399.          *
  400.          *         Modifications:
  401.          *            Taken from Jamsa's software package
  402.          *            and altered to fit into the computer
  403.          *            vision programming 22 August 1986.
  404.          *
  405.          ************************************************/
  406.  
  407.  
  408. get_long(n)
  409.    long *n;
  410. {
  411.    char string[80];
  412.  
  413.    read_string(string);
  414.    long_convert(string, n);
  415. }
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  long_convert (ascii_val, result)
  422.     char *ascii_val;
  423.     long *result;
  424.   {
  425.     int sign = 1;  /* -1 if negative */
  426.  
  427.     *result = 0;   /* value returned to the calling routine */
  428.  
  429.     /* read passed blanks */
  430.  
  431.     while (is_blank(*ascii_val))
  432.        ascii_val++;              /* get next letter */
  433.  
  434.     /* check for sign */
  435.  
  436.     if (*ascii_val == '-' || *ascii_val == '+')
  437.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  438.  
  439.    /*
  440.     * convert the ASCII representation to the actual
  441.     * decimal value by subtracting '0' from each character.
  442.     *
  443.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  444.     * by subtracting '0' (or 48 in decimal) we get the desired
  445.     * value.
  446.     *
  447.     * if we have already converted '9' to 9 and the next character
  448.     * is '3', we must first multiply 9 by 10 and then convert '3'
  449.     * to decimal and add it to the previous total yielding 93.
  450.     *
  451.     */
  452.  
  453.     while (*ascii_val)
  454.      if (is_digit(*ascii_val))
  455.        *result = *result * 10 + to_decimal(*ascii_val++);
  456.      else
  457.        return (IO_ERROR);
  458.  
  459.  
  460.     *result = *result * sign;
  461.  
  462.     return (NO_ERROR);
  463.   }
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.      /*******************************************************
  471.      *
  472.      *   file d:\cips\flocvrt.c
  473.      *
  474.      *   Functions: This file contains
  475.      *       get_float
  476.      *           float_convert
  477.      *       power
  478.      *
  479.      *   Purpose: This function converts a string of
  480.      *           characters to its number value.
  481.      *
  482.      *   Modifications:
  483.      *            This was taken from Jamsa's software
  484.      *            packages and modified to work in the
  485.      *            computer vision programs 22 August 1986.
  486.      *
  487.      *            16 June 1987 - the power function was not working
  488.      *                so Borland's Turbo C function pow10
  489.      *                was substituted for it.
  490.      *
  491.      ****************************************************/
  492.  
  493.  
  494. float get_float(f)
  495.    float *f;
  496. {
  497.    char string[80];
  498.  
  499.    read_string(string);
  500.    float_convert(string, f);
  501. }
  502.  
  503.  float_convert (ascii_val, result)
  504.     char *ascii_val;
  505.     float *result;
  506.   {
  507.     int count;            /* # of digits to the right of the
  508.                              decimal point. */
  509.     int sign = 1;         /* -1 if negative */
  510.  
  511.     double pow10();       /* Turbo C function */
  512.     float power();        /* function returning a value raised
  513.                              to the power specified. */
  514.  
  515.     *result = 0.0;        /* value desired by the calling routine */
  516.  
  517.     /* read passed blanks */
  518.  
  519.     while (is_blank(*ascii_val))
  520.        ascii_val++;              /* get the next letter */
  521.  
  522.     /* check for a sign */
  523.  
  524.     if (*ascii_val == '-' || *ascii_val == '+')
  525.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  526.  
  527.  
  528.    /*
  529.     * first convert the numbers on the left of the decimal point.
  530.     *
  531.     * if the number is 33.141592  this loop will convert 33
  532.     *
  533.     * convert ASCII representation to the  actual decimal
  534.     * value by subtracting '0' from each character.
  535.     *
  536.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  537.     * by subtracting '0' (or 48 in decimal) we get the desired
  538.     * value.
  539.     *
  540.     * if we have already converted '9' to 9 and the next character
  541.     * is '3', we must first multiply 9 by 10 and then convert '3'
  542.     * to decimal and add it to the previous total yielding 93.
  543.     *
  544.     */
  545.  
  546.     while (*ascii_val)
  547.      if (is_digit(*ascii_val))
  548.        *result = *result * 10 + to_decimal(*ascii_val++);
  549.  
  550.      else if (*ascii_val == '.')  /* start the fractional part */
  551.        break;
  552.  
  553.      else
  554.        return (IO_ERROR);
  555.  
  556.  
  557.    /*
  558.     * find number to the right of the decimal point.
  559.     *
  560.     * if the number is 33.141592 this portion will return 141592.
  561.     *
  562.     * by converting a character and then dividing it by 10
  563.     * raised to the number of digits to the right of the
  564.     * decimal place the digits are placed in the correct locations.
  565.     *
  566.     *     4 / power (10, 2) ==> 0.04
  567.     *
  568.     */
  569.  
  570.     if (*ascii_val != NULL2)
  571.      {
  572.         ascii_val++;   /* past decimal point */
  573.  
  574.         for (count = 1; *ascii_val != NULL2; count++, ascii_val++)
  575.  
  576.               /*********************************************
  577.               *
  578.               *   The following change was made 16 June 1987.
  579.               *   For some reason the power function below
  580.               *   was not working. Borland's Turbo C pow10
  581.               *   was substituted.
  582.               *
  583.               ***********************************************/
  584.          if (is_digit(*ascii_val)){
  585.            *result = *result + to_decimal(*ascii_val)/power(10.0,count);
  586.         /***********
  587.         *result = *result + to_decimal(*ascii_val)/((float)(pow10(count)));
  588.         ************/
  589.           }
  590.  
  591.          else
  592.           return (IO_ERROR);
  593.      }
  594.     *result = *result * sign; /* positive or negative value */
  595.  
  596.     return (NO_ERROR);
  597.   }
  598.  
  599.  
  600. float power(value, n)
  601.    float value;
  602.    int n;
  603. {
  604.    int   count;
  605.    float result;
  606.  
  607.    if(n < 0)
  608.       return(-1.0);
  609.  
  610.    result = 1;
  611.    for(count=1; count<=n; count++){
  612.       result = result * value;
  613.    }
  614.  
  615.    return(result);
  616. }
  617.  
  618. /*
  619.  * NAME: append_string (string1, string2)
  620.  *
  621.  * FUNCTION: Appends the contents of string1 to the contents
  622.  *           of string2.
  623.  *
  624.  * EXAMPLE:  string1    string2 before   string2 after
  625.  *           " there!"      "Hello"      "Hello there!"
  626.  *
  627.  *           append_string (string1, string2);
  628.  *
  629.  * VARIABLES USED:  string1: string to append.
  630.  *                  string2: string being appended to.
  631.  *                  index1:  index into string1.
  632.  *                  index2:  index into string2.
  633.  *
  634.  * PSEUDO CODE:  initialize index2 to 0
  635.  *
  636.  *               while (string2[index2] is not NULL)
  637.  *                  increment index2  -- to find the end of string2
  638.  *
  639.  *               initialize index1 to 0
  640.  *
  641.  *               while (string2[index2] is assigned the letter in
  642.  *                      string1[index1] and the letter is not NULL)
  643.  *                  increment index1 and index2
  644.  *
  645.  */
  646.  
  647.  
  648.  append_string (string1, string2)
  649.    char string1[];          /* string to append */
  650.    char string2[];                 /* string appended to */
  651.   {
  652.    int index1;       /* current location in string1 */
  653.    int index2;    /* current location in string2 */
  654.  
  655.    /* find the end of string2 */
  656.  
  657.    for (index2 = 0; string2[index2] != '\0'; index2++)
  658.      ;
  659.  
  660.    /* append string1 to string2 */
  661.  
  662.    for (index1 = 0;(string2[index2] = string1[index1])!= '\0'; ++index1)
  663.        ++index2;   /* also increment string2 */
  664.   }
  665.