SDDSlib
Loading...
Searching...
No Matches
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#include "SDDS.h"
21
22/**
23 * @brief Replaces macro tags in the input string with their corresponding values.
24 *
25 * This function iterates through each macro tag and replaces occurrences of the tag in the input string
26 * with its corresponding value. It manages memory allocation for temporary buffers and ensures that
27 * substitutions are correctly applied for different tag formats.
28 *
29 * @param input The input string where macro substitution is to be performed.
30 * @param buflen The length of the input buffer.
31 * @param macroTag An array of macro tag strings to be replaced.
32 * @param macroValue An array of values corresponding to each macro tag.
33 * @param macros The number of macros to process.
34 */
35void substituteTagValue(char *input, long buflen,
36 char **macroTag, char **macroValue, long macros) {
37 char *buffer;
38 long i;
39 char *version1 = NULL, *version2 = NULL;
40 if (!(buffer = malloc(sizeof(*buffer) * buflen)))
41 bomb("memory allocation failure doing macro substitution", NULL);
42 for (i = 0; i < macros; i++) {
43 if (i == 0) {
44 if (!(version1 = malloc(sizeof(*version1) * (strlen(macroTag[i]) + 10))) ||
45 !(version2 = malloc(sizeof(*version2) * (strlen(macroTag[i]) + 10))))
46 bomb("memory allocation failure doing macro substitution", NULL);
47 } else {
48 if (!(version1 = realloc(version1, sizeof(*version1) * (strlen(macroTag[i]) + 10))) ||
49 !(version2 = realloc(version2, sizeof(*version2) * (strlen(macroTag[i]) + 10))))
50 bomb("memory allocation failure doing macro substitution", NULL);
51 }
52 sprintf(version1, "<%s>", macroTag[i]);
53 sprintf(version2, "$%s", macroTag[i]);
54 if (replace_string(buffer, input, version1, macroValue[i]))
55 strcpy_ss(input, buffer);
56 if (replace_string(buffer, input, version2, macroValue[i]))
57 strcpy_ss(input, buffer);
58 }
59 free(buffer);
60 if (version1)
61 free(version1);
62 if (version2)
63 free(version2);
64}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
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.