working kirill's UNTESTED NEWLINE
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
g++ task18.cpp -o task18
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
abcdef
|
|
||||||
abcdefg
|
|
||||||
abcdefghij
|
|
||||||
abc def
|
|
||||||
x
|
|
||||||
12
|
|
||||||
123
|
|
||||||
(three spaces)
|
|
||||||
abc
|
|
||||||
ab cs s dds
|
|
||||||
\tabcde\tf
|
|
||||||
2
others/kirill`s/lab1/in.txt
Normal file
2
others/kirill`s/lab1/in.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
123456
|
||||||
|
123456
|
||||||
@@ -1,96 +1,123 @@
|
|||||||
#include <iostream>
|
#include <unistd.h>
|
||||||
#include <fstream>
|
|
||||||
#include <string>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <cstdlib>
|
#include <fcntl.h>
|
||||||
#include <cerrno>
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
static bool file_exists(const std::string &path) {
|
#define RBUFSZ 4096
|
||||||
struct stat st{};
|
#define WBUFSZ 4096
|
||||||
return ::stat(path.c_str(), &st) == 0;
|
|
||||||
|
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 std::string make_out_name(const std::string &in) {
|
static long long parse_ll(const char *s) {
|
||||||
std::size_t pos = in.find_last_of('.');
|
char *end = NULL;
|
||||||
if (pos == std::string::npos) return in + ".out";
|
|
||||||
return in.substr(0, pos) + ".out";
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool parse_nonneg_ll(const char *s, long long &out) {
|
|
||||||
if (!s || *s == '\0') return false;
|
|
||||||
char *end = nullptr;
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
long long v = std::strtoll(s, &end, 10);
|
long long v = strtoll(s, &end, 10);
|
||||||
if (errno != 0 || end == s || *end != '\0') return false;
|
if (errno != 0 || end == s || *end != '\0' || v < 0) {
|
||||||
if (v < 0) return false;
|
errno = EINVAL;
|
||||||
out = v;
|
return -1;
|
||||||
return true;
|
}
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc != 3) {
|
if (argc != 4) {
|
||||||
std::cerr << "Usage: " << argv[0] << " input_file replace_count\n";
|
const char *usage =
|
||||||
|
"Usage: lab1_var_thirds_line <input.txt> <output.txt> <max_replacements>\n"
|
||||||
|
"Replace every third byte in each line with a space; counter resets after each newline.\n";
|
||||||
|
(void) write(STDERR_FILENO, usage, strlen(usage));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string in_path = argv[1];
|
const char *in_path = argv[1];
|
||||||
long long target = 0;
|
const char *out_path = argv[2];
|
||||||
if (!parse_nonneg_ll(argv[2], target)) {
|
long long cap = parse_ll(argv[3]);
|
||||||
std::cerr << "Usage: " << argv[0] << " input_file replace_count\n";
|
if (cap < 0) die_perror("invalid max_replacements", argv[3], -1);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!file_exists(in_path)) {
|
int in_fd = open(in_path, O_RDONLY);
|
||||||
std::cerr << "Error: input file does not exist.\n";
|
if (in_fd < 0) die_perror("open input failed", in_path, -1);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ifstream in(in_path);
|
mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /* 0666 */
|
||||||
if (!in) {
|
int out_fd = open(out_path, O_CREAT | O_WRONLY | O_TRUNC, perms);
|
||||||
std::cerr << "Error: cannot open input file.\n";
|
if (out_fd < 0) die_perror("open output failed", out_path, -1);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string out_path = make_out_name(in_path);
|
char rbuf[RBUFSZ];
|
||||||
std::ofstream out(out_path);
|
char wbuf[WBUFSZ];
|
||||||
if (!out) {
|
size_t wlen = 0;
|
||||||
std::cerr << "Error: cannot create output file.\n";
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
long long replacements = 0;
|
long long total = 0;
|
||||||
std::string line;
|
long long col = 0; /* position in current line starting from 1 */
|
||||||
|
int replacing_enabled = 1;
|
||||||
|
|
||||||
while (std::getline(in, line)) {
|
for (;;) {
|
||||||
if (!line.empty() && replacements < target) {
|
ssize_t n = read(in_fd, rbuf, sizeof(rbuf));
|
||||||
for (std::size_t idx = 0; idx < line.size() && replacements < target; ++idx) {
|
if (n > 0) {
|
||||||
std::size_t pos1 = idx + 1;
|
for (ssize_t i = 0; i < n; i++) {
|
||||||
if ((pos1 % 3) == 0) {
|
unsigned char c = (unsigned char) rbuf[i];
|
||||||
if (line[idx] != ' ') {
|
|
||||||
line[idx] = ' ';
|
if (c == '\n') {
|
||||||
++replacements;
|
/* newline is written as-is and resets counter */
|
||||||
|
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++] = '\n';
|
||||||
|
col = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
col++; /* advance within the current line */
|
||||||
|
|
||||||
|
unsigned char outc = c;
|
||||||
|
if (replacing_enabled && (col % 3 == 0)) {
|
||||||
|
if (total < cap) {
|
||||||
|
outc = ' ';
|
||||||
|
total++;
|
||||||
|
if (total == cap) replacing_enabled = 0;
|
||||||
|
} 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) {
|
||||||
out << line;
|
if (wlen > 0) {
|
||||||
if (!in.eof()) out << '\n';
|
ssize_t wrote = write(out_fd, wbuf, wlen);
|
||||||
if (!out) {
|
if (wrote < 0 || (size_t) wrote != wlen) die_perror("write failed", out_path, -1);
|
||||||
std::cerr << "Error: write error.\n";
|
wlen = 0;
|
||||||
return -1;
|
}
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
die_perror("read failed", in_path, -1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!in.eof() && in.fail()) {
|
if (close(in_fd) < 0) die_perror("close input failed", in_path, -1);
|
||||||
std::cerr << "Error: read error.\n";
|
if (close(out_fd) < 0) die_perror("close output failed", out_path, -1);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (replacements != target) {
|
char res[64];
|
||||||
std::cerr << "Error: could not perform exactly requested replacements.\n";
|
int m = snprintf(res, sizeof(res), "%lld\n", total);
|
||||||
return -1;
|
if (m > 0) (void) write(STDOUT_FILENO, res, (size_t) m);
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << replacements << std::endl;
|
int exit_code = (int) (total & 0xFF);
|
||||||
return 0;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
aaaaabababbabababaaabbbbabababbbaabbaabbabb
|
aaaaaa
|
||||||
|
aaaaaa
|
||||||
Reference in New Issue
Block a user