IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 25, 2006, 7:31:54 PM (20 years ago)
Author:
drobbin
Message:

added psStringSplit from bug 657.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psString.c

    r5569 r6201  
    1111 *
    1212 *  @author Eric Van Alst, MHPCC
    13  *
    14  *  @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2005-11-22 19:11:07 $
     13 *  @author David Robbins, MHPCC
     14 *
     15 *  @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2006-01-26 05:31:54 $
    1617 *
    1718 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3536}
    3637
    37 psString psStringNCopy(const char *string, unsigned int nChar)
     38psString psStringNCopy(const char *string,
     39                       unsigned int nChar)
    3840{
    3941    char *returnValue = NULL;
     
    5961}
    6062
    61 ssize_t psStringAppend(char **dest, const char *format, ...)
     63ssize_t psStringAppend(char **dest,
     64                       const char *format,
     65                       ...)
    6266{
    6367    va_list         args;
     
    104108}
    105109
    106 ssize_t psStringPrepend(char **dest, const char *format, ...)
     110ssize_t psStringPrepend(char **dest,
     111                        const char *format,
     112                        ...)
    107113{
    108114    va_list         args;
     
    157163    return length;
    158164}
     165
     166// XXX: This should probably be implemented using strpbrk
     167psList *psStringSplit(const char *string,
     168                      const char *splitters)
     169{
     170    psList *values = psListAlloc(NULL); // The list of values to return
     171    unsigned int length = strlen(string); // The length of the string
     172    unsigned int numSplitters = strlen(splitters); // Number of characters that might split
     173    unsigned int start = 0;             // The position of the start of a word
     174    for (int i = 1; i < length; i++) {
     175        bool split = false;             // Is this character a splitter?
     176        for (int j = 0; j < numSplitters && ! split; j++) {
     177            if (string[i] == splitters[j]) {
     178                split = true;
     179            }
     180        }
     181        if (split) {
     182            if (i == start) {
     183                // Some idiot put in two spaces, or two commas or something
     184                start++;
     185            } else {
     186                // We're at the end of the word
     187                psString word = psStringNCopy(&string[start], i - start);
     188                (void)psListAdd(values, PS_LIST_TAIL, word);
     189                start = i + 1;
     190                psFree(word);
     191            }
     192        }
     193    }
     194    if (start < length) {
     195        // Copy the last word
     196        psString word = psStringNCopy(&string[start], length - start);
     197        (void)psListAdd(values, PS_LIST_TAIL, word);
     198        psFree(word);
     199    }
     200
     201    return values;
     202}
     203
Note: See TracChangeset for help on using the changeset viewer.