-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.c
181 lines (147 loc) · 4.36 KB
/
files.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "files.h"
char* getDirName(const char* filePath)
{
char* lastSlash = strrchr(filePath, '/');
if (lastSlash == NULL)
return NULL;
char* tmp = malloc( (strlen(filePath)+1)*sizeof(char) );
unsigned int i;
for (i=0; &filePath[i]!=lastSlash+1 && filePath[i]!='\0'; i++)
tmp[i] = filePath[i];
tmp[i] = '\0';
char* dirName = malloc( (strlen(tmp)+1)*sizeof(char) );
strcpy(dirName, tmp);
free(tmp);
return dirName;
}
char* getBaseName(const char* filePath)
{
char* lastSlash = strrchr(filePath, '/');
if (lastSlash == NULL)
return NULL;
char* baseName = malloc( strlen(lastSlash)*sizeof(char) );
strcpy(baseName, lastSlash+1);
return baseName;
}
char* getOutputNameFromInputName(const char* inputFileName)
{
if (inputFileName == NULL)
{
WARNING_FUNC("getOutputNameFromInputName", "Tried to get output file name from input file name while input file name wasn't set");
return NULL;
}
char* outputFileName;
unsigned short inputLength = strlen(inputFileName);
if (inputLength <= 3)
{
outputFileName = malloc( (inputLength+1)*sizeof(char) );
strcpy(outputFileName, inputFileName);
return outputFileName;
}
if (inputFileName[inputLength-4] == '.')
{
outputFileName = malloc( (inputLength-3)*sizeof(char) );
strncpy(outputFileName, inputFileName, inputLength-4);
outputFileName[inputLength-4] = '\0';
return outputFileName;
}
outputFileName = malloc( (inputLength+1)*sizeof(char) );
strcpy(outputFileName, inputFileName);
return outputFileName;
}
char* addLmdExtension(const char* fileName)
{
char* answer;
unsigned short length = strlen(fileName);
if (fileName[length-4] == '.' && fileName[length-3] == 'l' && fileName[length-2] == 'm' && fileName[length-1] == 'd')
return (char*) fileName;//cast removes compiler warning
answer = malloc( (length+5)*sizeof(char) );
strcpy(answer, fileName);
answer[length] = '.';
answer[length+1] = 'l';
answer[length+2] = 'm';
answer[length+3] = 'd';
answer[length+4] = '\0';
return answer;
}
char* addTexExtension(const char* fileName)
{
if (fileName == NULL || strcmp(fileName, "") == 0)
{
ERROR_MSG("addTexExtension", "Tried to add extension to empty file name");
return NULL;
}
unsigned short fileNameLength = strlen(fileName);
char* fileNameWithExtension = malloc( (fileNameLength+5)*sizeof(char) );
strcpy(fileNameWithExtension, fileName);
fileNameWithExtension[fileNameLength] = '.';
fileNameWithExtension[fileNameLength+1] = 't';
fileNameWithExtension[fileNameLength+2] = 'e';
fileNameWithExtension[fileNameLength+3] = 'x';
fileNameWithExtension[fileNameLength+4] = '\0';
return fileNameWithExtension;
}
char* addPdfExtension(const char* fileName)
{
if (fileName == NULL || strcmp(fileName, "") == 0)
{
ERROR_MSG("addPdfExtension", "Tried to add extension to empty file name");
return NULL;
}
unsigned short fileNameLength = strlen(fileName);
char* fileNameWithExtension = malloc( (fileNameLength+5)*sizeof(char) );
strcpy(fileNameWithExtension, fileName);
fileNameWithExtension[fileNameLength] = '.';
fileNameWithExtension[fileNameLength+1] = 'p';
fileNameWithExtension[fileNameLength+2] = 'd';
fileNameWithExtension[fileNameLength+3] = 'f';
fileNameWithExtension[fileNameLength+4] = '\0';
return fileNameWithExtension;
}
char* getRandomStringOf10Chars()
{
srand(time(NULL));
char* answer = malloc(11*sizeof(char));
unsigned char i;
unsigned short randomNb;
for (i=0; i<10; i++)
{
randomNb = RAND_INT(0, 2);
if (randomNb == 0)
randomNb = RAND_INT(48, 57);
else if (randomNb == 1)
randomNb = RAND_INT(65, 90);
else
randomNb = RAND_INT(97, 122);
answer[i] = randomNb;
}
return answer;
}
char* getTmpFileName()
{
char* tmpBodyOutputFilePath;
tmpBodyOutputFilePath = malloc( (strlen(TMP_OUTPUT_FILENAME)+11)*sizeof(char) );
char* randomPart = getRandomStringOf10Chars();
sprintf(tmpBodyOutputFilePath, TMP_OUTPUT_FILENAME, randomPart);//TMP_OUTPUT_FILENAME == "LMD-%s.tmp"
free(randomPart);
return tmpBodyOutputFilePath;
}
STATUS deleteFile(const char* filePath)
{
if (unlink(filePath) != 0)
{
if (errno != ENOENT)//if file exists
{
char msg[256] = "Couldn't delete temporary file";
snprintf(msg, 256, "%s '%s'", msg, filePath);
ERROR_MSG("deleteFile", msg);
perror(NULL);
return RETURN_FAILURE;
}
}
return RETURN_SUCCESS;
}
bool fileExists(const char* filePath)
{
return (access(filePath, F_OK) != -1);
}