SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
searchPath.c File Reference

Detailed Description

Implementation of search path management and file locating functions.

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 searchPath.c.

#include "mdb.h"

Go to the source code of this file.

Functions

void setSearchPath (char *input)
 Sets the search path for file lookup.
 
char * findFileInSearchPath (const char *filename)
 Finds a file within the configured search path.
 

Function Documentation

◆ findFileInSearchPath()

char * findFileInSearchPath ( const char * filename)

Finds a file within the configured search path.

This function searches for the specified filename in each directory listed in the search_path. If the filename includes SDDS tags (indicated by '=' and '+'), the tags are processed and appended to the found file path.

Parameters
filenameThe name of the file to locate. It may include SDDS tags in the format <filename>=<x>+<y>.
Returns
A dynamically allocated string containing the path to the found file with tags, or NULL if the file is not found.

Definition at line 49 of file searchPath.c.

49 {
50 if (!filename || !strlen(filename))
51 return NULL;
52
53 /* Work on a modifiable copy of filename */
54 char *localFilename = strdup(filename);
55 if (!localFilename)
56 return NULL;
57
58 char *sddsTags = NULL;
59 /* Look for '=' in the copy */
60 if ((sddsTags = strchr(localFilename, '='))) {
61 /* Check for SDDS tag format "<filename>=<x>+<y>" */
62 if (!strchr(sddsTags + 1, '+'))
63 sddsTags = NULL;
64 else {
65 /* Split the string without modifying the original */
66 *sddsTags = '\0';
67 sddsTags++;
68 }
69 }
70
71 char *result = NULL;
72 if (search_path && strlen(search_path)) {
73 char *pathList;
74 cp_str(&pathList, search_path);
75 char *path;
76 while ((path = get_token(pathList))) {
77 size_t needed = strlen(localFilename) + strlen(path) + 2 +
78 (sddsTags ? strlen(sddsTags) + 2 : 0);
79 char *tmpName = malloc(needed);
80 sprintf(tmpName, "%s/%s", path, localFilename);
81 free(path);
82 if (fexists(tmpName)) {
83 if (sddsTags) {
84 strcat(tmpName, "=");
85 strcat(tmpName, sddsTags);
86 }
87 cp_str(&result, tmpName);
88 free(tmpName);
89 free(pathList);
90 free(localFilename);
91 return result;
92 }
93 free(tmpName);
94 }
95 free(pathList);
96 }
97 if (fexists(localFilename)) {
98 cp_str(&result, localFilename);
99 free(localFilename);
100 return result;
101 }
102 free(localFilename);
103 return NULL;
104}
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
Definition cp_str.c:28
char * get_token(char *s)
Extracts the next token from the input string.
Definition data_scan.c:413
long fexists(const char *filename)
Checks if a file exists.
Definition fexists.c:27

◆ setSearchPath()

void setSearchPath ( char * input)

Sets the search path for file lookup.

This function updates the global search_path variable. If a new input path is provided, it copies the input string to search_path, freeing any previously allocated memory. If the input is NULL, search_path is set to NULL.

Parameters
inputThe new search path to set. If NULL, the search path is cleared.

Definition at line 28 of file searchPath.c.

28 {
29 if (search_path)
30 free(search_path);
31 if (input)
32 cp_str(&search_path, input);
33 else
34 search_path = NULL;
35}