home *** CD-ROM | disk | FTP | other *** search
/ Sams Cobol 24 Hours / Sams_Cobol_24_Hours.iso / Cobol32 / CRW / 32BIT / DISK6 / UFLBAR.C_ / UFLBAR.C
C/C++ Source or Header  |  1995-08-29  |  14KB  |  316 lines

  1. /*
  2. ** File:    UFLBAR.c
  3. ** Authors: Kai Chan, Craig Chaplin, Randy Spiers
  4. ** Date:    28 Oct 94
  5. **
  6. ** Purpose: Conversion functions to support Azalea bar code fonts
  7. */
  8.  
  9. #include <Windows.h>
  10. #include <math.h>
  11. #include <ctype.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <UFDll.h>
  15. #include "UFMain.h"
  16. #include "UFUser.h"
  17. #include "UFJob.h"
  18.  
  19. ExternC UFError FAR _export PASCAL NumberToPostnet (UFParamBlock * ParamBlock);
  20. ExternC UFError FAR _export PASCAL StringToPostnet (UFParamBlock * ParamBlock);
  21. ExternC UFError FAR _export PASCAL StringToCode39 (UFParamBlock * ParamBlock);
  22. ExternC UFError FAR _export PASCAL NumberToCode39 (UFParamBlock * ParamBlock);
  23.  
  24. UFFunctionDefStrings  FunctionDefStrings [] =
  25. {
  26.     {"String NumberToPostnet (Number)", NumberToPostnet},
  27.     {"String StringToPostnet (String)", StringToPostnet},
  28.    {"String StringToCode39 (String)", StringToCode39},
  29.    {"String NumberToCode39 (Number, Number)", NumberToCode39},
  30.     {NULL, NULL, NULL}
  31. };
  32.  
  33.  
  34. UFFunctionTemplates FunctionTemplates [] =
  35. {
  36.        {"NumberToPostnet (!)"},
  37.        {"StringToPostnet (!)"},
  38.        {"StringToCode39 (!)"},
  39.        {"NumberToCode39 (!, )"},
  40.        {NULL}
  41. };
  42.  
  43.  
  44. UFFunctionExamples FunctionExamples [] =
  45. {
  46.        {"\tNumberToPostnet (number)"},
  47.        {"\tStringToPostnet (string)"},
  48.        {"\tStringToCode39 (string)"},
  49.        {"\tNumberToCode39 (number, #places)"},
  50.        {NULL}
  51. };
  52.  
  53. char *ErrorTable [] =
  54. {
  55.     "no error"
  56. };
  57.  
  58. void InitJob (struct JobInfo *jobInfo)
  59. {
  60. }
  61.  
  62. void TermJob (struct JobInfo *jobInfo)
  63. {
  64. }
  65.  
  66. /******************************************************************************/
  67. /* Function:  static void GetCheckDigit (double Value, char *check)           */
  68. /*                                                                            */
  69. /* Author:    Kai Chan                                                        */
  70. /*                                                                            */
  71. /* Date:      Oct. 28, 1994                                                   */
  72. /*                                                                            */
  73. /* Purpose:   Takes a number, sums up all the digit in this number and        */
  74. /*            returns a number which when added to the sum is a multiple      */
  75. /*            of ten.                                                         */
  76. /******************************************************************************/
  77. static void GetCheckDigit (const double numval, char *check)
  78. {
  79.     double total, value, checkbit;
  80.     int dig, sign;
  81.  
  82.     value = numval;
  83.     total = 0.0;
  84.  
  85.     do {
  86.        value /= 10.0;
  87.        total += modf(value, &value);    /* add up all the remainders */
  88.     } while (value);
  89.     checkbit = (ceil(total) * 10.0)  - (total * 10.0);   /* get the checkbit  */
  90.  
  91.     if (checkbit > 9.001 || checkbit <  .999)    /* converting the checkbit to string */
  92.        wsprintf(check,"%s","0\0");
  93.     else
  94.        wsprintf(check,"%s",ecvt(checkbit,1,&dig, &sign));
  95.  
  96. }
  97.  
  98.  
  99. /******************************************************************************/
  100. /* Function:  ExternC UFError NumberToPostnet (UFParamBlock * ParamBlock)     */
  101. /*                                                                            */
  102. /* Author:    Kai Chan                                                        */
  103. /*                                                                            */
  104. /* Date:      Oct. 28, 1994                                                   */
  105. /*                                                                            */
  106. /* Purpose:   Takes a number and converts it to an Azalea PostNet string.     */
  107. /******************************************************************************/
  108. ExternC UFError FAR _export PASCAL NumberToPostnet (UFParamBlock * ParamBlock)
  109. {
  110.    UFParamListElement * Param1;
  111.    int dec, sign;
  112.    double ndig, value;
  113.    long rslt;
  114.    char err[20], check[1];
  115.  
  116.    Param1 = GetParam (ParamBlock, 1);
  117.  
  118.    if (Param1 == NULL)
  119.       return UFNotEnoughParameters;
  120.  
  121.    value = (UFTNumber)(Param1->Parameter.ParamNumber/100);
  122.  
  123.    /* get the number of digit */
  124.    if (value == 0)
  125.       ndig = 1;
  126.    else
  127.       ndig = 1 + log10(value);
  128.  
  129.       
  130.    GetCheckDigit(value, check);
  131.    wsprintf(ParamBlock->ReturnValue.ReturnString,"s%s%ss",ecvt(value, (int) ndig, &dec, &sign),
  132.         check);
  133.  
  134.    return UFNoError;
  135. }
  136.  
  137.  
  138. /******************************************************************************/
  139. /* Function:  ExternC UFError StringToPostnet (UFParamBlock * ParamBlock)     */
  140. /*                                                                            */
  141. /* Author:    Kai Chan                                                        */
  142. /*                                                                            */
  143. /* Date:      Oct. 28, 1994                                                   */
  144. /*                                                                            */
  145. /* Purpose:   Takes a string and converts it to an Azalea PostNet string.     */
  146. /******************************************************************************/
  147. ExternC UFError FAR _export PASCAL StringToPostnet (UFParamBlock * ParamBlock)
  148. {
  149.    UFParamListElement * Param1;
  150.    char *endptr, check[1];
  151.  
  152.    Param1 = GetParam (ParamBlock, 1);
  153.  
  154.    if (Param1 == NULL)
  155.       return UFNotEnoughParameters;
  156.  
  157.    GetCheckDigit(strtod(Param1->Parameter.ParamString, &endptr), check);
  158.    wsprintf(ParamBlock->ReturnValue.ReturnString,"s%s%ss",Param1->Parameter.ParamString,
  159.         check);
  160.  
  161.    return UFNoError;
  162. }
  163.  
  164. /******************************************************************************/
  165. /* Function:  static void Build39String (const char *source, char *dest)      */
  166. /*                                                                            */
  167. /* Author:    Craig Chaplin                                                   */
  168. /*                                                                            */
  169. /* Date:      Oct. 28, 1994                                                   */
  170. /*                                                                            */
  171. /* Purpose:   Converts source string into a Code 39 string which can be       */
  172. /*            translated by Azalea's Code 39 bar code fonts.  Converted       */
  173. /*            string is stored in dest.                                       */
  174. /******************************************************************************/
  175. static void Build39String (const char *source, char *dest)
  176. {
  177.    *(dest++) = '*'; // Azalea's Code 39 string prefix
  178.  
  179.    // loop until end of source string reached
  180.    while (*source) {
  181.       if (islower(*source)) {     // if source character is lower case
  182.          *(dest++) = '+';            // convert into '+X' sequence used to
  183.          *dest = toupper(*source);   // represent lowercase letters in bar code
  184.       }
  185.       else
  186.          *dest = *source;         // else just copy the character
  187.       ++source;
  188.       ++dest;
  189.    }
  190.  
  191.    *(dest++) = '*'; // Azalea's Code 39 string suffix
  192.    *dest = '\0';    // null terminated dest string
  193. }
  194.  
  195.  
  196. /******************************************************************************/
  197. /* Function:  ExternC UFError StringToCode39 (UFParamBlock * ParamBlock)      */
  198. /*                                                                            */
  199. /* Author:    Craig Chaplin                                                   */
  200. /*                                                                            */
  201. /* Date:      Oct. 28, 1994                                                   */
  202. /*                                                                            */
  203. /* Purpose:   User function that takes a string and converts it to a format   */
  204. /*            compatible with Azalea's code 39 bar code fonts.                */
  205. /******************************************************************************/
  206. ExternC UFError FAR _export PASCAL StringToCode39 (UFParamBlock * ParamBlock)
  207. {
  208.    UFParamListElement *ParamPtr; // pointer to list of parameters passed
  209.  
  210.    // check to ensure parameter was passed
  211.    if ((ParamPtr = GetParam (ParamBlock, 1)) == NULL)
  212.       return UFNotEnoughParameters;
  213.  
  214.    *(ParamBlock->ReturnValue.ReturnString) = '\0'; // null terminate return string
  215.  
  216.    // if parameter is not a null string, convert it to a Code 39 string
  217.    if (*(ParamPtr->Parameter.ParamString))
  218.       Build39String (ParamPtr->Parameter.ParamString,
  219.                    ParamBlock->ReturnValue.ReturnString);
  220.  
  221.    return UFNoError;
  222. }
  223.  
  224. /******************************************************************************/
  225. /* Function:  static void NumberToCode39String (char *dest, double cvtnum     */
  226. /*                                                        , double decnum)    */
  227. /* Author:    Randy Spiers                                                    */
  228. /*                                                                            */
  229. /* Date:      Oct. 28, 1994                                                   */
  230. /*                                                                            */
  231. /* Purpose:   Converts source number into a Code 39 string which can be       */
  232. /*            translated by Azalea's Code 39 bar code fonts.  Converted       */
  233. /*            string is stored in dest.                                       */
  234. /******************************************************************************/
  235. static void NumberToCode39String (char *dest, double cvtnum, double decnum)
  236. {
  237.   char string1[30], string2[30], string3[30];
  238.   int i, j;
  239.   int founddecimal = 0;
  240.  
  241.   if (decnum > 1100.0)                   //Limit the number of decimal places to 10
  242.      decnum = 1100.0;
  243.  
  244.   itoa((int)decnum/100, string1, 10);    //Convert decimal number input to a string
  245.  
  246.   lstrcpy(string3, "%.");                //Create the format specifier string ("%.")
  247.   lstrcat(string3, string1);             //                                   ("%.10")
  248.   lstrcat(string3, "f");                 //                                   ("%.10f")
  249.   sprintf(string2, string3, cvtnum/100);//Convert the input value to a string with the
  250.                                          // specified number of decimal places
  251.  
  252.   for (i = 0; string2[i] != '\0' && i != 17; i++) //Set a flag if there is a 
  253.     if (string2[i] == '.')                        // decimal point in the string
  254.       founddecimal = 1;
  255.  
  256. //  MessageBox(NULL, string2, "String 2 before null added", MB_OK);
  257.  
  258.   if (lstrlen(string2) > 17 && !founddecimal)  //If the numeric string is longer than 17
  259.   {
  260.     string2[17] = '\0';                        // characters AND there is no decimal, 
  261.                                                // set the 18th character to end-of-line.
  262.  
  263.     MessageBox(NULL, "The value you have input is too large.\n Output will be truncated."
  264.                    , "Input Value Exceeds Limit", MB_ICONEXCLAMATION | MB_OK);
  265.   }
  266.   else                                         
  267.     if (lstrlen(string2) > 17 && founddecimal) //If the numeric string is longer than 17
  268.       string2[18] = '\0';                      // characters AND there is a decimal, 
  269.                                                // set the 19th character to end-of-line.
  270.  
  271. //  MessageBox(NULL, string2, "String 2 after null added", MB_OK);
  272.  
  273.   for (i = 0; string2[i] != '.' && string2[i] != '\0'; i++) //Check if there is a decimal
  274.     ;                                                       // point in the string.
  275.  
  276.   if (string2[i] == '.')                               //if the string contains a decimal
  277.   {                                                    // point, advance until a maximum of
  278.     for (j = 0; string2[i] != '\0' && j < 10; i++, j++)// ten more characters are read or the
  279.       ;                                                // end-of-line character is reached.
  280.     string2[++i] = '\0';                               // Put in an end-of-line character
  281.   }                                                     
  282.     
  283.   lstrcpy(string1, "*");                 //Place asterisk in front of string
  284.   lstrcat(string1, string2);             //Append string
  285.   lstrcat(string1, "*");                 //Place asterisk at end of string
  286.  
  287.     lstrcpy(dest, string1);                //Copy the string to the destination
  288. }
  289.  
  290. /******************************************************************************/
  291. /* Function:  ExternC UFError NumberToCode39 (UFParamBlock * ParamBlock)      */
  292. /*                                                                            */
  293. /* Author:    Randy Spiers                                                    */
  294. /*                                                                            */
  295. /* Date:      Oct. 28, 1994                                                   */
  296. /*                                                                            */
  297. /* Purpose:   User function that takes a number and converts it to a format   */
  298. /*            compatible with Azalea's code 39 bar code fonts.                */
  299. /******************************************************************************/
  300. ExternC UFError FAR _export PASCAL NumberToCode39 (UFParamBlock * ParamBlock)
  301. {
  302.     UFParamListElement *FirstParam, *SecondParam;
  303.     FirstParam = GetParam (ParamBlock, 1);
  304.     SecondParam = GetParam (ParamBlock, 2);
  305.  
  306.     if (FirstParam == NULL || SecondParam == NULL)
  307.        return UFNotEnoughParameters;
  308.  
  309.     NumberToCode39String (ParamBlock->ReturnValue.ReturnString,
  310.                          FirstParam->Parameter.ParamNumber,
  311.                          SecondParam->Parameter.ParamNumber);
  312.  
  313.     return UFNoError;
  314. }
  315.  
  316.