/[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 158 by capela, Tue Jun 29 16:39:11 2004 UTC revision 187 by capela, Wed Jul 7 23:41:07 2004 UTC
# Line 223  char *lscp_unquote ( char **ppsz, int du Line 223  char *lscp_unquote ( char **ppsz, int du
223      return psz;      return psz;
224  }  }
225    
226    // Unquote and make a duplicate of an in-split string.
227    void lscp_unquote_dup ( char **ppszDst, char **ppszSrc )
228    {
229         // Free desteny string, if already there.
230         if (*ppszDst)
231             free(*ppszDst);
232         *ppszDst = NULL;
233         // Unquote and duplicate.
234         if (*ppszSrc)
235             *ppszDst = lscp_unquote(ppszSrc, 1);
236    }
237    
238    
239  // Custom tokenizer.  // Custom tokenizer.
240  char *lscp_strtok ( char *pchBuffer, const char *pszSeps, char **ppch )  char *lscp_strtok ( char *pchBuffer, const char *pszSeps, char **ppch )
# Line 346  int *lscp_isplit_create ( const char *ps Line 358  int *lscp_isplit_create ( const char *ps
358      int iSize, i, j, cchSeps;      int iSize, i, j, cchSeps;
359      int *piSplit, *piNewSplit;      int *piSplit, *piNewSplit;
360    
361        // Get it clean first.
362        pchHead = lscp_ltrim((char *) pszCsv);
363        if (*pchHead == (char) 0)
364            return NULL;
365        
366      // Initial size is one chunk away.      // Initial size is one chunk away.
367      iSize = LSCP_SPLIT_CHUNK1;      iSize = LSCP_SPLIT_CHUNK1;
368      // Allocate and split...      // Allocate and split...
# Line 355  int *lscp_isplit_create ( const char *ps Line 372  int *lscp_isplit_create ( const char *ps
372    
373      // Make a copy of the original string.      // Make a copy of the original string.
374      i = 0;      i = 0;
     pchHead = (char *) pszCsv;  
375      if ((piSplit[i++] = atoi(pchHead)) < 0) {      if ((piSplit[i++] = atoi(pchHead)) < 0) {
376          free(piSplit);          free(piSplit);
377          return NULL;          return NULL;
# Line 445  lscp_param_t *lscp_psplit_create ( const Line 461  lscp_param_t *lscp_psplit_create ( const
461          ppSplit[i].key = pszHead;          ppSplit[i].key = pszHead;
462          pszHead = pch + cchSeps1;          pszHead = pch + cchSeps1;
463          *pch = (char) 0;          *pch = (char) 0;
464          ppSplit[i].value.psz = lscp_unquote(&pszHead, 0);          ppSplit[i].value = lscp_unquote(&pszHead, 0);
465          if ((pch = strpbrk(pszHead, pszSeps2)) != NULL) {          if ((pch = strpbrk(pszHead, pszSeps2)) != NULL) {
466              pszHead = pch + cchSeps2;              pszHead = pch + cchSeps2;
467              *pch = (char) 0;              *pch = (char) 0;
# Line 455  lscp_param_t *lscp_psplit_create ( const Line 471  lscp_param_t *lscp_psplit_create ( const
471              ppNewSplit = (lscp_param_t *) malloc(iSize * sizeof(lscp_param_t));              ppNewSplit = (lscp_param_t *) malloc(iSize * sizeof(lscp_param_t));
472              if (ppNewSplit) {              if (ppNewSplit) {
473                  for (j = 0; j < i; j++) {                  for (j = 0; j < i; j++) {
474                      ppNewSplit[j].key = ppSplit[j].key;                      ppNewSplit[j].key   = ppSplit[j].key;
475                      ppNewSplit[j].value.psz = ppSplit[j].value.psz;                      ppNewSplit[j].value = ppSplit[j].value;
476                  }                  }
477                  free(ppSplit);                  free(ppSplit);
478                  ppSplit = ppNewSplit;                  ppSplit = ppNewSplit;
# Line 468  lscp_param_t *lscp_psplit_create ( const Line 484  lscp_param_t *lscp_psplit_create ( const
484          free(pszHead);          free(pszHead);
485    
486      for ( ; i < iSize; i++) {      for ( ; i < iSize; i++) {
487          ppSplit[i].key = NULL;          ppSplit[i].key   = NULL;
488          ppSplit[i].value.psz = NULL;          ppSplit[i].value = NULL;
489      }      }
490    
491      return ppSplit;      return ppSplit;
# Line 506  int lscp_psplit_size ( lscp_param_t *ppS Line 522  int lscp_psplit_size ( lscp_param_t *ppS
522  #endif // LSCP_PSPLIT_COUNT  #endif // LSCP_PSPLIT_COUNT
523    
524    
525    // Allocate a parameter list, optionally copying an existing one.
526    void lscp_plist_alloc (lscp_param_t **ppList)
527    {
528        lscp_param_t *pParams;
529        int iSize, i;
530    
531        if (ppList) {
532            iSize = LSCP_SPLIT_CHUNK1;
533            pParams = (lscp_param_t *) malloc(iSize * sizeof(lscp_param_t));
534            if (pParams) {
535                for (i = 0 ; i < iSize; i++) {
536                    pParams[i].key   = NULL;
537                    pParams[i].value = NULL;
538                }
539            }
540            *ppList = pParams;
541        }
542    }
543    
544    
545    // Destroy a parameter list, including all it's contents.
546    void lscp_plist_free ( lscp_param_t **ppList )
547    {
548        lscp_param_t *pParams;
549        int i;
550        
551        if (ppList) {
552            if (*ppList) {
553                pParams = *ppList;
554                for (i = 0; pParams && pParams[i].key; i++) {
555                    free(pParams[i].key);
556                    free(pParams[i].value);
557                }
558                free(pParams);
559            }
560            *ppList = NULL;
561        }
562    }
563    
564    
565    // Add an item to a parameter list, growing it as fit.
566    void lscp_plist_append ( lscp_param_t **ppList, const char *pszKey, const char *pszValue )
567    {
568        lscp_param_t *pParams;
569        lscp_param_t *pNewParams;
570        int iSize, iNewSize;
571        int i = 0;
572        
573        if (ppList && *ppList) {
574            pParams = *ppList;
575            while (pParams[i].key) {
576                if (strcasecmp(pParams[i].key, pszKey) == 0) {
577                    if (pParams[i].value)
578                        free(pParams[i].value);
579                    pParams[i].value = strdup(pszValue);
580                    return;
581                }
582                i++;
583            }
584            iSize = LSCP_SPLIT_SIZE(i);
585            pParams[i].key   = strdup(pszKey);
586            pParams[i].value = strdup(pszValue);
587            if (++i >= iSize) {
588                iNewSize   = iSize + LSCP_SPLIT_CHUNK1;
589                pNewParams = (lscp_param_t *) malloc(iNewSize * sizeof(lscp_param_t));
590                for (i = 0; i < iSize; i++) {
591                    pParams[i].key   = pParams[i].key;
592                    pParams[i].value = pParams[i].value;
593                }
594                for ( ; i < iNewSize; i++) {
595                    pNewParams[i].key   = NULL;
596                    pNewParams[i].value = NULL;
597                }
598                free(pParams);
599                *ppList = pNewParams;
600            }
601        }
602    }
603    
604    #ifdef LSCP_PLIST_COUNT
605    
606    // Compute a parameter list valid item count.
607    int lscp_plist_count ( lscp_param_t **ppList )
608    {
609        lscp_param_t *pParams;
610        int i = 0;
611        if (ppList && *ppList) {
612            pParams = *ppList;
613            while (pParams[i].key)
614                i++;
615        }
616        return i;
617    }
618    
619    // Compute the legal parameter list size.
620    int lscp_plist_size ( lscp_param_t **ppList )
621    {
622        return LSCP_SPLIT_SIZE(lscp_plist_count(ppList));
623    }
624    
625    #endif // LSCP_PLIST_COUNT
626    
627    
628  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
629  // Engine info struct helper functions.  // Engine info struct helper functions.
630    
# Line 515  void lscp_engine_info_init ( lscp_engine Line 634  void lscp_engine_info_init ( lscp_engine
634      pEngineInfo->version     = NULL;      pEngineInfo->version     = NULL;
635  }  }
636    
637  void lscp_engine_info_reset ( lscp_engine_info_t *pEngineInfo )  void lscp_engine_info_free ( lscp_engine_info_t *pEngineInfo )
638  {  {
639      if (pEngineInfo->description)      if (pEngineInfo->description)
640          free(pEngineInfo->description);          free(pEngineInfo->description);
641      if (pEngineInfo->version)      if (pEngineInfo->version)
642          free(pEngineInfo->version);          free(pEngineInfo->version);
643    }
644    
645    void lscp_engine_info_reset ( lscp_engine_info_t *pEngineInfo )
646    {
647        lscp_engine_info_free(pEngineInfo);
648      lscp_engine_info_init(pEngineInfo);      lscp_engine_info_init(pEngineInfo);
649  }  }
650    
# Line 544  void lscp_channel_info_init ( lscp_chann Line 667  void lscp_channel_info_init ( lscp_chann
667      pChannelInfo->volume            = 0.0;      pChannelInfo->volume            = 0.0;
668  }  }
669    
670  void lscp_channel_info_reset ( lscp_channel_info_t *pChannelInfo )  void lscp_channel_info_free ( lscp_channel_info_t *pChannelInfo )
671  {  {
672      if (pChannelInfo->engine_name)      if (pChannelInfo->engine_name)
673          free(pChannelInfo->engine_name);          free(pChannelInfo->engine_name);
# Line 552  void lscp_channel_info_reset ( lscp_chan Line 675  void lscp_channel_info_reset ( lscp_chan
675          lscp_szsplit_destroy(pChannelInfo->audio_routing);          lscp_szsplit_destroy(pChannelInfo->audio_routing);
676      if (pChannelInfo->instrument_file)      if (pChannelInfo->instrument_file)
677          free(pChannelInfo->instrument_file);          free(pChannelInfo->instrument_file);
678    }
679    
680    void lscp_channel_info_reset ( lscp_channel_info_t *pChannelInfo )
681    {
682        lscp_channel_info_free(pChannelInfo);
683      lscp_channel_info_init(pChannelInfo);      lscp_channel_info_init(pChannelInfo);
684  }  }
685    
# Line 567  void lscp_driver_info_init ( lscp_driver Line 694  void lscp_driver_info_init ( lscp_driver
694      pDriverInfo->parameters  = NULL;      pDriverInfo->parameters  = NULL;
695  }  }
696    
697  void lscp_driver_info_reset ( lscp_driver_info_t *pDriverInfo )  void lscp_driver_info_free ( lscp_driver_info_t *pDriverInfo )
698  {  {
699      if (pDriverInfo->description)      if (pDriverInfo->description)
700          free(pDriverInfo->description);          free(pDriverInfo->description);
701      if (pDriverInfo->version)      if (pDriverInfo->version)
702          free(pDriverInfo->version);          free(pDriverInfo->version);
703      lscp_szsplit_destroy(pDriverInfo->parameters);      lscp_szsplit_destroy(pDriverInfo->parameters);
704    }
705    
706    void lscp_driver_info_reset ( lscp_driver_info_t *pDriverInfo )
707    {
708        lscp_driver_info_free(pDriverInfo);
709      lscp_driver_info_init(pDriverInfo);      lscp_driver_info_init(pDriverInfo);
710  }  }
711    
712    
713    //-------------------------------------------------------------------------
714    // Device info struct functions.
715    
716    void lscp_device_info_init ( lscp_device_info_t *pDeviceInfo )
717    {
718        pDeviceInfo->driver = NULL;
719        lscp_plist_alloc(&(pDeviceInfo->params));
720    }
721    
722    void lscp_device_info_free ( lscp_device_info_t *pDeviceInfo )
723    {
724        if (pDeviceInfo->driver)
725            free(pDeviceInfo->driver);
726        lscp_plist_free(&(pDeviceInfo->params));
727    }
728    
729    void lscp_device_info_reset ( lscp_device_info_t *pDeviceInfo )
730    {
731        lscp_device_info_free(pDeviceInfo);
732        lscp_device_info_init(pDeviceInfo);
733    }
734    
735    
736    //-------------------------------------------------------------------------
737    // Device channel/port info struct functions.
738    
739    void lscp_device_port_info_init ( lscp_device_port_info_t *pDevicePortInfo )
740    {
741        pDevicePortInfo->name = NULL;
742        lscp_plist_alloc(&(pDevicePortInfo->params));
743    }
744    
745    void lscp_device_port_info_free ( lscp_device_port_info_t *pDevicePortInfo )
746    {
747        if (pDevicePortInfo->name)
748            free(pDevicePortInfo->name);
749        lscp_plist_free(&(pDevicePortInfo->params));
750    }
751    
752    void lscp_device_port_info_reset ( lscp_device_port_info_t *pDevicePortInfo )
753    {
754        lscp_device_port_info_free(pDevicePortInfo);
755        lscp_device_port_info_init(pDevicePortInfo);
756    }
757    
758    
759    //-------------------------------------------------------------------------
760    // Parameter struct helper functions.
761    
762    void lscp_param_info_init ( lscp_param_info_t *pParamInfo )
763    {
764        pParamInfo->type          = LSCP_TYPE_NONE;
765        pParamInfo->description   = NULL;
766        pParamInfo->mandatory     = 0;
767        pParamInfo->fix           = 0;
768        pParamInfo->multiplicity  = 0;
769        pParamInfo->depends       = NULL;
770        pParamInfo->defaultv      = NULL;
771        pParamInfo->range_min     = NULL;
772        pParamInfo->range_max     = NULL;
773        pParamInfo->possibilities = NULL;
774    }
775    
776    void lscp_param_info_free ( lscp_param_info_t *pParamInfo )
777    {
778        if (pParamInfo->description)
779            free(pParamInfo->description);
780        lscp_szsplit_destroy(pParamInfo->depends);
781        if (pParamInfo->defaultv)
782            free(pParamInfo->defaultv);
783        if (pParamInfo->range_min)
784            free(pParamInfo->range_min);
785        if (pParamInfo->range_max)
786            free(pParamInfo->range_max);
787        lscp_szsplit_destroy(pParamInfo->possibilities);
788    }
789    
790    void lscp_param_info_reset ( lscp_param_info_t *pParamInfo )
791    {
792        lscp_param_info_free(pParamInfo);
793        lscp_param_info_init(pParamInfo);
794    }
795    
796    
797    //-------------------------------------------------------------------------
798    // Concatenate a parameter list (key='value'...) into a string,
799    // appending a crlf terminator.
800    
801    int lscp_param_concat ( char *pszBuffer, int cchMaxBuffer, lscp_param_t *pParams )
802    {
803        int cchBuffer, cchParam, i;
804    
805        if (pszBuffer == NULL)
806            return 0;
807    
808        cchBuffer = strlen(pszBuffer);
809        for (i = 0; pParams && pParams[i].key && pParams[i].value; i++) {
810            cchParam = strlen(pParams[i].key) + strlen(pParams[i].value) + 4;
811            if (cchBuffer + cchParam + 2 < cchMaxBuffer) {
812                sprintf(pszBuffer + cchBuffer, " %s='%s'", pParams[i].key, pParams[i].value);
813                cchBuffer += cchParam;
814            }
815        }
816        
817        if (cchBuffer + 2 < cchMaxBuffer) {
818            pszBuffer[cchBuffer++] = '\r';
819            pszBuffer[cchBuffer++] = '\n';
820        }
821        
822        return cchBuffer;
823    }
824    
825    
826  // end of common.c  // end of common.c

Legend:
Removed from v.158  
changed lines
  Added in v.187

  ViewVC Help
Powered by ViewVC