1 /******************************************************************************
2  *
3  * Project:  SDTS Translator
4  * Purpose:  Implementation of SDTSAttrReader class.
5  * Author:   Frank Warmerdam, warmerdam@pobox.com
6  *
7  ******************************************************************************
8  * Copyright (c) 1999, Frank Warmerdam
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  ****************************************************************************/
28 
29 #include "sdts_al.h"
30 
31 CPL_CVSID("$Id: sdtsattrreader.cpp 7e07230bbff24eb333608de4dbd460b7312839d0 2017-12-11 19:08:47Z Even Rouault $")
32 
33 /************************************************************************/
34 /* ==================================================================== */
35 /*                             SDTSAttrRecord                           */
36 /* ==================================================================== */
37 /************************************************************************/
38 
39 /************************************************************************/
40 /*                           SDTSAttrRecord()                           */
41 /************************************************************************/
42 
SDTSAttrRecord()43 SDTSAttrRecord::SDTSAttrRecord() :
44     poWholeRecord(nullptr),
45     poATTR(nullptr)
46 {}
47 
48 /************************************************************************/
49 /*                          ~SDTSAttrRecord()                           */
50 /************************************************************************/
51 
~SDTSAttrRecord()52 SDTSAttrRecord::~SDTSAttrRecord()
53 
54 {
55     if( poWholeRecord != nullptr )
56         delete poWholeRecord;
57 }
58 
59 /************************************************************************/
60 /*                                Dump()                                */
61 /************************************************************************/
62 
Dump(FILE * fp)63 void SDTSAttrRecord::Dump( FILE * fp )
64 
65 {
66     if( poATTR != nullptr )
67         poATTR->Dump( fp );
68 }
69 
70 /************************************************************************/
71 /* ==================================================================== */
72 /*                             SDTSAttrReader                           */
73 /*                                                                      */
74 /*      This is the class used to read a primary attribute module.      */
75 /* ==================================================================== */
76 /************************************************************************/
77 
78 /************************************************************************/
79 /*                           SDTSAttrReader()                           */
80 /************************************************************************/
81 
SDTSAttrReader()82 SDTSAttrReader::SDTSAttrReader() :
83     bIsSecondary(FALSE)
84 {}
85 
86 /************************************************************************/
87 /*                          ~SDTSAttrReader()                           */
88 /************************************************************************/
89 
~SDTSAttrReader()90 SDTSAttrReader::~SDTSAttrReader()
91 {
92     Close();
93 }
94 
95 /************************************************************************/
96 /*                               Close()                                */
97 /************************************************************************/
98 
Close()99 void SDTSAttrReader::Close()
100 
101 {
102     ClearIndex();
103     oDDFModule.Close();
104 }
105 
106 /************************************************************************/
107 /*                                Open()                                */
108 /*                                                                      */
109 /*      Open the requested attr file, and prepare to start reading      */
110 /*      data records.                                                   */
111 /************************************************************************/
112 
Open(const char * pszFilename)113 int SDTSAttrReader::Open( const char *pszFilename )
114 
115 {
116     bool bSuccess = CPL_TO_BOOL(oDDFModule.Open( pszFilename ));
117 
118     if( bSuccess )
119         bIsSecondary = (oDDFModule.FindFieldDefn("ATTS") != nullptr);
120 
121     return bSuccess;
122 }
123 
124 /************************************************************************/
125 /*                           GetNextRecord()                            */
126 /************************************************************************/
127 
GetNextRecord(SDTSModId * poModId,DDFRecord ** ppoRecord,int bDuplicate)128 DDFField *SDTSAttrReader::GetNextRecord( SDTSModId * poModId,
129                                          DDFRecord ** ppoRecord,
130                                          int bDuplicate )
131 
132 {
133 /* -------------------------------------------------------------------- */
134 /*      Fetch a record.                                                 */
135 /* -------------------------------------------------------------------- */
136     if( ppoRecord != nullptr )
137         *ppoRecord = nullptr;
138 
139     if( oDDFModule.GetFP() == nullptr )
140         return nullptr;
141 
142     DDFRecord *poRecord = oDDFModule.ReadRecord();
143 
144     if( poRecord == nullptr )
145         return nullptr;
146 
147 /* -------------------------------------------------------------------- */
148 /*      Make a copy of the record for persistent use if requested by    */
149 /*      the caller.                                                     */
150 /* -------------------------------------------------------------------- */
151     if( bDuplicate )
152         poRecord = poRecord->Clone();
153 
154 /* -------------------------------------------------------------------- */
155 /*      Find the ATTP field.                                            */
156 /* -------------------------------------------------------------------- */
157     DDFField *poATTP = poRecord->FindField( "ATTP", 0 );
158     if( poATTP == nullptr )
159     {
160         poATTP = poRecord->FindField( "ATTS", 0 );
161     }
162 
163     if( poATTP == nullptr )
164         return nullptr;
165 
166 /* -------------------------------------------------------------------- */
167 /*      Update the module ID if required.                               */
168 /* -------------------------------------------------------------------- */
169     if( poModId != nullptr )
170     {
171         DDFField *poATPR = poRecord->FindField( "ATPR" );
172 
173         if( poATPR == nullptr )
174             poATPR = poRecord->FindField( "ATSC" );
175 
176         if( poATPR != nullptr )
177             poModId->Set( poATPR );
178     }
179 
180 /* -------------------------------------------------------------------- */
181 /*      return proper answer.                                           */
182 /* -------------------------------------------------------------------- */
183     if( ppoRecord != nullptr )
184         *ppoRecord = poRecord;
185 
186     return poATTP;
187 }
188 
189 /************************************************************************/
190 /*                         GetNextAttrRecord()                          */
191 /************************************************************************/
192 
GetNextAttrRecord()193 SDTSAttrRecord *SDTSAttrReader::GetNextAttrRecord()
194 
195 {
196     SDTSModId   oModId;
197     DDFRecord   *poRawRecord = nullptr;
198 
199     DDFField *poATTRField = GetNextRecord( &oModId, &poRawRecord, TRUE );
200 
201     if( poATTRField == nullptr )
202         return nullptr;
203 
204     SDTSAttrRecord *poAttrRecord = new SDTSAttrRecord();
205 
206     poAttrRecord->poWholeRecord = poRawRecord;
207     poAttrRecord->poATTR = poATTRField;
208     memcpy( &(poAttrRecord->oModId), &oModId, sizeof(SDTSModId) );
209 
210     return poAttrRecord;
211 }
212