-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
191 lines (176 loc) · 5.07 KB
/
functions.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
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <dirent.h>
#include <string.h>
#include "include/functions.h"
extern char *strdup(const char *src);
//Merger operations
void mapfilepositions(POSITION pos[],int filecount, char ** files) {
for (int i = 0; i < filecount; i++)
{
const char *currentfile = files[i];
//read positions
//Future version: find first integer in string
pos[i].x = atoi(¤tfile[5]);
//Future version: find second integer in string
pos[i].y = atoi(¤tfile[7]);
//Confirm positions
printf("%s positions -> x: %i y: %i \n", currentfile, pos[i].x, pos[i].y);
}
}
int merge(char **outputfiledata,const char* outputfile, int TOTAL_WIDTH, int FRAME_WIDTH,int mx,int my) {
//Calculate the rows we need to loop for writing
int totalrows = strlen(outputfiledata[0]) / (FRAME_WIDTH) * (my+1);
int row; //the byte position of row
int page = -1; //Page multiplier
FILE *fp;
fp = fopen(outputfile, "w+");
if (fp == NULL)
exit(EXIT_FAILURE);
for (int y = 0; y < totalrows; y++)
{
printf("%i: ",y+1);
int pager = y % FRAME_WIDTH;
if (pager == 0)
page++;
row = y % FRAME_WIDTH * (TOTAL_WIDTH / (mx + 1));
int cpos = (page * (mx + 1));
for (int fileindex = cpos; fileindex < cpos + (mx + 1); fileindex++)
{
for (int i = row; i < (row + FRAME_WIDTH); i++)
{
printf("%c", outputfiledata[fileindex][i]);
fputc(outputfiledata[fileindex][i], fp);
}
}
printf("\n");
fputc('\n', fp);
}
fclose(fp);
return totalrows; //return total rows written
}
//String operations
int getfilesize(const char *filename)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL)
exit(EXIT_FAILURE);
fseek(fp, 0, SEEK_END); // seek to end of file
int size = ftell(fp); // get current file pointer
fclose(fp);
return size;
}
void readfromfile(const char *fileName, char *fcontent)
{
FILE *fp = fopen(fileName, "r");
size_t len = 0;
if (fp == NULL)
exit(EXIT_FAILURE);
//fill buffer until we have a line
char chard = fgetc(fp);
while (chard != EOF)
{
//Add to buffer;
fcontent[len] = chard;
len++;
chard = fgetc(fp);
}
fcontent[len] = '\0';
fclose(fp);
}
//Get files in directory
void getfiles(char ***files, int *count, const char *directory)
{
DIR *dpdf;
struct dirent *epdf;
dpdf = opendir(directory);
if (dpdf == NULL)
exit(EXIT_FAILURE);
//Folder has files, enumerate
int fileCount = 0;
while ((epdf = readdir(dpdf)))
{
//Dropping two bytes in 2 blocks somewhere in this code, don't know where.
char *directoryName = malloc(strlen(epdf->d_name) + 1); //= epdf->d_name; //tried pointing to this, but that didnt work well
strcpy(directoryName, epdf->d_name);
if ((directoryName[0] != '.'))
{
*files = realloc(*files, sizeof(char *) * (fileCount + 1)); //again, +1 for nullterminator
(*files)[fileCount++] = directoryName;
printf("Found: %s\n", (*files)[fileCount - 1]);
}
else
{
//Free the unwanted directories
free(directoryName);
}
}
(*count) = fileCount;
closedir(dpdf);
}
//Merger
void load_in_order(int mx, int my, char **files, int filecount, const char *folder, char *outputfiledata[], int filesize, POSITION pos[])
{
//load the files in the order we want
//we want to load the vertical files after the horizontal
int order = 0;
for (int y = 0; y <= my; y++)
{
for (int x = 0; x <= mx; x++)
{
for (int i = 0; i < filecount; i++)
{
if (pos[i].y == y && pos[i].x == x)
{
//get the full filepath
char *fullfilepath = concat(folder, files[i]);
outputfiledata[order] = malloc(sizeof(char) * filesize + 1);
printf("Reading file %s\n", fullfilepath);
readfromfile(fullfilepath, outputfiledata[order]);
free(fullfilepath);
order++;
}
}
}
}
}
//Math operations
//Get the max x value of POSITION
int max_x(const POSITION *a, int n)
{
int c, max;
max = a[0].x;
for (c = 1; c < n; c++)
{
if (a[c].x > max)
{
max = a[c].x;
}
}
return max;
}
//Get the max Y value of POSITION
int max_y(const POSITION a[], int n)
{
int c, max;
max = a[0].y;
for (c = 1; c < n; c++)
{
if (a[c].y > max)
{
max = a[c].y;
}
}
return max;
}
//String operations
//Combine
char *concat(const char *s1, const char *s2)
{
size_t stringsize = strlen(s1) + strlen(s2) + 1;
char *result = malloc(stringsize); // +1 for the null-terminator
strcpy(result, s1);
strcat(result, s2);
return result;
}