SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
unpack.c File Reference

Detailed Description

Provides functions for determining unpacking types and opening unpacked files.

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

Definition in file unpack.c.

#include "mdb.h"
#include "scan.h"

Go to the source code of this file.

Functions

long PackSuffixType (char *filename, char **unpackedName, unsigned long mode)
 Determines the unpacking type based on the file extension.
 
FILE * UnpackFopen (char *filename, unsigned long mode, short *popenUsed, char **tmpFileUsed)
 Opens a file, potentially unpacking it based on its extension and mode.
 

Function Documentation

◆ PackSuffixType()

long PackSuffixType ( char * filename,
char ** unpackedName,
unsigned long mode )

Determines the unpacking type based on the file extension.

This function checks the file extension of the given filename to determine the type of unpacking required. It can optionally provide the unpacked filename without its extension and verify if the file conforms to the SDDS format if the UNPACK_REQUIRE_SDDS flag is set in the mode.

Parameters
filenameThe name of the file to check.
unpackedNamePointer to store the unpacked filename without extension (optional).
modeFlags indicating unpacking requirements and options.
Returns
The index of the unpacking type if the extension matches, or -1 if no matching type is found or an error occurs.

Definition at line 43 of file unpack.c.

43 {
44 char *extension, buffer[10];
45 FILE *fp;
46 long i;
47
48 if (!(extension = strrchr(filename, '.')))
49 return -1;
50
51 extension++;
52 for (i = 0; i < UNPACK_TYPES; i++)
53 if (strcmp(extension, unpackSuffix[i]) == 0) {
54 if (unpackedName) {
55 cp_str(unpackedName, filename);
56 extension = strrchr(*unpackedName, '.');
57 *extension = 0;
58 }
59 break;
60 }
61 if (i == UNPACK_TYPES)
62 return -1;
63
64 if (mode & UNPACK_REQUIRE_SDDS) {
65 if (!(fp = fopen(filename, FOPEN_READ_MODE)))
66 return -1;
67 if (fread(buffer, sizeof(*buffer), 4, fp) == 4 && strncmp(buffer, "SDDS", 4) == 0) {
68 fclose(fp);
69 return -1;
70 }
71 fclose(fp);
72 }
73 return i;
74}
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
Definition cp_str.c:28

◆ UnpackFopen()

FILE * UnpackFopen ( char * filename,
unsigned long mode,
short * popenUsed,
char ** tmpFileUsed )

Opens a file, potentially unpacking it based on its extension and mode.

This function attempts to open a file, determining whether it needs to be unpacked based on its extension. If unpacking is required and specified by the mode, it either uses a pipe or creates a temporary file to access the unpacked data.

Parameters
filenameThe name of the file to open.
modeFlags indicating unpacking requirements and options.
popenUsedPointer to a short that will be set to 1 if popen is used, otherwise 0 (optional).
tmpFileUsedPointer to store the name of the temporary file used for unpacking (optional).
Returns
A FILE pointer to the opened file, or NULL if the file could not be opened or an error occurred.

Definition at line 89 of file unpack.c.

89 {
90 char *command = NULL;
91 FILE *fp;
92 long type;
93 char *tmpName;
94
95 if (popenUsed)
96 *popenUsed = 0;
97 if (tmpFileUsed)
98 *tmpFileUsed = NULL;
99 if (!filename)
100 return NULL;
101 if ((type = PackSuffixType(filename, NULL, mode)) < 0)
102 return fopen(filename, FOPEN_READ_MODE);
103
104 if (!(command = trealloc(command, sizeof(*command) * (strlen(filename) + 100))))
105 return NULL;
106 if (mode & UNPACK_USE_PIPE) {
107 sprintf(command, unpackCommand[type], filename);
108 if (popenUsed)
109 *popenUsed = 1;
110#if defined(vxWorks)
111 fprintf(stderr, "popen is not supported in vxWorks\n");
112 exit(1);
113 return NULL;
114#else
115 fp = popen(command, FOPEN_READ_MODE);
116 free(command);
117 return fp;
118#endif
119 } else {
120 sprintf(command, unpackCommand[type], filename);
121 tmpName = tmpname(NULL);
122 strcat(command, "> /tmp/");
123 strcat(command, tmpName);
124 int ret = system(command);
125 (void)ret;
126
127 sprintf(command, "/tmp/%s", tmpName);
128 if (tmpFileUsed)
129 cp_str(tmpFileUsed, command);
130
131 fp = fopen(command, FOPEN_READ_MODE);
132 free(tmpName);
133 free(command);
134 return fp;
135 }
136}
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
Definition array.c:190
char * tmpname(char *s)
Supplies a unique temporary filename.
Definition tmpname.c:37
long PackSuffixType(char *filename, char **unpackedName, unsigned long mode)
Determines the unpacking type based on the file extension.
Definition unpack.c:43