-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfileTools.c
More file actions
121 lines (105 loc) · 3.02 KB
/
fileTools.c
File metadata and controls
121 lines (105 loc) · 3.02 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h> // Contains permission bits such as: S_IRWXU
#include "fileTools.h"
#define BUFSIZE 1024
int append(char* _head, char* _tail)
{
FILE* head = fopen(_head, "ab");
FILE* tail = fopen(_tail, "rb");
appendFiles(head,tail);
exit(EXIT_SUCCESS);
}
int appendFiles(FILE* head, FILE* tail)
{
if(!head || !tail){
printf("Error opening files?\n");
abort();
}
char buf[BUFSIZ];
size_t n;
while ((n = fread(buf, 1, sizeof buf, tail)) > 0){
if (fwrite(buf, 1, n, head) != n){
printf("Error writing?\n");
abort();
}
}
if(ferror(tail)){
printf("Error in tail?\n");
abort();
}
fclose(head);
fclose(tail);
exit(EXIT_SUCCESS);
}
int getCommentLineFromFile(char* inputFile, char* commentDescText, char* returnString){
FILE* data = fopen(inputFile,"r");
char* pointer;
if (!data) {
printf("Unable to open file %s\n",inputFile);
exit(1);
}
do{
fgets(returnString,BUFSIZE,data);
//printf("Searching for comment line...\n");
} while(strncmp(returnString,commentDescText,strlen(commentDescText)));
pointer = strtok(returnString,"\t");
pointer = strtok(NULL,"\t");
strcpy(returnString,pointer);
returnString[strcspn(returnString,"\n")]=0;
return 0;
}
int createTimeStampedFileWithID(char* returnFilename,char* fileID){
time_t rawtime;
struct tm * timeinfo;
struct stat st = {0};
char dateString[BUFSIZE];
char timeString[BUFSIZE];
char temp[BUFSIZE];
// Get file name. Use format "fileID"+$DATE+$TIME+".dat"
time(&rawtime);
timeinfo=localtime(&rawtime);
strftime(temp,BUFSIZE,"/home/pi/RbData/%F",timeinfo); //INCLUDE
if (stat(temp, &st) == -1){
mkdir(temp,S_IRWXU | S_IRWXG | S_IRWXO );
}
strftime(dateString,BUFSIZE,"%F",timeinfo);
strftime(timeString,BUFSIZE,"%H%M%S",timeinfo);
sprintf(returnFilename,BUFSIZE,"/home/pi/RbData/%s/%s%s_%s.dat",dateString,fileID,dateString,timeString);
}
int createSummaryFileWithID(char* returnFilename,char* fileID){
time_t rawtime;
struct tm * timeinfo;
struct stat st = {0};
char dateString[BUFSIZE];
char timeString[BUFSIZE];
char temp[BUFSIZE];
// Get file name. Use format "fileID"+$DATE+$TIME+".dat"
time(&rawtime);
timeinfo=localtime(&rawtime);
strftime(temp,BUFSIZE,"/home/pi/RbData/%F",timeinfo); //INCLUDE
if (stat(temp, &st) == -1){
mkdir(temp,S_IRWXU | S_IRWXG | S_IRWXO );
}
strftime(dateString,BUFSIZE,"%F",timeinfo);
strftime(timeString,BUFSIZE,"%H%M%S",timeinfo);
sprintf(returnFilename,BUFSIZE,"/home/pi/RbData/%s/%s%s.dat",dateString,fileID,dateString);
}
int getLineNumberForComment(char* inputFile, char* commentDescText){
int i=-1;
FILE* data = fopen(inputFile,"r");
char buffer[1024];
if (!data) {
printf("Unable to open file %s\n",inputFile);
exit(1);
}
do{
i++;
fgets(buffer,1024,data);
} while(strncmp(buffer,commentDescText,strlen(commentDescText)) && i <25);
if(i==25)i=-1;
fclose(data);
return i;
}