Вроде написал, пользуйся.
| Код | #include "stdafx.h" #include <windows.h> #include <tchar.h> #include <list> using namespace std; typedef struct { TCHAR Name[MAX_PATH]; DWORD Size; DWORD StartLoc; DWORD EndLoc; }MYFILE, *PMYFILE; class Disk { private: list<MYFILE> cont; DWORD size; DWORD min; DWORD max; void Add(TCHAR* name, DWORD size); void Del(TCHAR* name); void Log(); BOOL Start(); public: Disk(DWORD size, DWORD min, DWORD max); }; Disk::Disk(DWORD size, DWORD min, DWORD max) { this->size = size; this->min = min; this->max = max; BOOL b = TRUE; while(b) { b = Start(); } } void Disk::Del(TCHAR* name) { BOOL b = TRUE; if(name == NULL) { _tprintf(_T("ERROR! File name error!\n")); b = FALSE; } if(b) { b = FALSE; DWORD t = 0; DWORD x = _tcslen(name) + 1; TCHAR* str = new TCHAR[x]; while (name[t] != NULL) { str[t] = _totupper(name[t]); t++; } str[x - 1] = 0; name = str; for(list<MYFILE>::iterator i = cont.begin(); i != cont.end(); i++) { if(!_tcscmp(name, i->Name)) { cont.erase(i); b = TRUE; break; } } if(!b) { _tprintf(_T("File not found!\n")); } } }
void Disk::Add(TCHAR* name, DWORD size) { BOOL b = TRUE; DWORD k = 0; if(name == NULL) { _tprintf(_T("ERROR! File name error!\n")); return; } if(size < min || size > max) { _tprintf(_T("ERROR! File size error!\n")); return; } if(b) { b = FALSE; DWORD t = 0; DWORD x = _tcslen(name) + 1; TCHAR* str = new TCHAR[x]; while (name[t] != NULL) { str[t] = _totupper(name[t]); t++; } str[x - 1] = 0; name = str; MYFILE tmp; _tcscpy(tmp.Name, name); tmp.Size = size; if(cont.empty()) { tmp.StartLoc = 0; tmp.EndLoc = size; cont.push_front(tmp); b = TRUE; } for(list<MYFILE>::iterator i = cont.begin(); i != cont.end() && !b; i++) { b = FALSE; if(!_tcscmp(str, i->Name)) { _tprintf(_T("ERROR! Such file already existed!\n")); b = TRUE; break; } } int xx = -1; for(list<MYFILE>::iterator i = cont.begin(); i != cont.end() && !b; i++, x++) { if(i == cont.begin()) { if(i->StartLoc > size) { tmp.StartLoc = 0; tmp.EndLoc = size; cont.insert(i, tmp); b = TRUE; } continue; } list<MYFILE>::iterator prev = cont.begin(); for(int j = 0; j < xx; j++) { prev++; } if(prev->EndLoc - i->StartLoc > size) { tmp.StartLoc = prev->EndLoc; tmp.EndLoc = tmp.StartLoc + size; cont.insert(i, tmp); b = TRUE; } } if(!b) { list<MYFILE>::iterator i = --cont.end(); if(this->size - i->EndLoc > size) { tmp.StartLoc = i->EndLoc; tmp.EndLoc = tmp.StartLoc + size; cont.push_back(tmp); b = TRUE; } } } if(!b) { _tprintf(_T("ERROR! No such place!\n")); } } void Disk::Log() { for(list<MYFILE>::iterator i = cont.begin(); i != cont.end(); i++) { _tprintf(_T("File %s bytes %i - %i\n"), i->Name, i->StartLoc, i->EndLoc - 1); } _tprintf(_T("\n")); } BOOL Disk::Start() { TCHAR* cl = new TCHAR[MAX_PATH]; _tprintf(_T("Command Line>")); _tscanf(_T("%s"), cl); DWORD t = 0; DWORD x = _tcslen(cl) + 1; TCHAR* str = new TCHAR[x]; while (cl[t] != NULL) { str[t] = _totupper(cl[t]); t++; } str[x - 1] = 0; cl = str; if(!_tcscmp(cl, _T("EXIT"))) { return FALSE; } else if(!_tcscmp(cl, _T("ADD"))) { TCHAR* name = new TCHAR[MAX_PATH]; DWORD* size = new DWORD; _tscanf(_T("%s%i"), name, size); Add(name, *size); } else if(!_tcscmp(cl, _T("DEL"))) { TCHAR* name = new TCHAR[MAX_PATH]; _tscanf(_T("%s"), name); Del(name); } else if(!_tcscmp(cl, _T("LOG"))) { Log(); } else { _tprintf(_T("Cannot recognize command\n")); } return TRUE; } int _tmain(int argc, _TCHAR* argv[]) { Disk* t = new Disk(360 * 1024, 18, 32 * 1024); return 0; }
|
|