cgma
|
#include <CubitUtil.hpp>
Static Public Member Functions | |
static void | set_digits (int new_digits) |
static int | get_digits () |
static void | convert_string_to_lowercase (char *string) |
static int | strcmp_case_insensitive (const char *s1, const char *s2) |
static int | strncmp_case_insensitive (const char *s1, const char *s2, int n) |
static void | list_ids (const char *const heading, const DLIList< CubitEntity * > &entity_list, int should_sort=CUBIT_FALSE, int report_once=CUBIT_FALSE, int wrap=80) |
static void | sort_and_print_ids (const char *const heading, DLIList< int > &id_list, int should_sort=CUBIT_FALSE, int report_once=CUBIT_FALSE, int wrap=80) |
static void | list_entity_ids (const char *pre_string, const DLIList< CubitEntity * > &entity_list, int width=80, const char *post_string="\n", int sort=CUBIT_TRUE, int unique=CUBIT_TRUE, int tab=3, const char *sep_string=",", const char *post_string_none="none\n") |
static void | list_entity_ids (const char *pre_string, DLIList< int > &id_list, int width=80, const char *post_string="\n", int sort=CUBIT_TRUE, int unique=CUBIT_TRUE, int tab_len=3, const char *sep_string=",", const char *post_string_none="none\n") |
static CubitString | get_entity_ids_str (const char *pre_string, DLIList< int > &int_list, int width=80, const char *post_string="\n", int sort=CUBIT_TRUE, int unique=CUBIT_TRUE, int left_tab=3, const char *sep_string=",", const char *post_string_none="none\n") |
static void | process_entity_ids (int method, CubitString &ret_str, const char *pre_string, DLIList< int > &id_list, int max_len, const char *post_string, int sort, int unique, int tab_len, const char *sep_string, const char *post_string_none="none\n") |
static int | int_len (int num) |
static void | set_file_ptr (FILE *file_ptr) |
static void | reset_file_ptr () |
static FILE * | get_file_ptr () |
static CubitBoolean | compare (const char *a, const char *b) |
static CubitSense | opposite_sense (CubitSense sense) |
static CubitString | get_temporary_filename () |
static int | string_length (const char *string, int tabsize=8) |
static void | print_columns (const std::vector< CubitString > &array, const CubitString &indent=CubitString()) |
static char * | util_strdup (const char *s1) |
static void | util_strdup_free (char *s1) |
static void | cubit_sleep (int duration_in_seconds) |
static CubitBoolean | file_exist (const char *buffer) |
static CubitBoolean | file_exist (const CubitString &buffer) |
static CubitString | getenv (const CubitString &str) |
static void | setenv (const CubitString &var, const CubitString &value) |
static CubitString | get_computer_name () |
static CubitString | get_os () |
static int | num_cpu () |
Private Member Functions | |
CubitUtil () | |
Static Private Attributes | |
static FILE * | fp = NULL |
Definition at line 30 of file CubitUtil.hpp.
CubitUtil::CubitUtil | ( | ) | [inline, private] |
Definition at line 130 of file CubitUtil.hpp.
{}
static CubitBoolean CubitUtil::compare | ( | const char * | a, |
const char * | b | ||
) | [inline, static] |
Definition at line 96 of file CubitUtil.hpp.
{ return ( a != NULL && b != NULL && 0 == strcmp(a, b) ); }
void CubitUtil::convert_string_to_lowercase | ( | char * | string | ) | [static] |
Definition at line 51 of file CubitUtil.cpp.
{ register char *p = string; while (*p) { if (isascii(*p) && isupper(*p)) *p = tolower(*p); p++; } }
void CubitUtil::cubit_sleep | ( | int | duration_in_seconds | ) | [static] |
Definition at line 781 of file CubitUtil.cpp.
{ #ifdef _WIN32 ::Sleep(duration_in_seconds*1000); #else sleep(duration_in_seconds); #endif }
static CubitBoolean CubitUtil::file_exist | ( | const char * | buffer | ) | [static] |
static CubitBoolean CubitUtil::file_exist | ( | const CubitString & | buffer | ) | [static] |
CubitString CubitUtil::get_computer_name | ( | ) | [static] |
Definition at line 810 of file CubitUtil.cpp.
{ CubitString name = "unknown"; #ifdef _WIN32 wchar_t machine_buf[MAX_COMPUTERNAME_LENGTH + 1]; DWORD machine_buf_length = MAX_COMPUTERNAME_LENGTH + 1; if (::GetComputerNameW(machine_buf, &machine_buf_length)) name = CubitString::toUtf8(machine_buf); #else struct utsname uname_data; if( uname( &uname_data ) >= 0 ) { name = uname_data.nodename; } #endif return name; }
int CubitUtil::get_digits | ( | ) | [static] |
Definition at line 40 of file CubitUtil.cpp.
{ return displayDigits; }
CubitString CubitUtil::get_entity_ids_str | ( | const char * | pre_string, |
DLIList< int > & | int_list, | ||
int | width = 80 , |
||
const char * | post_string = "\n" , |
||
int | sort = CUBIT_TRUE , |
||
int | unique = CUBIT_TRUE , |
||
int | left_tab = 3 , |
||
const char * | sep_string = "," , |
||
const char * | post_string_none = "none\n" |
||
) | [static] |
Definition at line 226 of file CubitUtil.cpp.
{ CubitString ret_str; process_entity_ids( 0, ret_str, pre_string, id_list, width, post_string, sort, unique, tab_len, sep_string, post_string_none ); return ret_str; }
FILE * CubitUtil::get_file_ptr | ( | ) | [inline, static] |
Definition at line 157 of file CubitUtil.hpp.
{return fp;}
CubitString CubitUtil::get_os | ( | ) | [static] |
Definition at line 828 of file CubitUtil.cpp.
{ #ifdef _WIN32 CubitString os = "Microsoft Windows"; OSVERSIONINFO osvi; ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if(GetVersionEx(&osvi)) { std::stringstream str; str << " "; str << osvi.dwMajorVersion << "." << osvi.dwMinorVersion; str << " "; str << osvi.szCSDVersion; DWORD build = osvi.dwBuildNumber & 0xFFFF; str << " (Build " << build << ")"; os += str.str().c_str(); } #else CubitString os = "unknown"; struct utsname uname_data; if( uname( &uname_data ) >= 0 ) { os = uname_data.sysname; os += " "; os += uname_data.release; } #endif return os; }
CubitString CubitUtil::get_temporary_filename | ( | ) | [static] |
Definition at line 641 of file CubitUtil.cpp.
{ CubitString ret_str; #ifdef _WIN32 //get a place to put the temporary file CubitString temp_path = get_temp_directory(); // make an empty temporary and return the name for it wchar_t temp_file_name[MAX_PATH]; if( GetTempFileNameW(CubitString::toUtf16(temp_path.c_str()).c_str(), L"CBT", 0, temp_file_name) != 0 ) ret_str = CubitString::toUtf8(temp_file_name); #else CubitString tmpdir = get_temp_directory(); const char* filepattern = "CBT.XXXXXX"; //needs to be two longer because of the "/"? char *temp_file_name = new char[tmpdir.length() + strlen(filepattern) + 2]; sprintf(temp_file_name, "%s/%s", tmpdir.c_str(), filepattern); // make an empty file and return the name for it int fd = mkstemp(temp_file_name); if( fd != -1 ) { ret_str = temp_file_name; // release the open done by mkstemp, // temporary file still exists close(fd); } delete [] temp_file_name; #endif return ret_str; }
CubitString CubitUtil::getenv | ( | const CubitString & | str | ) | [static] |
Definition at line 790 of file CubitUtil.cpp.
{ #ifdef _WIN32 return CubitString::toUtf8(_wgetenv(CubitString::toUtf16(var).c_str())); #else return CubitString(::getenv(var.c_str())); #endif }
int CubitUtil::int_len | ( | int | num | ) | [static] |
Definition at line 543 of file CubitUtil.cpp.
{ int len = 0; // length of the string to hold the integer number unsigned long n; // absolute value of the integer value // If the number is negative, add 1 for the negative sign if (num < 0) len++; // Loop until the absolute value of the number reaches 0 n = INTABS(num); do { // Increment the length and divide the number by 10 len++; n /= 10; } while (n); return len; }
void CubitUtil::list_entity_ids | ( | const char * | pre_string, |
const DLIList< CubitEntity * > & | entity_list, | ||
int | width = 80 , |
||
const char * | post_string = "\n" , |
||
int | sort = CUBIT_TRUE , |
||
int | unique = CUBIT_TRUE , |
||
int | tab = 3 , |
||
const char * | sep_string = "," , |
||
const char * | post_string_none = "none\n" |
||
) | [static] |
Definition at line 198 of file CubitUtil.cpp.
{ DLIList <int> id_list( entity_list.size() ); for ( int i=0; i<entity_list.size(); i++ ) id_list.append( entity_list.next(i)->id() ); list_entity_ids( pre_string, id_list, width, post_string, sort, unique, tab, sep_string, post_string_none ); }
void CubitUtil::list_entity_ids | ( | const char * | pre_string, |
DLIList< int > & | id_list, | ||
int | width = 80 , |
||
const char * | post_string = "\n" , |
||
int | sort = CUBIT_TRUE , |
||
int | unique = CUBIT_TRUE , |
||
int | tab_len = 3 , |
||
const char * | sep_string = "," , |
||
const char * | post_string_none = "none\n" |
||
) | [static] |
Definition at line 213 of file CubitUtil.cpp.
{ CubitString ret_str; process_entity_ids( 1, ret_str, pre_string, id_list, width, post_string, sort, unique, tab_len, sep_string, post_string_none ); }
void CubitUtil::list_ids | ( | const char *const | heading, |
const DLIList< CubitEntity * > & | entity_list, | ||
int | should_sort = CUBIT_FALSE , |
||
int | report_once = CUBIT_FALSE , |
||
int | wrap = 80 |
||
) | [static] |
Definition at line 108 of file CubitUtil.cpp.
{ if ( entity_list.size() == 0 ) { PRINT_INFO(" No %s.\n", heading ); return; } DLIList <int> id_list( entity_list.size() ); for ( int j = 0; j < entity_list.size(); j++ ) { id_list.append( entity_list[j]->id() ); } if ( id_list.size() == 1 ) { PRINT_INFO(" The 1 %s id is %d.\n", heading, id_list[0]); return; } sort_and_print_ids( heading, id_list, should_sort, report_once, wrap ); }
int CubitUtil::num_cpu | ( | ) | [static] |
Definition at line 861 of file CubitUtil.cpp.
CubitSense CubitUtil::opposite_sense | ( | CubitSense | sense | ) | [inline, static] |
Definition at line 144 of file CubitUtil.hpp.
{ assert( sense == CUBIT_UNKNOWN || sense == CUBIT_FORWARD || sense == CUBIT_REVERSED ); if ( sense == CUBIT_UNKNOWN ) return CUBIT_UNKNOWN; else { CubitSense opp_sense = (CubitSense) (1 - sense); return opp_sense; } }
void CubitUtil::print_columns | ( | const std::vector< CubitString > & | array, |
const CubitString & | indent = CubitString() |
||
) | [static] |
Definition at line 680 of file CubitUtil.cpp.
{ int term_height, term_width; if( ! AppUtil::instance()->get_terminal_size( term_height, term_width ) ) { for( size_t i = 0; i < array.size(); i++ ) { PRINT_INFO("%s%s\n", indent.c_str(), array[i].c_str() ); } return; } // find lenth of longest string int maxlen = 0; for( size_t i = 0; i < array.size(); i++ ) { int len = CubitUtil::string_length(array[i].c_str()); if( len > maxlen ) maxlen = len; } char* const line = new char[CUBIT_MAX(maxlen,term_width)+2]; // calculate number of columns of output term_width -= string_length(indent.c_str()); int width = maxlen + 1; int columns = term_width > width ? term_width / width : 1; // calculate number of rows of output int rows = array.size() / columns; if( array.size() % columns ) rows++; // calculate the width of one column if (columns > 1) width = maxlen + (term_width - columns * maxlen) / (columns - 1); else width = term_width; // now write output for( int i = 0; i < rows; i++ ) { size_t idx; const char* str; char* ptr = line + sprintf( line, "%s", indent.c_str() ); for(int j = 0; j < columns - 1; j++ ) { idx = j * rows + i; if (idx < array.size() ) str = array[idx].c_str(); else str = ""; ptr += sprintf( ptr, "%-*s", width, str); } idx = (columns - 1) * rows + i; if (idx < array.size() ) sprintf(ptr, "%s\n", array[idx].c_str()); else sprintf(ptr, "\n"); PRINT_INFO( "%s", line ); } delete [] line; }
void CubitUtil::process_entity_ids | ( | int | method, |
CubitString & | ret_str, | ||
const char * | pre_string, | ||
DLIList< int > & | id_list, | ||
int | max_len, | ||
const char * | post_string, | ||
int | sort, | ||
int | unique, | ||
int | tab_len, | ||
const char * | sep_string, | ||
const char * | post_string_none = "none\n" |
||
) | [static] |
Definition at line 240 of file CubitUtil.cpp.
{ // Method: 0 - to a string // 1 - to PRINT_INFO char temp[200]; if ( id_list.size() == 0 ) { if( method ) PRINT_INFO("%s%s", pre_string, post_string_none ); else { sprintf( temp, "%s%s", pre_string, post_string_none ); ret_str = temp; } if( fp ) fprintf( fp, "%s%s", pre_string, post_string_none ); return; } // sort if( sort ) { id_list.sort(); // make unique if( unique ) { int i; DLIList <int> id_list_2( id_list ); id_list_2.reset(); id_list.clean_out(); id_list.append( id_list_2.get_and_step() ); for ( i=id_list_2.size()-1; i--; ) { if ( id_list_2.get() != id_list_2.prev() ) id_list.append( id_list_2.get() ); id_list_2.step(); } } } if( max_len < 0 ) max_len = CUBIT_INT_MAX/2; // TODO: wrap prestring, if necessary if( method ) PRINT_INFO( "%s", pre_string ); else ret_str = pre_string; if( fp ) fprintf( fp, "%s", pre_string ); // Keep track of length printed int curr_len = strlen(pre_string); int num = 0; int begin = id_list.get(); int previous = begin; int current; int comma = 0; // Is comma needed int beg_len, prev_len; int sep_len = strlen( sep_string ); // Setup the tab char* tab = new char[tab_len+1]; for( int i=0; i<tab_len; i++ ) tab[i] = ' '; tab[tab_len] = '\0'; // Loop until all the ids are printed. Use ranges if possible. while( num < id_list.size()+1 ) { current = id_list.get_and_step(); num++; // Handle last entity if( num <= id_list.size() ) { if( num==1 ) // Handle 1st time in loop continue; if( current==previous+1 ) { previous = current; continue; } } // If we are here, we are no longer tracking a range and // need to print the range or a number. if( comma ) { if( method ) PRINT_INFO("%s", sep_string ); else ret_str += sep_string; if( fp ) fprintf( fp, "%s", sep_string ); curr_len += sep_len; } if( begin==previous ) { // a single number prev_len = int_len(previous); if( curr_len+1+prev_len+sep_len > max_len ) { if( method ) { PRINT_INFO( "\n" ); PRINT_INFO( "%s%d", tab, previous ); } else { sprintf( temp, "\n%s%d", tab, previous ); ret_str += temp; } if( fp ) fprintf( fp, "\n%s%d", tab, previous ); curr_len = tab_len + prev_len; } else { if( comma ) // Don't print space before first item { if( method ) PRINT_INFO( " " ); else ret_str += " "; if( fp ) fprintf( fp, " " ); curr_len++; } if( method ) PRINT_INFO( "%d", previous ); else { sprintf( temp, "%d", previous ); ret_str += temp; } if( fp ) fprintf( fp, "%d", previous ); curr_len = curr_len + prev_len; } } else if( previous==begin+1 ) { // a range, but only 2 consecutive numbers prev_len = int_len(previous); beg_len = int_len(begin); // Print 1st if( curr_len+1+beg_len+sep_len > max_len ) { if( method ) { PRINT_INFO( "\n" ); PRINT_INFO( "%s%d%s", tab, begin, sep_string ); } else { sprintf( temp, "\n%s%d%s", tab, begin, sep_string ); ret_str += temp; } if( fp ) fprintf( fp, "\n%s%d%s", tab, begin, sep_string ); curr_len = tab_len + beg_len + sep_len; } else { if( comma ) // Don't print space before first item { if( method ) PRINT_INFO( " " ); else ret_str += " "; if( fp ) fprintf( fp, " " ); curr_len++; } if( method ) PRINT_INFO( "%d%s", begin, sep_string ); else { sprintf( temp, "%d%s", begin, sep_string ); ret_str += temp; } if( fp ) fprintf( fp, "%d%s", begin, sep_string ); curr_len = curr_len + beg_len + sep_len; } // Print 2nd if( curr_len+1+prev_len+sep_len > max_len ) { if( method ) { PRINT_INFO( "\n" ); PRINT_INFO( "%s%d", tab, previous ); } else { sprintf( temp, "\n%s%d", tab, previous ); ret_str += temp; } if( fp ) fprintf( fp, "\n%s%d", tab, previous ); curr_len = tab_len + prev_len; } else { if( method ) PRINT_INFO( " %d", previous ); else { sprintf( temp, " %d", previous ); ret_str += temp; } if( fp ) fprintf( fp, " %d", previous ); curr_len = curr_len + 1+prev_len; } } else { // a range of 3 or more consecutive numbers prev_len = int_len(previous); beg_len = int_len(begin); if( curr_len+beg_len+prev_len+5+sep_len > max_len ) { if( method ) { PRINT_INFO( "\n" ); PRINT_INFO( "%s%d to %d", tab, begin, previous ); } else { sprintf( temp, "\n%s%d to %d", tab, begin, previous ); ret_str += temp; } if( fp ) fprintf( fp, "\n%s%d to %d", tab, begin, previous ); curr_len = tab_len + beg_len+prev_len+4; } else { if( comma ) // Don't print space before first item { if( method ) PRINT_INFO( " " ); else ret_str += " "; if( fp ) fprintf( fp, " " ); curr_len++; } if( method ) PRINT_INFO( "%d to %d", begin, previous ); else { sprintf( temp, "%d to %d", begin, previous ); ret_str += temp; } if( fp ) fprintf( fp, "%d to %d", begin, previous ); curr_len = curr_len + beg_len+4+prev_len; } } begin = current; previous = current; comma = 1; } //TODO: wrap poststring, if required if (post_string) { if( method ) PRINT_INFO( "%s", post_string ); else ret_str += post_string; if( fp ) fprintf( fp, "%s", post_string ); } delete [] tab; }
void CubitUtil::reset_file_ptr | ( | ) | [inline, static] |
Definition at line 141 of file CubitUtil.hpp.
{fp=NULL;}
void CubitUtil::set_digits | ( | int | new_digits | ) | [static] |
Definition at line 45 of file CubitUtil.cpp.
{ displayDigits = new_digits; }
void CubitUtil::set_file_ptr | ( | FILE * | file_ptr | ) | [inline, static] |
Definition at line 137 of file CubitUtil.hpp.
{fp=file_ptr;}
void CubitUtil::setenv | ( | const CubitString & | var, |
const CubitString & | value | ||
) | [static] |
Definition at line 799 of file CubitUtil.cpp.
{ #ifdef _WIN32 CubitString tmp = var + "=" + value; _wputenv(CubitString::toUtf16(tmp).c_str()); #else ::setenv(var.c_str(), value.c_str(), 1); #endif }
void CubitUtil::sort_and_print_ids | ( | const char *const | heading, |
DLIList< int > & | id_list, | ||
int | should_sort = CUBIT_FALSE , |
||
int | report_once = CUBIT_FALSE , |
||
int | wrap = 80 |
||
) | [static] |
Definition at line 132 of file CubitUtil.cpp.
{ // sort, if desired if ( should_sort ) { id_list.sort(); } if ( report_once ) { DLIList <int> id_list_2( id_list ); id_list_2.reset(); id_list.clean_out(); id_list.append( id_list_2.get_and_step() ); for ( int j = id_list_2.size()-1; j--; ) { if ( id_list_2.get() != id_list_2.prev() ) id_list.append( id_list_2.get() ); id_list_2.step(); } } if( wrap == -1 ) { // print out ranges int begin = id_list.get_and_step(); int end = begin; int current = -1; PRINT_INFO(" The %d %s ids are %d", id_list.size(), heading, begin); for (int i=id_list.size()-1; i > 0; i--) { current = id_list.get_and_step(); if (current == end+1) { end++; } else { if (end == begin) { PRINT_INFO(", %d", current); } else if (end == begin+1) { PRINT_INFO(", %d, %d", end, current); } else { PRINT_INFO(" to %d, %d", end, current); } begin = current; end = begin; } } if (current == begin + 1) { PRINT_INFO(", %d", current); } else if (current != begin) { PRINT_INFO(" to %d", current); } PRINT_INFO(".\n"); } else { char pre_string[67]; sprintf( pre_string, " The %d %s ids are: ", id_list.size(),heading ); CubitUtil::list_entity_ids( pre_string, id_list, wrap, ".\n", CUBIT_FALSE, CUBIT_FALSE ); } }
int CubitUtil::strcmp_case_insensitive | ( | const char * | s1, |
const char * | s2 | ||
) | [static] |
Definition at line 62 of file CubitUtil.cpp.
{ char c1, c2; do { c1 = *s1++; if(isupper(c1)) c1 = tolower(c1); c2 = *s2++; if(isupper(c2)) c2 = tolower(c2); if(c1 != c2) return c1 - c2; } while(c1 != '\0'); return 0; }
int CubitUtil::string_length | ( | const char * | string, |
int | tabsize = 8 |
||
) | [static] |
Definition at line 750 of file CubitUtil.cpp.
{ int result = 0; for( ; *string ; string++ ) { if( *string == '\t' ) result += tabsize; else if( *string >= ' ' ) result ++; } return result; }
int CubitUtil::strncmp_case_insensitive | ( | const char * | s1, |
const char * | s2, | ||
int | n | ||
) | [static] |
Definition at line 84 of file CubitUtil.cpp.
{ char c1, c2; do { c1 = *s1++; if(isupper(c1)) c1 = tolower(c1); c2 = *s2++; if(isupper(c2)) c2 = tolower(c2); if(c1 != c2) return c1 - c2; n--; } while(c1 && n > 0); return 0; }
char * CubitUtil::util_strdup | ( | const char * | s1 | ) | [static] |
Definition at line 765 of file CubitUtil.cpp.
{ #ifdef CUBIT_NO_STRDUP int len = strlen(s1)+1; char* ret_char = (char*) malloc ( (unsigned long) len * sizeof(char)); strcpy(ret_char, s1); return ret_char; #else #ifdef _WIN32 return _strdup(s1); #else return strdup(s1); #endif #endif }
static void CubitUtil::util_strdup_free | ( | char * | s1 | ) | [inline, static] |
Definition at line 113 of file CubitUtil.hpp.
{free(s1);}
FILE * CubitUtil::fp = NULL [static, private] |
Definition at line 132 of file CubitUtil.hpp.