const
bufsize=161024;
var
s2:string;
f1,f2:file;
d:searchrec;
nr,nw,i,j:u_val;
bu:array[0..bufsize-1]of byte;
bo:array[0..bufsize2-1]of byte;
function posplus(i:integer;c:char;s:string):integer;
begin
while i<=length(s) do begin
result:=i+1;
if s[i]=c then exit;
inc(i);
end;
result:=-1;
end;
var
op,p:integer;
curr:string;
begin
if (paramcount<1) then error('Usage: crlf [files...]');
randomize;
repeat
s2:='~';for i:=1 to 7 do s2:=s2+mktchar;
s2:=s2+'.~99';
doserror:=FindFirst(s2,flag,D);
until doserror<>0;
for i:=1 to paramcount do begin
doserror:=FindFirst(paramstr(i),flag,D);
while (DosError = 0) do begin
if (paramstr(0)<>fexpand(d.name)) then begin
add(d.name);
end else begin
write(d.name);
write(str_eself);
end;
doserror:=FindNext(D);
end;
p:=posplus(0,'|',all);
op:=1;
while p<>-1 do begin
curr:=copy(all,op,p-op-1);
op:=p;
write(str_conv+curr);
assign(f1,curr);
assign(f2,s2);
reset(f1,1);
if ioresult<>0 then error(str_eopen+curr);
rewrite(f2,1);
if ioresult<>0 then error(str_eopen+s2);
nr:=1;
while not(eof(f1)) do begin
blockread(f1,bu,sizeof(bu), nr);
nw:=0;
if nr>0 then for j:=0 to nr-1 do begin
case bu[j] of
10:begin bo[nw]:=13;inc(nw);bo[nw]:=bu[j];inc(nw);end;
13:;
else begin bo[nw]:=bu[j];inc(nw);end;
end;
end;
blockwrite(f2,bo,nw);
end;
close(f1);
erase(f1);
close(f2);
rename(f2,curr);
if ioresult<>0 then error(str_ewrite);
write(str_done);
p:=posplus(p,'|',all);
end;
end;
write(str_alldone);
end.
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <sys/types.h>
#include <dirent.h>
const int flag = FILE_ATTRIBUTE_ANY - FILE_ATTRIBUTE_DIRECTORY;
const std::string str_done = " ...OK\n";
const std::string str_alldone = "--- Hotovo ---\n";
const std::string str_conv = "Zpracovavam:";
const std::string str_eopen = "\nNelze otevrit soubor: ";
const std::string str_ewrite = "Chyba pri zapisu dat.\n";
const std::string str_eself = ": Nemohu prevest sam sebe.\n";
const int ent = 13;
const int bufsize = 16 * 1024;
void error(const std::string& s) {
std::cout << s;
exit(1);
}
char mktchar() {
return static_cast<char>(rand() % 25 + 'a');
}
std::string all;
bool add(const std::string& s) {
all += s + "|";
return true;
}
int posplus(int i, char c, const std::string& s) {
while (i <= s.length()) {
if (s[i] == c) return i + 1;
++i;
}
return -1;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
error("Usage: crlf [files...]");
}
srand(time(0));
std::string s2("~");
for (int i = 1; i <= 7; ++i) {
s2 += mktchar();
}
s2 += ".~99";
std::vector<std::string> files;
for (int i = 1; i < argc; ++i) {
files.push_back(argv[i]);
}
for (const auto& file : files) {
std::string currFile = file;
std::string searchName = "~";
for (int i = 1; i <= 7; ++i) {
searchName += mktchar();
}
searchName += ".~99";
HANDLE findHandle = FindFirstFileA(searchName.c_str(), reinterpret_cast<WIN32_FIND_DATA*>(&d));
if (findHandle != INVALID_HANDLE_VALUE) {
FindClose(findHandle);
error("File already exists.");
}
findHandle = FindFirstFileA(file.c_str(), reinterpret_cast<WIN32_FIND_DATA*>(&d));
while (FindNextFileA(findHandle, reinterpret_cast<WIN32_FIND_DATA*>(&d)) != 0) {
if (file != d.cFileName) {
add(d.cFileName);
} else {
std::cout << d.cFileName << str_eself << std::endl;
}
}
FindClose(findHandle);
int p = 0;
int op = 1;
while ((p = posplus(op, '|', all)) != -1) {
currFile = all.substr(op, p - op - 1);
op = p;
std::fstream f1(currFile, std::ios::in | std::ios::binary);
std::fstream f2(searchName, std::ios::out | std::ios::binary);
if (!f1.is_open()) {
error(str_eopen + currFile);
}
if (!f2.is_open()) {
error(str_eopen + searchName);
}
u_val nr = 1, nw = 0;
while (!f1.eof()) {
f1.read(reinterpret_cast<char*>(bu), bufsize);
nr = f1.gcount();
for (u_val j = 0; j < nr; ++j) {
if (bu[j] == 10) {
f2.put(13);
++nw;
f2.put(10);
++nw;
} else if (bu[j] == 13) {
// Do nothing
} else {
f2.put(bu[j]);
++nw;
}
}
}
f1.close();
f2.close();
f1.clear();
f2.clear();
f1.open(currFile, std::ios::out | std::ios::binary);
f1.close();
f1.clear();
RemoveFileA(currFile.c_str());
if (MoveFileA(searchName.c_str(), currFile.c_str()) == 0) {
error(str_ewrite);
}
std::cout << str_done << std::endl;
}
}
std::cout << str_alldone << std::