221 lines
4.6 KiB
C++
221 lines
4.6 KiB
C++
|
|
// IFPQYY | Tóth Dániel Mátyás | V1
|
||
|
|
#include <iostream>
|
||
|
|
#include <fstream>
|
||
|
|
#include <filesystem>
|
||
|
|
#include "feladat3.h"
|
||
|
|
using namespace std;
|
||
|
|
|
||
|
|
static int CopyStr(char* dest, const char* src, const char* concat = "\0");
|
||
|
|
static void BubbleSort(unsigned char* arr, int len);
|
||
|
|
|
||
|
|
int GetFilenames(int argc, char* argv[],
|
||
|
|
char infilenames[MYMAXFILES][MYMAXFN],
|
||
|
|
char outfilenames[MYMAXFILES][MYMAXFN]) {
|
||
|
|
if (argc != 2 || argv == nullptr || infilenames == nullptr
|
||
|
|
|| outfilenames == nullptr) {
|
||
|
|
return NOK;
|
||
|
|
}
|
||
|
|
|
||
|
|
cout << "Parameter allomany:\n" << argv[1] << '\n';
|
||
|
|
ifstream argifs(argv[1]);
|
||
|
|
if (!argifs.is_open())
|
||
|
|
{
|
||
|
|
cout << "Nem lehet megnyitni a " << argv[1] << " nevu allomanyt\n";
|
||
|
|
return NOK;
|
||
|
|
}
|
||
|
|
int readAmount = 0;
|
||
|
|
int i = 0;
|
||
|
|
char buf[MYMAXFN-1];
|
||
|
|
cout << "Feldolgozando allomanyok:\n";
|
||
|
|
while (readAmount < 5 && argifs.getline(buf, MYMAXFN-1))
|
||
|
|
{
|
||
|
|
readAmount++;
|
||
|
|
ifstream inputfile(buf);
|
||
|
|
if (!inputfile.fail())
|
||
|
|
{
|
||
|
|
CopyStr(infilenames[i], buf);
|
||
|
|
CopyStr(outfilenames[i], buf, ".srt");
|
||
|
|
cout << infilenames[i] << " " << outfilenames[i] << "\tOK\n";
|
||
|
|
i++;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
cout << "<" << buf << ">" << "\tNOK\n";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
argifs.close();
|
||
|
|
int status = OK;
|
||
|
|
if (i == 0)
|
||
|
|
{
|
||
|
|
status = NOK;
|
||
|
|
cout << "Ures\tNOK\n";
|
||
|
|
}
|
||
|
|
while (i < 5) {
|
||
|
|
infilenames[i][0] = '\0';
|
||
|
|
outfilenames[i][0] = '\0';
|
||
|
|
i++;
|
||
|
|
}
|
||
|
|
cout << "*DONE\n";
|
||
|
|
return status;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int CopyStr(char* dest, const char* src, const char* concat) {
|
||
|
|
if (dest == nullptr || src == nullptr) {
|
||
|
|
return NOK;
|
||
|
|
}
|
||
|
|
int i = 0;
|
||
|
|
while (src[i] != '\0')
|
||
|
|
{
|
||
|
|
dest[i++] = src[i];
|
||
|
|
}
|
||
|
|
int j = 0;
|
||
|
|
while (concat[j] != '\0')
|
||
|
|
{
|
||
|
|
dest[i++] = concat[j++];
|
||
|
|
}
|
||
|
|
dest[i] = '\0';
|
||
|
|
return OK;
|
||
|
|
}
|
||
|
|
|
||
|
|
int SortFile(char* infn, char* outfn, unsigned char* buf,
|
||
|
|
unsigned short* pmin, unsigned short* pmax,
|
||
|
|
unsigned short* pavg, unsigned short* pn,
|
||
|
|
unsigned short bufsize, bool test) {
|
||
|
|
if (infn == nullptr || outfn == nullptr
|
||
|
|
|| buf == nullptr || pmin == nullptr
|
||
|
|
|| pmax == nullptr || pavg == nullptr
|
||
|
|
|| pn == nullptr) // there has to be a better way than this
|
||
|
|
{
|
||
|
|
return NOK;
|
||
|
|
}
|
||
|
|
if (bufsize != MYBUFSIZE) // I guess?
|
||
|
|
{
|
||
|
|
return NOK;
|
||
|
|
}
|
||
|
|
|
||
|
|
ifstream readifs(infn, ios_base::binary);
|
||
|
|
ofstream outofs(outfn, ios_base::binary);
|
||
|
|
if (!readifs.is_open() || outofs.bad())
|
||
|
|
{
|
||
|
|
return NOK;
|
||
|
|
}
|
||
|
|
unsigned short files[16][MYBUFSIZE/2]; //this is probably a terrible solution. too bad!
|
||
|
|
int filesizes[16] = { 0 };
|
||
|
|
int ptrshifts[16] = { 0 };
|
||
|
|
|
||
|
|
*pmin = MYMAXVALUE;
|
||
|
|
*pmax = 0;
|
||
|
|
*pavg = 0;
|
||
|
|
*pn = filesystem::file_size(infn)/2; // this can't be more than MYMAXVALUE anyways, so warning dully ignored
|
||
|
|
|
||
|
|
if (test)
|
||
|
|
{
|
||
|
|
cout << "\nSTART: " << infn << " ----> " << outfn << "\n";
|
||
|
|
cout << "*READ\n";
|
||
|
|
}
|
||
|
|
|
||
|
|
unsigned short n;
|
||
|
|
int i = 0;
|
||
|
|
int j = 0;
|
||
|
|
int usedfilescnt = 0;
|
||
|
|
while (readifs.read((char*)&n, sizeof(n))) {
|
||
|
|
if (*pmax < n) {
|
||
|
|
*pmax = n;
|
||
|
|
}
|
||
|
|
if (*pmin > n) {
|
||
|
|
*pmin = n;
|
||
|
|
}
|
||
|
|
*pavg += n;
|
||
|
|
buf[(i * 2) % (MYBUFSIZE)] = n;
|
||
|
|
buf[((i * 2)+1) % (MYBUFSIZE)] = n >> 8;
|
||
|
|
if (test)
|
||
|
|
{
|
||
|
|
cout << i << ".\t" << n << "\n";
|
||
|
|
}
|
||
|
|
i++;
|
||
|
|
if (i % (MYBUFSIZE / 2) == 0)
|
||
|
|
{
|
||
|
|
BubbleSort(buf, MYBUFSIZE);
|
||
|
|
while (j < MYBUFSIZE/2)
|
||
|
|
{
|
||
|
|
files[usedfilescnt][j] = buf[j * 2] + buf[(j * 2) + 1] * 256;
|
||
|
|
j++;
|
||
|
|
}
|
||
|
|
filesizes[usedfilescnt] = MYBUFSIZE / 2;
|
||
|
|
usedfilescnt++;
|
||
|
|
j = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
readifs.close();
|
||
|
|
|
||
|
|
int size = (*pn * 2) % MYBUFSIZE;
|
||
|
|
BubbleSort(buf, size);
|
||
|
|
while (j < size / 2)
|
||
|
|
{
|
||
|
|
files[usedfilescnt][j] = buf[j * 2] + buf[(j * 2) + 1] * 256;
|
||
|
|
j++;
|
||
|
|
}
|
||
|
|
filesizes[usedfilescnt] = size/2;
|
||
|
|
usedfilescnt++;
|
||
|
|
|
||
|
|
if (test)
|
||
|
|
{
|
||
|
|
cout << "\n*SORT, WRITE\n";
|
||
|
|
}
|
||
|
|
|
||
|
|
i = 0;
|
||
|
|
while (i < *pn)
|
||
|
|
{
|
||
|
|
int savedJ = 0;
|
||
|
|
unsigned short smallest = MYMAXVALUE;
|
||
|
|
for (j = 0; j < usedfilescnt; j++)
|
||
|
|
{
|
||
|
|
if (ptrshifts[j] >= filesizes[j])
|
||
|
|
{
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (files[j][ptrshifts[j]] < smallest)
|
||
|
|
{
|
||
|
|
smallest = files[j][ptrshifts[j]];
|
||
|
|
savedJ = j;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (test)
|
||
|
|
{
|
||
|
|
cout << i << ".\t" << smallest << "\n";
|
||
|
|
}
|
||
|
|
outofs.write((char *)&smallest, sizeof(smallest));
|
||
|
|
ptrshifts[savedJ]++;
|
||
|
|
i++;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
*pavg = *pavg / *pn;
|
||
|
|
|
||
|
|
if (test)
|
||
|
|
{
|
||
|
|
cout << "\n*DONE\n";
|
||
|
|
}
|
||
|
|
return OK;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void BubbleSort(unsigned char* arr, int len) { //note: this is a char*. you need to sort unsigned short*. kill 7 billion people
|
||
|
|
for (int i = 0; i < len / 2; i++)
|
||
|
|
{
|
||
|
|
for (int j = 0; j < (len / 2) - i - 1; j++)
|
||
|
|
{
|
||
|
|
unsigned short first = arr[j * 2] + arr[j * 2 + 1] * 256;
|
||
|
|
unsigned short second = arr[(j + 1) * 2] + arr[(j + 1) * 2 + 1] * 256;
|
||
|
|
if (first > second)
|
||
|
|
{
|
||
|
|
unsigned char temp = arr[j * 2];
|
||
|
|
arr[j * 2] = arr[(j + 1) * 2];
|
||
|
|
arr[(j + 1) * 2] = temp;
|
||
|
|
|
||
|
|
temp = arr[j * 2 + 1];
|
||
|
|
arr[j * 2 + 1] = arr[(j + 1) * 2 + 1];
|
||
|
|
arr[(j + 1) * 2 + 1] = temp;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|