| 2038 | #endif |
| 2039 | |
| 2040 | static char** str_width_split( const char *str, int width ) |
| 2041 | { |
| 2042 | const char * pos; |
| 2043 | const char * end; |
| 2044 | char ** ret; |
| 2045 | int count, ii; |
| 2046 | int len; |
| 2047 | char delem = ' '; |
| 2048 | |
| 2049 | if ( str == NULL || str[0] == 0 ) |
| 2050 | { |
| 2051 | ret = malloc( sizeof(char*) ); |
| 2052 | if ( ret == NULL ) return ret; |
| 2053 | *ret = NULL; |
| 2054 | return ret; |
| 2055 | } |
| 2056 | |
| 2057 | len = strlen(str); |
| 2058 | |
| 2059 | // Find number of elements in the string |
| 2060 | count = 1; |
| 2061 | pos = str; |
| 2062 | end = pos + width; |
| 2063 | while (end < str + len) |
| 2064 | { |
| 2065 | end = reverse_search_char(pos, end, delem); |
| 2066 | if (end == pos) |
| 2067 | { |
| 2068 | // Shouldn't happen for reasonable input |
| 2069 | break; |
| 2070 | } |
| 2071 | count++; |
| 2072 | pos = end + 1; |
| 2073 | end = pos + width; |
| 2074 | } |
| 2075 | count++; |
| 2076 | ret = calloc( ( count + 1 ), sizeof(char*) ); |
| 2077 | if ( ret == NULL ) return ret; |
| 2078 | |
| 2079 | pos = str; |
| 2080 | end = pos + width; |
| 2081 | for (ii = 0; ii < count - 1 && end < str + len; ii++) |
| 2082 | { |
| 2083 | end = reverse_search_char(pos, end, delem); |
| 2084 | if (end == pos) |
| 2085 | { |
| 2086 | break; |
| 2087 | } |
| 2088 | ret[ii] = my_strndup(pos, end - pos); |
| 2089 | pos = end + 1; |
| 2090 | end = pos + width; |
| 2091 | } |
| 2092 | if (*pos != 0 && ii < count - 1) |
| 2093 | { |
| 2094 | ret[ii] = my_strndup(pos, width); |
| 2095 | } |
| 2096 | |
| 2097 | return ret; |
no test coverage detected