OAIP/lab24/main.c

127 lines
2.4 KiB
C
Raw Normal View History

2024-11-22 19:46:24 +04:00
#define _CRT_SECURE_NO_WARNINGS
2024-11-22 17:26:41 +04:00
#include <stdio.h>
#include <ctype.h>
2024-11-22 19:46:24 +04:00
#include <string.h>
#include "../string_my/string_my.h"
2024-11-22 19:46:24 +04:00
#define BUFLEN 128
2024-11-22 17:26:41 +04:00
void task1() {
FILE* fin = fopen("files/text1.txt", "r");
if (fin == NULL) {
puts("File cannot be opened");
2024-11-22 19:46:24 +04:00
return;
2024-11-22 17:26:41 +04:00
}
2024-11-22 19:46:24 +04:00
FILE* fout = fopen("files/out1.html", "w");
2024-11-22 17:26:41 +04:00
if (fin == NULL) {
puts("File cannot be opened");
2024-11-22 19:46:24 +04:00
return;
2024-11-22 17:26:41 +04:00
}
fprintf(fout, "<!DOCTYPE html>");
fprintf(fout, "<html>");
fprintf(fout, "<head>");
fprintf(fout, "<meta http - equiv = \"Content-Type\" content = \"text/html; charset=utf-8\" />");
fprintf(fout, "<title>HTML Document</title>");
fprintf(fout, "</head>");
fprintf(fout, "<body>");
char ch;
while ((ch = getc(fin)) != EOF) {
if (ch == '\n') {
fprintf(fout, "<br>");
}
else {
putc(ch, fout);
}
}
fprintf(fout, "</body>");
fprintf(fout, "</html>");
2024-11-22 19:46:24 +04:00
fclose(fin);
fclose(fout);
2024-11-22 17:26:41 +04:00
}
2024-11-22 19:46:24 +04:00
void task2() {
FILE* fin = fopen("files/text2.txt", "r");
if (fin == NULL) {
puts("File cannot be opened");
return;
}
FILE* fout = fopen("files/out2.html", "w");
if (fout == NULL) {
puts("File cannot be opened");
return;
}
fprintf(fout, "<!DOCTYPE html>");
fprintf(fout, "<html>");
fprintf(fout, "<head>");
fprintf(fout, "<meta http - equiv = \"Content-Type\" content = \"text/html; charset=utf-8\" />");
fprintf(fout, "<title>HTML Document</title>");
fprintf(fout, "</head>");
fprintf(fout, "<body>");
char buf[BUFLEN];
char tmp[BUFLEN];
int ch;
while (feof(fin) == 0 && ferror(fin) == 0) {
while (getNextDelim(fin, &ch)) {
if (ch == '\n') {
fprintf(fout, "<br>");
}
else {
fprintf(fout, "%c", ch);
}
}
if (getNextWord(fin, buf, BUFLEN)) {
int isbold = 0;
int isitalic = 0;
int len = (int)strlen(buf);
if (len >= 2 && buf[len-2] == buf[len-1]) {
isbold = 1;
}
int count = 0;
for (int i = 0; i < len; i++) {
if (isupper(buf[i])) {
count += 1;
}
}
if (count == len) {
isitalic = 1;
}
if (isbold) {
strcpy(tmp, buf);
sprintf(buf, "<b>%s</b>", tmp);
}
if (isitalic) {
strcpy(tmp, buf);
sprintf(buf, "<i>%s</i>", tmp);
}
fprintf(fout, "%s", buf);
}
}
fprintf(fout, "</body>");
fprintf(fout, "</html>");
fclose(fin);
fclose(fout);
}
2024-11-22 17:26:41 +04:00
int main() {
2024-11-22 19:46:24 +04:00
//task1();
task2();
2024-11-22 17:26:41 +04:00
return 0;
}