Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: NTF Translator 4 : * Purpose: NTFCodeList class implementation. 5 : * Author: Frank Warmerdam, warmerdam@pobox.com 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2001, 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 <algorithm> 30 : #include <stdarg.h> 31 : #include "ntf.h" 32 : #include "cpl_conv.h" 33 : #include "cpl_string.h" 34 : 35 : /************************************************************************/ 36 : /* NTFCodeList */ 37 : /************************************************************************/ 38 : 39 0 : NTFCodeList::NTFCodeList(NTFRecord *poRecord) 40 0 : : nNumCode(std::max(0, atoi(poRecord->GetField(20, 22)))), 41 0 : papszCodeVal(static_cast<char **>(CPLMalloc(sizeof(char *) * nNumCode))), 42 0 : papszCodeDes(static_cast<char **>(CPLMalloc(sizeof(char *) * nNumCode))) 43 : { 44 : 45 0 : CPLAssert(EQUAL(poRecord->GetField(1, 2), "42")); 46 : 47 0 : snprintf(szValType, sizeof(szValType), "%s", poRecord->GetField(13, 14)); 48 0 : snprintf(szFInter, sizeof(szFInter), "%s", poRecord->GetField(15, 19)); 49 : 50 0 : const int nRecordLen = poRecord->GetLength(); 51 0 : const char *pszText = poRecord->GetData() + 22; 52 0 : int iThisField = 0; 53 0 : for (; nRecordLen > 22 && *pszText != '\0' && iThisField < nNumCode; 54 : iThisField++) 55 : { 56 0 : char szVal[128] = {}; 57 0 : int iLen = 0; 58 0 : while (iLen < static_cast<int>(sizeof(szVal)) - 1 && *pszText != '\\' && 59 0 : *pszText != '\0') 60 : { 61 0 : szVal[iLen++] = *(pszText++); 62 : } 63 0 : szVal[iLen] = '\0'; 64 : 65 0 : if (*pszText == '\\') 66 0 : pszText++; 67 : 68 0 : iLen = 0; 69 0 : char szDes[128] = {}; 70 0 : while (iLen < static_cast<int>(sizeof(szDes)) - 1 && *pszText != '\\' && 71 0 : *pszText != '\0') 72 : { 73 0 : szDes[iLen++] = *(pszText++); 74 : } 75 0 : szDes[iLen] = '\0'; 76 : 77 0 : if (*pszText == '\\') 78 0 : pszText++; 79 : 80 0 : papszCodeVal[iThisField] = CPLStrdup(szVal); 81 0 : papszCodeDes[iThisField] = CPLStrdup(szDes); 82 : } 83 : 84 0 : if (iThisField < nNumCode) 85 : { 86 0 : nNumCode = iThisField; 87 0 : CPLDebug("NTF", "Didn't get all the expected fields from a CODELIST."); 88 : } 89 0 : } 90 : 91 : /************************************************************************/ 92 : /* ~NTFCodeList() */ 93 : /************************************************************************/ 94 : 95 0 : NTFCodeList::~NTFCodeList() 96 : 97 : { 98 0 : for (int i = 0; i < nNumCode; i++) 99 : { 100 0 : CPLFree(papszCodeVal[i]); 101 0 : CPLFree(papszCodeDes[i]); 102 : } 103 : 104 0 : CPLFree(papszCodeVal); 105 0 : CPLFree(papszCodeDes); 106 0 : } 107 : 108 : /************************************************************************/ 109 : /* Lookup() */ 110 : /************************************************************************/ 111 : 112 0 : const char *NTFCodeList::Lookup(const char *pszCode) 113 : 114 : { 115 0 : for (int i = 0; i < nNumCode; i++) 116 : { 117 0 : if (EQUAL(pszCode, papszCodeVal[i])) 118 0 : return papszCodeDes[i]; 119 : } 120 : 121 0 : return nullptr; 122 : }