added g_strtrim

This commit is contained in:
jsorg71 2007-10-27 05:53:19 +00:00
parent ca37e8cb1d
commit d3fa8fd6f6
3 changed files with 123 additions and 0 deletions

View File

@ -188,6 +188,8 @@ file_split_name_value(char* text, char* name, char* value)
name[name_index] = 0;
}
}
g_strtrim(name, 3); /* trim both right and left */
g_strtrim(value, 3); /* trim both right and left */
return 0;
}

View File

@ -1198,6 +1198,125 @@ g_wcstombs(char* dest, const twchar* src, int n)
return rv;
}
/*****************************************************************************/
/* returns error */
/* trim spaces and tabs, anything <= space */
/* trim_flags 1 trim left, 2 trim right, 3 trim both, 4 trim through */
/* this will always shorten the string or not change it */
int APP_CC
g_strtrim(char* str, int trim_flags)
{
int index;
int len;
int text1_index;
int got_char;
wchar_t* text;
wchar_t* text1;
len = mbstowcs(0, str, 0);
if (len < 1)
{
return 0;
}
if ((trim_flags < 1) || (trim_flags > 4))
{
return 1;
}
text = (wchar_t*)malloc(len * sizeof(wchar_t) + 8);
text1 = (wchar_t*)malloc(len * sizeof(wchar_t) + 8);
text1_index = 0;
mbstowcs(text, str, len + 1);
switch (trim_flags)
{
case 4: /* trim through */
for (index = 0; index < len; index++)
{
if (text[index] > 32)
{
text1[text1_index] = text[index];
text1_index++;
}
}
text1[text1_index] = 0;
break;
case 3: /* trim both */
got_char = 0;
for (index = 0; index < len; index++)
{
if (got_char)
{
text1[text1_index] = text[index];
text1_index++;
}
else
{
if (text[index] > 32)
{
text1[text1_index] = text[index];
text1_index++;
got_char = 1;
}
}
}
text1[text1_index] = 0;
len = text1_index;
/* trim right */
for (index = len - 1; index >= 0; index--)
{
if (text1[index] > 32)
{
break;
}
}
text1_index = index + 1;
text1[text1_index] = 0;
break;
case 2: /* trim right */
/* copy it */
for (index = 0; index < len; index++)
{
text1[text1_index] = text[index];
text1_index++;
}
/* trim right */
for (index = len - 1; index >= 0; index--)
{
if (text1[index] > 32)
{
break;
}
}
text1_index = index + 1;
text1[text1_index] = 0;
break;
case 1: /* trim left */
got_char = 0;
for (index = 0; index < len; index++)
{
if (got_char)
{
text1[text1_index] = text[index];
text1_index++;
}
else
{
if (text[index] > 32)
{
text1[text1_index] = text[index];
text1_index++;
got_char = 1;
}
}
}
text1[text1_index] = 0;
break;
}
wcstombs(str, text1, text1_index + 1);
free(text);
free(text1);
return 0;
}
/*****************************************************************************/
long APP_CC
g_load_library(char* in)

View File

@ -161,6 +161,8 @@ int APP_CC
g_mbstowcs(twchar* dest, const char* src, int n);
int APP_CC
g_wcstombs(char* dest, const twchar* src, int n);
int APP_CC
g_strtrim(char* str, int trim_flags);
long APP_CC
g_load_library(char* in);
int APP_CC