home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / recio213.zip / usage.txt < prev    next >
Text File  |  1995-09-05  |  34KB  |  945 lines

  1.     Title: STANDARD USAGE OF C LANGUAGE RECIO LIBRARY
  2. Copyright: (C) 1994-1995, William Pierpoint
  3.   Version: 2.13
  4.      Date: September 4, 1995
  5.  
  6.  
  7.  
  8. 1.0 INTRODUCTION
  9.  
  10. The implementation descibed by this standard usage is a superset of the
  11. recio specification.  Enhancements are noted in the text.
  12.  
  13.  
  14. 1.1 Mnemonics
  15.  
  16. The recio functions have been given a consistent mnemonic naming
  17. convention.  All recio functions are in lower case and start with
  18. the letter r.  Function names are analogous to <stdio.h> functions.
  19. Mnemonics are as follows:
  20.  
  21. Single letter (field functions)               Multi-letter
  22. ----------------------------------------      -----------------
  23. b - base (prefix)                             beg - beginning
  24. c - column (prefix), character (suffix)       ch  - character
  25. d - double (suffix)                           col - column
  26. f - float (suffix)                            cxt - context
  27. i - integer (suffix)                          eof - end of file
  28. l - long (suffix)                             err - error
  29. n - number                                    fld - field buffer
  30. r - record pointer (first letter)             fmt - format  
  31. s - string pointer (suffix)                   fn  - function
  32. t - time_t time (suffix)                      no  - number         
  33. u - unsigned (suffix)                         num - number
  34.                                                 rec - record buffer  
  35.                           siz - size of buffer
  36.                           str - string
  37.                           tm  - struct tm time
  38.                           txt - text           
  39.  
  40. 1.2 Order
  41.  
  42. The order in which the prefix mnemonics appear indicates the order in which
  43. the arguments appear in the function.  The suffix mnemonics of the input
  44. functions tell you what the function returns.  The suffix mnemonics of the
  45. output functions indicate the last argument and tell you what the function
  46. outputs.  All output functions return an integer with a zero value if the
  47. function executed successfully.
  48.  
  49. For example, the input function rbgetui():
  50.  
  51.     arguments:  r - record pointer
  52.                 b - base (radix) of input
  53.       returns: ui - unsigned integer
  54.  
  55. The output function rbputui():
  56.  
  57.     arguments:  r - record pointer
  58.                 b - base (radix) of input
  59.                ui - unsigned integer is output
  60.  
  61. Note: c is used in the prefix of a function's name only once even if there are
  62.       two column arguments.  If the function inputs or outputs a character,
  63.       there is only one column argument; otherwise there are two.
  64.  
  65.  
  66.  
  67. 2.0 ERRORS AND WARNINGS
  68.  
  69. The functions declared in the header <recio.h> make use of the errno macro
  70. defined in section 4.1.3 of ANSI X3.159-1989.  This mechanism was chosen
  71. because (1) the <stdlib.h> conversion functions (strtod(), strtol(), etc.)
  72. make use of this error reporting mechanism and (2) the <recio.h> functions
  73. make use of the <stdlib.h> conversion functions.
  74.  
  75. In this implementation, errno can return the following macro constants:
  76.  
  77.          0 - No error.
  78.     EACCES - permission denied.
  79.       EDOM - argument out of domain.
  80.     EINVAL - invalid argument.
  81.     EMFILE - too many open files.
  82.     ENOENT - no such file or directory.
  83.     ENOMEM - out of memory.
  84.     ERANGE - out of range.
  85.  
  86. where
  87.  
  88. * EACCESS means that you don't have permission to access this file.  All
  89.   MSDOS files have read permission.
  90.  
  91. * EDOM says that the argument is ouside of the defined domain.  For 
  92.   example, if the days of the month are defined as 1 to 31 and you 
  93.   convert a string "1/32/1995" using sftotm(), an EDOM error will occur.
  94.  
  95. * EINVAL indicates an invalid argument to a function, usually a NULL record
  96.   pointer.  This is most often caused by a programming error.
  97.  
  98. * EMFILE means the program tried to open more files than the maximum allotted
  99.   by ROPEN_MAX or FOPEN_MAX.  If your program is interactive, the user can
  100.   close one or more open record streams.  Or you might decide that ROPEN_MAX
  101.   or FOPEN_MAX needs to be a larger value.
  102.  
  103. * ENOENT says that ropen() could not find the requested file to open.
  104.   Perhaps the name of the file was misspelled, or your program looked in
  105.   wrong directory.  If your program was trying to read a configuration file,
  106.   it could use internal default values when the configuration file does
  107.   not exist.
  108.  
  109. * ENOMEM indicates that the program ran out of heap space.  You may be able
  110.   to correct this if you are able to deallocate memory you no longer need.
  111.   For example, you could reduce the size of buffers when the size only
  112.   affects speed.  Such buffers need to be flushed first.  Buffers used by
  113.   the recio library do not fit this criteria.
  114.  
  115. * ERANGE tells you that the data is outside the capabilities of the data 
  116.   type to represent it.  The data value is either too large or too small.  
  117.   Either the data is incorrect or you need a more robust data type to hold 
  118.   the data.
  119.  
  120. Beginning with version 1.1, recio functions set errno when the record
  121. pointer is invalid and set an internal error number when the record pointer
  122. is valid.  The recio error number is accessed through the rerror function.
  123.  
  124. The rerror function can return the following macro constants:
  125.  
  126.          0 - No error.
  127.     R_EDOM - out of domain.
  128.   R_EINVAL - invalid argument (not the record pointer).
  129.  R_EINVDAT - invalid data.
  130.  R_EINVMOD - invalid mode.
  131.  R_EMISDAT - missing data.
  132.   R_ENOMEM - out of memory.
  133.   R_ENOPUT - unable to write data to output.
  134.   R_ERANGE - out of range.
  135.   R_EFAULT - application defined error.
  136.  
  137. where
  138.  
  139. * R_EDOM indicates an argument to a function is outside the defined 
  140.   domain.  For example, if base is defined to have a valid range of 
  141.   2 - 32 and you pass a 1, an R_EDOM error occurs.
  142.   
  143.   If the following conditions are not met, an R_EDOM error will result:
  144.   
  145.   Input of integral numbers:  2 <= base <= 32 or base==0
  146.   Output of integral numbers: 2 <= base <= 32
  147.   Input of time:              1 <= month <= 12
  148.                               1 <= day <= 31
  149.                               0 <= hour <= 23
  150.                               0 <= minute <= 59
  151.                               0 <= second <= 61 (includes 2 leap seconds)
  152.  
  153. * R_EINVDAT says the data is invalid.  Invalid data is caused by an
  154.   unrecognized character in the field.  For example, if you read an 
  155.   integral number that contained a letter, a R_EINVDAT error would 
  156.   occur.
  157.  
  158. * R_EINVMOD indicates that you opened a file in read mode, then used an 
  159.   output function or opened a file in write or append mode, then used an 
  160.   input function.
  161.  
  162. * R_EMISDAT says the data is missing.  Missing data means the field is empty.
  163.   If you expect a number, you could substitute either zero or some unique
  164.   number to indicate an empty field.
  165.  
  166. * R_ENOMEM indicates that the program ran out of heap space.  You may be able
  167.   to correct this if you are able to deallocate memory you no longer need.
  168.   For example, you could reduce the size of buffers when the size only
  169.   affects speed.  Such buffers need to be flushed first.  Buffers used by
  170.   the recio library do not fit this criteria.
  171.  
  172. * R_ENOPUT says the program was unable to write the data to the output.  
  173.   One possible cause is that the disk is full.
  174.  
  175. * R_ERANGE tells you that the data is outside the capabilities of the
  176.   data type to represent it.  For instance, suppose you used rgeti() 
  177.   to get an integer and the data value is 32768.  If a 16-bit integer 
  178.   has an upper limit of 32767, the value is too large.  If the data is 
  179.   wrong, you can have the error function correct it.  If the data is 
  180.   right, you need to correct the data type within the program.
  181.  
  182.   If the following types of data are not within the following ranges,
  183.   a R_ERANGE error will occur:
  184.   
  185.   integer: INT_MIN <= x <= INT_MAX
  186.   long: LONG_MIN <= x <= LONG_MAX
  187.   unsigned integer: 0 <= x <= UINT_MAX
  188.   unsigned long: 0 <= x <= ULONG_MAX
  189.   float: -FLT_MAX <= x <= -FLT_MIN or FLT_MIN <= x <= FLT_MAX or x==0.0
  190.   double: -DBL_MAX <= x <= -DBL_MIN or DBL_MIN <= x <= DBL_MAX or x==0.0
  191.   time_t: TIME_T_MIN <= x <= TIME_T_MAX
  192.  
  193. * R_EFAULT can be used with rseterr() in your own application.
  194.  
  195. The recio library also has warning numbers.  There is not a global warning 
  196. number equivalent to errno.  The warning indicator is associated with a 
  197. record stream.  Thus warnings can only be set for valid record streams.  
  198. The warning number for a record stream is accessed through the rwarning 
  199. function.
  200.  
  201. The rwarning function can return the following macro constants:
  202.  
  203.          0 - No warning.
  204.  R_WEMPSTR - empty data string.
  205.   R_WNOREG - unable to register exit function with atexit().
  206.   R_WWIDTH - data too wide for columnar output.
  207.   R_WTMFMT - incomplete data in a time field.
  208.   R_WFAULT - application defined warning.
  209.  
  210. where
  211.  
  212. * R_WEMPSTR says that an empty data string was input.  If you want to
  213.   substitute another string from within your program, use the rsetfldstr 
  214.   function in your callback warning function to place the new string 
  215.   into the field buffer.
  216.  
  217. * R_WWIDTH indicates that on output, the data will not fit between the
  218.   columns specified.  If the data is numeric, the recio output functions
  219.   write asterisks to the output; if the data is a string, a truncated string
  220.   is written.
  221.  
  222. * R_WNOREG means the program was unable to register the internal recio exit
  223.   function with the ANSI atexit() function.  The internal recio exit
  224.   function ensures that all open record streams are closed and all dynamic
  225.   memory allocated by the recio library is deallocated.
  226.  
  227. * R_WTMFMT means the data in a time field was incomplete.  For example, 
  228.   the time format may have been looking for a date and a time of day but 
  229.   the time field only contained a date.  Data is only compared left to 
  230.   right.  Any mismatch in the delimiters that separate the time elements 
  231.   (month, hour, etc) generates an R_EINVDAT error.
  232.  
  233. * R_WFAULT can be used with rsetwarn() in your own applications.
  234.  
  235.  
  236. 2.1 Define Callback Error Function
  237.  
  238. First define a callback error function to be used by the recio functions.
  239. You may give the function any name you wish.  In the sample function below,
  240. the name rerrfn is used.  The function takes one argument, a record pointer
  241. (REC *).  It returns nothing (void).  The function must first check for a
  242. valid record pointer using the risvalid function.  Other than that, you can
  243. customize it to do whatever you want.
  244.  
  245. A simple callback error function rerrmsg is included in the RECIO library.  
  246. In the initial prototyping stages of development, you may wish to use this 
  247. function rather than taking the time to develop your own function.  Later 
  248. you can substitute a more robust callback error function.
  249.  
  250. The recio functions use a callback error function in order to give the most 
  251. flexibility in handling errors.  The rerrfn function shown below just sends
  252. information to stderr.  You may wish to send information to a printer, a 
  253. file, a window, or a dialog box.  You might even want to give users the 
  254. ability to examine errors and enter corrections.  If the error is corrected, 
  255. you will want to call the rclearerr function before your callback error 
  256. function returns.
  257.  
  258. When your callback error function is invoked, check rerror() or errno
  259. to determine the cause of the error.
  260.  
  261. The main purpose of this sample callback error function is to show some of
  262. kinds of things you can do in a callback error function.  Note that when an
  263. error occurs, the column number indicator rcolno() has moved just beyond
  264. the error.  To make it clearer to the user where the error occurred, rerrfn()
  265. displays rcolno()-1, but not less than rbegcolno(), the column number for the
  266. first column.
  267.  
  268. A more detailed callback error function is given in the source code for the
  269. test program TESTCHG.C.  The test program callback error function makes use
  270. of the rfix functions to fix up bad data (primarily overflows and underflows)
  271. and continue processing.  If appropriate for your application, you can use
  272. these functions as well.  They have been compiled into the recio libraries
  273. for your potential use.
  274.  
  275. /* define callback error function */
  276. void rerrfn(REC *rp)
  277. {
  278.     /* if rp is a valid record pointer */
  279.     if (risvalid(rp)) {
  280.  
  281.       /* if reof indicator set */
  282.       if (reof(rp)) {
  283.           fprintf(stderr, "ERROR reading %s -- "
  284.            "tried to read past end of file\n\n", rnames(rp));
  285.  
  286.       /* else rerror indicator set */
  287.       } else {
  288.  
  289.           /* determine cause of error */
  290.           switch (rerror(rp)) {
  291.  
  292.           /* input data errors */
  293.           case R_ERANGE:
  294.           case R_EINVDAT:
  295.           case R_EMISDAT:
  296.           /* output data errors */
  297.           case R_ENOPUT:
  298.  
  299.               /* print location of error */
  300.               fprintf(stderr, "DATA ERROR in FILE %s at LINE %ld,"
  301.                " FIELD %u, COLUMN %u -- %s\n", rnames(rp), rrecno(rp),
  302.                rfldno(rp), max(rcolno(rp)-1, rbegcolno(rp)), rerrstr(rp));
  303.               break;
  304.  
  305.           /* fatal errors (R_EINVMOD, R_EINVAL, R_ENOMEM) */
  306.           default:
  307.             fprintf(errout, "FATAL ERROR reading FILE %s -- %s\n",
  308.              rnames(rp), rerrstr(rp));
  309.             abort();
  310.             break;
  311.           }
  312.       }
  313.  
  314.     /* else invalid record pointer */
  315.     } else {
  316.         switch (errno) {
  317.  
  318.         /* non-fatal errors */
  319.         case EACCES:
  320.         case EMFILE:
  321.           fprintf(errout, "WARNING: %s\n", strerror(errno));
  322.           break;
  323.  
  324.         /* fatal errors (EINVAL, ENOMEM) */
  325.         default:
  326.           fprintf(errout, "FATAL ERROR: %s\n", strerror(errno));
  327.           abort();
  328.           break;
  329.         }
  330.     }
  331. }
  332.  
  333.  
  334. 2.2 Define Callback Warning Function
  335.  
  336. Next define a callback warning function.  You may give the function any name
  337. you want.  In the sample function below, the name rwarnfn is used.  The
  338. function takes one argument, a record pointer (REC *).  It returns nothing
  339. (void).
  340.  
  341. A simple callback warning function rwarnmsg is included in the recio library.  
  342. In the initial prototyping stages of development, you may wish to use this 
  343. function rather than taking the time to develop your own function.  Later 
  344. you can substitute a more robust callback warning function.
  345.  
  346. The recio functions use a callback warning function in order to give the
  347. most flexibility in handling unusual conditions.  For example, the recio
  348. library considers empty data strings to be legal but your application may
  349. want to flag an empty data string as a data error.  You can do that by
  350. checking for R_WEMPSTR warnings in your callback warning function.
  351.  
  352. When your callback warning function is invoked, check rwarning() to
  353. determine the cause of the warning.
  354.  
  355. You can also use your callback warning function to keep track of the number
  356. of warnings, then print a summary of any warnings just before you close a
  357. record stream.  You will find an example in the test program TESTCHP.C.
  358.  
  359. void rwarnfn(REC *rp)
  360. {
  361.   if (risvalid(rp)) {
  362.     switch (rwarning(rp)) {
  363.     case R_WNOREG:   /* atexit() full */
  364.       fprintf (errout, "WARNING %s\n", rwarnstr(rp));
  365.       break;
  366.     case R_WEMPSTR:  /* empty data string */
  367.     case R_WWIDTH:   /* data too wide for columns */
  368.     case R_WTMFMT:   /* time data incomplete */
  369.       fprintf(errout, "WARNING reading %s at record %lu and field %u -- %s\n",
  370.        rnames(rp), rrecno(rp), rfldno(rp), rwarnstr(rp));
  371.       break;
  372.     }
  373.   }
  374. }
  375.  
  376. 2.3 Register Callback Error and Warning Functions
  377.  
  378. Once you have written your callback error and warning functions, you must let
  379. the other recio functions know that they exist.  You use the rseterrfn and
  380. rsetwarnfn functions to individually register your callback functions or 
  381. the rinit to register both functions at once.  You may find use of rinit 
  382. easier to remember.
  383.  
  384.     /* register callback error and warning functions */
  385.     rseterrfn(rerrfn);
  386.     rsetwarnfn(rwarnfn);
  387.  
  388. OR
  389.  
  390.     /* register callback error and warning functions */
  391.     rinit(rerrfn, rwarnfn);
  392.  
  393. OR to use the simple built-in callback error/warning functions
  394.  
  395.     /* register built-in callback error and warning functions */
  396.     rinit(rerrmsg, rwarnmsg);
  397.  
  398.  
  399. OR to use the simple built-in error function without a warning function
  400.  
  401.     /* register callback error function */
  402.     rinit(rerrmsg, NULL);
  403.  
  404.  
  405.  
  406. 3.0 OPEN FILE
  407.  
  408.  
  409. 3.1 Open File and Get Record Pointer
  410.  
  411. Use the ropen function to open the file you want to read, write, or append.
  412. Store the record pointer returned by the ropen function.  Do not open recin,
  413. recout, recerr, or recprn (MSDOS printer).  They are always open, so they do
  414. not need to be opened or closed.
  415.  
  416.     REC *rp = ropen("FILENAME.DAT", "r");
  417.  
  418.  
  419. 3.2 Check Record Pointer
  420.  
  421. Following the ropen function, you need to check to see if the file was
  422. opened correctly.  If ropen returned a NULL pointer, then the file was not
  423. opened.
  424.  
  425. Errors other than ENOENT are reported to your callback error function.
  426. ENOENT is not reported since you may want to use default values if the
  427. data file is not available.
  428.  
  429.     /* if ropen() failed */
  430.     if (!rp) {
  431.         /* if it failed because file does not exist */
  432.         if (errno==ENOENT) {
  433.             /* action to take when file does not exist */
  434.             ...
  435.         }
  436.     /* else ropen() succeeded */
  437.     } else {
  438.         /* set up stream (see sections 3.3 - 3.6) */
  439.         ...
  440.         /* read or write file (see sections 4 and 5) */
  441.         ...
  442.         /* close file (see section 6) */
  443.         rclose(rp);
  444.     }
  445.  
  446.  
  447. 3.3 Set Field and Text Delimiters and Time Format
  448.  
  449. The space character is the default value for both the field and text
  450. delimiters.  The space character matches any white space.  The default 
  451. time format string is "%m/%d/%y".  If you need to something else, use 
  452. the rsetfldch, rsettxtch, and rsettmfmt functions to explicitly set the 
  453. values.  Application maintenance will be easier if you get in the habit 
  454. of setting these values for each record stream.
  455.  
  456.     rsetfldch(rp, ',');         /* set field delimiter character */
  457.     rsettxtch(rp, '"');         /* set text delimiter character */
  458.     rsettmfmt(rp, "%m/%d/%Y");  /* set time format */
  459.  
  460.  
  461. 3.4 Set Field and Record Buffer Sizes
  462.  
  463. Setting the field and record buffer sizes is optional.  Buffers will be
  464. automatically reallocated as necessary.  However if you set the field and
  465. record sizes in advance to the maximum value needed, you can reduce memory
  466. fragmentation.  The field and record buffers are not used for output streams.
  467.  
  468.     rsetfldsiz(rp, 41);  /* set size of field buffer */
  469.     rsetrecsiz(rp, 133); /* set size of record buffer */
  470.  
  471.  
  472. 3.5 Set Context Number
  473.  
  474. If your application opens record streams with more than one data format, you
  475. will want to set a context number.  You use the context number so that your
  476. callback error function can determine (using the rcxtno function) which data
  477. format it is dealing with.  Each context number must be a positive integer;
  478. zero and negative numbers are reserved.  Predefined context symbolic
  479. constants are RECIN, RECOUT, RECERR, and (for MSDOS) RECPRN.
  480.  
  481. #define SOILS_DB      1
  482. #define BUILDINGS_DB  2
  483.  
  484.     rsetcxtno(rp, SOILS_DB); /* set context number */
  485.  
  486.  
  487. 3.6 Set Beginning Column Number
  488.  
  489. The first column number in the record buffer defaults to zero.  If you prefer
  490. column numbering to start at one, use the rsetbegcolno function.  It is mainly
  491. useful if using column delimited data.  If a number takes up the first ten
  492. columns of the record, the column numbering will be 0 to 9 if rsetbegcolno()
  493. is set to 0, or 1 to 10 is rsetbegcolno() is set to 1.
  494.  
  495.     rsetbegcolno(rp, 1); /* first column is column 1 */
  496.  
  497.  
  498. 3.7 Set Beginning Year
  499.  
  500. If you are going to read time data (time_t or struct tm) using the %y 
  501. time format, you may need to set the beginning year.  The %y format 
  502. represents the year as a two digit number.  Thus the year 76 may represent 
  503. 1776 if your application deals with the signing of the U.S. Declaration 
  504. of Independence, or 1976 if your application deals with the 20th Century.  
  505. The default beginning year is controlled by RECBEGYR and is set in recio.h 
  506. to 1951.  Thus the default range for the %y format corresponds to the years 
  507. 1951 to 2050.
  508.  
  509.     rsetbegyr(1980);  /* %y range 1980 to 2079 */
  510.  
  511. Note that the rsetbegyr function applies globally to the application;
  512. it does not contain a record stream argument.
  513.  
  514. You could also have an application that sets the beginning year dynamically.
  515. The implementation below is restricted by the range of time_t.
  516.  
  517.     time_t curtime;      /* current time */
  518.     struct tm *curtmp;   /* pointer to current tm */
  519.     
  520.     if (time(&curtime) != (time_t) -1) {
  521.         curtmp = localtime(&curtime);
  522.         rsetbegyr(curtmp->tm_year + 1900 - 50); /* current year - 50 years */
  523.     } else {
  524.         rseterr(NULL, EDOM);
  525.     }
  526.  
  527.  
  528.     
  529. 4.0 RECORD FUNCTIONS
  530.  
  531. 4.1 The rgetrec Function
  532.  
  533. If all the records in a data file have the same format, you will want to
  534. loop through all the records until the end of file is reached.  If each
  535. record has a different format, you must call the rgetrec function each
  536. time you want to get the next record.  Calling rgetrec() is optional for
  537. the first record.
  538.  
  539.     /* read all records in file */
  540.     while (rgetrec(rp)) {
  541.         /* Section 5 field functions go here ... */
  542.     }
  543.  
  544.  
  545. 4.2 The rputrec Function
  546.  
  547. After you write all the fields in one record, use the rputrec function
  548. to put the end-of-record newline character to the output and to reset the
  549. internal recio library variables for the next record.
  550.  
  551.     /* write end-of-record */
  552.     rputrec(rp);
  553.  
  554.  
  555. 4.3 The rrecs Macro
  556.  
  557. To get a pointer to the start of the record buffer, use the rrecs macro.
  558.  
  559.     /* echo record contents to stdout */
  560.     printf("%s\n", rrecs(rp));
  561.  
  562.  
  563. 4.4 The rrecno Macro
  564.  
  565. To get the record number, use the rrecno macro.
  566.  
  567.     /* echo record number and record contents to stdout */
  568.     printf("%ld: %s\n", rrecno(rp), rrecs(rp));
  569.  
  570.  
  571. 4.5 The rsetrecstr Function
  572.  
  573. There may be times when you will find it useful to stuff the record buffer
  574. with your own string, then use the field input functions to scan the fields.
  575. Use the rsetrecstr function for this.
  576.  
  577. 4.6 The rresetrec Macro
  578.  
  579. The rresetrec macro resets internals so that you can start reading fields
  580. from the beginning of the record buffer.  This allows you to scan through
  581. the fields in the record buffer multiple times.
  582.  
  583.  
  584. 5.0 GET AND PUT FIELD DATA
  585.  
  586. 5.1 Field functions
  587.  
  588. There are 56 functions that can be used to read or write data.  A mnemonic
  589. system makes it easy to construct the name of any function you want.  All you
  590. need to remember is that there are four prefixes, two bodies, and eight
  591. suffixes, and that the rb and rcb prefixes are used only with the i, l, ui,
  592. and ul suffixes.
  593.  
  594. If the prefix contains the letter 'c', it is a column delimited field function.
  595. If the prefix contains the letter 'b', it is a field function that reads or
  596. writes an integral number in a specified base (radix).  The ten suffixes
  597. indicate the ten data types: character, double, float, integer, long,
  598. string, time_t, struct tm, unsigned integer, and unsigned long.
  599.  
  600.     Prefix   Body   Suffix      Prefix   Body   Suffix
  601.     ------   ----   ------      ------   ----   ------
  602.       r       get      c          rb      get      i
  603.       rc      put      d          rcb     put      l
  604.                        f                          ui
  605.                        i                          ul
  606.                        l
  607.                        s
  608.                        t
  609.                       tm
  610.                       ui
  611.                       ul
  612.  
  613.  
  614. 5.2 The rskipfld Macro
  615.  
  616. If your application does not need to read the data in a field, you can skip
  617. over the field by using the rskipfld macro.
  618.  
  619.     /* skip over a field */
  620.     if (rskipfld(rp) != 1) printf("Unable to skip field.\n");
  621.  
  622.  
  623. 5.3 The rskipnfld Function
  624.  
  625. If your application does not need to read the data in several adjacent
  626. fields, you can skip over the fields by using the rskipnfld function.
  627.  
  628.     /* skip over three fields */
  629.     if (rskipnfld(rp, 3) != 3) printf("Unable to skip 3 fields.\n");
  630.  
  631.  
  632. 5.4 The ristxtfld Macro
  633.  
  634. You can use the ristxtfld macro to determine if the current field (the field
  635. most recently input or output) was quoted with the text delimiter character
  636. (provided the text delimiter character is not the space character).  For
  637. example, if you need to write out a string field in the same format in
  638. which was read, you can use this macro to determine if the original field
  639. was quoted.
  640.  
  641.     /* write field in same format as read */
  642.     rgets(recin);
  643.     if (ristxtfld(recin)) rsettxtch(recout, '"');
  644.     else rsettxtch(recout, ' ');
  645.     rputs(recout, rflds(recin));
  646.  
  647.  
  648. 5.5 The reof Macro
  649.  
  650. Use the reof macro to determine when the record stream has reached the
  651. end of file.
  652.  
  653.     /* if error or end of file reached */
  654.     while (rgetrec(rp)) {
  655.     ...
  656.     }
  657.     /* if end of file */
  658.     if (reof(rp)) {
  659.        ...
  660.     /* else error */
  661.     } else {
  662.        ...
  663.     }
  664.  
  665.  
  666. 5.6 The rerror Function
  667.  
  668. Use the rerror function to determine if an error has occurred on a record
  669. stream.  You can access a text string for the error using the rerrstr
  670. function.
  671.  
  672.     if (rerror(rp))
  673.         printf("ERROR reading %s - %s\n",
  674.          rnames(rp), rerrstr(rp));
  675.     rclose(rp);
  676.  
  677. It is a good practice to check for any errors just before closing
  678. a record stream.  If the error indicator is clear, you have additional
  679. confidence that the stream was processed correctly.
  680.  
  681.  
  682. 5.7 The rseterr Function
  683.  
  684. If you write wrapper functions or other functions that interact with
  685. recio functions, your code will need to handle errors.  If can use
  686. the rseterr function to set the error number and to call the record
  687. stream callback error function.
  688.  
  689. /* get integer and validate range */
  690. int rrgeti(REC *rp, int min, int max) {
  691.     int result;
  692.  
  693.     result = rgeti(rp);
  694.     if (result < min || result > max) {
  695.         rseterr(rp, R_ERANGE);
  696.     }
  697.     return result;
  698. }
  699.  
  700.  
  701. 5.8 The rwarning Function
  702.  
  703. Use the rwarning macro to determine if a warning has occurred on a record
  704. stream.
  705.  
  706.     if (rwarning(rp)) {
  707.         printf("ERROR %s - %s\n", rnames(rp), rwarnstr(rp));
  708.     }
  709.  
  710. It is good practice to check for any warnings just before closing an output
  711. record stream.
  712.  
  713.  
  714. 5.9 The rsetwarn Function
  715.  
  716. If you write wrapper functions or other functions that interact with recio
  717. functions, you may need to provide warnings.  The rsetwarn function sets the
  718. warning number and calls the record stream callback warning function.
  719.  
  720.     rsetwarn(rp, R_WWIDTH);
  721.  
  722.  
  723. 5.10 The rnumfld Function
  724.  
  725. You can determine the number of character delimited fields in the current
  726. record of an input record stream with the rnumfld function.
  727.  
  728.     unsigned numfld;
  729.     
  730.     numfld = rnumfld(recin);
  731.     
  732.  
  733. 5.11 The rgetfldpos and rsetfldpos Functions
  734.  
  735. If you want to mark a specific position within the current record and later 
  736. return to the same position, use the rgetfldpos and rsetfldpos functions. 
  737. If the record number has changed between calls, a R_EINVAL error is generated 
  738. and passed to the callback error function.  The pos argument is passed by 
  739. reference, but a macro takes care of placing the & for you.
  740.  
  741.  
  742.      rpos_t pos;
  743.      
  744.      rgetrec(recin);
  745.      rgetfldpos(recin, pos);   /* get bookmark at start of record */
  746.      rskipnfld(recin, 3);      /* skip three fields */
  747.      rsetfldpos(recin, pos);   /* reset to bookmark position */
  748.  
  749.  
  750.  
  751. 6.0 CLOSE FILE
  752.  
  753. 6.1 Close File
  754.  
  755. When finished reading or writing a data file, close it.  Do not close recin,
  756. recout, recerr, or recprn as they are always open.
  757.  
  758.     /* close record file */
  759.     rclose(rp);
  760.  
  761.  
  762. 6.2 Close All Files
  763.  
  764. Rather than closing record files one at a time, one can close all open
  765. record files at once using the rcloseall function.
  766.  
  767.     /* all done */
  768.     rcloseall();
  769.  
  770.  
  771.  
  772. 7.0 STRING FUNCTIONS
  773.  
  774. The recio library contains a small set of string functions.  Ideally these 
  775. would be part of a larger separate string library, but are included here 
  776. because the recio functions need them.  You may also find them useful for 
  777. your application.
  778.  
  779.  
  780. 7.1 Trim a string
  781.  
  782. The scntrimbegs, scntrimends, and scntrims functions allow you to trim the 
  783. same character from the beginning, end, and both ends of string up to the 
  784. maximum number specified by the last argument.  The sctrimbegs, sctrimends, 
  785. and sctrims functions let you to trim all multiple occurances of the same 
  786. character from the beginning, end, and both ends of a string.  If you 
  787. specify the space character, all whitespace is trimmed.  You can use the 
  788. strimbegs, strimends, and strims macros to trim whitespace from the ends.
  789.  
  790.     char str[]="\t  Hello, World?!!!\n";
  791.     
  792.     strims(str);              /* trim whitespace from both ends of string */
  793.     sctrimends(str, '!');     /* then trim all ! chars from end of string */
  794.     scntrimends(str, '?', 1); /* finally trim one ? char from end of string */
  795.  
  796.  
  797. 7.2 Dynamically copy and concatenate a string
  798.  
  799. The scpys and scats macros are used to dynamically copy and concatenate a 
  800. string.  To use these macros you must adhere to these points:
  801.  
  802.      1. Initialize pointers of destination strings to NULL.
  803.      2. These macros pass the destination string by reference (and place
  804.         the & for you).
  805.      3. A pointer to the destination string is returned.
  806.      4. Free any dynamically allocated strings when finished with them.
  807.      
  808.      char *dst=NULL;
  809.      char src1[]="Hello ";
  810.      char src2[]="world";
  811.      
  812.      scpys(dst, src1);
  813.      puts(scats(dst, src2));  /* "Hello world" */
  814.  
  815.      /* however the following will not work here as in C++ where you can 
  816.       * declare a function to pass an argument by reference, i.e. 
  817.       * char *scats(char *&dst, const char *src)
  818.       *
  819.       * puts(scats(scpys(dst, src1), src2));  /* C++ */
  820.       */
  821.      
  822.      free(dst);
  823.  
  824.  
  825.  
  826. 8.0 TIME CONVERSION FUNCTIONS
  827.  
  828. The recio library contains a few time conversion functions.
  829.  
  830.  
  831. 8.1 Convert from string to time_t or struct tm types.
  832.  
  833. You can convert a string to either the time_t or struct tm time types using 
  834. the sftotime or sftotm functions.  These functions complement the ANSI 
  835. X3.159-1989 strftime function.  However sftotime and sftotm use a subset 
  836. of the specifiers found with strftime.  The recio library uses the sftotime 
  837. and sftotm functions to get time from the input stream, but uses the strftime 
  838. function to put time to the output stream.  As a consequence, you can use the 
  839. complete set of strftime specifiers for any output you do not later have to 
  840. read back into a program.  If there is an error, the struct tm type will 
  841. contain the time "01/01/70 00:00:00" and the time_t type will contain the 
  842. value (time_t)-1, i.e. cast -1 to a time_t type.
  843.  
  844.  
  845.      time_t time;
  846.      struct tm t;
  847.      
  848.      time = sftotime("11/24/94", "%m/%d/%y");
  849.      t = sftotm("11/24/94", "%m/%d/%y");
  850.  
  851.  
  852. 8.2 Convert between time_t and struct tm types.
  853.  
  854. You can convert between the time_t and struct tm types using the tmtotime and 
  855. timetotm functions.  The functions are similar to the mktime and localtime 
  856. functions in ANSI X3.159-1989, but the arguments don't use pointers and they 
  857. may be easier to remember.  If there is an error, the struct tm type will 
  858. contain the time "01/01/70 00:00:00" and the time_t type will contain the 
  859. value (time_t)-1, i.e. cast -1 to a time_t type.
  860.  
  861.      time_t time;
  862.      struct tm t;
  863.      
  864.      time = sftotime("12/25/94", "%m/%d/%y");
  865.      t = timetotm(time);
  866.  
  867.  
  868.  
  869. 9.0 REFERENCES
  870.  
  871. 1. ANSI X3.159-1989.  American National Standard for Information Systems -
  872.    Programming Language - C.  American National Standards Institute,
  873.    11 West 42nd Street, New York, NY 10036, 1990.
  874.  
  875.  
  876.  
  877. 10.0 INDEX
  878.  
  879. errno macro ............ 2.0, 2.1, 3.2
  880. rbegcolno macro ........ 2.1
  881. rclearerr function ..... 2.1
  882. rclose function ........ 6.1
  883. rcloseall function ..... 6.2
  884. rcolno macro ........... 2.1
  885. rcxtno macro ........... 3.5
  886. recin expression ....... 3.1, 6.1
  887. reof macro ............. 2.1, 5.5
  888. rerror function ........ 2.1, 5.6
  889. rerrmsg function ....... 2.1, 2.3
  890. rerrstr function ....... 2.1, 5.6
  891. rfix functions ......... 2.1
  892. rflds macro ............ 5.4
  893. rfldno macro ........... 2.1
  894. rbget functions ........ 5.1
  895. rbput functions ........ 5.1
  896. rcbget functions ....... 5.1
  897. rcbput functions ....... 5.1
  898. rcget functions ........ 5.1
  899. rcput functions ........ 5.1
  900. rget functions ......... 5.1
  901. rgetfldpos function .... 5.11
  902. rgetrec function ....... 4.1
  903. rinit function ......... 2.3
  904. ristxtfld macro ........ 5.4
  905. risvalid function ...... 2.1
  906. rnames macro ........... 2.1
  907. rnumfld function ....... 5.10
  908. ropen function  ........ 3.1
  909. rput functions ......... 5.1
  910. rrecs macro ............ 2.1, 4.3
  911. rrecno macro ........... 2.1, 4.4
  912. rresetrec macro ........ 4.6
  913. rsetbegcolno function .. 3.6
  914. rsetbegyr function ..... 3.7
  915. rsetcxtno function ..... 3.5
  916. rseterr function ....... 5.7
  917. rseterrfn function ..... 2.3
  918. rsetfldch function ..... 3.3
  919. rsetfldpos function .... 5.11
  920. rsetfldsiz function .... 3.4
  921. rsetfldstr function .... 2.1
  922. rsetrecsiz function .... 3.4
  923. rsetrecstr function .... 4.5
  924. rsettmfmt function ..... 3.3
  925. rsettxtch function ..... 3.3, 5.4
  926. rsetwarn function ...... 5.9
  927. rsetwarnfn function .... 2.3
  928. rskipfld macro  ........ 5.2
  929. rskipnfld function ..... 5.3
  930. rwarning function ...... 2.2, 5.8
  931. rwarnmsg function ...... 2.2, 2.3
  932. rwarnstr function ...... 2.2, 5.7
  933. scats macro ............ 7.2
  934. scpys macro ............ 7.2
  935. sctrimbegs function .... 7.1
  936. sctrimends function .... 7.1
  937. sctrims function ....... 7.1
  938. sftotime function ...... 8.1
  939. sftotm function ........ 8.1
  940. strimbegs macro ........ 7.1
  941. strimends macro ........ 7.1
  942. strims macro ........... 7.1
  943. timetotm function ...... 8.2
  944. tmtotime function ...... 8.2
  945.