You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
5.5 KiB

/*==================================================================================================================
Copyright(c) 2012 ORIGIN.
Unpublished - All rights reserved
====================================================================================================================
File description:
Filename: string_utils.c
Module : Common module.
This file includes some operations of the string.
====================================================================================================================
Date Name Description of Change
3-Feb-2015 Ray li Initialize creation
$HISTORY$
===================================================================================================================*/
#ifndef _cplusplus
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif
#endif
#include <fclasses/tc_string.h>
#include <tc/tc_util.h>
#include <itk/mem.h>
#include <tccore/workspaceobject.h>
#include <stdlib.h>
#include <ctype.h>
#include "string_utils.h"
void Split( string strArg, string spliter, vector<string> &ans )
{
ans.clear();
size_t index0;
string one_arg;
if ( strArg.find_first_not_of(' ') == string::npos )
strArg = "";
while( strArg.size()>0 )
{
index0 = strArg.find(spliter);
if( index0 != string::npos )
{
one_arg = strArg.substr( 0, index0 );
strArg = strArg.substr( index0 + spliter.size() );
ans.push_back( one_arg );
}
else
{
ans.push_back( strArg );
break;
}
}
}
char* GSTR_clone( char **dst, const char *src )
{
char *retVal = NULL;
int srcLen = 0;
*dst = NULL;
if (src == NULL)
return NULL;
srcLen = (int)tc_strlen( src ) + 1;
*dst = (char*)MEM_alloc( srcLen * sizeof(char) );
retVal = tc_strncpy( *dst, src, srcLen );
(*dst)[srcLen - 1] = '\0';
return retVal;
}
char* GSTR_copy( char *dst, const char *src, int dstSize )
{
char *retVal = tc_strncpy( dst, src, dstSize );
dst[dstSize - 1] = '\0';
return retVal;
}
char* GSTR_int_to_string( char **dst, int value )
{
char strVal[128 + 1];
*dst = NULL;
memset( strVal, 0, sizeof(strVal)/sizeof(char) );
sprintf( strVal, "%d", value );
return GSTR_clone( dst, strVal );
}
void GSTR_format_int_to_string( char *dst, int digitNum, int value )
{
char sNum[WSO_desc_size_c + 1];
sprintf( sNum, "%%0%dd", digitNum );
sprintf( dst, sNum, value );
}
void GSTR_format_string( const char *dst, int m, const char *fill_char, char **out )
{
char sNum[WSO_name_size_c + 1] = {0};
char sNew[WSO_name_size_c + 1] = {0};
sprintf( sNum, "%%%d.%ds", m, m );
sprintf( sNew, sNum, dst );
STRNG_replace_str( sNew, " ", fill_char, out );
}
char* GSTR_string_append( const char *s1, const char *s2 )
{
char *s = NULL;
if (s1 == NULL || s2 == NULL)
{
GSTR_clone(&s, s1 == NULL ? (s2 == NULL ? "" : s2) : s1 );
}
else
{
int size = (int)tc_strlen(s1) + (int)tc_strlen(s2) + 1;
s = (char *)MEM_alloc( size );
tc_strcpy( s, s1 );
tc_strcat( s, s2 );
s[size - 1] = '\0';
}
return s;
}
logical GSTR_is_float(const char *str)
{
logical isfloat = true;
char *pStr = (char *)str;
logical hasPositive = false;
logical hasMinus = false;
logical hasDot = false;
if (str == NULL)
return false;
while (*pStr != '\0' && isfloat == true)
{
if ( (*pStr >= '0' && *pStr <= '9'))
{
//continue;
}
else if ( *pStr == '+' )
{
isfloat = (hasPositive ? false : (hasPositive = true));
}
else if ( *pStr == '-' )
{
isfloat = (hasMinus ? false : (hasMinus = true));
}
else if ( *pStr == '.' )
{
isfloat = (hasDot ? false : (hasDot = true));
}
else
isfloat = false;
pStr ++;
}
return isfloat;
}
logical GSTR_is_number(const char *str)
{
logical is_number = true;
char *pStr = (char *)str;
if (str == NULL)
return false;
while (*pStr != '\0')
{
if ( !( (*pStr >= '0' && *pStr <= '9') || *pStr == '-' ) )
{
is_number = false;
break;
}
pStr ++;
}
return is_number;
}
logical GSTR_is_ascii(char ch)
{
return ((unsigned int)ch) < 128;
}
int GSTR_trim_l( char *str, char s )
{
int count = 0;
char *pointer = str, *poffset = NULL;
if (str == NULL || str[0] == '\0')
return 0;
while ( *pointer != '\0' )
{
if ( *pointer != s )
{
break;
}
count++;
pointer++;
}
if (count == 0)
return 0;
poffset = str + count;
pointer = str;
while ( *poffset != '\0' )
{
*pointer = *poffset;
pointer ++;
poffset ++;
}
*pointer = '\0';
return count;
}
int GSTR_trim_r( char *str, char s )
{
int count = 0;
char *pointer = NULL;
if (str == NULL || str[0] == '\0')
return 0;
pointer = str + ((int) strlen(str) - 1);
while ( pointer != str )
{
if ( *pointer != s )
{
break;
}
*pointer = '\0';
count++;
pointer--;
}
return count;
}
void GSTR_trim_float( char *floatValue )
{
if ( !IS_EMPTY(floatValue) && tc_strstr(floatValue, ".") != NULL )
{
int len = 0;
GSTR_trim_r(floatValue, '0');
len = (int)tc_strlen(floatValue);
if (floatValue[ len - 1 ] == '.')
floatValue[ len - 1 ] = '\0';
}
}