kirill-refactor
This commit is contained in:
107
mine/lab_1/.old/12_first_symbols_to_spaces.cpp
Normal file
107
mine/lab_1/.old/12_first_symbols_to_spaces.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <climits>
|
||||
|
||||
static bool file_exists(const std::string &path) {
|
||||
struct stat st{};
|
||||
return ::stat(path.c_str(), &st) == 0; // stat success => exists [web:59]
|
||||
}
|
||||
|
||||
static std::string make_out_name(const std::string &in) {
|
||||
std::size_t pos = in.find_last_of('.');
|
||||
if (pos == std::string::npos) return in + ".out";
|
||||
return in.substr(0, pos) + ".out";
|
||||
}
|
||||
|
||||
static void print_usage(const char* prog) {
|
||||
std::cerr << "Usage: " << prog << " <input_file> <replace_count>\n"; // positional args [web:59]
|
||||
}
|
||||
|
||||
static bool parse_ll(const char* s, long long& out) {
|
||||
if (!s || !*s) return false;
|
||||
errno = 0;
|
||||
char* end = nullptr;
|
||||
long long v = std::strtoll(s, &end, 10); // use endptr and errno [web:59]
|
||||
if (errno == ERANGE) return false;
|
||||
if (end == s || *end != '\0') return false;
|
||||
out = v;
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 3) {
|
||||
print_usage(argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string in_path = argv[1];
|
||||
long long replace_limit = 0;
|
||||
if (!parse_ll(argv[2], replace_limit) || replace_limit < 0) {
|
||||
std::cerr << "Error: replace_count must be a non-negative integer.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!file_exists(in_path)) {
|
||||
std::cerr << "Error: input file does not exist.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::ifstream in(in_path);
|
||||
if (!in) {
|
||||
std::cerr << "Error: cannot open input file.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string out_path = make_out_name(in_path);
|
||||
std::ofstream out(out_path);
|
||||
if (!out) {
|
||||
std::cerr << "Error: cannot create output file.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
long long performed = 0;
|
||||
std::string line;
|
||||
|
||||
while (performed < replace_limit && std::getline(in, line)) { // stop early when limit hit [web:59]
|
||||
if (!line.empty()) {
|
||||
char first = line.front();
|
||||
for (char &c: line) {
|
||||
if (c == first && c != ' ') {
|
||||
c = ' ';
|
||||
++performed;
|
||||
if (performed >= replace_limit) break; // cap reached [web:44]
|
||||
}
|
||||
}
|
||||
}
|
||||
out << line;
|
||||
if (!in.eof()) out << '\n';
|
||||
if (!out) {
|
||||
std::cerr << "Error: write error.\n";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// If limit reached mid-file, copy the rest unchanged
|
||||
if (performed >= replace_limit) { // stream may be mid-line or between lines [web:59]
|
||||
// Write remaining lines as-is
|
||||
if (!in.eof()) {
|
||||
// Flush any remaining part of current line already written; then dump rest
|
||||
std::string rest;
|
||||
while (std::getline(in, rest)) {
|
||||
out << rest;
|
||||
if (!in.eof()) out << '\n';
|
||||
if (!out) {
|
||||
std::cerr << "Error: write error.\n";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << performed << std::endl; // print actual replacements up to limit [web:59]
|
||||
return 0; // success [web:59]
|
||||
}
|
||||
1
mine/lab_1/.old/compile.sh
Executable file
1
mine/lab_1/.old/compile.sh
Executable file
@@ -0,0 +1 @@
|
||||
g++ -std=c++17 -O2 -Wall -Wextra 12_first_symbols_to_spaces.cpp -o 12_first_symbols_to_spaces
|
||||
21
mine/lab_1/.old/email
Normal file
21
mine/lab_1/.old/email
Normal file
@@ -0,0 +1,21 @@
|
||||
Здравствуйте, Андрей Васильевич!
|
||||
|
||||
TLDR:
|
||||
ssh pajjilykk@cs.webmaple.ru
|
||||
cd /home/pajjilykk/CS_LABS/lab_1
|
||||
cat README.txt
|
||||
|
||||
Сообщаю, что лабораторная работа №1 по программированию выполнена и размещена на сервере. Данные для доступа и проверки:
|
||||
|
||||
Адрес сервера: cs.webmaple.ru
|
||||
|
||||
Логин: pajjilykk
|
||||
|
||||
Пароль: NSTU.CS
|
||||
|
||||
Путь к коду: /home/pajjilykk/CS_LABS/lab_1
|
||||
|
||||
Готов ответить на вопросы по коду и/или защите.
|
||||
|
||||
С уважением,
|
||||
Куриленко Платон, группа АВТ-418
|
||||
8
mine/lab_1/.old/file.in
Normal file
8
mine/lab_1/.old/file.in
Normal file
@@ -0,0 +1,8 @@
|
||||
abcabcABC
|
||||
Aaaaaa
|
||||
1112233111
|
||||
,,,,.,,
|
||||
leading spaces here
|
||||
tab leading here
|
||||
first-letter f repeats: f fff f-f_f
|
||||
ZzzZzZ
|
||||
1
mine/lab_1/.old/run.sh
Executable file
1
mine/lab_1/.old/run.sh
Executable file
@@ -0,0 +1 @@
|
||||
./12_first_symbols_to_spaces file.in 3
|
||||
13
mine/lab_1/Makefile
Normal file
13
mine/lab_1/Makefile
Normal file
@@ -0,0 +1,13 @@
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -O2
|
||||
|
||||
TARGET = task12
|
||||
SRC = task12.c
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(SRC)
|
||||
$(CC) $(CFLAGS) -o $(TARGET) $(SRC)
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET) out.txt
|
||||
53
mine/lab_1/examples/copy.c
Normal file
53
mine/lab_1/examples/copy.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#define BUF_SIZE 1024
|
||||
// пример программы обработки текстового файла средствами системых вызовов Linux
|
||||
// учебник "Системное программирование в среде Linux", Гунько А.В., стр. 22
|
||||
int main (int argc, char * argv [ ])
|
||||
{
|
||||
int inputFd, outputFd, openFlags;
|
||||
mode_t filePerms ;
|
||||
ssize_t numRead;
|
||||
char buf[BUF_SIZE];
|
||||
if (argc != 3)
|
||||
{
|
||||
printf("Usage: %s old-file new-file \n", argv[0]); exit(-1);
|
||||
}
|
||||
/* Открытие файлов ввода и вывода */
|
||||
inputFd = open (argv[1], O_RDONLY);
|
||||
if (inputFd == -1)
|
||||
{
|
||||
printf ("Error opening file %s\n", argv[1]) ; exit(-2);
|
||||
}
|
||||
openFlags = O_CREAT | O_WRONLY | O_TRUNC;
|
||||
filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /* rw - rw - rw - */
|
||||
outputFd = open (argv [2], openFlags, filePerms);
|
||||
if (outputFd == -1)
|
||||
{
|
||||
printf ("Error opening file %s\n ", argv[2]) ; exit(-3);
|
||||
}
|
||||
/* Перемещение данных до достижения конца файла ввода или возникновения ошибки */
|
||||
while ((numRead = read (inputFd, buf, BUF_SIZE)) > 0)
|
||||
{
|
||||
if (write (outputFd, buf, numRead) != numRead)
|
||||
{
|
||||
printf ("couldn't write whole buffer\n "); exit(-4);
|
||||
}
|
||||
if (numRead == -1)
|
||||
{
|
||||
printf ("read error\n "); exit(-5);
|
||||
}
|
||||
if (close (inputFd ) == -1 )
|
||||
{
|
||||
printf ("close input error\n"); exit(-6);
|
||||
}
|
||||
if (close (outputFd ) == -1 )
|
||||
{
|
||||
printf ("close output error\n"); exit(-7);
|
||||
}
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
32
mine/lab_1/examples/file.c
Normal file
32
mine/lab_1/examples/file.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
// пример программы обработки текстового файла средствами системых вызовов Linux
|
||||
// учебник "Системное программирование в среде Linux", Гунько А.В.
|
||||
int main(int argc, char *argv[]) {
|
||||
int handle, numRead, total= 0;
|
||||
char buf;
|
||||
if (argc<2) {
|
||||
printf("Usage: file textfile\n");
|
||||
exit(-1);
|
||||
}
|
||||
// низкоуровневое открытие файла на чтение
|
||||
handle = open( argv[1], O_RDONLY);
|
||||
if (handle<0) {
|
||||
printf("Error %d (%s) while open file: %s!\n",errno, strerror(errno),argv[1]);
|
||||
exit(-2);
|
||||
}
|
||||
// цикл до конца файла
|
||||
do {
|
||||
// посимвольное чтение из файла
|
||||
numRead = read( handle, &buf, 1);
|
||||
if (buf == 0x20) total++; }
|
||||
while (numRead > 0);
|
||||
// Закрытие файла
|
||||
close( handle);
|
||||
printf("(PID: %d), File %s, spaces = %d\n", getpid(), argv[1], total);
|
||||
// возвращаемое программой значение
|
||||
return( total); }
|
||||
5
mine/lab_1/ilya`s/file.in
Normal file
5
mine/lab_1/ilya`s/file.in
Normal file
@@ -0,0 +1,5 @@
|
||||
Hi! This is a test.
|
||||
Wait: are commas, periods, and exclamations replaced?
|
||||
Let's check!
|
||||
@#$%&*)_+
|
||||
|
||||
6
mine/lab_1/ilya`s/file.out
Normal file
6
mine/lab_1/ilya`s/file.out
Normal file
@@ -0,0 +1,6 @@
|
||||
Hi9 This is a test9
|
||||
Wait9 are commas9 periods9 and exclamations replaced9
|
||||
Let's check9
|
||||
@#$%&*)_+
|
||||
|
||||
|
||||
BIN
mine/lab_1/ilya`s/signs
Executable file
BIN
mine/lab_1/ilya`s/signs
Executable file
Binary file not shown.
76
mine/lab_1/ilya`s/signs.cpp
Normal file
76
mine/lab_1/ilya`s/signs.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// ???????? ????????????? ?????
|
||||
bool fileExists(const string& filename) {
|
||||
struct stat buffer;
|
||||
return (stat(filename.c_str(), &buffer) == 0);
|
||||
}
|
||||
|
||||
// ????????? ????? ????????? ????? ? ??????? ?????????? ?? ".out"
|
||||
string getOutputFileName(const string& inputFileName) {
|
||||
size_t pos = inputFileName.find_last_of('.');
|
||||
if (pos == string::npos) {
|
||||
// ???? ?????????? ???, ????????? .out
|
||||
return inputFileName + ".out";
|
||||
}
|
||||
return inputFileName.substr(0, pos) + ".out"; // ???????? ??????????
|
||||
}
|
||||
|
||||
// ????????, ???????? ?? ?????? ?????? ??????????, ?????????? ??????
|
||||
bool isSign(char c) {
|
||||
return (c == '!' || c == '?' || c == '.' || c == ',' || c == ';' || c == ':');
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 3) {
|
||||
cerr << "Error: Missing arguments.\nUsage: " << argv[0] << " <inputfile> <symbol>\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
string inputFileName = argv[1];
|
||||
char replacementSymbol = argv[2][0];
|
||||
|
||||
if (!fileExists(inputFileName)) {
|
||||
cerr << "Error: Input file \"" << inputFileName << "\" does not exist.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
string outputFileName = getOutputFileName(inputFileName);
|
||||
|
||||
ifstream inFile(inputFileName);
|
||||
if (!inFile) {
|
||||
cerr << "Error: Cannot open input file.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
ofstream outFile(outputFileName);
|
||||
if (!outFile) {
|
||||
cerr << "Error: Cannot create output file.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
int replaceCount = 0;
|
||||
char ch;
|
||||
while (inFile.get(ch)) {
|
||||
if (isSign(ch)) {
|
||||
ch = replacementSymbol;
|
||||
replaceCount++;
|
||||
}
|
||||
outFile.put(ch);
|
||||
if (!outFile) {
|
||||
cerr << "Error: Write error occurred.\n";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
inFile.close();
|
||||
outFile.close();
|
||||
|
||||
cout << replaceCount << endl; // ??????? ????? ?????
|
||||
return replaceCount;
|
||||
}
|
||||
5
mine/lab_1/in.txt
Normal file
5
mine/lab_1/in.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
abbaabbaabbaabbaabbaabbaabbaabba
|
||||
xyzx
|
||||
..
|
||||
.. ./.
|
||||
----
|
||||
4
mine/lab_1/kirill`s/in.txt
Normal file
4
mine/lab_1/kirill`s/in.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
123456123456123456123456123456123456123456123456
|
||||
123456123456
|
||||
123456123456
|
||||
123456123456
|
||||
129
mine/lab_1/kirill`s/task18.c
Normal file
129
mine/lab_1/kirill`s/task18.c
Normal file
@@ -0,0 +1,129 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define RBUFSZ 4096
|
||||
#define WBUFSZ 4096
|
||||
|
||||
static void die_perror(const char *what, const char *path, int exit_code) {
|
||||
int saved = errno;
|
||||
char msg[512];
|
||||
int n;
|
||||
if (path) {
|
||||
n = snprintf(msg, sizeof(msg), "%s: %s: %s\n", what, path, strerror(saved));
|
||||
} else {
|
||||
n = snprintf(msg, sizeof(msg), "%s: %s\n", what, strerror(saved));
|
||||
}
|
||||
if (n > 0) (void) write(STDERR_FILENO, msg, (size_t) n);
|
||||
_exit(exit_code);
|
||||
}
|
||||
|
||||
static void xwrite_all(int fd, const char *buf, size_t len, const char *path) {
|
||||
size_t off = 0;
|
||||
while (off < len) {
|
||||
ssize_t n = write(fd, buf + off, len - off);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
die_perror("write failed", path, -1);
|
||||
}
|
||||
off += (size_t) n;
|
||||
}
|
||||
}
|
||||
|
||||
static long long parse_ll(const char *s) {
|
||||
char *end = NULL;
|
||||
errno = 0;
|
||||
long long v = strtoll(s, &end, 10);
|
||||
if (errno != 0 || end == s || *end != '\0' || v < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 4) {
|
||||
const char *usage =
|
||||
"Usage: lab1_var_thirds_line <input.txt> <output.txt> <max_replacements>\n"
|
||||
"Replace every third non-newline byte in each line with a space; counter resets after LF.\n";
|
||||
(void) write(STDERR_FILENO, usage, strlen(usage));
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *in_path = argv[1];
|
||||
const char *out_path = argv[2];
|
||||
long long cap = parse_ll(argv[3]);
|
||||
if (cap < 0) die_perror("invalid max_replacements", argv[3], -1);
|
||||
|
||||
int in_fd = open(in_path, O_RDONLY);
|
||||
if (in_fd < 0) die_perror("open input failed", in_path, -1);
|
||||
|
||||
mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
|
||||
int out_fd = open(out_path, O_CREAT | O_WRONLY | O_TRUNC, perms);
|
||||
if (out_fd < 0) die_perror("open output failed", out_path, -1);
|
||||
|
||||
char rbuf[RBUFSZ];
|
||||
char wbuf[WBUFSZ];
|
||||
size_t wlen = 0;
|
||||
|
||||
long long total = 0;
|
||||
long long col = 0;
|
||||
int replacing_enabled = 1;
|
||||
|
||||
for (;;) {
|
||||
ssize_t n = read(in_fd, rbuf, sizeof(rbuf));
|
||||
if (n > 0) {
|
||||
for (ssize_t i = 0; i < n; i++) {
|
||||
unsigned char c = (unsigned char) rbuf[i];
|
||||
|
||||
if (c == '\n') {
|
||||
if (wlen == sizeof(wbuf)) {
|
||||
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||
wlen = 0;
|
||||
}
|
||||
wbuf[wlen++] = '\n';
|
||||
col = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
unsigned char outc = c;
|
||||
|
||||
col++;
|
||||
|
||||
if (replacing_enabled && (col % 3 == 0) && total < cap) {
|
||||
outc = ' ';
|
||||
total++;
|
||||
if (total == cap) replacing_enabled = 0;
|
||||
}
|
||||
|
||||
if (wlen == sizeof(wbuf)) {
|
||||
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||
wlen = 0;
|
||||
}
|
||||
wbuf[wlen++] = (char) outc;
|
||||
}
|
||||
} else if (n == 0) {
|
||||
if (wlen > 0) {
|
||||
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||
wlen = 0;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
if (errno == EINTR) continue;
|
||||
die_perror("read failed", in_path, -1);
|
||||
}
|
||||
}
|
||||
|
||||
if (close(in_fd) < 0) die_perror("close input failed", in_path, -1);
|
||||
if (close(out_fd) < 0) die_perror("close output failed", out_path, -1);
|
||||
|
||||
char res[64];
|
||||
int m = snprintf(res, sizeof(res), "%lld\n", total);
|
||||
if (m > 0) (void) write(STDOUT_FILENO, res, (size_t) m);
|
||||
|
||||
return 0;
|
||||
}
|
||||
135
mine/lab_1/task12.c
Normal file
135
mine/lab_1/task12.c
Normal file
@@ -0,0 +1,135 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define RBUFSZ 4096
|
||||
#define WBUFSZ 4096
|
||||
|
||||
static void die_perror(const char *what, const char *path, int exit_code) {
|
||||
int saved = errno;
|
||||
char msg[512];
|
||||
if (path) {
|
||||
snprintf(msg, sizeof(msg), "%s: %s: %s\n", what, path, strerror(saved));
|
||||
} else {
|
||||
snprintf(msg, sizeof(msg), "%s: %s\n", what, strerror(saved));
|
||||
}
|
||||
(void) write(STDERR_FILENO, msg, strlen(msg));
|
||||
_exit(exit_code);
|
||||
}
|
||||
|
||||
static long long parse_ll(const char *s) {
|
||||
char *end = NULL;
|
||||
errno = 0;
|
||||
long long v = strtoll(s, &end, 10);
|
||||
if (errno != 0 || end == s || *end != '\0' || v < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 4) {
|
||||
const char *usage =
|
||||
"Usage: lab1_var12 <input.txt> <output.txt> <max_replacements>\n"
|
||||
"Variant 12: per line, take the first character as key and replace its matches with spaces,\n"
|
||||
" stopping after <max_replacements> replacements globally.\n";
|
||||
(void) write(STDERR_FILENO, usage, strlen(usage));
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *in_path = argv[1];
|
||||
const char *out_path = argv[2];
|
||||
long long cap = parse_ll(argv[3]);
|
||||
if (cap < 0) {
|
||||
die_perror("invalid max_replacements", argv[3], -1);
|
||||
}
|
||||
|
||||
int in_fd = open(in_path, O_RDONLY);
|
||||
if (in_fd < 0) {
|
||||
die_perror("open input failed", in_path, -1);
|
||||
}
|
||||
|
||||
mode_t filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /* rw-rw-rw- */
|
||||
int out_fd = open(out_path, O_CREAT | O_WRONLY | O_TRUNC, filePerms);
|
||||
if (out_fd < 0) {
|
||||
die_perror("open output failed", out_path, -1);
|
||||
}
|
||||
|
||||
char rbuf[RBUFSZ];
|
||||
char wbuf[WBUFSZ];
|
||||
size_t wlen = 0;
|
||||
|
||||
long long total_replacements = 0;
|
||||
int at_line_start = 1;
|
||||
unsigned char line_key = 0;
|
||||
int replacing_enabled = 1; /* flips to 0 when cap reached */
|
||||
|
||||
for (;;) {
|
||||
ssize_t n = read(in_fd, rbuf, sizeof(rbuf));
|
||||
if (n > 0) {
|
||||
for (ssize_t i = 0; i < n; i++) {
|
||||
unsigned char c = (unsigned char) rbuf[i];
|
||||
|
||||
if (at_line_start) {
|
||||
line_key = c;
|
||||
at_line_start = 0;
|
||||
}
|
||||
|
||||
unsigned char outc = c;
|
||||
if (c == '\n') {
|
||||
at_line_start = 1;
|
||||
} else if (replacing_enabled && c == line_key) {
|
||||
if (total_replacements < cap) {
|
||||
outc = ' ';
|
||||
total_replacements++;
|
||||
if (total_replacements == cap) {
|
||||
replacing_enabled = 0; /* hit the cap; from now on just copy */
|
||||
}
|
||||
} else {
|
||||
replacing_enabled = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (wlen == sizeof(wbuf)) {
|
||||
ssize_t wrote = write(out_fd, wbuf, wlen);
|
||||
if (wrote < 0 || (size_t) wrote != wlen) {
|
||||
die_perror("write failed", out_path, -1);
|
||||
}
|
||||
wlen = 0;
|
||||
}
|
||||
wbuf[wlen++] = (char) outc;
|
||||
}
|
||||
} else if (n == 0) {
|
||||
if (wlen > 0) {
|
||||
ssize_t wrote = write(out_fd, wbuf, wlen);
|
||||
if (wrote < 0 || (size_t) wrote != wlen) {
|
||||
die_perror("write failed", out_path, -1);
|
||||
}
|
||||
wlen = 0;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
die_perror("read failed", in_path, -1);
|
||||
}
|
||||
}
|
||||
|
||||
if (close(in_fd) < 0) {
|
||||
die_perror("close input failed", in_path, -1);
|
||||
}
|
||||
if (close(out_fd) < 0) {
|
||||
die_perror("close output failed", out_path, -1);
|
||||
}
|
||||
|
||||
char res[64];
|
||||
int m = snprintf(res, sizeof(res), "%lld\n", total_replacements);
|
||||
if (m > 0) {
|
||||
(void) write(STDOUT_FILENO, res, (size_t) m);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
8
mine/lab_1/vlad`s/in.txt
Normal file
8
mine/lab_1/vlad`s/in.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
aaaaaa
|
||||
aaaaaaaaaaaa
|
||||
aaaaaaaaaaaa
|
||||
aaaaaaaaaaaa
|
||||
2
|
||||
aaaaaaaaaaaa
|
||||
aaaaaaaaaaaa
|
||||
aaaaaa
|
||||
170
mine/lab_1/vlad`s/task11.c
Normal file
170
mine/lab_1/vlad`s/task11.c
Normal file
@@ -0,0 +1,170 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define RBUFSZ 4096
|
||||
#define WBUFSZ 4096
|
||||
|
||||
static void die_perror(const char *what, const char *path, int exit_code) {
|
||||
int saved = errno;
|
||||
char msg[512];
|
||||
int n;
|
||||
if (path) {
|
||||
n = snprintf(msg, sizeof(msg), "%s: %s: %s\n", what, path, strerror(saved));
|
||||
} else {
|
||||
n = snprintf(msg, sizeof(msg), "%s: %s\n", what, strerror(saved));
|
||||
}
|
||||
if (n > 0) (void) write(STDERR_FILENO, msg, (size_t) n);
|
||||
_exit(exit_code);
|
||||
}
|
||||
|
||||
static long long parse_ll(const char *s) {
|
||||
char *end = NULL;
|
||||
errno = 0;
|
||||
long long v = strtoll(s, &end, 10);
|
||||
if (errno != 0 || end == s || *end != '\0' || v < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static void xwrite_all(int fd, const char *buf, size_t len, const char *path) {
|
||||
size_t off = 0;
|
||||
while (off < len) {
|
||||
ssize_t n = write(fd, buf + off, len - off);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
die_perror("write failed", path, -1);
|
||||
}
|
||||
off += (size_t) n;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 4) {
|
||||
const char *usage =
|
||||
"Usage: lab1_var11 <input.txt> <output.txt> <max_replacements>\n"
|
||||
"Variant 11: for every pair of identical consecutive characters, replace the second with a space.\n"
|
||||
" Stop after <max_replacements> replacements globally.\n";
|
||||
(void) write(STDERR_FILENO, usage, strlen(usage));
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *in_path = argv[1];
|
||||
const char *out_path = argv[2];
|
||||
long long cap = parse_ll(argv[3]);
|
||||
if (cap < 0) {
|
||||
die_perror("invalid max_replacements", argv[3], -1);
|
||||
}
|
||||
|
||||
int in_fd = open(in_path, O_RDONLY);
|
||||
if (in_fd < 0) {
|
||||
die_perror("open input failed", in_path, -1);
|
||||
}
|
||||
|
||||
mode_t filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
|
||||
int out_fd = open(out_path, O_CREAT | O_WRONLY | O_TRUNC, filePerms);
|
||||
if (out_fd < 0) {
|
||||
die_perror("open output failed", out_path, -1);
|
||||
}
|
||||
|
||||
char rbuf[RBUFSZ];
|
||||
char wbuf[WBUFSZ];
|
||||
size_t wlen = 0;
|
||||
|
||||
long long total_replacements = 0;
|
||||
int replacing_enabled = 1;
|
||||
|
||||
int have_prev = 0;
|
||||
unsigned char prev = 0;
|
||||
|
||||
for (;;) {
|
||||
ssize_t n = read(in_fd, rbuf, sizeof(rbuf));
|
||||
if (n > 0) {
|
||||
for (ssize_t i = 0; i < n; i++) {
|
||||
unsigned char c = (unsigned char) rbuf[i];
|
||||
|
||||
if (!have_prev) {
|
||||
prev = c;
|
||||
have_prev = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == prev) {
|
||||
unsigned char out1 = prev;
|
||||
unsigned char out2 = c;
|
||||
|
||||
if (replacing_enabled && total_replacements < cap) {
|
||||
out2 = ' ';
|
||||
total_replacements++;
|
||||
if (total_replacements == cap) {
|
||||
replacing_enabled = 0;
|
||||
}
|
||||
} else {
|
||||
replacing_enabled = 0;
|
||||
}
|
||||
|
||||
if (wlen == sizeof(wbuf)) {
|
||||
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||
wlen = 0;
|
||||
}
|
||||
wbuf[wlen++] = (char) out1;
|
||||
|
||||
if (wlen == sizeof(wbuf)) {
|
||||
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||
wlen = 0;
|
||||
}
|
||||
wbuf[wlen++] = (char) out2;
|
||||
|
||||
have_prev = 0;
|
||||
} else {
|
||||
if (wlen == sizeof(wbuf)) {
|
||||
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||
wlen = 0;
|
||||
}
|
||||
wbuf[wlen++] = (char) prev;
|
||||
|
||||
prev = c;
|
||||
have_prev = 1;
|
||||
}
|
||||
}
|
||||
} else if (n == 0) {
|
||||
if (have_prev) {
|
||||
if (wlen == sizeof(wbuf)) {
|
||||
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||
wlen = 0;
|
||||
}
|
||||
wbuf[wlen++] = (char) prev;
|
||||
have_prev = 0;
|
||||
}
|
||||
if (wlen > 0) {
|
||||
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||
wlen = 0;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
if (errno == EINTR) continue;
|
||||
die_perror("read failed", in_path, -1);
|
||||
}
|
||||
}
|
||||
|
||||
if (close(in_fd) < 0) {
|
||||
die_perror("close input failed", in_path, -1);
|
||||
}
|
||||
if (close(out_fd) < 0) {
|
||||
die_perror("close output failed", out_path, -1);
|
||||
}
|
||||
|
||||
char res[64];
|
||||
int m = snprintf(res, sizeof(res), "%lld\n", total_replacements);
|
||||
if (m > 0) {
|
||||
(void) write(STDOUT_FILENO, res, (size_t) m);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user