-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadWriteFunctions.c
134 lines (117 loc) · 3.57 KB
/
ReadWriteFunctions.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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "Struct.h"
#include "Initializer.h"
#include "InternalLogic.h"
extern drawing *head;
FILE *getFile(char *fileFlag) {
char *pathAndFile = getUserInput();
if (!pathAndFile) {
printf("<<< Couldn\'t get path+file to override or create.\r\n");
return NULL;
}
FILE *saveTextFile = fopen(pathAndFile, fileFlag);
free(pathAndFile);
if (!saveTextFile) {
printf("<<< Couldn\'t create or find file, check the path and file supplied or your hard disk.\r\n");
return NULL;
}
return saveTextFile;
}
void saveText() {
FILE *saveTextFile = getFile("wb");
if (!saveTextFile)
return;
drawing *temp = head;
for (unsigned int i = 1; temp; i++) {
i % rowSize == 0 ? fprintf(saveTextFile, "%s", temp->rowData) : fprintf(saveTextFile, "%s\n", temp->rowData);
temp = temp->next;
}
fclose(saveTextFile);
printf("<<< File been saved!\r\n");
}
void openText() {
char dump = '\0';
FILE *readTextFile = getFile("rb");
if (!readTextFile)
return;
drawing *temp = head;
while (!feof(readTextFile)) {
if (!temp) {
printf("<<< Make sure the picture you trying to open size is matching this program settings.\r\n");
break;
}
for (unsigned int i = 0; i < columnSize; i++)
temp->rowData[i] = fgetc(readTextFile);
dump = fgetc(readTextFile);
while (dump != '\n' && dump != EOF)
dump = fgetc(readTextFile);
temp = temp->next;
}
fclose(readTextFile);
printDrawing();
}
void RLEWrite(FILE *saveRLEFile, unsigned int counter, char flag) {
char byte[8] = {0};
unsigned int twoPowNum = 0;
byte[0] = flag == filledChar ? 1 : 0;
for (int i = 7; i >= 1; i--) {
twoPowNum = pow(2, i - 1);
byte[8 - i] = counter >= twoPowNum ? 1 : 0;
counter -= counter >= twoPowNum ? twoPowNum : 0;
}
for (int i = 0; i < 8; i++)
fputc(byte[i], saveRLEFile);
}
void saveRLE() {
FILE *saveRLEFile = getFile("wb");
if (!saveRLEFile)
return;
drawing *temp = head;
unsigned int counter = 0;
char flag = temp->rowData[0];
while (temp) {
for (unsigned int i = 0; i < columnSize; i++) {
if (flag == temp->rowData[i] && counter < 127)
counter++;
else {
RLEWrite(saveRLEFile, counter, flag);
flag = temp->rowData[i];
counter = 1;
}
}
temp = temp->next;
}
RLEWrite(saveRLEFile, counter, flag);
fclose(saveRLEFile);
printf("<<< File been saved!\r\n");
}
void openRLE() {
FILE *openRLEFile = getFile("rb");
if (!openRLEFile)
return;
drawing *temp = head;
unsigned int amount = 0, counter = 0;
char byte[8] = {0}, charToDraw = '\0';
while (!feof(openRLEFile) && temp) {
for (int i = 0; i < 8; i++) {
byte[i] = fgetc(openRLEFile);
if (byte[i] == EOF)
break;
}
charToDraw = byte[0] ? filledChar : blankChar;
amount = 0;
for (unsigned int i = 1; i < 8; i++)
amount += byte[i] ? pow(2, 7 - i) : 0;
for (unsigned int i = 0; i < amount && temp; i++, counter++) {
if (counter != 0 && counter % columnSize == 0)
temp = temp->next;
if (!temp)
break;
temp->rowData[counter % columnSize] = charToDraw;
}
}
fclose(openRLEFile);
printDrawing();
}