/*====================================================================*\ FILE : UtilString.c PURPOSE : String and wide-string utilities for Pro/TOOLKIT applications. HISTORY.. DATE AUTHOR MODIFICATIONS 26 Jan 96 Alistair $$1 Adapted from Pro/DEVELOP. 22 Apr 96 Michael $$2 Changed header scheme 24 Sep 96 H-01-10 Xuekai $$3 Remove "protk.h" 15 Sep 97 H-03-22 Pavel $$4 replace Pro/D on Pro/E 18 Sep 97 H-03-22 akula $$5 correct to free error compiling on sgi 06 Oct 97 H-03-25 Pavel $$6 More includes 19 Oct 97 H-03-27 Pavel $$7 TEST_CALL_REPORT 10 Dec 97 H-03-32 Philippe $$8 add ProUtilWstrcmp 05 Jan 98 H-03-35 Steve $$9 Added ProUtilWstrLen 20-Jan-98 H-03-37 aab $$10 Fixed some bugs for c++ compiler 29-Jan-98 H-03-37 Philippe $$11 add ProUtilWstrcat() ProUtilWstrStrcatToWstr() ProUtilStrWstrcatToWstr() ProUtilWstrStrcatToStr() ProUtilStrWstrcatToStr() ProUtilWstringArrayToFile() ProUtilFileToWstringArray() 19-Feb-98 H-03-39 Philippe $$12 fix type casting for C++ compilers 03-Jul-98 H-03-49 Misha $$13 add pd_prototypes include 16-Jul-98 I-01-14 AKH $$14 Remove pd_prototypes include to fix pt_install compilation error. 24-Jul-98 I-01-14+ AKH $$15 Use ProWstringToString instead of pro_wstr_to_str. It fxs C++ compil.err. \*====================================================================*/ /*--------------------------------------------------------------------*\ Pro/TOOKIT includes \*--------------------------------------------------------------------*/ #include "ProToolkit.h" #include "ProUtil.h" /*--------------------------------------------------------------------*\ C System includes \*--------------------------------------------------------------------*/ #include /*--------------------------------------------------------------------*\ Application includes \*--------------------------------------------------------------------*/ #include "UtilString.h" #include "TestError.h" /*--------------------------------------------------------------------*\ Application macros \*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*\ Application data types \*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*\ Application global/external data \*--------------------------------------------------------------------*/ /*====================================================================*\ FUNCTION : ProUtilStrcmp() PURPOSE : Like strcmp but case-independent \*====================================================================*/ int ProUtilStrcmp( char *s, char *t) { int i = 0; while( toupper(s[i]) == toupper(t[i])) { if( s[i++] == '\0' ) return(0); } return(s[i] - t[i]); } /*====================================================================*\ FUNCTION : ProUtilStrncmp() PURPOSE : Like strncmp but case-independent \*====================================================================*/ int ProUtilStrncmp( char *s, char *t, int n) { int i = 0; int counter = 0; while( toupper(s[i]) == toupper(t[i])) { if(( s[i++] == '\0') || (counter++ >= n) ) return(0); } return(s[i] - t[i]); } /*====================================================================*\ FUNCTION : ProUtilStringUpper() PURPOSE : Convert a string to upper case \*====================================================================*/ void ProUtilStringUpper( char *input_string, char *output_string) { int i = 0; while ( input_string[i] != '\0') { output_string[i] = (char)toupper(input_string[i]); i++; } output_string[i] = '\0'; } /*====================================================================*\ FUNCTION : ProUtilStringLower() PURPOSE : Convert a string to lower case \*====================================================================*/ void ProUtilStringLower( char *input_string, char *output_string) { int i = 0; while ( input_string[i] != '\0') { output_string[i] = (char)tolower(input_string[i]); i++; } output_string[i] = '\0'; } /*====================================================================*\ FUNCTION : ProUtilWstrcmp() PURPOSE : strcmp() but for wide strings, and case-sensitive. \*====================================================================*/ int ProUtilWstrcmp( wchar_t *ws1, wchar_t *ws2) { char s1[PRO_PATH_SIZE], s2[PRO_PATH_SIZE]; ProWstringToString(s1, ws1); ProWstringToString(s2, ws2); return (strcmp(s1, s2)); } /*====================================================================*\ FUNCTION : ProUtilWstrCmp() PURPOSE : strcmp() but for wide strings, and case-insensitive. \*====================================================================*/ int ProUtilWstrCmp( wchar_t *ws1, wchar_t *ws2) { char s1[PRO_PATH_SIZE], s2[PRO_PATH_SIZE]; ProWstringToString(s1, ws1); ProWstringToString(s2, ws2); return (ProUtilStrcmp(s1, s2)); } /*====================================================================*\ FUNCTION : ProUtilWstrncmp() PURPOSE : strncmp() but for wide strings, and case-insensitive. \*====================================================================*/ int ProUtilWstrncmp( wchar_t *ws1, wchar_t *ws2, int n) { char s1[PRO_PATH_SIZE], s2[PRO_PATH_SIZE]; ProWstringToString(s1, ws1); ProWstringToString(s2, ws2); return (ProUtilStrncmp(s1, s2, n)); } /*====================================================================*\ FUNCTION : ProUtilWstrcpy() PURPOSE : strcpy() but for wide strings \*====================================================================*/ wchar_t *ProUtilWstrcpy( wchar_t *ws1, wchar_t *ws2) { char s[PRO_PATH_SIZE]; TEST_CALL_REPORT("ProStringToWstring()", "ProUtilWstrcpy()", PRO_TK_NO_ERROR, 0); TEST_CALL_REPORT("ProWstringToString()", "ProUtilWstrcpy()", PRO_TK_NO_ERROR, 0); return(wchar_t *)(ProStringToWstring(ws1, ProWstringToString(s, ws2))); } /*====================================================================*\ FUNCTION : ProUtilStrwscmp() PURPOSE : Compare a string to a wide string \*====================================================================*/ int ProUtilStrwscmp( char *s1, wchar_t *ws2) { char s2[PRO_PATH_SIZE]; TEST_CALL_REPORT("ProWstringToString()", "ProUtilWstrcpy()", PRO_TK_NO_ERROR, 0); ProWstringToString(s2, ws2); return (ProUtilStrcmp(s1, s2)); } /*====================================================================*\ FUNCTION : ProUtilStrnwscmp() PURPOSE : strncmp() for a string and a wide string \*====================================================================*/ int ProUtilStrnwscmp( char *s1, wchar_t *ws2, int n) { char s2[PRO_PATH_SIZE]; TEST_CALL_REPORT("ProWstringToString()", "ProUtilWstrcpy()", PRO_TK_NO_ERROR, 0); ProWstringToString(s2, ws2); return (ProUtilStrncmp(s1, s2, n)); } /*====================================================================*\ FUNCTION : ProUtilWstrPrint() PURPOSE : printf a wide string - for diagnostics \*====================================================================*/ int ProUtilWstrPrint( wchar_t *ws1, int width) { char s1[PRO_PATH_SIZE], format[10]; sprintf(format,"%%-%ds", width); ProWstringToString(s1, ws1); printf(format, s1); return (0); } /*=====================================================================*\ Function: ProUtilWstrLen() Purpose: Get the length of a wide string \*=====================================================================*/ int ProUtilWstrLen( wchar_t *wstr) { int n; for(n=0; *wstr!=(wchar_t)0; wstr++) n++; return(n); } /*=====================================================================*\ Function: ProUtilWstrcat() Purpose: Concatenates two wide strings \*=====================================================================*/ wchar_t *ProUtilWstrcat( wchar_t *ws1, wchar_t *ws2) { char s1[PRO_PATH_SIZE], s2[PRO_PATH_SIZE]; ProWstringToString(s1, ws1); ProWstringToString(s2, ws2); return( ProStringToWstring (ws1, strcat(s1,s2))); } /*=====================================================================*\ Function: ProUtilWstrStrcatToWstr() Purpose: Concatenates a wstring and a string and returns a wstring \*=====================================================================*/ wchar_t *ProUtilWstrStrcatToWstr( wchar_t *ws1, char *s2) { char s1[PRO_PATH_SIZE]; ProWstringToString(s1,ws1); return( ProStringToWstring (ws1, strcat(s1,s2))); } /*=====================================================================*\ Function: ProUtilStrWstrcatToWstr Purpose: Concatenates a string and a wstring and returns a wstring NOTE : the return value doesn't point to s1 \*=====================================================================*/ wchar_t *ProUtilStrWstrcatToWstr( char *s1, wchar_t *ws2) { char s2[PRO_PATH_SIZE]; wchar_t ws_return[PRO_PATH_SIZE]; ProWstringToString(s2,ws2); return( ProStringToWstring (ws_return, strcat(s1,s2))); } /*=====================================================================*\ Function: ProUtilWstrStrcatToStr Purpose: Concatenates a wstring and a string and returns a string NOTE : the return value doesn't point to ws1 \*=====================================================================*/ char *ProUtilWstrStrcatToStr(wchar_t *ws1, char *s2) { char s1[PRO_PATH_SIZE]; ProWstringToString(s1,ws1); return( strcat(s1, s2)); } /*=====================================================================*\ Function: ProUtilStrWstrcatToStr Purpose: Concatenates a string and a wstring and returns a string \*=====================================================================*/ char *ProUtilStrWstrcatToStr(char *s1, wchar_t *ws2) { char s2[PRO_PATH_SIZE]; ProWstringToString(s2,ws2); return( strcat(s1, s2)); } /*=====================================================================*\ Function: ProUtilWstringArrayToFile Purpose: prints an array of Wstrings into a file \*=====================================================================*/ ProError ProUtilWstringArrayToFile (ProWstring* str_xar, ProPath wpath) { ProError status; int str_num, str_count; ProCharLine path; FILE * fp; ProCharLine a_string; ProWstringToString (path, wpath); fp = fopen(path, "w"); status = ProArraySizeGet(str_xar, &str_num); TEST_CALL_REPORT("ProArraySizeGet", "ProUtilWstringArrayToFile", status, (status != PRO_TK_NO_ERROR)); if (status != PRO_TK_NO_ERROR) return (status); for (str_count = 0 ; str_count < str_num; str_count++) { ProWstringToString(a_string, (str_xar)[str_count]); fprintf(fp, "%s\n",a_string); } fclose(fp); return(PRO_TK_NO_ERROR); } /*=====================================================================*\ Function: ProUtilFileToWstringArray Purpose: parses the lines of a file into a wide string array \*=====================================================================*/ ProError ProUtilFileToWstringArray (ProPath wpath, ProWstring** str_xar) { ProError status; FILE * fp; ProCharLine a_line; ProLine a_wline; ProWstring a_wstring; ProCharLine path; /*------------------------------------------------------------------*\ Get the file pointer \*------------------------------------------------------------------*/ ProWstringToString (path, wpath); fp = fopen(path, "r"); /*------------------------------------------------------------------*\ Add each line of the file into a location of the array \*------------------------------------------------------------------*/ while(!feof(fp)) { if (fgets (a_line, PRO_LINE_SIZE, fp) == NULL) return (PRO_TK_NO_ERROR); ProStringToWstring(a_wline, a_line); a_wstring = a_wline; status = ProWstringArrayObjectAdd(str_xar, PRO_VALUE_UNUSED, 1, &a_wstring); TEST_CALL_REPORT("ProWstringArrayObjectAdd", "ProUtilFileToWstringArray", status, (status != PRO_TK_NO_ERROR)); if (status != PRO_TK_NO_ERROR) return (status); } fclose(fp); return (PRO_TK_NO_ERROR); }