/[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 171 by capela, Mon Jul 5 16:26:44 2004 UTC
# Line 445  lscp_param_t *lscp_psplit_create ( const Line 445  lscp_param_t *lscp_psplit_create ( const
445          ppSplit[i].key = pszHead;          ppSplit[i].key = pszHead;
446          pszHead = pch + cchSeps1;          pszHead = pch + cchSeps1;
447          *pch = (char) 0;          *pch = (char) 0;
448          ppSplit[i].value.psz = lscp_unquote(&pszHead, 0);          ppSplit[i].value = lscp_unquote(&pszHead, 0);
449          if ((pch = strpbrk(pszHead, pszSeps2)) != NULL) {          if ((pch = strpbrk(pszHead, pszSeps2)) != NULL) {
450              pszHead = pch + cchSeps2;              pszHead = pch + cchSeps2;
451              *pch = (char) 0;              *pch = (char) 0;
# Line 455  lscp_param_t *lscp_psplit_create ( const Line 455  lscp_param_t *lscp_psplit_create ( const
455              ppNewSplit = (lscp_param_t *) malloc(iSize * sizeof(lscp_param_t));              ppNewSplit = (lscp_param_t *) malloc(iSize * sizeof(lscp_param_t));
456              if (ppNewSplit) {              if (ppNewSplit) {
457                  for (j = 0; j < i; j++) {                  for (j = 0; j < i; j++) {
458                      ppNewSplit[j].key = ppSplit[j].key;                      ppNewSplit[j].key   = ppSplit[j].key;
459                      ppNewSplit[j].value.psz = ppSplit[j].value.psz;                      ppNewSplit[j].value = ppSplit[j].value;
460                  }                  }
461                  free(ppSplit);                  free(ppSplit);
462                  ppSplit = ppNewSplit;                  ppSplit = ppNewSplit;
# Line 468  lscp_param_t *lscp_psplit_create ( const Line 468  lscp_param_t *lscp_psplit_create ( const
468          free(pszHead);          free(pszHead);
469    
470      for ( ; i < iSize; i++) {      for ( ; i < iSize; i++) {
471          ppSplit[i].key = NULL;          ppSplit[i].key   = NULL;
472          ppSplit[i].value.psz = NULL;          ppSplit[i].value = NULL;
473      }      }
474    
475      return ppSplit;      return ppSplit;
# Line 506  int lscp_psplit_size ( lscp_param_t *ppS Line 506  int lscp_psplit_size ( lscp_param_t *ppS
506  #endif // LSCP_PSPLIT_COUNT  #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 579  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.158  
changed lines
  Added in v.171

  ViewVC Help
Powered by ViewVC