home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0330 - 0339 / ibm0330-0339 / ibm0333.tar / ibm0333 / CCML2107.ZIP / IE-EX3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-22  |  8.0 KB  |  243 lines

  1. /***********************************************************************
  2. IE-EX3.C: Import/Export example program #3
  3. by MSW
  4. Copyright (c) 1992 by cc:Mail, Inc.  All rights reserved.
  5.  
  6. This source code, any derivatives of it, and the resulting object code
  7. may be freely used and/or copied, so long as this copyright notice is
  8. included and remains unmodified.  cc:Mail, Inc. does not make any
  9. representation or warranty, express or implied with respect to this
  10. source code.  cc:Mail, Inc. disclaims any liability whatsoever for any
  11. use of such code.
  12.  
  13. ************************************************************************
  14.  
  15.     This program is an example of how to write an Import/Export message
  16.     format file so that the ITEMSIZE parameter of Import can be used.
  17.     This is recommended when the contents of a text item may contain a
  18.     keyword (e.g. "Message:").  In particular, this program shows how
  19.     to write the sizes of the various parts of the message without
  20.     calculating all the sizes beforehand.
  21.  
  22.     For this example, the message is written to the file CCMAIL.IMP,
  23.     has a single recipient, and has a variable number of text items.
  24.     The usage is:
  25.         ie-ex3 author recip txtfil1 [txtfil2 [txtfil3 ...]]
  26.  
  27.             where author   is the message author (enclose in quotes if
  28.                            it contains spaces)
  29.                   recip    is the single message recipient (enclose in
  30.                            quotes if it contains spaces)
  31.                   txtfil1  is the file containing primary text item
  32.                   txtfil2+ are the files containing secondary text items
  33.                            (optional)
  34.  
  35.     IE-EX3 returns an ERRORLEVEL of 0 for success and 1 for any error.
  36.  
  37.     Note that no testing is done on the specified files to determine if
  38.     they are in fact valid text items.  This could be added to a
  39.     production program.
  40.  
  41.     Build with Microsoft C version 5.1 or later using any memory model.
  42. ***********************************************************************/
  43.  
  44.  
  45. // Include file(s)
  46. #include <fcntl.h>
  47. #include <io.h>
  48. #include <share.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <sys\types.h>
  53. #include <sys\stat.h>
  54.  
  55. // Define constants
  56. #define BUFFSIZE    1024        // Size of buffer for copying text items
  57.  
  58. // Define file statics
  59. static char allocSpace[] = "          \r\n";
  60.                                 // Spaces to write section size over
  61. static int hOut = -1;           // File handle for output file
  62.  
  63. // Declare prototypes
  64. int copyFile(char *name);
  65. void endSection(long *posP);
  66. void startSection(long *posP);
  67. void writeStr(char *str);
  68.  
  69.  
  70. /***********************************************************************
  71. IE-EX3 main routine (described above)
  72. ***********************************************************************/
  73. int main(int argc, char *argv[])
  74. {
  75.     int i;
  76.     long posMsg, posCnt, posItm;
  77.  
  78.     // Check argument count
  79.     if (argc < 4) {
  80.         fprintf(stderr,
  81.             "Usage: ie-ex3 author recip txtfil1 [txtfil2 ...]\n");
  82.         return  1;
  83.     }
  84.  
  85.     // Open output file (binary mode)
  86.     if ((hOut = sopen("CCMAIL.IMP",
  87.             O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, SH_DENYWR,
  88.             S_IREAD | S_IWRITE)) == -1) {
  89.         fprintf(stderr, "ie-ex3: Error opening output file\n");
  90.         return  1;
  91.     }
  92.  
  93.     // Do the work
  94.     writeStr("Message:\r\n");
  95.     startSection(&posMsg);
  96.     writeStr("From: ");
  97.     writeStr(argv[1]);
  98.     writeStr("\r\nTo: ");
  99.     writeStr(argv[2]);
  100.     writeStr("\r\nSubject: IE-EX3 program\r\nContents:\r\n");
  101.     startSection(&posCnt);
  102.     for (i=3; i<argc; i++) {
  103.         writeStr("\r\nText item: From file ");
  104.         writeStr(argv[i]);
  105.         writeStr("\r\n");
  106.         startSection(&posItm);
  107.         if (!copyFile(argv[i])) {
  108.             break;
  109.         }
  110.         endSection(&posItm);
  111.     }
  112.     endSection(&posCnt);
  113.     endSection(&posMsg);
  114.  
  115.     // Close output file (ignore errors) and exit with no error
  116.     close(hOut);
  117.     return  0;
  118. } // main
  119.  
  120.  
  121. /***********************************************************************
  122. Routine    : copyFile
  123. Description: Copy the specified file to the output file.  Both files are
  124.            : accessed in binary mode (it is assumed that the output file
  125.            : is already opened in binary mode).
  126. Inputs     : name       input file specification (null-terminated)
  127. Uses       : hOut
  128. Returns    : Zero (false) if the input file cannot be opened
  129. ***********************************************************************/
  130. int copyFile(char *name)
  131. {
  132.     char buff[BUFFSIZE];
  133.     int hIn, readCnt;
  134.  
  135.     // Open input file (binary mode)
  136.     if ((hIn = sopen(name, O_RDONLY | O_BINARY, SH_DENYNO)) == -1) {
  137.         fprintf(stderr, "ie-ex3: Error opening %s\n", name);
  138.         return  0;
  139.     }
  140.  
  141.     // Copy input file to output file (ignore read problems)
  142.     while ((readCnt = read(hIn, buff, sizeof(buff))) > 0) {
  143.         if (write(hOut, buff, readCnt) != readCnt) {
  144.             fprintf(stderr, "ie-ex3: Error writing to output file\n");
  145.             close(hIn);
  146.             close(hOut);
  147.             exit(1);
  148.         }
  149.     }
  150.  
  151.     // Close input file (ignore errors) and return success
  152.     close(hIn);
  153.     return  1;
  154. } // copyFile
  155.  
  156.  
  157. /***********************************************************************
  158. Routine    : endSection
  159. Description: Go back and write the size of the section started when
  160.            : startSection was called.  This function and startSection
  161.            : must be called in matching pairs with the same argument.
  162. Inputs     : posP       same pointer passed to startSection
  163. Uses       : hOut
  164. ***********************************************************************/
  165. void endSection(long *posP)
  166. {
  167.     char tmp[sizeof(allocSpace)];
  168.     long curPos;
  169.  
  170.     // Remember current position
  171.     if ((curPos = lseek(hOut, 0L, SEEK_CUR)) == -1L) {
  172.         fprintf(stderr, "ie-ex3: Error determining file position\n");
  173.         close(hOut);
  174.         exit(1);
  175.     }
  176.  
  177.     // Return to file position to write section size
  178.     if (lseek(hOut, *posP, SEEK_SET) == -1L) {
  179.         fprintf(stderr, "ie-ex3: Error seeking output file\n");
  180.         close(hOut);
  181.         exit(1);
  182.     }
  183.  
  184.     // Write the section size
  185.     sprintf(tmp, "%lu",
  186.         curPos - *posP - strlen(allocSpace));
  187.     writeStr(tmp);
  188.  
  189.     // Restore position
  190.     if (lseek(hOut, curPos, SEEK_SET) == -1L) {
  191.         fprintf(stderr, "ie-ex3: Error seeking output file\n");
  192.         close(hOut);
  193.         exit(1);
  194.     }
  195. } // endSection
  196.  
  197.  
  198. /***********************************************************************
  199. Routine    : startSection
  200. Description: Remember where the start of a section is and allocate space
  201.            : to write the section size later.  This function and
  202.            : endSection must be called in matching pairs with the same
  203.            : argument.
  204. Inputs     : posP       pointer to a variable in which to save the
  205.            :            starting position of the section
  206. Uses       : hOut
  207. ***********************************************************************/
  208. void startSection(long *posP)
  209. {
  210.     // Remember current file offset
  211.     if ((*posP = lseek(hOut, 0L, SEEK_CUR)) == -1L) {
  212.         fprintf(stderr, "ie-ex3: Error determining file position\n");
  213.         close(hOut);
  214.         exit(1);
  215.     }
  216.  
  217.     // Allocate space in output file to write size later
  218.     writeStr(allocSpace);
  219. } // startSection
  220.  
  221.  
  222. /***********************************************************************
  223. Routine    : writeStr
  224. Description: Write the specified string to the output file, checking for
  225.            : errors.
  226. Inputs     : str        string to write (null-terminated)
  227. Uses       : hOut
  228. ***********************************************************************/
  229. void writeStr(char *str)
  230. {
  231.     unsigned len;
  232.  
  233.     len = strlen(str);
  234.     if (write(hOut, str, len) != len) {
  235.         fprintf(stderr, "ie-ex3: Error writing to output file\n");
  236.         close(hOut);
  237.         exit(1);
  238.     }
  239. } // writeStr
  240.  
  241. // End of IE-EX3.C
  242. 
  243.