📄 toolbox.c
字号:
mid = (low + high) >> 1; arrayItemPtr = (void *) (((char *)array) + (mid*elementSize)); cmp = compareFunction ? compareFunction(itemPtr, arrayItemPtr) : ListMemBlockCmp(itemPtr, arrayItemPtr, elementSize); if (cmp == 0) return mid; else if (cmp < 0) high= mid - 1; else low= mid + 1; } if (cmp > 0) mid++; return -mid-1;}/********************************************************************/ /* Swap numBytes between block1 and block2. */void CVIFUNC SwapBlock(void *block1, void *block2, int numBytes){ if(((int)block1 | (int)block2 | numBytes) & 3) { char *p, *q; char x; for(p= (char *)block1, q= (char *)block2; --numBytes >= 0; p++, q++){ x= *p; *p= *q; *q= x; } } else { long int *p, *q, x; for(p= (long int*)block1, q= (long int*)block2, numBytes >>= 2; --numBytes >= 0; p++, q++){ x= *p; *p= *q; *q= x; } }}/********************************************************************/ /* returns negative UIL error, 0 if time conversion/formatting problem, or 1 if successful */int CVIFUNC GetFileCLibTime(char filePath[], time_t *time){ int error = 0; int month; int day; int year; int hours; int minutes; int seconds; struct tm fileTM; int savedBole = SetBOLE(FALSE); /* don't break on GetFileDate() errors */ switch (GetFileDate (filePath, &month, &day, &year)) { case 0: break; case -1: errChk( UIEFileWasNotFound); break; case -4: errChk( UIEOutOfMemory); break; case -3: default: errChk( UIEIOError); break; } switch (GetFileTime (filePath, &hours, &minutes, &seconds)) { case 0: break; case -1: errChk( UIEFileWasNotFound); break; case -4: errChk( UIEOutOfMemory); break; case -3: default: errChk( UIEIOError); break; } fileTM.tm_sec = seconds; fileTM.tm_min = minutes; fileTM.tm_hour = hours; fileTM.tm_mday = day; fileTM.tm_mon = month - 1; fileTM.tm_year = year - 1900; fileTM.tm_isdst = -1; /* -1 means "I don't know, you tell me..." */ *time = mktime(&fileTM); return *time != (time_t)-1;Error: SetBOLE(savedBole); return error;}/********************************************************************/ /* returns negative UIL error, # of characters formatted if success, or 0 if time conversion/formatting problem */int CVIFUNC InternationalFileTime(char filePath[], char timeString[], int bufferSize){ time_t time; int error = 0; int success; errChk( success = GetFileCLibTime(filePath, &time)); if (success) return InternationalTimeString(time, timeString, bufferSize); else return 0;Error: return error;}/********************************************************************/ /* returns negative UIL error, # of characters formatted if success, or 0 if time conversion/formatting problem */int CVIFUNC InternationalFileDate(char filePath[], char dateString[], int bufferSize){ time_t time; int error = 0; int success; errChk( success = GetFileCLibTime(filePath, &time)); if (success) return InternationalDateString(time, dateString, bufferSize); else return 0;Error: return error;}/********************************************************************/ /* returns # of characters formatted if success, or 0 if time conversion/formatting problem */int CVIFUNC InternationalTime(char timeString[], int bufferSize){ time_t rawTime; time (&rawTime); return InternationalTimeString(rawTime, timeString, bufferSize);}/********************************************************************/ /* returns # of characters formatted if success, or 0 if time conversion/formatting problem */int CVIFUNC InternationalDate(char dateString[], int bufferSize){ time_t rawTime; time (&rawTime); return InternationalDateString(rawTime, dateString, bufferSize);}/********************************************************************/static int CVIFUNC InternationalTimeString(time_t rawTime, char timeString[], int bufferSize){ char *oldLocale; struct tm *localTime; int result; oldLocale = setlocale (LC_TIME, NULL); setlocale (LC_TIME, ""); localTime = localtime (&rawTime); result = strftime (timeString, bufferSize, "%X", localTime); setlocale (LC_TIME, oldLocale); return result;}/********************************************************************/static int CVIFUNC InternationalDateString(time_t rawTime, char dateString[], int bufferSize){ char *oldLocale; struct tm *localTime; int result; oldLocale = setlocale (LC_TIME, NULL); setlocale (LC_TIME, ""); localTime = localtime (&rawTime); result = strftime (dateString, bufferSize, "%x", localTime); setlocale (LC_TIME, oldLocale); return result;}/********************************************************************/char * CVIFUNC StrDup(char *stringToDuplicate){ char *duplicatedString = 0; int size; if (stringToDuplicate) { size = strlen(stringToDuplicate) + 1; duplicatedString = (char *)malloc(size); if (duplicatedString) memcpy(duplicatedString, stringToDuplicate, size); } return duplicatedString;}/**************************************************************************/ /* This function adds stringToAdd to the end of *string. */int CVIFUNC AppendString(char **string, char *stringToAdd, int lengthToAdd){ int currLen; char *newString; AssertMsg(string, "NULL passed to first parameter of AppendString()"); if (!stringToAdd) return TRUE; if (lengthToAdd < 0) lengthToAdd = strlen(stringToAdd); else { int index; /* If the stringToAdd contains a terminator, we have to stop there */ for (index = 0; index < lengthToAdd && stringToAdd[index] != 0; index++); lengthToAdd = index; } if (lengthToAdd == 0) return TRUE; if (!*string) { newString = (char *)malloc(lengthToAdd + 1); currLen = 0; } else { currLen = strlen(*string); newString = realloc(*string, currLen + lengthToAdd + 1); } if (newString) { memmove(newString + currLen, stringToAdd, lengthToAdd); newString[currLen + lengthToAdd] = 0; *string = newString; return TRUE; } else return FALSE;}/*****************************************************************************************//* Creates a dynamically allocated copy of a string without any leading or trailing white space.*/char * CVIFUNC StrDupWithoutSurrWhiteSpace(char *string){ char *start, *copy; int len; start = SkipWhiteSpace(string); len = strlen(start); while ((len > 0) && isspace((unsigned char)start[len-1])) /* look backwards for last non white space */ len--; copy = malloc (len+1); if (copy) { memcpy (copy, start, len); copy[len] = '\0'; } return copy;}/**************************************************************************/char * CVIFUNC SkipWhiteSpace(char *string){ char *p=string; if (p) while (*p && isspace((unsigned char)*p)) p++; return p;}/**************************************************************************/char * CVIFUNC SkipNonWhiteSpace(char *string){ char *p=string; if (p) while (*p && !isspace((unsigned char)*p)) p++; return p;}/**************************************************************************/int CVIFUNC HasNonWhiteSpace (char *s){ if (s == NULL) return FALSE; else return *SkipWhiteSpace(s) != '\0';}/**************************************************************************/void CVIFUNC StringCopyMax(char *dest, char *source, int destBufferSize){ int destSize, sourceSize;#ifndef TB_NO_SAFETY_CHECKS AssertMsg(dest, "Null string passed to first parameter of StringCopyMax");#endif if (source && destBufferSize > 0) { sourceSize = strlen(source) + 1; destSize = Min(sourceSize, destBufferSize); memmove(dest, source, Min(destBufferSize, destSize)); if (destSize < sourceSize) dest[destSize - 1] = 0; /* add null terminator since we didn't copy it */ }}/***********************************************************************************/char * CVIFUNC StringInsert(char *destinationString, char *stringToInsert, int destinationBufferSize){ int destinationLength, lengthOfStringToInsert; /* insert stringToInsert before destinationString in destinationString but don't exceed destinationBufferSize in destinationString if destinationString is a pointer in a larger string then destinationBufferSize applies only to the section pointed to by destinationString */#ifndef TB_NO_SAFETY_CHECKS AssertMsg(stringToInsert, "NULL string passed to first parameter of StringInsert"); AssertMsg(destinationString, "NULL string passed to second parameter of StringInsert");#endif destinationLength = strlen(destinationString); lengthOfStringToInsert = strlen(stringToInsert); if ((destinationLength + lengthOfStringToInsert) > destinationBufferSize) lengthOfStringToInsert = destinationBufferSize - destinationLength; if (lengthOfStringToInsert > 0) { memmove(destinationString + lengthOfStringToInsert, destinationString, destinationLength+1); /* slide the original string forward, +1 to keep NULL byte */ memmove(destinationString, stringToInsert, lengthOfStringToInsert); /* copy new string into the new space in front */ } return destinationString+lengthOfStringToInsert;}/**************************************************************************/int CVIFUNC StrICmp (char *s1, char *s2){ int ch1, ch2; if (s1 == NULL) if (s2 == NULL) return 0; else return -1; else if (s2 == NULL) return 1; else for (;;) { ch1 = toupper((unsigned char)*s1++); ch2 = toupper((unsigned char)*s2++); if (ch1 < ch2) return -1; else if (ch1 > ch2) return 1; else if (ch1 == 0 && ch2 == 0) return 0; }}/**************************************************************************/void CVIFUNC RemoveSurroundingWhiteSpace(char string[]){ char *start, *end; int lengthWithoutLeadingWS; if (string) { start = SkipWhiteSpace(string); lengthWithoutLeadingWS = strlen(start); memmove (string, start, lengthWithoutLeadingWS); end = string + lengthWithoutLeadingWS; while (end > string && isspace((unsigned char)*(end - 1))) end--; *end = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -