1 : /******************************************************************************
2 : * $Id: ogrvrtdriver.cpp 24154 2012-03-23 21:47:11Z warmerdam $
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 24154 2012-03-23 21:47:11Z warmerdam $");
34 :
35 : /************************************************************************/
36 : /* ~OGRVRTDriver() */
37 : /************************************************************************/
38 :
39 214 : OGRVRTDriver::~OGRVRTDriver()
40 :
41 : {
42 214 : }
43 :
44 : /************************************************************************/
45 : /* GetName() */
46 : /************************************************************************/
47 :
48 14442 : const char *OGRVRTDriver::GetName()
49 :
50 : {
51 14442 : return "VRT";
52 : }
53 :
54 : /************************************************************************/
55 : /* Open() */
56 : /************************************************************************/
57 :
58 918 : OGRDataSource *OGRVRTDriver::Open( const char * pszFilename,
59 : int bUpdate )
60 :
61 : {
62 : OGRVRTDataSource *poDS;
63 918 : char *pszXML = NULL;
64 :
65 : /* -------------------------------------------------------------------- */
66 : /* Are we being passed the XML definition directly? */
67 : /* Skip any leading spaces/blanks. */
68 : /* -------------------------------------------------------------------- */
69 918 : const char *pszTestXML = pszFilename;
70 1846 : while( *pszTestXML != '\0' && isspace( (unsigned char)*pszTestXML ) )
71 10 : pszTestXML++;
72 :
73 918 : if( EQUALN(pszTestXML,"<OGRVRTDataSource>",18) )
74 : {
75 16 : 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[512];
85 :
86 902 : fp = VSIFOpenL( pszFilename, "rb" );
87 :
88 902 : if( fp == NULL )
89 190 : return NULL;
90 :
91 712 : memset( achHeader, 0, sizeof(achHeader) );
92 712 : VSIFReadL( achHeader, 1, sizeof(achHeader)-1, fp );
93 :
94 712 : if( strstr(achHeader,"<OGRVRTDataSource") == NULL )
95 : {
96 538 : VSIFCloseL( fp );
97 538 : return NULL;
98 : }
99 :
100 : VSIStatBufL sStatBuf;
101 174 : if ( VSIStatL( pszFilename, &sStatBuf ) != 0 ||
102 : sStatBuf.st_size > 1024 * 1024 )
103 : {
104 0 : CPLDebug( "VRT", "Unreasonable long file, not likely really VRT" );
105 0 : VSIFCloseL( fp );
106 0 : return NULL;
107 : }
108 :
109 : /* -------------------------------------------------------------------- */
110 : /* It is the right file, now load the full XML definition. */
111 : /* -------------------------------------------------------------------- */
112 174 : int nLen = (int) sStatBuf.st_size;
113 :
114 174 : VSIFSeekL( fp, 0, SEEK_SET );
115 :
116 174 : pszXML = (char *) VSIMalloc(nLen+1);
117 174 : if (pszXML == NULL)
118 : {
119 0 : VSIFCloseL( fp );
120 0 : return NULL;
121 : }
122 174 : pszXML[nLen] = '\0';
123 174 : if( ((int) VSIFReadL( pszXML, 1, nLen, fp )) != nLen )
124 : {
125 0 : CPLFree( pszXML );
126 0 : VSIFCloseL( fp );
127 :
128 0 : return NULL;
129 : }
130 174 : VSIFCloseL( fp );
131 : }
132 :
133 : /* -------------------------------------------------------------------- */
134 : /* Parse the XML. */
135 : /* -------------------------------------------------------------------- */
136 190 : CPLXMLNode *psTree = CPLParseXMLString( pszXML );
137 190 : CPLFree( pszXML );
138 :
139 190 : if( psTree == NULL )
140 0 : return NULL;
141 :
142 : /* -------------------------------------------------------------------- */
143 : /* Create a virtual datasource configured based on this XML input. */
144 : /* -------------------------------------------------------------------- */
145 190 : poDS = new OGRVRTDataSource();
146 190 : poDS->SetDriver(this);
147 : /* psTree is owned by poDS */
148 190 : if( !poDS->Initialize( psTree, pszFilename, bUpdate ) )
149 : {
150 0 : delete poDS;
151 0 : return NULL;
152 : }
153 :
154 190 : return poDS;
155 : }
156 :
157 : /************************************************************************/
158 : /* TestCapability() */
159 : /************************************************************************/
160 :
161 0 : int OGRVRTDriver::TestCapability( const char * pszCap )
162 :
163 : {
164 0 : return FALSE;
165 : }
166 :
167 : /************************************************************************/
168 : /* RegisterOGRVRT() */
169 : /************************************************************************/
170 :
171 226 : void RegisterOGRVRT()
172 :
173 : {
174 226 : OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRVRTDriver );
175 226 : }
|