summaryrefslogtreecommitdiff
path: root/bin/dbfxtrct.cpp
blob: 5d36b1e82a27652c4b0207f6677768c48e9521d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* 
    Xbase64 project source code

    This program extracts data from a dbf data file and puts it in
    a comma delimited output file,  suitable for input into an awk or
    perl script

    This program excludes all memo fields

    Copyright (C) 1997,2003  Gary A. Kunkel   

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Contact:
    
     Email:
    
      xbase64-devel@lists.sourceforge.net
      xbase64-users@lists.sourceforge.net
      
      
     Regular Mail:
     
       XBase Support
       149C South Main St
       Keller Texas, 76248     
       USA

*/

#include <xbase64/xbase64.h>

/*************************************************************************/
void Usage();
void Usage()
{
  std::cout << "\nUsage: dbfxtrct -iDATABASE.DBF -sINDEX.N[TD]X -f -F -dMM/DD/YY\n";
  std::cout << "\nWhere DATABASE.DBF is the name of the database file to dump\n";
  std::cout << "INDEX.NTX or .NDX is an optional index sort paramater\n";
  std::cout << "-f  optional field name list in first record\n";
  std::cout << "-F  optional field name and attributes in first record\n";
  std::cout << "MM/DD/YY is an optional output date format for any date fields\n";
  std::cout << "\nThis program creates output suitable for awk and perl scripts\n";
  std::cout << "\nThis program does not support memo fields (yet).\n";
}
/*************************************************************************/
int main(int ac,char** av)
{
   xbXBase x;
   xbDbf d( &x );
   xbShort rc, FieldOption = 0;
   xbIndex *ix = 0;
   xbNdx z(&d);

   char *dbfname = NULL;
   char *ixname  = NULL;
   char *p;
   char buf[200];
   xbExpn exp( &x );

/*  Get the input paramaters  

    -i  input datafile name
    -s  optional sort index name
    -f  optional field names in record one
    -F  optional field names and attributes in record one
    -d  date format  
*/
  for( int i = 1; i < ac; i++ )
  {
    p = av[i];
    if( *p != '-' ){
      std::cout << "Invalid paramater " << *p << std::endl;
      Usage();
      return 1;
    }
    p++;
    if( *p == 'i' )
     dbfname = ++p;
    else if( *p == 's' )
     ixname = ++p;
    else if( *p == 'f' )
      FieldOption = 1;
    else if( *p == 'F' )
      FieldOption = 2;
    else if( *p == 'd' )
      x.SetDefaultDateFormat( ++p );
    else{
      std::cout << "Invalid paramater " << *p << std::endl;
      Usage();
      return 1;
    }
  }

/* if missing params, display a usage message and exit */
  
  if( !dbfname ){
     Usage();
     return 1;
   }

/*  open the database file */

   if(( rc = d.OpenDatabase( dbfname )) != XB_NO_ERROR )
   {
      std::cout << "\nCould not open file " << dbfname << " rc = " << rc
                << "\n";
      return 2;
   }

/*  if an index was specified, open the index file */

   if( ixname ){

#ifdef XB_INDEX_NTX
     if( strstr( ixname, "NTX" ))
       ix = new xbNtx( &d );
#endif

#ifdef XB_INDEX_NDX
     if( strstr( ixname, "NDX" ))
       ix = new xbNdx( &d );
#endif
     if( !ix ){
       std::cout << "Unknown index type. .NTX and .NDX index file support only\n";
       return 3;
     }
     if(( rc = ix->OpenIndex( ixname )) != XB_NO_ERROR )
     {
       std::cout << "\nCould not open index " << ixname << " rc = " << rc
                 << "\n";
       return 4;
     }
   }


/*  if -f or -F paramater, dump the header information */
  if( FieldOption ){
    for( xbLong l = 0; l < d.FieldCount(); l++ ){
      if( l ) std::cout << ",";
      std::cout << d.GetFieldName(l);
      if( FieldOption == 2 ){
        std::cout << "|" << d.GetFieldType(l) << "|" << d.GetFieldLen(l);
	std::cout << "|" << d.GetFieldDecimal(l);
      }
    }
    std::cout << std::endl;
  }

/*  if an index used, then loop thru each record, based on index, else
    dump in dbf sort order */
  if( ixname )
    rc = ix->GetFirstKey();
  else
    rc = d.GetFirstRecord();
    
  while( rc == XB_NO_ERROR ){
    for( xbLong l = 0; l < d.FieldCount(); l++ ){
      if( l ) std::cout << ",";
      strcpy( buf, exp.LTRIM( d.GetStringField( l )));
      if( d.GetFieldType( l ) == 'D' )
        std::cout << exp.DTOC( buf );
      else
        std::cout << exp.TRIM( buf );
    }
    if( ixname )
      rc = ix->GetNextKey();
    else
      rc = d.GetNextRecord();
    std::cout << std::endl;
  }

/*  close everything */
   d.CloseDatabase();
   return 0;
}