/[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 163 by capela, Wed Jun 30 15:16:03 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  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
510  // Engine info struct helper functions.  // Engine info struct helper functions.
511    
# Line 492  void lscp_driver_info_reset ( lscp_drive Line 579  void lscp_driver_info_reset ( lscp_drive
579  }  }
580    
581    
582    //-------------------------------------------------------------------------
583    // Parameter struct helper functions.
584    
585    void lscp_param_info_init ( lscp_param_info_t *pParamInfo )
586    {
587        pParamInfo->type          = LSCP_TYPE_NONE;
588        pParamInfo->description   = NULL;
589        pParamInfo->mandatory     = 0;
590        pParamInfo->fix           = 0;
591        pParamInfo->multiplicity  = 0;
592        pParamInfo->depends       = NULL;
593        pParamInfo->defaultv      = NULL;
594        pParamInfo->range_min     = NULL;
595        pParamInfo->range_max     = NULL;
596        pParamInfo->possibilities = NULL;
597    }
598    
599    void lscp_param_info_reset ( lscp_param_info_t *pParamInfo )
600    {
601        if (pParamInfo->description)
602            free(pParamInfo->description);
603        lscp_szsplit_destroy(pParamInfo->depends);
604        if (pParamInfo->defaultv)
605            free(pParamInfo->defaultv);
606        if (pParamInfo->range_min)
607            free(pParamInfo->range_min);
608        if (pParamInfo->range_max)
609            free(pParamInfo->range_max);
610        lscp_szsplit_destroy(pParamInfo->possibilities);
611        
612        lscp_param_info_init(pParamInfo);
613    }
614    
615    
616    //-------------------------------------------------------------------------
617    // Concatenate a parameter list (key='value'...) into a string.
618    
619    int lscp_param_concat ( char *pszBuffer, int cchMaxBuffer, lscp_param_t *pParams )
620    {
621        int cchBuffer, cchParam, i;
622    
623        if (pszBuffer == NULL || pParams == NULL)
624            return 0;
625    
626        cchBuffer = strlen(pszBuffer);
627        for (i = 0; pParams[i].key && pParams[i].value; i++) {
628            cchParam = strlen(pParams[i].key) + strlen(pParams[i].value) + 4;
629            if (cchBuffer + cchParam < cchMaxBuffer) {
630                sprintf(pszBuffer + cchBuffer, " %s='%s'", pParams[i].key, pParams[i].value);
631                cchBuffer += cchParam;
632            }
633        }
634        return cchBuffer;
635    }
636    
637    
638  // end of common.c  // end of common.c

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

  ViewVC Help
Powered by ViewVC