SDDSlib
Loading...
Searching...
No Matches
substituteTagValue.c File Reference

Handles macro substitution within input strings. More...

#include "mdb.h"
#include "scan.h"
#include "match_string.h"
#include <ctype.h>
#include "SDDS.h"

Go to the source code of this file.

Functions

void substituteTagValue (char *input, long buflen, char **macroTag, char **macroValue, long macros)
 Replaces macro tags in the input string with their corresponding values.
 

Detailed Description

Handles macro substitution within input strings.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
H. Shang, M. Borland, R. Soliday

Definition in file substituteTagValue.c.

Function Documentation

◆ substituteTagValue()

void substituteTagValue ( char * input,
long buflen,
char ** macroTag,
char ** macroValue,
long macros )

Replaces macro tags in the input string with their corresponding values.

This function iterates through each macro tag and replaces occurrences of the tag in the input string with its corresponding value. It manages memory allocation for temporary buffers and ensures that substitutions are correctly applied for different tag formats.

Parameters
inputThe input string where macro substitution is to be performed.
buflenThe length of the input buffer.
macroTagAn array of macro tag strings to be replaced.
macroValueAn array of values corresponding to each macro tag.
macrosThe number of macros to process.

Definition at line 35 of file substituteTagValue.c.

36 {
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}
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