1 : /******************************************************************************
2 : * $Id: ogrvrtdriver.cpp 23575 2011-12-14 20:24:08Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Implements OGRVRTDriver 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_vrt.h"
31 : #include "cpl_conv.h"
32 :
33 : CPL_CVSID("$Id: ogrvrtdriver.cpp 23575 2011-12-14 20:24:08Z rouault $");
34 :
35 : /************************************************************************/
36 : /* ~OGRVRTDriver() */
37 : /************************************************************************/
38 :
39 169 : OGRVRTDriver::~OGRVRTDriver()
40 :
41 : {
42 169 : }
43 :
44 : /************************************************************************/
45 : /* GetName() */
46 : /************************************************************************/
47 :
48 10290 : const char *OGRVRTDriver::GetName()
49 :
50 : {
51 10290 : return "VRT";
52 : }
53 :
54 : /************************************************************************/
55 : /* Open() */
56 : /************************************************************************/
57 :
58 654 : OGRDataSource *OGRVRTDriver::Open( const char * pszFilename,
59 : int bUpdate )
60 :
61 : {
62 : OGRVRTDataSource *poDS;
63 654 : char *pszXML = NULL;
64 :
65 : /* -------------------------------------------------------------------- */
66 : /* Are we being passed the XML definition directly? */
67 : /* Skip any leading spaces/blanks. */
68 : /* -------------------------------------------------------------------- */
69 654 : const char *pszTestXML = pszFilename;
70 1318 : while( *pszTestXML != '\0' && isspace( (unsigned char)*pszTestXML ) )
71 10 : pszTestXML++;
72 :
73 654 : if( EQUALN(pszTestXML,"<OGRVRTDataSource>",18) )
74 : {
75 12 : pszXML = CPLStrdup(pszTestXML);
76 : }
77 :
78 : /* -------------------------------------------------------------------- */
79 : /* Open file and check if it contains appropriate XML. */
80 : /* -------------------------------------------------------------------- */
81 : else
82 : {
83 : VSILFILE *fp;
84 : char achHeader[18];
85 :
86 642 : fp = VSIFOpenL( pszFilename, "rb" );
87 :
88 642 : if( fp == NULL )
89 161 : return NULL;
90 :
91 481 : if( VSIFReadL( achHeader, sizeof(achHeader), 1, fp ) != 1 )
92 : {
93 17 : VSIFCloseL( fp );
94 17 : return NULL;
95 : }
96 :
97 464 : if( !EQUALN(achHeader,"<OGRVRTDataSource>",18) )
98 : {
99 365 : VSIFCloseL( fp );
100 365 : return NULL;
101 : }
102 :
103 : VSIStatBufL sStatBuf;
104 99 : if ( VSIStatL( pszFilename, &sStatBuf ) != 0 ||
105 : sStatBuf.st_size > 1024 * 1024 )
106 : {
107 0 : VSIFCloseL( fp );
108 0 : return NULL;
109 : }
110 :
111 : /* -------------------------------------------------------------------- */
112 : /* It is the right file, now load the full XML definition. */
113 : /* -------------------------------------------------------------------- */
114 99 : int nLen = (int) sStatBuf.st_size;
115 :
116 99 : VSIFSeekL( fp, 0, SEEK_SET );
117 :
118 99 : pszXML = (char *) VSIMalloc(nLen+1);
119 99 : if (pszXML == NULL)
120 : {
121 0 : VSIFCloseL( fp );
122 0 : return NULL;
123 : }
124 99 : pszXML[nLen] = '\0';
125 99 : if( ((int) VSIFReadL( pszXML, 1, nLen, fp )) != nLen )
126 : {
127 0 : CPLFree( pszXML );
128 0 : VSIFCloseL( fp );
129 :
130 0 : return NULL;
131 : }
132 99 : VSIFCloseL( fp );
133 : }
134 :
135 : /* -------------------------------------------------------------------- */
136 : /* Parse the XML. */
137 : /* -------------------------------------------------------------------- */
138 111 : CPLXMLNode *psTree = CPLParseXMLString( pszXML );
139 111 : CPLFree( pszXML );
140 :
141 111 : if( psTree == NULL )
142 0 : return NULL;
143 :
144 : /* -------------------------------------------------------------------- */
145 : /* Create a virtual datasource configured based on this XML input. */
146 : /* -------------------------------------------------------------------- */
147 111 : poDS = new OGRVRTDataSource();
148 111 : poDS->SetDriver(this);
149 : /* psTree is owned by poDS */
150 111 : if( !poDS->Initialize( psTree, pszFilename, bUpdate ) )
151 : {
152 0 : delete poDS;
153 0 : return NULL;
154 : }
155 :
156 111 : return poDS;
157 : }
158 :
159 : /************************************************************************/
160 : /* TestCapability() */
161 : /************************************************************************/
162 :
163 0 : int OGRVRTDriver::TestCapability( const char * pszCap )
164 :
165 : {
166 0 : return FALSE;
167 : }
168 :
169 : /************************************************************************/
170 : /* RegisterOGRVRT() */
171 : /************************************************************************/
172 :
173 178 : void RegisterOGRVRT()
174 :
175 : {
176 178 : OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRVRTDriver );
177 178 : }
178 :
|