/[svn]/liblscp/trunk/src/common.c
ViewVC logotype

Diff of /liblscp/trunk/src/common.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 124 by capela, Fri Jun 4 14:32:51 2004 UTC revision 125 by capela, Mon Jun 14 21:04:04 2004 UTC
# Line 193  int lscp_szsplit_size ( char **ppszSplit Line 193  int lscp_szsplit_size ( char **ppszSplit
193  #endif // LSCP_SZSPLIT_COUNT  #endif // LSCP_SZSPLIT_COUNT
194    
195    
196    // Split a comma separated string into a -1 terminated array of positive integers.
197    int *lscp_isplit_create ( const char *pszCsv, const char *pszSeps )
198    {
199        char *pchHead, *pch;
200        int iSize, i, j, cchSeps;
201        int *piSplit, *piNewSplit;
202    
203        // Initial size is one chunk away.
204        iSize = LSCP_SPLIT_CHUNK1;
205        // Allocate and split...
206        piSplit = (int *) malloc(iSize * sizeof(int));
207        if (piSplit == NULL)
208            return NULL;
209    
210        // Make a copy of the original string.
211        i = 0;
212        pchHead = (char *) pszCsv;
213        if ((piSplit[i++] = atoi(pchHead)) < 0) {
214            free(piSplit);
215            return NULL;
216        }
217    
218        // Go on for it...
219        cchSeps = strlen(pszSeps);
220        while ((pch = strpbrk(pchHead, pszSeps)) != NULL) {
221            // Pre-advance to next item.
222            pchHead = pch + cchSeps;
223            // Make it official.
224            piSplit[i++] = atoi(pchHead);
225            // Do we need to grow?
226            if (i >= iSize) {
227                // Yes, but only grow in chunks.
228                iSize += LSCP_SPLIT_CHUNK1;
229                // Allocate and copy to new split array.
230                piNewSplit = (int *) malloc(iSize * sizeof(int));
231                if (piNewSplit) {
232                    for (j = 0; j < i; j++)
233                        piNewSplit[j] = piSplit[j];
234                    free(piSplit);
235                    piSplit = piNewSplit;
236                }
237            }
238        }
239    
240        // NULL terminate split array.
241        for ( ; i < iSize; i++)
242            piSplit[i] = -1;
243    
244        return piSplit;
245    }
246    
247    
248    // Destroy a integer splitted array.
249    void lscp_isplit_destroy ( int *piSplit )
250    {
251        if (piSplit)
252            free(piSplit);
253    }
254    
255    
256    #ifdef LSCP_ISPLIT_COUNT
257    
258    // Compute a string list valid item count.
259    int lscp_isplit_count ( int *piSplit )
260    {
261        int i = 0;
262        while (piSplit && piSplit[i] >= 0)
263            i++;
264        return i;
265    }
266    
267    // Compute a string list size.
268    int lscp_isplit_size ( int *piSplit )
269    {
270        return LSCP_SPLIT_SIZE(lscp_isplit_count(piSplit));
271    }
272    
273    #endif // LSCP_ISPLIT_COUNT
274    
275    
276  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
277  // Engine info struct helper functions.  // Engine info struct helper functions.
278    

Legend:
Removed from v.124  
changed lines
  Added in v.125

  ViewVC Help
Powered by ViewVC