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 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #include "cpl_port.h" 14 : #include "iso8211.h" 15 : 16 : #include <cstdlib> 17 : #include <cstring> 18 : 19 : #include "cpl_conv.h" 20 : 21 : /************************************************************************/ 22 : /* DDFScanInt() */ 23 : /* */ 24 : /* Read up to nMaxChars from the passed string, and interpret */ 25 : /* as an integer. */ 26 : /************************************************************************/ 27 : 28 24112 : int DDFScanInt(const char *pszString, int nMaxChars) 29 : 30 : { 31 24112 : char szWorking[33] = {}; 32 : 33 24112 : if (nMaxChars > 32 || nMaxChars == 0) 34 0 : nMaxChars = 32; 35 : 36 24112 : memcpy(szWorking, pszString, nMaxChars); 37 24112 : szWorking[nMaxChars] = '\0'; 38 : 39 24112 : return atoi(szWorking); 40 : } 41 : 42 : /************************************************************************/ 43 : /* DDFScanVariable() */ 44 : /* */ 45 : /* Establish the length of a variable length string in a */ 46 : /* record. */ 47 : /************************************************************************/ 48 : 49 0 : int DDFScanVariable(const char *pszRecord, int nMaxChars, int nDelimChar) 50 : 51 : { 52 0 : int i = 0; // Used after for. 53 : 54 0 : for (; i < nMaxChars - 1 && pszRecord[i] != nDelimChar; i++) 55 : { 56 : } 57 : 58 0 : return i; 59 : } 60 : 61 : /************************************************************************/ 62 : /* DDFFetchVariable() */ 63 : /* */ 64 : /* Fetch a variable length string from a record, and allocate */ 65 : /* it as a new string (with CPLStrdup()). */ 66 : /************************************************************************/ 67 : 68 5868 : char *DDFFetchVariable(const char *pszRecord, int nMaxChars, int nDelimChar1, 69 : int nDelimChar2, int *pnConsumedChars) 70 : 71 : { 72 5868 : int i = 0; // Used after for. 73 139383 : for (; i < nMaxChars - 1 && pszRecord[i] != nDelimChar1 && 74 133515 : pszRecord[i] != nDelimChar2; 75 : i++) 76 : { 77 : } 78 : 79 5868 : *pnConsumedChars = i; 80 5868 : if (i < nMaxChars && 81 5527 : (pszRecord[i] == nDelimChar1 || pszRecord[i] == nDelimChar2)) 82 5527 : (*pnConsumedChars)++; 83 : 84 5868 : char *pszReturn = static_cast<char *>(CPLMalloc(i + 1)); 85 5868 : pszReturn[i] = '\0'; 86 5868 : strncpy(pszReturn, pszRecord, i); 87 : 88 5868 : return pszReturn; 89 : }