SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
pvMultList.c
Go to the documentation of this file.
1/**
2 * @file pvMultList.c
3 * @brief Helper Functions used by cavget and cavput.
4 *
5 * This file contains routines for creating PV (Process Variable) names through
6 * multiplication of string lists or numeric ranges.
7 *
8 * @copyright
9 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
10 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
11 *
12 * @license
13 * This file is distributed under the terms of the Software License Agreement
14 * found in the file LICENSE included with this distribution.
15 *
16 * @authors
17 * - M. Borland
18 * - R. Soliday
19 * - H. Shang
20 */
21
22#include "mdb.h"
23#include "SDDS.h"
24#include "pvMultList.h"
25#include <stdlib.h>
26
27void multiplyWithList(PV_VALUE **PVvalue, long *PVvalues, TERM_LIST *List, long listEntries) {
28 long newPVvalues, i, j, k;
29 PV_VALUE *newPVvalue;
30
31 if (!*PVvalues) {
32 *PVvalues = listEntries;
33 *PVvalue = tmalloc(sizeof(**PVvalue) * listEntries);
34 for (i = 0; i < listEntries; i++) {
35 SDDS_CopyString(&(*PVvalue)[i].name, List[i].string);
36 if (List[i].flags & VALUE_GIVEN)
37 SDDS_CopyString(&(*PVvalue)[i].value, List[i].value);
38 else
39 (*PVvalue)[i].value = 0;
40 }
41 } else {
42 newPVvalues = *PVvalues * listEntries;
43 newPVvalue = tmalloc(sizeof(*newPVvalue) * (newPVvalues));
44 for (i = k = 0; i < *PVvalues; i++) {
45 for (j = 0; j < listEntries; j++, k++) {
46 newPVvalue[k].name = tmalloc(sizeof(*newPVvalue[k].name) *
47 (strlen(List[j].string) + strlen((*PVvalue)[i].name) + 1));
48 strcpy(newPVvalue[k].name, (*PVvalue)[i].name);
49 strcat(newPVvalue[k].name, List[j].string);
50 if (List[j].flags & VALUE_GIVEN)
51 SDDS_CopyString(&newPVvalue[k].value, List[j].value);
52 else
53 SDDS_CopyString(&newPVvalue[k].value, (*PVvalue)[i].value);
54 }
55 }
56 for (i = 0; i < *PVvalues; i++) {
57 free((*PVvalue)[i].name);
58 if ((*PVvalue)[i].value)
59 free((*PVvalue)[i].value);
60 }
61 free(*PVvalue);
62 *PVvalue = newPVvalue;
63 *PVvalues = newPVvalues;
64 }
65}
66
67void multiplyWithRange(PV_VALUE **PVvalue, long *PVvalues, long begin, long end, long interval, char *format) {
68 long newPVvalues, value, i, j, k;
69 PV_VALUE *newPVvalue;
70 char buffer[256];
71
72 if (!*PVvalues) {
73 *PVvalue = tmalloc(sizeof(**PVvalue) * (end - begin + 1));
74 for (value = begin, j = k = 0; value <= end; value++, j++) {
75 if (j % interval)
76 continue;
77 sprintf(buffer, format, value);
78 SDDS_CopyString(&(*PVvalue)[k].name, buffer);
79 (*PVvalue)[k].value = 0;
80 k++;
81 }
82 *PVvalues = k;
83 } else {
84 newPVvalue = tmalloc(sizeof(*newPVvalue) * (*PVvalues * (end - begin + 1)));
85 newPVvalues = 0;
86 for (i = k = 0; i < *PVvalues; i++) {
87 for (value = begin, j = 0; value <= end; value++, j++) {
88 if (j % interval)
89 continue;
90 sprintf(buffer, format, value);
91 newPVvalue[k].name = tmalloc(sizeof(*newPVvalue[k].name) *
92 (strlen(buffer) + strlen((*PVvalue)[i].name) + 1));
93 strcpy(newPVvalue[k].name, (*PVvalue)[i].name);
94 strcat(newPVvalue[k].name, buffer);
95 newPVvalue[k].value = (*PVvalue)[i].value;
96 k++;
97 }
98 }
99 newPVvalues = k;
100 for (i = 0; i < *PVvalues; i++) {
101 free((*PVvalue)[i].name);
102 /* if ((*PVvalue)[i].value) free((*PVvalue)[i].value); */
103 }
104 free(*PVvalue);
105 *PVvalue = newPVvalue;
106 *PVvalues = newPVvalues;
107 }
108}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
Definition SDDS_utils.c:856
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
Helper Functions used by cavget and cavput.