Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: ISO 8211 Access 4 : * Purpose: Various utility functions. 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 "cpl_port.h" 30 : #include "iso8211.h" 31 : 32 : #include <cstdlib> 33 : #include <cstring> 34 : 35 : #include "cpl_conv.h" 36 : 37 : /************************************************************************/ 38 : /* DDFScanInt() */ 39 : /* */ 40 : /* Read up to nMaxChars from the passed string, and interpret */ 41 : /* as an integer. */ 42 : /************************************************************************/ 43 : 44 24112 : int DDFScanInt(const char *pszString, int nMaxChars) 45 : 46 : { 47 24112 : char szWorking[33] = {}; 48 : 49 24112 : if (nMaxChars > 32 || nMaxChars == 0) 50 0 : nMaxChars = 32; 51 : 52 24112 : memcpy(szWorking, pszString, nMaxChars); 53 24112 : szWorking[nMaxChars] = '\0'; 54 : 55 24112 : return atoi(szWorking); 56 : } 57 : 58 : /************************************************************************/ 59 : /* DDFScanVariable() */ 60 : /* */ 61 : /* Establish the length of a variable length string in a */ 62 : /* record. */ 63 : /************************************************************************/ 64 : 65 0 : int DDFScanVariable(const char *pszRecord, int nMaxChars, int nDelimChar) 66 : 67 : { 68 0 : int i = 0; // Used after for. 69 : 70 0 : for (; i < nMaxChars - 1 && pszRecord[i] != nDelimChar; i++) 71 : { 72 : } 73 : 74 0 : return i; 75 : } 76 : 77 : /************************************************************************/ 78 : /* DDFFetchVariable() */ 79 : /* */ 80 : /* Fetch a variable length string from a record, and allocate */ 81 : /* it as a new string (with CPLStrdup()). */ 82 : /************************************************************************/ 83 : 84 5868 : char *DDFFetchVariable(const char *pszRecord, int nMaxChars, int nDelimChar1, 85 : int nDelimChar2, int *pnConsumedChars) 86 : 87 : { 88 5868 : int i = 0; // Used after for. 89 139383 : for (; i < nMaxChars - 1 && pszRecord[i] != nDelimChar1 && 90 133515 : pszRecord[i] != nDelimChar2; 91 : i++) 92 : { 93 : } 94 : 95 5868 : *pnConsumedChars = i; 96 5868 : if (i < nMaxChars && 97 5527 : (pszRecord[i] == nDelimChar1 || pszRecord[i] == nDelimChar2)) 98 5527 : (*pnConsumedChars)++; 99 : 100 5868 : char *pszReturn = static_cast<char *>(CPLMalloc(i + 1)); 101 5868 : pszReturn[i] = '\0'; 102 5868 : strncpy(pszReturn, pszRecord, i); 103 : 104 5868 : return pszReturn; 105 : }