// AutoFile.cpp: implementation of the CAutoFile class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "AutoFile.h" #include "serialport.h" #include "console.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CAutoFile::CAutoFile() { } CAutoFile::~CAutoFile() { } bool CAutoFile::OpenForReading(TCHAR* filename, int lastDepth) { m_Depth=lastDepth+1; if (m_Depth>MAX_DEPTH) { return false; } m_hFile=::CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile==INVALID_HANDLE_VALUE) { // // error opening file // return false; } return true; } bool CAutoFile::NextLine(CSerialPort& port, CConsole& console) { TCHAR line[MAX_LINE]; char asciiLine[MAX_LINE]; TCHAR filename[MAX_LINE]; int charsRead; TCHAR* pLocal; TCHAR* pCommand; unsigned int address; unsigned int length; int duration; bool bSuccess=true; bool bFileComplete; charsRead=ReadNextLine(line, asciiLine, MAX_LINE); if (charsRead==0) { bFileComplete=true; return false; } pLocal=_tcsstr(line, TEXT("local")); if (pLocal!=NULL) { // // local command // if (_stscanf(pLocal, TEXT("local dload %s %x"), filename, (int*)&address)==2) { // // dload // bSuccess=console.Dload(filename, address, port); } if (_stscanf(pLocal, TEXT("local load %s"), filename)==1) { // // load // bSuccess=console.Load(filename, port); } if (_stscanf(pLocal, TEXT("local dsave %s %x %x"), filename, (int*)&address, (int*)&length)==3) { // // dsave // bSuccess=console.Dsave(filename, address, length, port); } if (_stscanf(pLocal, TEXT("local auto %s"), filename)==1) { // // auto // //bSuccess=console.Auto(filename, m_Depth, port); } if (_stscanf(pLocal, TEXT("local sleep %d"), &duration)==1) { // // sleep // bSuccess=console.Sleep(duration); } pCommand=_tcsstr(pLocal, TEXT("echo ")); if (pCommand!=NULL) { // // echo // TCHAR* string=pCommand+5; bSuccess=console.Echo(string); } pCommand=_tcsstr(pLocal, TEXT("dos ")); if (pCommand!=NULL) { // // dos // TCHAR* command=pCommand+4; bSuccess=console.Dos(command); } pCommand=_tcsstr(pLocal, TEXT("sh ")); if (pCommand!=NULL) { // // sh=dos // TCHAR* command=pCommand+3; bSuccess=console.Dos(command); } pCommand=_tcsstr(pLocal, TEXT("!")); if (pCommand!=NULL) { // // !=dos, no space // TCHAR* command=pCommand+1; bSuccess=console.Dos(command); } if (_tcsstr(pLocal, TEXT("help"))) { // // help // bSuccess=console.Help(); } if (_tcsstr(pLocal, TEXT("beep"))) { // // beep // bSuccess=console.Beep(); } if (_tcsstr(pLocal, TEXT("pause"))) { // // pause // bSuccess=console.Pause(); } if (_tcsstr(pLocal, TEXT("wait-prompt"))) { // // wait for prompt // //bSuccess=console.WaitPrompt(port); } if (_tcsstr(pLocal, TEXT("auto-again"))) { // // auto again // ::SetFilePointer(m_hFile, 0, NULL, FILE_BEGIN); } if (_tcsstr(pLocal, TEXT("quit"))) { // // quit // bSuccess=false; } } else { // // non-local command // port.PutBytes((unsigned char*)asciiLine, charsRead); } return bSuccess; } int CAutoFile::ReadNextLine(TCHAR* string, char* asciiString, int maxLength) { int a=0; char character; DWORD bytesRead; do { ::ReadFile(m_hFile, &character, 1, &bytesRead, NULL); if (bytesRead==0) { break; } asciiString[a]=character; a++; } while(character!='\n' && a<(maxLength-1)); asciiString[a]='\0'; #ifdef _UNICODE // // convert line to unicode // ::MultiByteToWideChar(CP_ACP, 0, asciiString, -1, string, maxLength); #else strcpy(string, asciiString); #endif return a; }