Смекни!
smekni.com

Системное программирование в операционных системах (стр. 4 из 4)

i=GetDlgItemText (hDlg, IDC_TEXT,name,120);

if (i==0)

{

MessageBox (hDlg,"введите имя файла с расписанием","ожидается ввод",MB_OK);

return TRUE;

}

}

if ( (hFile=CreateFile (name,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)) ==INVALID_HANDLE_VALUE)

MessageBox (0,"Не удается открыть файл с расписанием","error",MB_APPLMODAL|MB_OK|MB_ICONSTOP);

filewasopened=FALSE;

return TRUE;

}

filewasopened=TRUE;

count=ReadShedule (hFile,sd);

if (count <=0)

{

MessageBox (hDlg,"Файл не соответствует формату",NULL,MB_OK|MB_ICONERROR);

return TRUE;

}

CloseHandle (hFile);

MakeItStandart (sd,count);

filewasopened=frst=FALSE;

}

nextexe=FindNext (sd,count);

if (nextexe<0)

{

SendMessage (hDlg,WM_COMMAND, IDC_STOP,0);

return TRUE;

}

delayexe=sd [nextexe]. delay;

sd [nextexe]. delay=-1;

SubtDelay (sd,count,delayexe);

SendMessage (hDlg,WM_SIZE,SIZE_MINIMIZED,0);

if (! SetTimer (hDlg,TM,1000*delayexe,NULL))

{

frst=TRUE;

if (MessageBox (hDlg,"В системе нет свободных таймеров. ",0,MB_RETRYCANCEL| MB_ICONERROR) ==IDRETRY)

SendMessage (hDlg,WM_COMMAND, IDC_START,0);

else

SendMessage (hDlg,WM_COMMAND, IDC_STOP,0);

}

return TRUE;

}

case IDC_STOP:

{

if (! frst)

{

KillTimer (hDlg,TM);

count =0;

nextexe=-1;

frst=TRUE;

MessageBox (hDlg,"в расписании больше нет задач","Завершение",MB_OK|MB_ICONINFORMATION);

}

return TRUE;

}

case IDC_RESTORE:

{

Shell_NotifyIcon (NIM_DELETE,&nfd);

ShowWindow (hDlg,SW_RESTORE);

SetForegroundWindow (hDlg);

return TRUE;

}

}

return TRUE;

}

return FALSE;

}

char * FileOpen (HWND hWnd)

{

OPENFILENAME filename;

char name [512] = "";

ZeroMemory (&filename, sizeof (filename));

filename. lStructSize = sizeof (filename);

filename. hwndOwner = hWnd;

filename. lpstrFile = name;

filename. lpstrFile [0] = '&bsol;0';

filename. nMaxFile = sizeof (name);

filename. lpstrFilter = "Text files&bsol;0*. txt&bsol;0";

filename. nFilterIndex = 1;

filename. lpstrFileTitle = "Выберите имя файла для сравнения... ";

filename. nMaxFileTitle = 0;

filename. lpstrInitialDir = NULL;

filename. Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

if (GetOpenFileName (&filename) ! = NULL)

return (filename. lpstrFile);

else

return 0;

}

int ReadShedule (HANDLE hFile,SHEDULEDATA sd [30])

{

char str [200] ="";

char symb;

char buff [2] ="";

int w;

BOOL IsComm;

int numb;

int i;

int j;

for (i=0; i<30; i++)

{

j=-1;

w=0;

while (1)

{

ReadFile (hFile,&symb,1,&numb,NULL);

if (numb==0)

return i+1;

if (symb=='&bsol;')

symb='/';

if ( (j==-1) && (symb! ='"'))

return - 1;

else

if (j==-1)

{

IsComm=TRUE;

j++;

continue;

}

else

if (IsComm)

if (symb=='"')

{

IsComm=FALSE;

sd [i]. fname [j] =0;

j=0;

continue;

}

else

{

sd [i]. fname [j] =symb;

j++;

continue;

}

else

if ( (symb==0xd) || (symb==' ') || (symb==': '))

continue;

if ( (symb! =0xa))

{

if (j>5)

return - 1;

sd [i]. tname [j] =symb;

j++;

continue;

}

else

break;

}

}

return i+1;

}

void cstrcpy (char *a,char *b, int from, int count)

{

int i;

int j=0;

for (i=from; i<from+count; i++)

{

b [j] =a [i] ;

j++;

}

}

void MakeItStandart (SHEDULEDATA sd [30], int count)

{

int i;

SYSTEMTIME st;

char buffer [2] ;

int uis;

int uil;

int ui;

GetLocalTime (&st);

for (i=0; i<count; i++)

{

cstrcpy (sd [i]. tname,buffer,0,2);

sd [i]. h=atoi (buffer);

cstrcpy (sd [i]. tname,buffer,2,2);

sd [i]. m=atoi (buffer);

cstrcpy (sd [i]. tname,buffer,4,2);

sd [i]. s=atoi (buffer);

uil=sd [i]. h*3600+sd [i]. m*60+sd [i]. s;

uis=st. wHour*3600+st. wMinute*60+st. wSecond;

ui=uil-uis;

if (ui<0)

sd [i]. delay=ui+24*3600;

else

sd [i]. delay=ui;

}

}

int FindNext (SHEDULEDATA sd [30], int count)

{

int i,min=0xFFFFFFF;

int minnumber=-1;

for (i=0; i<count; i++)

{

if (sd [i]. delay<0)

continue;

else

if (sd [i]. delay<min)

{

min=sd [i]. delay;

minnumber=i;

}

}

return minnumber;

}

void SubtDelay (SHEDULEDATA sd [30], int count, int delay)

{

int i;

for (i=0; i<count; i++)

if (sd [i]. delay>=0)

sd [i]. delay-=delay;

}