SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
substituteTagValue.c
Go to the documentation of this file.
1/**
2 * @file substituteTagValue.c
3 * @brief Handles macro substitution within input strings.
4 *
5 * @copyright
6 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
7 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
8 *
9 * @license
10 * This file is distributed under the terms of the Software License Agreement
11 * found in the file LICENSE included with this distribution.
12 *
13 * @author H. Shang, M. Borland, R. Soliday
14 */
15
16#include "mdb.h"
17#include "scan.h"
18#include "match_string.h"
19#include <ctype.h>
20
21/**
22 * @brief Replaces macro tags in the input string with their corresponding values.
23 *
24 * This function iterates through each macro tag and replaces occurrences of the tag in the input string
25 * with its corresponding value. It manages memory allocation for temporary buffers and ensures that
26 * substitutions are correctly applied for different tag formats.
27 *
28 * @param input The input string where macro substitution is to be performed.
29 * @param buflen The length of the input buffer.
30 * @param macroTag An array of macro tag strings to be replaced.
31 * @param macroValue An array of values corresponding to each macro tag.
32 * @param macros The number of macros to process.
33 */
34void substituteTagValue(char *input, long buflen,
35 char **macroTag, char **macroValue, long macros) {
36 char *buffer;
37 long i;
38 char *version1 = NULL, *version2 = NULL;
39 if (!(buffer = malloc(sizeof(*buffer) * buflen)))
40 bomb("memory allocation failure doing macro substitution", NULL);
41 for (i = 0; i < macros; i++) {
42 if (i == 0) {
43 if (!(version1 = malloc(sizeof(*version1) * (strlen(macroTag[i]) + 10))) ||
44 !(version2 = malloc(sizeof(*version2) * (strlen(macroTag[i]) + 10))))
45 bomb("memory allocation failure doing macro substitution", NULL);
46 } else {
47 if (!(version1 = realloc(version1, sizeof(*version1) * (strlen(macroTag[i]) + 10))) ||
48 !(version2 = realloc(version2, sizeof(*version2) * (strlen(macroTag[i]) + 10))))
49 bomb("memory allocation failure doing macro substitution", NULL);
50 }
51 sprintf(version1, "<%s>", macroTag[i]);
52 sprintf(version2, "$%s", macroTag[i]);
53 if (replace_string(buffer, input, version1, macroValue[i]))
54 strcpy_ss(input, buffer);
55 if (replace_string(buffer, input, version2, macroValue[i]))
56 strcpy_ss(input, buffer);
57 }
58 free(buffer);
59 if (version1)
60 free(version1);
61 if (version2)
62 free(version2);
63}
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
int replace_string(char *t, char *s, char *orig, char *repl)
Replace all occurrences of one string with another string.
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.
Definition str_copy.c:34
void substituteTagValue(char *input, long buflen, char **macroTag, char **macroValue, long macros)
Replaces macro tags in the input string with their corresponding values.