1 : /******************************************************************************
2 : * $Id: ogrsuadatasource.cpp 23042 2011-09-04 15:07:22Z rouault $
3 : *
4 : * Project: SUA Translator
5 : * Purpose: Implements OGRSUADataSource class
6 : * Author: Even Rouault, even dot rouault at mines dash paris dot org
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2010, Even Rouault <even dot rouault at mines dash paris dot org>
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_sua.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 :
34 : CPL_CVSID("$Id: ogrsuadatasource.cpp 23042 2011-09-04 15:07:22Z rouault $");
35 :
36 : /************************************************************************/
37 : /* OGRSUADataSource() */
38 : /************************************************************************/
39 :
40 232 : OGRSUADataSource::OGRSUADataSource()
41 :
42 : {
43 232 : papoLayers = NULL;
44 232 : nLayers = 0;
45 :
46 232 : pszName = NULL;
47 232 : }
48 :
49 : /************************************************************************/
50 : /* ~OGRSUADataSource() */
51 : /************************************************************************/
52 :
53 232 : OGRSUADataSource::~OGRSUADataSource()
54 :
55 : {
56 234 : for( int i = 0; i < nLayers; i++ )
57 2 : delete papoLayers[i];
58 232 : CPLFree( papoLayers );
59 :
60 232 : CPLFree( pszName );
61 232 : }
62 :
63 : /************************************************************************/
64 : /* TestCapability() */
65 : /************************************************************************/
66 :
67 0 : int OGRSUADataSource::TestCapability( const char * pszCap )
68 :
69 : {
70 0 : return FALSE;
71 : }
72 :
73 : /************************************************************************/
74 : /* GetLayer() */
75 : /************************************************************************/
76 :
77 2 : OGRLayer *OGRSUADataSource::GetLayer( int iLayer )
78 :
79 : {
80 2 : if( iLayer < 0 || iLayer >= nLayers )
81 0 : return NULL;
82 : else
83 2 : return papoLayers[iLayer];
84 : }
85 :
86 : /************************************************************************/
87 : /* Open() */
88 : /************************************************************************/
89 :
90 232 : int OGRSUADataSource::Open( const char * pszFilename, int bUpdateIn)
91 :
92 : {
93 232 : if (bUpdateIn)
94 : {
95 26 : return FALSE;
96 : }
97 :
98 206 : pszName = CPLStrdup( pszFilename );
99 :
100 : // --------------------------------------------------------------------
101 : // Does this appear to be a .sua file?
102 : // --------------------------------------------------------------------
103 :
104 206 : VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
105 206 : if (fp == NULL)
106 78 : return FALSE;
107 :
108 : char szBuffer[10000];
109 128 : int nbRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fp);
110 128 : szBuffer[nbRead] = '\0';
111 :
112 : int bIsSUA = (strstr(szBuffer, "\nTYPE=") != NULL &&
113 : strstr(szBuffer, "\nTITLE=") != NULL &&
114 : (strstr(szBuffer, "\nPOINT=") != NULL ||
115 128 : strstr(szBuffer, "\nCIRCLE ") != NULL));
116 :
117 128 : if (bIsSUA)
118 : {
119 2 : VSIFSeekL( fp, 0, SEEK_SET );
120 2 : nLayers = 1;
121 2 : papoLayers = (OGRLayer**) CPLMalloc(sizeof(OGRLayer*));
122 2 : papoLayers[0] = new OGRSUALayer(fp);
123 : }
124 : else
125 126 : VSIFCloseL(fp);
126 :
127 128 : return bIsSUA;
128 : }
|