| 1 | /********************************************************************************** |
| 2 | * iniElement |
| 3 | * |
| 4 | * Use when; |
| 5 | * 1. strtok is scary (also does not handle empty elements well) |
| 6 | * 2. strstr is not portable |
| 7 | * 3. performance is less important than simplicity and the above (feel free to improve on this) |
| 8 | * |
| 9 | ************************************************** |
| 10 | * This code was created by Peter Harvey @ CodeByDesign. |
| 11 | * Released under LGPL 28.JAN.99 |
| 12 | * |
| 13 | * Contributions from... |
| 14 | * ----------------------------------------------- |
| 15 | * Peter Harvey - pharvey@codebydesign.com |
| 16 | **************************************************/ |
| 17 | |
| 18 | #include <config.h> |
| 19 | #include "ini.h" |
| 20 | |
| 21 | int iniElement( char *pszData, char cSeperator, char cTerminator, int nElement, char *pszElement, int nMaxElement ) |
| 22 | { |
| 23 | int nCurElement = 0; |
| 24 | int nChar = 0; |
| 25 | int nCharInElement = 0; |
| 26 | |
| 27 | memset( pszElement, '\0', nMaxElement ); |
| 28 | for ( ; nCurElement <= nElement && (nCharInElement+1) < nMaxElement; nChar++ ) |
| 29 | { |
| 30 | /* check for end of data */ |
| 31 | if ( cSeperator != cTerminator && pszData[nChar] == cTerminator ) |
| 32 | { |
| 33 | break; |
| 34 | } |
| 35 | |
| 36 | if ( cSeperator == cTerminator && pszData[nChar] == cSeperator && pszData[nChar+1] == cTerminator ) |
| 37 | { |
| 38 | break; |
| 39 | } |
| 40 | |
| 41 | /* check for end of element */ |
| 42 | if ( pszData[nChar] == cSeperator ) |
| 43 | { |
| 44 | nCurElement++; |
| 45 | } |
| 46 | else if ( nCurElement == nElement ) |
| 47 | { |
| 48 | pszElement[nCharInElement] = pszData[nChar]; |
| 49 | nCharInElement++; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if ( pszElement[0] == '\0' ) |
| 54 | { |
| 55 | return INI_NO_DATA; |
| 56 | } |
| 57 | |
| 58 | return INI_SUCCESS; |
| 59 | } |
| 60 | |
| 61 | /* Like iniElement(), but rather than a terminator, the input buffer length is given */ |
| 62 | |
| 63 | int iniElementMax( char *pData, char cSeperator, int nDataLen, int nElement, char *pszElement, int nMaxElement ) |
| 64 | { |
| 65 | int nCurElement = 0; |
| 66 | int nChar = 0; |
| 67 | int nCharInElement = 0; |
| 68 | |
| 69 | memset( pszElement, '\0', nMaxElement ); |
| 70 | for ( ; nCurElement <= nElement && (nCharInElement+1) < nMaxElement && nChar < nDataLen ; nChar++ ) |
| 71 | { |
| 72 | /* check for end of element */ |
| 73 | if ( pData[nChar] == cSeperator ) |
| 74 | { |
| 75 | nCurElement++; |
| 76 | } |
| 77 | else if ( nCurElement == nElement ) |
| 78 | { |
| 79 | pszElement[nCharInElement] = pData[nChar]; |
| 80 | nCharInElement++; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if ( pszElement[0] == '\0' ) |
| 85 | { |
| 86 | return INI_NO_DATA; |
| 87 | } |
| 88 | |
| 89 | return INI_SUCCESS; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | int iniElementEOL( char *pszData, char cSeperator, char cTerminator, int nElement, char *pszElement, int nMaxElement ) |
| 94 | { |
| 95 | int nCurElement = 0; |
| 96 | int nChar = 0; |
| 97 | int nCharInElement = 0; |
| 98 | |
| 99 | memset( pszElement, '\0', nMaxElement ); |
| 100 | for ( ;(nCharInElement+1) < nMaxElement; nChar++ ) |
| 101 | { |
| 102 | /* check for end of data */ |
| 103 | if ( cSeperator != cTerminator && pszData[nChar] == cTerminator ) |
| 104 | { |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | if ( cSeperator == cTerminator && pszData[nChar] == cSeperator && pszData[nChar+1] == cTerminator ) |
| 109 | { |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | /* check for end of element */ |
| 114 | if ( pszData[nChar] == cSeperator && nCurElement < nElement ) |
| 115 | { |
| 116 | nCurElement++; |
| 117 | } |
| 118 | else if ( nCurElement >= nElement ) |
| 119 | { |
| 120 | pszElement[nCharInElement] = pszData[nChar]; |
| 121 | nCharInElement++; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if ( pszElement[0] == '\0' ) |
| 126 | { |
| 127 | return INI_NO_DATA; |
| 128 | } |
| 129 | |
| 130 | return INI_SUCCESS; |
| 131 | } |
| 132 | |
| 133 | int iniElementToEnd( char *pszData, char cSeperator, char cTerminator, int nElement, char *pszElement, int nMaxElement ) |
| 134 | { |
| 135 | int nCurElement = 0; |
| 136 | int nChar = 0; |
| 137 | int nCharInElement = 0; |
| 138 | |
| 139 | memset( pszElement, '\0', nMaxElement ); |
| 140 | for ( ; nCurElement <= nElement && (nCharInElement+1) < nMaxElement; nChar++ ) |
| 141 | { |
| 142 | /* check for end of data */ |
| 143 | if ( cSeperator != cTerminator && pszData[nChar] == cTerminator ) |
| 144 | break; |
| 145 | |
| 146 | if ( cSeperator == cTerminator && pszData[nChar] == cSeperator && pszData[nChar+1] == cTerminator ) |
| 147 | break; |
| 148 | |
| 149 | /* check for end of element */ |
| 150 | if ( pszData[nChar] == cSeperator && ( nCurElement < nElement )) |
| 151 | nCurElement++; |
| 152 | else if ( nCurElement == nElement ) |
| 153 | { |
| 154 | pszElement[nCharInElement] = pszData[nChar]; |
| 155 | nCharInElement++; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if ( pszElement[0] == '\0' ) |
| 160 | return INI_NO_DATA; |
| 161 | |
| 162 | return INI_SUCCESS; |
| 163 | } |
| 164 | |
| 165 | |