-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW3.c
224 lines (203 loc) · 7.15 KB
/
HW3.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// Created by nimrod machlav
// Id: 315230185
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <malloc.h>
#include <stdlib.h>
/**
* Prints chapter information as instructed in the assignment
* @param name : Chapter name
* @param lines_count : Chapter lines size
* @param words_count : Chapter words size
* @param char_count : Chapter char size
*/
void print_chapter_info(const char *name, unsigned int lines_count, unsigned int words_count,
unsigned int char_count);
/**
* Generates chapters files by searching for "CHAPTER" in each line, also prints basic stats for each chapter
* @param src_file_name : src file name
* @param output_prefix : prefix for each generated chapter file
* @param output_suffix : suffix for each chapter name
*/
void generate_chapters(const char *src_file_name, const char *output_prefix, const char *output_suffix);
/**
* Trims dest string at the end, replaces all other white spaces with "char_replace"
* @param dest : destination string
* @param src : source string to copy from
* @param char_replace : char replace for in string spaces
*/
void trim_and_replace_spaces(char *dest, const char *src, char char_replace);
/**
* My impl for strlen
* @param str : string
* @return : length if the given str
*/
unsigned int my_strlen(const char str[]) {
unsigned int size = 0;
while (str[size] != '\0') {
size++;
}
return size;
}
/**
* My impl for strcat - copy src string to the end of dest string
* @param dest : destination string
* @param src : source string
*/
void my_strcat(char dest[], const char src[]) {
unsigned int dest_size = my_strlen(dest);
unsigned int src_size = my_strlen(src);
for (int i = 0; i <= src_size; ++i) {
dest[dest_size + i] = src[i];
}
}
/**
*
* @param s : string to check
* @param prefix : string
* @return true if "s" contains the prefix, "prefix", otherwise false.
*/
bool starts_with(const char s[], const char prefix[]) {
unsigned int prefix_size = my_strlen(prefix);
for (int i = 0; i < prefix_size; ++i) {
if (prefix[i] != s[i]) {
return false;
}
}
return true;
}
/**
* Works by basically counting the number of switches between a space and a regular char
* @param str : string to size
* @return number of words in the given "str" string
*/
unsigned int num_words(const char str[]) {
unsigned int words_size = my_strlen(str);
unsigned int words_count = 0;
char last_char = ' ';
for (int i = 0; i < words_size; ++i) {
if (isspace(last_char) && !isspace(str[i])) {
words_count++;
}
last_char = str[i];
}
return words_count;
}
/**
*
* @param s : chapter name string
* @param prefix : chapter prefix string
* @param suffix : chapter suffix string
* @return Chapter name after normalizing "s" (see "trim_and_replace_spaces"), and concatenating as: prefix + s + suffix
*
*/
char *get_chapter_file_name(const char s[], const char prefix[], const char suffix[]) {
unsigned int s_size = my_strlen(s);
unsigned int prefix_size = my_strlen(prefix);
unsigned int suffix_size = my_strlen(suffix);
unsigned total_size = s_size + prefix_size + suffix_size + 1;
char *title = calloc(total_size, sizeof(char));
if (!title) {
fprintf(stderr, "Failed to allocate %lu bytes", sizeof(char) * total_size);
exit(1);
}
for (int i = 0; i < prefix_size; ++i) {
title[i] = prefix[i];
}
trim_and_replace_spaces(title, s, '-');
my_strcat(title, suffix);
return title;
}
//int main(int argc, char *argv[]) {
// setbuf(stdout, NULL);
// if (argc <= 2) {
// fprintf(stderr, "\nUsage: %s <file-path> <output-prefix> [output-suffix] \n", argv[0]);
// return 1;
// }
// char *src_file_name = argv[1];
// char *output_prefix = argv[2];
// char *output_suffix = ".txt";
// if (argc > 3) {
// output_suffix = argv[3];
// }
//
// generate_chapters(src_file_name, output_prefix, output_suffix);
// return 0;
//}
#pragma clang diagnostic push
#pragma ide diagnostic ignored "DanglingPointer"
void generate_chapters(const char *src_file_name, const char *output_prefix, const char *output_suffix) {
char *dest_file_name = get_chapter_file_name("PREFACE", output_prefix, output_suffix);
FILE *f_in = fopen(src_file_name, "r"), *f_out = fopen(dest_file_name, "w");
char *buf = NULL;
size_t buf_size = 0;
if (!f_in || !f_out) {
fprintf(stderr, "Failed to open files: %s , %s\n", src_file_name, dest_file_name);
exit(2);
}
unsigned int current_lines_count = 0;
unsigned int current_words_count = 0;
unsigned int current_char_count = 0;
unsigned int total_lines_count = 0;
unsigned int total_words_count = 0;
unsigned int total_char_count = 0;
while (getline(&buf, &buf_size, f_in) != EOF) {
if (starts_with(buf, "CHAPTER")) { //new chapter? let's switch to next chapter and file output accordingly
//But first we need to print chapter info and reset current counters
print_chapter_info(dest_file_name, current_lines_count, current_words_count, current_char_count);
total_lines_count += current_lines_count;
total_words_count += current_words_count;
total_char_count += current_char_count;
current_lines_count = 0;
current_words_count = 0;
current_char_count = 0;
free(dest_file_name);
dest_file_name = get_chapter_file_name(buf, output_prefix, output_suffix);
fclose(f_out);
f_out = fopen(dest_file_name, "w");
if (!f_out) {
fprintf(stderr, "Failed to create file: %s", dest_file_name);
exit(3);
}
}
current_lines_count++;
current_words_count += num_words(buf);
current_char_count += my_strlen(buf);
fprintf(f_out, "%s", buf);
}
fclose(f_in);
fclose(f_out);
free(buf);
print_chapter_info(dest_file_name, current_lines_count, current_words_count, current_char_count);
free(dest_file_name);
total_lines_count += current_lines_count;
total_words_count += current_words_count;
total_char_count += current_char_count;
print_chapter_info("Total", total_lines_count, total_words_count, total_char_count);
}
#pragma clang diagnostic pop
void trim_and_replace_spaces(char *dest, const char *src, char char_replace) {
unsigned int dest_size = my_strlen(dest);
unsigned int src_size = my_strlen(src);
bool trimming_mode = true; //Trimming mode for the white spaces at the beginning
for (int i = src_size - 1; i >= 0; --i) {
char c = src[i];
if (!isspace(c)) {
trimming_mode = false;
dest[i + dest_size] = c;
continue;
}
if (!trimming_mode) {
dest[i + dest_size] = char_replace;
}
}
}
void print_chapter_info(const char *name, unsigned int lines_count, unsigned int words_count, unsigned int char_count) {
printf("%-30s: ", name);
printf("%d lines, ", lines_count);
printf("%d words, ", words_count);
printf("%d characters", char_count);
printf("\n");
}