1 : /******************************************************************************
2 : * $Id: ogrrecdatasource.cpp 13025 2007-11-25 18:03:46Z rouault $
3 : *
4 : * Project: Epiinfo .REC Translator
5 : * Purpose: Implements OGRRECDataSource class
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "ogr_rec.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 :
34 : CPL_CVSID("$Id: ogrrecdatasource.cpp 13025 2007-11-25 18:03:46Z rouault $");
35 :
36 : /************************************************************************/
37 : /* OGRRECDataSource() */
38 : /************************************************************************/
39 :
40 171 : OGRRECDataSource::OGRRECDataSource()
41 :
42 : {
43 171 : poLayer = NULL;
44 :
45 171 : pszName = NULL;
46 171 : }
47 :
48 : /************************************************************************/
49 : /* ~OGRRECDataSource() */
50 : /************************************************************************/
51 :
52 342 : OGRRECDataSource::~OGRRECDataSource()
53 :
54 : {
55 171 : if( poLayer != NULL )
56 0 : delete poLayer;
57 :
58 171 : CPLFree( pszName );
59 342 : }
60 :
61 : /************************************************************************/
62 : /* TestCapability() */
63 : /************************************************************************/
64 :
65 0 : int OGRRECDataSource::TestCapability( const char * )
66 :
67 : {
68 0 : return FALSE;
69 : }
70 :
71 : /************************************************************************/
72 : /* GetLayer() */
73 : /************************************************************************/
74 :
75 0 : OGRLayer *OGRRECDataSource::GetLayer( int iLayer )
76 :
77 : {
78 0 : if( iLayer == 0 )
79 0 : return poLayer;
80 : else
81 0 : return NULL;
82 : }
83 :
84 : /************************************************************************/
85 : /* Open() */
86 : /************************************************************************/
87 :
88 171 : int OGRRECDataSource::Open( const char * pszFilename )
89 :
90 : {
91 171 : pszName = CPLStrdup( pszFilename );
92 :
93 : /* -------------------------------------------------------------------- */
94 : /* Verify that the extension is REC. */
95 : /* -------------------------------------------------------------------- */
96 171 : if( !(strlen(pszFilename) > 4 &&
97 : EQUAL(pszFilename+strlen(pszFilename)-4,".rec") ) )
98 171 : return FALSE;
99 :
100 : /* -------------------------------------------------------------------- */
101 : /* Open the file. */
102 : /* -------------------------------------------------------------------- */
103 : const char * pszLine;
104 : FILE * fp;
105 :
106 0 : fp = VSIFOpen( pszFilename, "r" );
107 0 : if( fp == NULL )
108 0 : return FALSE;
109 :
110 : /* -------------------------------------------------------------------- */
111 : /* Read a line, and verify that it consists of at least one */
112 : /* field that is a number greater than zero. */
113 : /* -------------------------------------------------------------------- */
114 : int nFieldCount;
115 0 : pszLine = CPLReadLine( fp );
116 :
117 0 : nFieldCount = atoi(pszLine);
118 0 : if( nFieldCount < 1 || nFieldCount > 1000 )
119 : {
120 0 : VSIFClose( fp );
121 0 : return FALSE;
122 : }
123 :
124 : /* -------------------------------------------------------------------- */
125 : /* Create a layer. */
126 : /* -------------------------------------------------------------------- */
127 0 : poLayer = new OGRRECLayer( CPLGetBasename(pszFilename), fp, nFieldCount );
128 :
129 0 : return poLayer->IsValid();
130 : }
|