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

Implementation of search path management and file locating functions. More...

#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 (char *filename)
 Finds a file within the configured search path.
 

Variables

static char * search_path = NULL
 

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.

Function Documentation

◆ findFileInSearchPath()

char * findFileInSearchPath ( 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 char *path, *pathList, *tmpName;
51 char *sddsTags = NULL;
52
53 if (!filename || !strlen(filename))
54 return NULL;
55 if ((sddsTags = strchr(filename, '='))) {
56 /* <filename>=<x>+<y> form ? */
57 if (!strchr(sddsTags + 1, '+'))
58 sddsTags = NULL;
59 else
60 /* yes */
61 *sddsTags++ = 0;
62 }
63 if (search_path && strlen(search_path)) {
64 cp_str(&pathList, search_path);
65 while ((path = get_token(pathList))) {
66 tmpName = malloc(strlen(filename) + strlen(path) + 2 + (sddsTags ? strlen(sddsTags) + 2 : 0));
67 sprintf(tmpName, "%s/%s", path, filename);
68 free(path);
69 if (fexists(tmpName)) {
70 if (sddsTags) {
71 /* put the sddsTags back on the end */
72 strcat(tmpName, "=");
73 strcat(tmpName, sddsTags);
74 }
75 free(pathList);
76 return tmpName;
77 }
78 free(tmpName);
79 }
80 free(pathList);
81 }
82 if (fexists(filename)) {
83 if (sddsTags)
84 *(sddsTags - 1) = '=';
85 cp_str(&tmpName, filename);
86 return tmpName;
87 }
88 return NULL;
89}
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}

Variable Documentation

◆ search_path

char* search_path = NULL
static

Definition at line 18 of file searchPath.c.