/[svn]/liblscp/trunk/src/common.c
ViewVC logotype

Diff of /liblscp/trunk/src/common.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 132 by capela, Fri Jun 18 14:19:19 2004 UTC revision 171 by capela, Mon Jul 5 16:26:44 2004 UTC
# Line 1  Line 1 
1  // client.c  // common.c
2  //  //
3  /****************************************************************************  /****************************************************************************
4     liblscp - LinuxSampler Control Protocol API     liblscp - LinuxSampler Control Protocol API
# Line 25  Line 25 
25  #include <ctype.h>  #include <ctype.h>
26    
27    
28  // Chunk size magic:  // Split chunk size magic:
29  // LSCP_SPLIT_CHUNK1 = 2 ^ LSCP_SPLIT_CHUNK2  // LSCP_SPLIT_CHUNK1 := 2 ^ LSCP_SPLIT_CHUNK2
30  #define LSCP_SPLIT_CHUNK1   4  #define LSCP_SPLIT_CHUNK1   4
31  #define LSCP_SPLIT_CHUNK2   2  #define LSCP_SPLIT_CHUNK2   2
32  // Chunk size legal calculator.  // Chunk size legal calculator.
# Line 419  int lscp_isplit_size ( int *piSplit ) Line 419  int lscp_isplit_size ( int *piSplit )
419  #endif // LSCP_ISPLIT_COUNT  #endif // LSCP_ISPLIT_COUNT
420    
421    
422    // Split a string into a null terminated array of parameter items.
423    lscp_param_t *lscp_psplit_create ( const char *pszCsv, const char *pszSeps1, const char *pszSeps2 )
424    {
425        char *pszHead, *pch;
426        int iSize, i, j, cchSeps1, cchSeps2;
427        lscp_param_t *ppSplit, *ppNewSplit;
428    
429        pszHead = strdup(pszCsv);
430        if (pszHead == NULL)
431            return NULL;
432    
433        iSize = LSCP_SPLIT_CHUNK1;
434        ppSplit = (lscp_param_t *) malloc(iSize * sizeof(lscp_param_t));
435        if (ppSplit == NULL) {
436            free(pszHead);
437            return NULL;
438        }
439    
440        cchSeps1 = strlen(pszSeps1);
441        cchSeps2 = strlen(pszSeps2);
442    
443        i = 0;
444        while ((pch = strpbrk(pszHead, pszSeps1)) != NULL) {
445            ppSplit[i].key = pszHead;
446            pszHead = pch + cchSeps1;
447            *pch = (char) 0;
448            ppSplit[i].value = lscp_unquote(&pszHead, 0);
449            if ((pch = strpbrk(pszHead, pszSeps2)) != NULL) {
450                pszHead = pch + cchSeps2;
451                *pch = (char) 0;
452            }
453            if (++i >= iSize) {
454                iSize += LSCP_SPLIT_CHUNK1;
455                ppNewSplit = (lscp_param_t *) malloc(iSize * sizeof(lscp_param_t));
456                if (ppNewSplit) {
457                    for (j = 0; j < i; j++) {
458                        ppNewSplit[j].key   = ppSplit[j].key;
459                        ppNewSplit[j].value = ppSplit[j].value;
460                    }
461                    free(ppSplit);
462                    ppSplit = ppNewSplit;
463                }
464            }
465        }
466    
467        if (i < 1)
468            free(pszHead);
469    
470        for ( ; i < iSize; i++) {
471            ppSplit[i].key   = NULL;
472            ppSplit[i].value = NULL;
473        }
474    
475        return ppSplit;
476    }
477    
478    
479    // Destroy a parameter list array.
480    void lscp_psplit_destroy ( lscp_param_t *ppSplit )
481    {
482        if (ppSplit && ppSplit[0].key)
483            free(ppSplit[0].key);
484        if (ppSplit)
485            free(ppSplit);
486    }
487    
488    
489    #ifdef LSCP_PSPLIT_COUNT
490    
491    // Compute a parameter list valid item count.
492    int lscp_psplit_count ( lscp_param_t *ppSplit )
493    {
494        int i = 0;
495        while (ppSplit && ppSplit[i].key)
496            i++;
497        return i;
498    }
499    
500    // Compute a parameter list size.
501    int lscp_psplit_size ( lscp_param_t *ppSplit )
502    {
503        return LSCP_SPLIT_SIZE(lscp_psplit_count(ppSplit));
504    }
505    
506    #endif // LSCP_PSPLIT_COUNT
507    
508    
509    // Allocate a parameter list, optionally copying an existing one.
510    void lscp_plist_alloc (lscp_param_t **ppList)
511    {
512        lscp_param_t *pParams;
513        int iSize, i;
514    
515        if (ppList) {
516            iSize = LSCP_SPLIT_CHUNK1;
517            pParams = (lscp_param_t *) malloc(iSize * sizeof(lscp_param_t));
518            if (pParams) {
519                for (i = 0 ; i < iSize; i++) {
520                    pParams[i].key   = NULL;
521                    pParams[i].value = NULL;
522                }
523            }
524            *ppList = pParams;
525        }
526    }
527    
528    
529    // Destroy a parameter list, including all it's contents.
530    void lscp_plist_free ( lscp_param_t **ppList )
531    {
532        lscp_param_t *pParams;
533        int i;
534        
535        if (ppList) {
536            if (*ppList) {
537                pParams = *ppList;
538                for (i = 0; pParams && pParams[i].key; i++) {
539                    free(pParams[i].key);
540                    free(pParams[i].value);
541                }
542                free(pParams);
543            }
544            *ppList = NULL;
545        }
546    }
547    
548    
549    // Add an item to a parameter list, growing it as fit.
550    void lscp_plist_append ( lscp_param_t **ppList, const char *pszKey, const char *pszValue )
551    {
552        lscp_param_t *pParams;
553        lscp_param_t *pNewParams;
554        int iSize, iNewSize;
555        int i = 0;
556        
557        if (ppList && *ppList) {
558            pParams = *ppList;
559            while (pParams[i].key)
560                i++;
561            iSize = LSCP_SPLIT_SIZE(i);
562            pParams[i].key   = strdup(pszKey);
563            pParams[i].value = strdup(pszValue);
564            if (++i >= iSize) {
565                iNewSize   = iSize + LSCP_SPLIT_CHUNK1;
566                pNewParams = (lscp_param_t *) malloc(iNewSize * sizeof(lscp_param_t));
567                for (i = 0; i < iSize; i++) {
568                    pParams[i].key   = pParams[i].key;
569                    pParams[i].value = pParams[i].value;
570                }
571                for ( ; i < iNewSize; i++) {
572                    pNewParams[i].key   = NULL;
573                    pNewParams[i].value = NULL;
574                }
575                free(pParams);
576                *ppList = pNewParams;
577            }
578        }
579    }
580    
581    
582    // Compute a parameter list valid item count.
583    int lscp_plist_count ( lscp_param_t **ppList )
584    {
585        lscp_param_t *pParams;
586        int i = 0;
587        if (ppList && *ppList) {
588            pParams = *ppList;
589            while (pParams[i].key)
590                i++;
591        }
592        return i;
593    }
594    
595    
596    // Compute the legal parameter list size.
597    int lscp_plist_size ( lscp_param_t **ppList )
598    {
599        return LSCP_SPLIT_SIZE(lscp_plist_count(ppList));
600    }
601    
602    
603    
604  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
605  // Engine info struct helper functions.  // Engine info struct helper functions.
606    
# Line 492  void lscp_driver_info_reset ( lscp_drive Line 674  void lscp_driver_info_reset ( lscp_drive
674  }  }
675    
676    
677    //-------------------------------------------------------------------------
678    // Device info struct functions.
679    
680    void lscp_device_info_init ( lscp_device_info_t *pDeviceInfo )
681    {
682        pDeviceInfo->driver = NULL;
683        lscp_plist_alloc(&(pDeviceInfo->params));
684    }
685    
686    void lscp_device_info_reset ( lscp_device_info_t *pDeviceInfo )
687    {
688        if (pDeviceInfo->driver)
689            free(pDeviceInfo->driver);
690        lscp_plist_free(&(pDeviceInfo->params));
691    
692        lscp_device_info_init(pDeviceInfo);
693    }
694    
695    
696    //-------------------------------------------------------------------------
697    // Device channel/port info struct functions.
698    
699    void lscp_device_port_info_init ( lscp_device_port_info_t *pDevicePortInfo )
700    {
701        pDevicePortInfo->name = NULL;
702        lscp_plist_alloc(&(pDevicePortInfo->params));
703    }
704    
705    void lscp_device_port_info_reset ( lscp_device_port_info_t *pDevicePortInfo )
706    {
707        if (pDevicePortInfo->name)
708            free(pDevicePortInfo->name);
709        lscp_plist_free(&(pDevicePortInfo->params));
710    
711        lscp_device_port_info_init(pDevicePortInfo);
712    }
713    
714    
715    //-------------------------------------------------------------------------
716    // Parameter struct helper functions.
717    
718    void lscp_param_info_init ( lscp_param_info_t *pParamInfo )
719    {
720        pParamInfo->type          = LSCP_TYPE_NONE;
721        pParamInfo->description   = NULL;
722        pParamInfo->mandatory     = 0;
723        pParamInfo->fix           = 0;
724        pParamInfo->multiplicity  = 0;
725        pParamInfo->depends       = NULL;
726        pParamInfo->defaultv      = NULL;
727        pParamInfo->range_min     = NULL;
728        pParamInfo->range_max     = NULL;
729        pParamInfo->possibilities = NULL;
730    }
731    
732    void lscp_param_info_reset ( lscp_param_info_t *pParamInfo )
733    {
734        if (pParamInfo->description)
735            free(pParamInfo->description);
736        lscp_szsplit_destroy(pParamInfo->depends);
737        if (pParamInfo->defaultv)
738            free(pParamInfo->defaultv);
739        if (pParamInfo->range_min)
740            free(pParamInfo->range_min);
741        if (pParamInfo->range_max)
742            free(pParamInfo->range_max);
743        lscp_szsplit_destroy(pParamInfo->possibilities);
744        
745        lscp_param_info_init(pParamInfo);
746    }
747    
748    
749    //-------------------------------------------------------------------------
750    // Concatenate a parameter list (key='value'...) into a string,
751    // appending a crlf terminator.
752    
753    int lscp_param_concat ( char *pszBuffer, int cchMaxBuffer, lscp_param_t *pParams )
754    {
755        int cchBuffer, cchParam, i;
756    
757        if (pszBuffer == NULL || pParams == NULL)
758            return 0;
759    
760        cchBuffer = strlen(pszBuffer);
761        for (i = 0; pParams[i].key && pParams[i].value; i++) {
762            cchParam = strlen(pParams[i].key) + strlen(pParams[i].value) + 4;
763            if (cchBuffer + cchParam + 2 < cchMaxBuffer) {
764                sprintf(pszBuffer + cchBuffer, " %s='%s'", pParams[i].key, pParams[i].value);
765                cchBuffer += cchParam;
766            }
767        }
768        
769        if (cchBuffer + 2 < cchMaxBuffer) {
770            pszBuffer[cchBuffer++] = '\r';
771            pszBuffer[cchBuffer++] = '\n';
772        }
773        
774        return cchBuffer;
775    }
776    
777    
778  // end of common.c  // end of common.c

Legend:
Removed from v.132  
changed lines
  Added in v.171

  ViewVC Help
Powered by ViewVC