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