home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_09 / 1009119a < prev    next >
Text File  |  1992-06-23  |  4KB  |  189 lines

  1. /*
  2.  Copyright (C) 1992 by Mark W. Schumann
  3.  
  4.   convtext.cpp -- Convert a text file into
  5.   one or more existing Clipper files under
  6.   marker-file control.
  7.  
  8.   Compiler: Borland C++ 2.0.
  9.   Library: SoftC version 3.0.
  10.  
  11.   20 April 1992, by Mark W. Schumann
  12.   Usenet: mark@whizbang.ncoast.org
  13.  
  14.   Compile (Borland) with:
  15.  
  16.   bcc -ms -lm convtext.cpp scdbp20s.lib
  17. */
  18.  
  19.  
  20. #include <softc.h>
  21. #include <sc_base.h>
  22. #include <fstream.h>
  23. #include <iostream.h>
  24. #include <ctype.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27.  
  28. #define LINELENGTH 200
  29.  
  30. int convtext (char *);
  31. char *strtrim (char *);
  32. int nextmark (int, long &, char *, char *);
  33.  
  34. int main (int argc, char *argv[])
  35.  
  36. {
  37.  
  38.     scdinit (5, 0);
  39.     convtext ("POLLSEND.TXT");
  40.     scdterm();
  41.  
  42.     }
  43.  
  44. int convtext (char *textfile)
  45.  
  46. {
  47.  
  48. ifstream ip;       // Text input stream.
  49.  
  50. char tempname[80];    // Builds file name.
  51. char s[LINELENGTH+2]; // Text record buffer.
  52. char *p;    // Throwaway pointer.
  53.  
  54. int dbf = -1; // Output database handle.
  55. long recno;   // Current record number.
  56.  
  57. int markdbf;       // Marker database handle.
  58. char marktext[30]; // Text to indicate mark record.
  59. char markfile[9];  // Output file indicated by mark.
  60. long markrec = 0L; // Current record in marker file.
  61. int marklen;       // Length of marker for comparison.
  62.  
  63.     s[0] = ' ';        // Record status byte.
  64.  
  65.     ip.open (textfile);
  66.  
  67.     if (!ip) {
  68.         cout << "Couldn't open " << textfile << ".\n";
  69.         return -1;
  70.         }
  71.  
  72.     scddopenx (&markdbf, "DATAMARK.DBF", 0);
  73.     if (scecode() < 0) {
  74.         cout << "Couldn't open marker file.  "
  75.          << "SoftC error was " << scecode() << '\n';
  76.         return -1;
  77.         }
  78.  
  79.     nextmark (markdbf, markrec, marktext, markfile);
  80.     strtrim (marktext);
  81.     marklen = strlen (marktext);
  82.  
  83.     while (ip.getline (&s[1], LINELENGTH)) {
  84.  
  85.         sceclr();
  86.  
  87.         // Trim trailing newline.
  88.         p = strchr (&s[1], '\n');
  89.         if (p != NULL) *p = '\0';
  90.  
  91.        // Next data marker record reached?
  92.         if (strncmp (marktext, &s[1], marklen) == 0) {
  93.  
  94.             // Close current output database.
  95.             if (dbf != -1) scddclose (dbf);
  96.  
  97.             // Build full name of the target file.
  98.             strcpy (tempname, markfile);
  99.             strtrim (tempname);
  100.             strcat (tempname, ".DBF");
  101.  
  102.             // Now open it.
  103.             cout << "\nOpening " << markfile << '\n';
  104.             scddopenx (&dbf, tempname,
  105.              SC_RDWR | SC_EXCLUDE);
  106.  
  107.             scddsize (dbf, &recno);
  108.  
  109.             // Use the next marker.
  110.             nextmark (markdbf, markrec,
  111.              marktext, markfile);
  112.  
  113.             if (scecode() < 0) {
  114.                 cout << "Error opening " <<
  115.                  tempname << ".  SoftC error was " <<
  116.                  scecode() << '\n';
  117.                 break;
  118.                 }
  119.  
  120.             }
  121.  
  122.         else {
  123.  
  124.             if (dbf == -1) {
  125.                 cout << "Input file doesn't begin with a" <<
  126.                  " data marker.\n";
  127.                 break;
  128.                 }
  129.  
  130.             recno++;
  131.  
  132.             // Write the record to the database.
  133.             if (scddrputx (dbf, s, &recno, SC_ADD) < 0) {
  134.                 cout << "Couldn't add record " << recno
  135.                  << "; SoftC error " << scecode()
  136.                  << ' ' << scemsg() << '\n';
  137.                 break;
  138.                 }
  139.             else
  140.                 cout << "Record number " << recno
  141.                  << " added.\r";
  142.  
  143.             }
  144.         }
  145.  
  146.     cout << '\n';
  147.     ip.close();
  148.     scddclose (markdbf);
  149.     if (dbf != -1) scddclose (dbf);
  150.     return 0;
  151.  
  152.     }
  153.  
  154.  
  155. // Remove trailing whitespace from a string.
  156.  
  157. char *strtrim (char *s)
  158.  
  159. {
  160.  
  161. register char *p;
  162. char *q = NULL;  // Last printing character.
  163.  
  164.     for (p = s; *p; p++)
  165.         if (!isspace (*p)) q = p;
  166.  
  167.     if (q != NULL)
  168.         *++q = '\0';
  169.  
  170.     return s;
  171.     }
  172.  
  173.  
  174. // This assumes that we have a marker text description
  175. // in field 1 and marker file name in field 2.  It is also
  176. // counting on character buffers long enough to hold the
  177. // results.
  178.  
  179. int nextmark (int dbf, long &rec, char *text, char *file)
  180.  
  181. {
  182.  
  183.     scddrget (dbf, ++rec);
  184.     scddfget (dbf, 1, text);
  185.     scddfget (dbf, 2, file);
  186.     return scecode();
  187.  
  188.     }
  189.