#include "windows.h" // Для настройки консольного выходного буфера
#include <iostream>
#include "string.h" // C- функции обработки C-строк
using namespace std;
//Объявления служебных функций и глобальных переменных
HANDLE hFile;
char * lpszFileName="ffile1";
unsigned int dwError=0x00000000; //Код ошибки
char * lpParam[5]; //Параметры
char lpszInfo[512]; //Информация (для команд ADDNEW и WRITEF)
char lpszCom[7]; //Команда
DWORD dwSize(0);
DWORD dwFree(0);
void FORMAT()
{
int nsize=atoi(lpParam[0]);
if (strcmp(lpParam[4],"RT-11"))
{
dwError=0x01000005;
return;
}
if ((nsize<=69)|(nsize>9999))
{
dwError=0x01000001;
return;
}
hFile=CreateFile(L"C:\\RT-11\\ffile1.dsk",GENERIC_READ|GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,NULL,NULL);
const int nbsize=nsize*512;
char tilda=126;
char * lpBuff=new char[nbsize];
int i=0;
DWORD dwWritten=0;
for(;i<nbsize;i++)
lpBuff[i]=tilda;
WriteFile(hFile,lpBuff,nbsize,&dwWritten,0);
SetFilePointer(hFile,982,NULL,FILE_BEGIN);
int nver=atoi(lpParam[1]);
if ((nver<=0)|(nver>99))
{
dwError=0x01000002;
return;
}
WriteFile(hFile,&nver,2,&dwWritten,0);
strncpy(lpBuff,lpParam[2],12);
SetFilePointer(hFile,984,NULL,FILE_BEGIN);
WriteFile(hFile,lpBuff,12,&dwWritten,0);
strncpy(lpBuff,lpParam[3],12);
SetFilePointer(hFile,996,NULL,FILE_BEGIN);
WriteFile(hFile,lpBuff,12,&dwWritten,0);
strncpy(lpBuff,lpParam[4],12);
SetFilePointer(hFile,1008,NULL,FILE_BEGIN);
WriteFile(hFile,lpBuff,12,&dwWritten,0);
//making the free-space file and first catalogue
int blf=2;
SetFilePointer(hFile,3074,NULL,FILE_BEGIN);
WriteFile(hFile,&blf,2,&dwWritten,0);
blf=1;
SetFilePointer(hFile,3076,NULL,FILE_BEGIN);
WriteFile(hFile,&blf,2,&dwWritten,0);
blf=68;
SetFilePointer(hFile,3080,NULL,FILE_BEGIN);
WriteFile(hFile,&blf,2,&dwWritten,0);
char * filename="freesp";
char * type=".xyz";
blf=10;
SetFilePointer(hFile,3082,NULL,FILE_BEGIN);
WriteFile(hFile,&blf,2,&dwWritten,0);
SetFilePointer(hFile,3084,NULL,FILE_BEGIN);
WriteFile(hFile,filename,6,&dwWritten,0);
SetFilePointer(hFile,3090,NULL,FILE_BEGIN);
WriteFile(hFile,type,4,&dwWritten,0);
blf=nsize-68;
DWORD freespc=blf;
SetFilePointer(hFile,3090,NULL,FILE_BEGIN);//Свободное место, используются слова 5-6
WriteFile(hFile,&freespc,4,&dwWritten,0);
blf=0x6460;
SetFilePointer(hFile,3096,NULL,FILE_BEGIN);
WriteFile(hFile,&blf,2,&dwWritten,0);
blf=40;
SetFilePointer(hFile,3098,NULL,FILE_BEGIN);
WriteFile(hFile,&blf,2,&dwWritten,0);
CloseHandle(hFile);
}
void DIRDIR(){}
void DIRSPC()
{
DWORD dwWritten;
dwSize=0;
dwFree=0;
DWORD curfsize=0;
char addr[50];
sprintf(addr,"C:\\RT-11\\%s.dsk",lpszFileName);
if (!strcmp(lpszFileName,lpParam[0]))
{
hFile=CreateFileA(addr,GENERIC_READ|GENERIC_WRITE,NULL,NULL,OPEN_ALWAYS,NULL,NULL);
char system[12];
SetFilePointer(hFile,1008,NULL,FILE_BEGIN);
ReadFile(hFile,system,12,&dwWritten,0);
if (strcmp(system,"RT-11"))
{
dwError=0x01000005;
return;
}
SetFilePointer(hFile,3082,NULL,FILE_BEGIN);
ReadFile(hFile,&curfsize,2,&dwWritten,0);
while (curfsize!=40)
{
if (curfsize==10)
{
SetFilePointer(hFile,6,NULL,FILE_CURRENT);
ReadFile(hFile,&curfsize,4,&dwWritten,0);
dwFree+=curfsize;
dwSize+=curfsize;
SetFilePointer(hFile,4,NULL,FILE_CURRENT);
}
else
{
SetFilePointer(hFile,10,NULL,FILE_CURRENT);
ReadFile(hFile,&curfsize,2,&dwWritten,0);
dwSize+=curfsize;
SetFilePointer(hFile,2,NULL,FILE_CURRENT);
}
ReadFile(hFile,&curfsize,2,&dwWritten,0);
}
}
else
{
dwError=0x02000001;
}
CloseHandle(hFile);
}
void CREATE(){}
void ERASEF(){}
void RENAME(){}
void ADDNEW(){}
void WRITEF(){}
void ARCHVE(){}
void SETATR(){}
void MARKAT()
{
}
int main()
{
int i=0;
for(;i<5;i++)
{
lpParam[i]=new char[20];
}
int counter=1;
//Настраиваем выходной буфер консоли (цвет текста ярко-красный)
HANDLE hStdOut;
hStdOut=GetStdHandle(STD_OUTPUT_HANDLE); //Получаем дескриптор выходного буфера консоли
WORD wAttribute=FOREGROUND_RED|FOREGROUND_INTENSITY;
SetConsoleTextAttribute(hStdOut,wAttribute);
//Обрабатываем входящие
char lpszBuff[526];
cout<<"RT-11 K5292 MODEL\nWAITING FOR COMMANDS. PRESS Q AND ENTER TO QUIT.\n";
while (true)
{
cin.getline(lpszBuff,526); //Строка, напечатанная оператором, заносится в буфер
if((lpszBuff[0]=='Q')||(lpszBuff[0]=='q')) //В случае команды выхода
{
return 0;
}
cout<<lpszBuff<<endl;
int j=0;
i=0;
counter=1;
int shift=7;
strncpy(lpszCom,lpszBuff,6); //Вычленяем команду
lpszCom[6]='\0';
int x=strcmp(lpszCom,"FORMAT");
int bx,by;
if (((strcmp(lpszCom,"FORMAT"))&&(strcmp(lpszCom,"DIRDIR"))&&(strcmp(lpszCom,"DIRSPC"))&&(strcmp(lpszCom,"CREATE"))&&(strcmp(lpszCom,"ERASEF "))&&(strcmp(lpszCom,"RENAME"))&&((bx=strcmp(lpszCom,"ADDNEW")))&&((by=strcmp(lpszCom,"WRITEF")))&&(strcmp(lpszCom,"ARCHVE"))&&(strcmp(lpszCom,"SETATR"))&&(strcmp(lpszCom,"MARKAT")))) //Проверяем корректность введенной команды
{
cout<<"Bad command\n";
continue;
}
if ((!bx)||(!by)) //Если используются команды ADDNEW и WRITEF
{
strncpy(lpszInfo,&lpszBuff[7],512);
}
else
{
cout<<"Command: "<<lpszCom<<endl; //Заполняем параметры
i=7;
while (lpszBuff[i]!='\0')
{
while (lpszBuff[i]!=' ')
{
if (lpszBuff[i]=='\0')
{
(lpParam[j])[i-shift]='\0';
break;
}
(lpParam[j])[i-shift]=lpszBuff[i];
i++;
}
if (lpszBuff[i]=='\0')
break;
(lpParam[j])[i-shift]='\0';
i++;
shift+=strlen(lpParam[j])+1;
j++;
counter++;
}
}
if (!strcmp(lpszCom,"FORMAT")) //Вызываем служебные функции
FORMAT();
if (!strcmp(lpszCom,"DIRDIR"))
DIRDIR();
if (!strcmp(lpszCom,"DIRSPC"))
{
DIRSPC();
cout<<"Free Space:\n"<<512*dwFree<<endl;
cout<<"Total Useful Space:\n"<<512*dwSize<<endl;
}
if (!strcmp(lpszCom,"CREATE"))
CREATE();
if (!strcmp(lpszCom,"ERASEF"))
ERASEF();
if (!strcmp(lpszCom,"RENAME"))
RENAME();
if (!strcmp(lpszCom,"ADDNEW"))
ADDNEW();
if (!strcmp(lpszCom,"WRITEF"))
WRITEF();
if (!strcmp(lpszCom,"ARCHVE"))
ARCHVE();
if (!strcmp(lpszCom,"SETATR"))
SETATR();
if (!strcmp(lpszCom,"MARKAT"))
MARKAT();
wAttribute=FOREGROUND_BLUE|FOREGROUND_INTENSITY; //Устанавливаемый синий цыет выводимого текста
SetConsoleTextAttribute(hStdOut,wAttribute);
switch(dwError) //Анализируем ошибку
{
case 0x00000000:
cout<<"No error\n";
break;
case 0x01000001:
cout<<"Incorrect size of the disk error\n";
break;
case 0x01000002:
cout<<"Incorrect system version error\n";
break;
case 0x01000003:
cout<<"Incorrect volume error\n";
break;
case 0x01000004:
cout<<"Incorrect owner's name error\n";
break;
case 0x01000005:
cout<<"Incorrect system name error\n";
break;
case 0x02000001:
cout<<"Incorrect disk name error\n";
break;
case 0x04000001:
cout<<"Incorrect file name error\n";
break;
case 0x04000002:
cout<<"Incorrect file type error\n";
break;
case 0x04000003:
cout<<"Not enough resources error\n";
break;
case 0x06000001:
cout<<"Incorrect new file name error\n";
break;
case 0x06000002:
cout<<"Incorrect old file name error\n";
break;
case 0x07000002:
cout<<"Info too long error\n";
break;
case 0x09000001:
cout<<"Incorrect archieve name error\n";
break;
case 0x0A000001:
cout<<"Incorrect new data error\n";
break;
case 0x0A000002:
cout<<"Incorrect new file name error\n";
break;
case 0x0A000003:
cout<<"Incorrect new file type name error\n";
break;
default:
cout<<"Unknown error\n";
}
if ((!bx)||(!by)) //Ввыеденные оператором параметры выводятся на экран
{
cout<<"Inserted info: \n";
cout<<lpszInfo<<endl;
}
else
{
cout<<"Inserted parameters: "<<endl;
for (i=0;i<counter;i++)
cout<<lpParam[i]<<endl;
}
wAttribute=FOREGROUND_RED|FOREGROUND_INTENSITY; //Возвращаем красный текст выходного текста
SetConsoleTextAttribute(hStdOut,wAttribute);
for(int k=0;k<5;k++)
{
ZeroMemory(lpParam[k],20);
}
}
return 0;
}