// IFPQYY | Tóth Dániel Mátyás | V2 #include #include #include #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; } if (argv[1] == 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; } char filenames[16][30]; *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"; } long long sum = 0; unsigned short n; int i = 0; int j = 0; int usedfilescnt = 0; char filename[15]; while (readifs.read((char*)&n, sizeof(n))) { if (*pmax < n) { *pmax = n; } if (*pmin > n) { *pmin = n; } sum += 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); sprintf_s(filename, "filename%d.dat", usedfilescnt); CopyStr(filenames[usedfilescnt], filename); ofstream tempoutofs(filename, ios_base::binary); while (j < MYBUFSIZE/2) { unsigned short insert = (unsigned char)buf[j * 2] + (unsigned char)buf[(j * 2) + 1] * 256; tempoutofs.write((char*)&insert, sizeof(insert)); j++; } tempoutofs.close(); usedfilescnt++; j=0; } } readifs.close(); int size = (*pn * 2) % MYBUFSIZE; if (size > 0) { BubbleSort(buf, size); sprintf_s(filename, "filename%d.dat", usedfilescnt); CopyStr(filenames[usedfilescnt], filename); ofstream tempoutofs(filename, ios_base::binary); while (j < size / 2) { unsigned short insert = buf[j * 2] + buf[(j * 2) + 1] * 256; tempoutofs.write((char*)&insert, sizeof(insert)); j++; } tempoutofs.close(); usedfilescnt++; } else { return NOK; } if (test) { cout << "\n*SORT, WRITE\n"; } i = 0; ifstream streams[16]; for (j = 0; j < usedfilescnt; j++) { streams[j].open(filenames[j], ios_base::binary); unsigned short num; streams[j].read((char*)&num, sizeof(num)); buf[j * 2] = num; buf[(j * 2) + 1] = num >> 8; } bool doneFile[16] = {false}; while (i < *pn) { int savedJ = 0; unsigned short smallest = MYMAXVALUE; for (size_t j = 0; j < usedfilescnt; j++) { if (doneFile[j]) { continue; } int currentnum = buf[j*2] + buf[j*2 + 1] * 256; if (currentnum < smallest) { smallest = currentnum; savedJ = j; } } if (test) { cout << i << ".\t" << smallest << "\n"; } outofs.write((char *)&smallest, sizeof(smallest)); ifstream tempinifs(filenames[savedJ], ios_base::binary); unsigned short num; if (streams[savedJ].read((char*)&num, sizeof(num))) { buf[savedJ * 2] = num; buf[savedJ * 2 + 1] = num >> 8; } else { doneFile[savedJ] = true; } i++; } for (i = 0; i < usedfilescnt; i++) { streams[i].close(); } *pavg = sum / *pn; if (test) { cout << "\n*DONE\n"; cout << "átlag:\t" << *pavg << "\tmin:\t" << *pmin << "\tmax:\t" << *pmax; } 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 = (unsigned char)arr[j * 2] + (unsigned char)arr[j * 2 + 1] * 256; unsigned short second = (unsigned char)arr[(j + 1) * 2] + (unsigned char)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; } } } }