⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 toolbox.c

📁 CVI教程,用于信号采集系统的多任务开发软件.学习简单,功能实用.
💻 C
📖 第 1 页 / 共 5 页
字号:
/**************************************************************************/int CVIFUNC StrICmpWithoutSurrWhiteSpace (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            {            while (isspace((unsigned char)*s1))                s1++;            while (isspace((unsigned char)*s2))                s2++;                            for (;; s1++,s2++)                {                ch1 = toupper((unsigned char)*s1);                ch2 = toupper((unsigned char)*s2);                if (ch1 == 0)                    {                    while (isspace((unsigned char)*s2))                        s2++;                    if (*s2 == 0)   /* check for trailing whitespace */                        return 0;                    else                        return -1;                    }                else                    {                    if (ch2 == 0)                        {                        while (isspace((unsigned char)*s1))                            s1++;                        if (*s1 == 0)  /* check for trailing whitespace */                            return 0;                        else                            return 1;                        }                    else                    if (ch1 != ch2)                        {                        if (ch1 < ch2)                            return -1;                        else if (ch1 > ch2)                            return 1;                        }                    }                }            }}/****************************************************************************/    /*  This function splices list2 into list1 and stores the result in the newly        allocated string splicedList.  All lists are strings of characters with commas separating        the items.  The resulting splicedList will have as many items as the longest list.  Each item        in the result splicedList will be the same as the corresponding item in list1 unless list1        does not have a corresponding item.  In that case, list2's corresponding item is used.        Example:        char    *list1 = "Time,,Pressure, Temperature";        char    *list2 = "Time, Column 1, Column 2, Column 3, Column 4, Column 5";        char    *splicedList = 0;        SpliceCommaSeparatedList(list1, list2, &splicedList);        printf("Spliced list = %s\n", splicedList);        This prints: Spliced list = Time, Column 1,Pressure, Temperature, Column 4, Column 5        This function is used to fill in any missing items in a partially specified list with        default items.        The function returns 0 if successful, or -12 (UIEOutOfMemory) if there is not        enough memory to create the splicedList.    */int CVIFUNC SpliceCommaSeparatedList(char *list1, char *list2, char **splicedList){    int     error = 0;    char    *newList = 0;    char    *currentList1;    char    *currentList2;    char    *list1Comma;    char    *list2Comma;    int     item1Length;    int     item2Length;    currentList1 = list1;    currentList2 = list2;    *splicedList = 0;    while (currentList1 || currentList2)        {            /*  Find next header item */        if (currentList1)            {            list1Comma = strchr(currentList1, ',');             if (list1Comma)                item1Length = list1Comma - currentList1;            else                item1Length = strlen(currentList1);            }        else            item1Length = 0;            /*  Find next default header item */        if (currentList2)            {            list2Comma = strchr(currentList2, ',');             if (list2Comma)                item2Length = list2Comma - currentList2;            else                item2Length = strlen(currentList2);            }        else            item2Length = 0;        if (item1Length > 0)    /* if there is a header item, use it */            nullChk( AppendString(&newList, currentList1, item1Length));        else        if (item2Length > 0)   /* else, use the default header item, if any */            nullChk( AppendString(&newList, currentList2, item2Length));        if (list1Comma || list2Comma)               /* add trailing comma if we aren't on the last item */               nullChk( AppendString(&newList, ",", -1));        if (list1Comma) /* skip to next header */            currentList1 = list1Comma + 1;        else            currentList1 = 0;        if (list2Comma) /* skip to next header */            currentList2 = list2Comma + 1;        else            currentList2 = 0;        }    if (!newList)       /* if list1 and list2 had no items to contribute to list3, then make it an empty list */        nullChk( AppendString(&newList, "", -1));Error:    if (error < 0)        {        free(newList);        *splicedList = 0;        return error;        }    else        {        *splicedList = newList;        return 0;        }}/***********************************************************************/int CVIFUNC StrToInt (char *str, int *n){    unsigned long   ulval;    int             negative;    if (!ConvertNumStrToUint (str, &ulval, &negative))        return FALSE;    if (negative)        {        if (ulval > 0x80000000L)     /* absolute value is too large */            return FALSE;        *n = -(long)ulval;        }    else        {        if (ulval > 0x7fffffffL)     /* absolute value is too large */            return FALSE;        *n = ulval;        }    return TRUE;}/**************************************************************************/int CVIFUNC StrToUInt (char *str, unsigned int *n){    unsigned long   ulval;    int             negative;    if (!ConvertNumStrToUint (str, &ulval, &negative))        return FALSE;    if (negative)        return FALSE;    *n = ulval;    return TRUE;}/***********************************************************************/static int CVIFUNC ConvertNumStrToUint (char *str, unsigned long *n, int *negative){    int             ch;    int             numDigits = 0;    int             signSeen = FALSE;    int             endOfNonWhiteSpace = FALSE;    unsigned long   ulval;    char            *s;    *negative = FALSE;    ulval = 0;    for (s=str; ch=*s; s++)        {        if (endOfNonWhiteSpace)     /* should be all blanks now */            if (isspace ((unsigned char)ch))                continue;            else            /* there was an embedded blank */                return FALSE;        if (isspace((unsigned char)ch))            {            if (signSeen || numDigits > 0)                endOfNonWhiteSpace = TRUE;            continue;            }        if ((ch == '+') || (ch == '-'))             {            if (signSeen || numDigits > 0)   /* embedded sign */                return FALSE;            if (ch == '-')                *negative = TRUE;            signSeen = TRUE;            continue;            }        if (isdigit((unsigned char)ch))            {            if (((ulval == 429496729L) && (ch > '5')) ||                                            (ulval > 429496729L))                return FALSE;     /* would overlflow */            ulval = (ulval * 10) + (ch - '0');            numDigits++;            continue;            }        return FALSE;    /* invalid character */        }    if (numDigits == 0)        return FALSE;    *n = ulval;    return TRUE;}/********************************************************************/    /*  this is a more convenient Set/Get-BreakOnLibraryErrors() function */int CVIFUNC SetBOLE(int on){    int wasOn;    wasOn = GetBreakOnLibraryErrors();    if (on)        EnableBreakOnLibraryErrors();    else        DisableBreakOnLibraryErrors();    return wasOn;}/********************************************************************/static void CVIFUNC InterleavePartialLastScan(void *list, int itemSize, int numChannels, int numScans, int numItems){    int     numChannelsLeft;    int     channel;    int     destination;    char    itemBuf[20];    /* should be large enough for one item of any conceivable numeric type */    Assert (itemSize < 20);    numChannelsLeft = numItems - numScans * numChannels;    for (channel = numChannelsLeft - 1; channel >= 0; channel--)        {        memmove(itemBuf, (char *)list + (numItems - 1) * itemSize, itemSize);        destination = (channel + 1) * numScans;        memmove((char *)list + (destination + 1) * itemSize, (char *)list + destination * itemSize,                (numItems - destination - 1) * itemSize);        memmove((char *)list + destination * itemSize, itemBuf, itemSize);        }}/*******************************************************/static void CVIFUNC TransposeDataInPlace(void *list, int itemSize, int numPoints, int numChannels){    int     scan;    int     item;    int     numScans;    int     numChannelsLeft = numChannels;    int     *listPtr = list;    Assert (itemSize < 20);    numScans = numPoints / numChannels;    if (numChannels > 1)        {        do  {            for (scan = 1; scan < numScans; scan++)                {                item = listPtr[scan*numChannelsLeft];  /* extract relevant channel from start of scan */                memmove(&listPtr[scan+1], &listPtr[scan], sizeof(int)* (scan*numChannelsLeft - scan));                listPtr[scan] = item;                }            listPtr = listPtr + numScans;            numChannelsLeft--;            } while (numChannelsLeft > 1);        InterleavePartialLastScan(list, itemSize, numChannels, numScans, numPoints);        }}/*******************************************************/void CVIFUNC TransposeData(void *data, int dataType, int numPoints, int numChannels){    char    *reOrderedData = 0;    int     dataSize;    int     numScans;    int     oldIndex;    int     newIndex;    int     scan = 0;    int     channel = 0;    int     numScansForThisChannel;    int     extraChannelsSampled;   /* > 0 if the last scan is incomplete */    if (numChannels < 2)        return;    dataSize = GetDataTypeSize(dataType);    numScans = numPoints / numChannels;    extraChannelsSampled = numPoints - numScans * numChannels;    if (numScans < 2 &&  extraChannelsSampled == 0)        return;    if (dataSize > 0 && numPoints > 0)        {        reOrderedData = (char *)malloc(numPoints * dataSize);        if (reOrderedData)            {            newIndex = 0;            for (channel = 0; channel < numChannels; channel++)                {                numScansForThisChannel = numScans;                if (channel < extraChannelsSampled)                    numScansForThisChannel++;                for (scan = 0; scan < numScansForThisChannel; scan++)                    {                    oldIndex = scan * numChannels + channel;                    memmove(reOrderedData + newIndex * dataSize, (char *)data + oldIndex * dataSize, dataSize);                    newIndex++;                    }                }            memmove(data, reOrderedData, numPoints * dataSize);            }        else    /* this is slower, but takes no memory */            TransposeDataInPlace(data, dataSize, numPoints, numChannels);        }    free(reOrderedData);}/********************************************************************/    /*  converts from 1-based heap index to zero based array index */#define HeapItemPtr(array, oneBasedIndex, elementSize)  ((void *)((char *)(array) + (elementSize) * ((oneBasedIndex)-1)))    /*  Expects the array to be a heap of index - 1 elements.        the index'th element (1-based) will be inserted into the heap at the correct        spot, making array a heap of index elements.    */static void CVIFUNC FilterUpToCorrectHeapLocation(void *array, int index, int elementSize, CompareFunction compareFunction){    int     parent, cmp;    void    *indexPtr, *parentPtr;    for (parent = index >> 1; parent > 0; parent = parent >> 1)        {        indexPtr = HeapItemPtr(array, index, elementSize);        parentPtr = HeapItemPtr(array, parent, elementSize);        cmp = compareFunction ? compareFunction(indexPtr, parentPtr)                              : ListMemBlockCmp(indexPtr, parentPtr, elementSize);        if (cmp <= 0)            break;  /* element is in correct spot */        SwapBlock(indexPtr, parentPtr, elementSize);        index = parent;        }}/**************************************************************************/    /*  Expects array to be a heap except for the root element (element 1).        The root element is moved to it correct location so that the array is a heap.    */static void CVIFUNC FilterDownToCorrectHeapPosition(void *array, int numElements, int elementSize, CompareFunction compareFunction){    int     leftChild, rightChild, largestChild;    int     root, cmp;    void    *rootPtr, *leftChildPtr, *rightChildPtr, *largestChildPtr;    /* use 1-based indexing inside function for array representation of binary tree */    for (root = 1;;)        {        leftChild = root << 1;        rightChild = leftChild + 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -