|
// MorseParser.cpp: implementation of the CMorseParser class.
// Part of "Morse" project. (C) Alexander Fedorov, 2000. lamer2000@hotmail.com
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Morse.h"
#include "MorseParser.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
const UINT CMorseParser::WM_WORK_FINISHED = ::RegisterWindowMessage("CMorseParser_WM_WORK_FINISHED");
CMorseParser::CMorseParser()
{
pSafeBuffer = NULL;
m_bThreadRunned = FALSE;
m_bStopThreadNow = FALSE;
}
CMorseParser::~CMorseParser()
{
m_bStopThreadNow = TRUE;
while (m_bThreadRunned)
Sleep(1);
if (pSafeBuffer)
delete pSafeBuffer;
}
BOOL CMorseParser::AddTextToStream(CString str)
{
return FALSE;
}
CString CMorseParser::Parse(CString str)
{
str.MakeLower();
CString sss;
int l = str.GetLength();
for (int i = 0; i < l; i++)
{
switch(str.GetAt(i))
{
case ' ': sss += "/"; break;
case 'a': sss += ".-"; break;
case 'b': sss += "-..."; break;
case 'c': sss += "-.-."; break;
case 'd': sss += "-.."; break;
case 'e': sss += "."; break;
case 'f': sss += "..-."; break;
case 'g': sss += "--."; break;
case 'h': sss += "...."; break;
case 'i': sss += ".."; break;
case 'j': sss += ".---"; break;
case 'k': sss += "-.-"; break;
case 'l': sss += ".-.."; break;
case 'm': sss += "--"; break;
case 'n': sss += "-."; break;
case 'o': sss += "---"; break;
case 'p': sss += ".--."; break;
case 'q': sss += "--.-"; break;
case 'r': sss += ".-."; break;
case 's': sss += "..."; break;
case 't': sss += "-"; break;
case 'u': sss += "..-"; break;
case 'v': sss += "...-"; break;
case 'w': sss += ".--"; break;
case 'x': sss += "-..-"; break;
case 'y': sss += "-.--"; break;
case 'z': sss += "--.."; break;
case '0': sss += "-----"; break;
case '1': sss += ".----"; break;
case '2': sss += "..---"; break;
case '3': sss += "...--"; break;
case '4': sss += "....-"; break;
case '5': sss += "....."; break;
case '6': sss += "-...."; break;
case '7': sss += "--..."; break;
case '8': sss += "---.."; break;
case '9': sss += "----."; break;
}
sss += " ";
}
return sss;
}
BOOL CMorseParser::AddParsedBufferToStreamAsync(CString str)
{
if (m_bThreadRunned)
return FALSE;
m_bStopThreadNow = FALSE;
if (pSafeBuffer)
delete pSafeBuffer;
int l = str.GetLength() + 1;
pSafeBuffer = new char[l];
ZeroMemory(pSafeBuffer, l);
strcpy(pSafeBuffer, str);
if (!AfxBeginThread(ThreadProc, (LPVOID) this))
return FALSE;
m_bThreadRunned = TRUE;
return TRUE;
}
UINT CMorseParser::ThreadProc(LPVOID lpThreadParameter)
{
CMorseParser* ziz = (CMorseParser*) lpThreadParameter;
if (!ziz)
{
ziz->m_bThreadRunned = FALSE;
return 0;
}
CString str = ziz->pSafeBuffer;
int l = str.GetLength();
for (int i = 0; i < l; i++)
{
if (ziz->m_bStopThreadNow)
break;
switch(str.GetAt(i))
{
case '.':
ziz->SafeAddSingleSound(ziz->m_lFreq, ziz->m_dShort);
break;
case '-':
ziz->SafeAddSingleSound(ziz->m_lFreq, ziz->m_dLong);
break;
case ' ':
ziz->SafeAddSingleSound(0, ziz->m_dDelay);
break;
}
ziz->SafeAddSingleSound(0, ziz->m_dDelay);
}
ziz->m_bThreadRunned = FALSE;
::PostMessage(ziz->m_hWnd, WM_WORK_FINISHED, (WPARAM)ziz, 0);
return 0;
}
BOOL CMorseParser::SafeAddSingleSound(long Freq, double Delay)
{
BOOL bRC = FALSE;
while (!bRC)
{
bRC = m_play.AddSingleSound(Freq, Delay);
// bRC = m_play.Beep08(Freq, Delay);
if (!bRC) Sleep(1);
if (m_bStopThreadNow)
break;
}
return bRC;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.