** Splits a string into multiple strings based on ch. Consecutive ch's are ignored. */
| 769 | ** Splits a string into multiple strings based on ch. Consecutive ch's are ignored. |
| 770 | */ |
| 771 | char **msStringSplit(const char *string, char ch, int *num_tokens) |
| 772 | { |
| 773 | int i,j,k; |
| 774 | int length,n; |
| 775 | char **token; |
| 776 | char last_ch='\0'; |
| 777 | |
| 778 | n = 1; /* always at least 1 token, the string itself */ |
| 779 | length = strlen(string); |
| 780 | for(i=0; i<length; i++) { |
| 781 | if(string[i] == ch && last_ch != ch) |
| 782 | n++; |
| 783 | last_ch = string[i]; |
| 784 | } |
| 785 | |
| 786 | token = (char **) msSmallMalloc(sizeof(char *)*n); |
| 787 | if(!token) return(NULL); |
| 788 | |
| 789 | k = 0; |
| 790 | token[k] = (char *)msSmallMalloc(sizeof(char)*(length+1)); |
| 791 | if(!token[k]) return(NULL); |
| 792 | |
| 793 | j = 0; |
| 794 | last_ch='\0'; |
| 795 | for(i=0; i<length; i++) { |
| 796 | if(string[i] == ch) { |
| 797 | |
| 798 | if(last_ch == ch) |
| 799 | continue; |
| 800 | |
| 801 | token[k][j] = '\0'; /* terminate current token */ |
| 802 | |
| 803 | k++; |
| 804 | token[k] = (char *)msSmallMalloc(sizeof(char)*(length+1)); |
| 805 | if(!token[k]) return(NULL); |
| 806 | |
| 807 | j = 0; |
| 808 | } else { |
| 809 | token[k][j] = string[i]; |
| 810 | j++; |
| 811 | } |
| 812 | |
| 813 | last_ch = string[i]; |
| 814 | } |
| 815 | |
| 816 | token[k][j] = '\0'; /* terminate last token */ |
| 817 | |
| 818 | *num_tokens = n; |
| 819 | |
| 820 | return(token); |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | This function is a copy of CSLTokenizeString2() function of the CPL component. |
no test coverage detected