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

Provides functions for determining unpacking types and opening unpacked files. More...

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

Go to the source code of this file.

Macros

#define UNPACK_TYPES   3
 

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.
 

Variables

static char * unpackSuffix [UNPACK_TYPES]
 
static char * unpackCommand [UNPACK_TYPES]
 

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.

Macro Definition Documentation

◆ UNPACK_TYPES

#define UNPACK_TYPES   3

Definition at line 23 of file unpack.c.

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 47 of file unpack.c.

47 {
48 char *extension, buffer[10];
49 FILE *fp;
50 long i;
51
52 if (!(extension = strrchr(filename, '.')))
53 return -1;
54
55 extension++;
56 for (i = 0; i < UNPACK_TYPES; i++)
57 if (strcmp(extension, unpackSuffix[i]) == 0) {
58 if (unpackedName) {
59 cp_str(unpackedName, filename);
60 extension = strrchr(*unpackedName, '.');
61 *extension = 0;
62 }
63 break;
64 }
65 if (i == UNPACK_TYPES)
66 return -1;
67
68 if (mode & UNPACK_REQUIRE_SDDS) {
69 if (!(fp = fopen(filename, FOPEN_READ_MODE)))
70 return -1;
71 if (fread(buffer, sizeof(*buffer), 4, fp) == 4 && strncmp(buffer, "SDDS", 4) == 0) {
72 fclose(fp);
73 return -1;
74 }
75 fclose(fp);
76 }
77 return i;
78}
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 93 of file unpack.c.

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

Variable Documentation

◆ unpackCommand

char* unpackCommand[UNPACK_TYPES]
static
Initial value:
= {
"gzip -dcn %s 2> /dev/null ",
"freeze -dc %s ",
"uncompress -c %s ",
}

Definition at line 29 of file unpack.c.

29 {
30 "gzip -dcn %s 2> /dev/null ",
31 "freeze -dc %s ",
32 "uncompress -c %s ",
33};

◆ unpackSuffix

char* unpackSuffix[UNPACK_TYPES]
static
Initial value:
= {
"gz",
"F",
"Z",
}

Definition at line 24 of file unpack.c.

24 {
25 "gz",
26 "F",
27 "Z",
28};