2012年3月30日 星期五

Get full file path from current directory

We have to get full file path from current directory if we are using CreateFile api to open a file.


    wchar_t* fileName = L"\\test.txt";

    // get current directory
    char currentDirectory[_MAX_PATH];  
    getcwd(currentDirectory, MAX_PATH);

    // convert from char to wchar_t
    const size_t size = sizeof(currentDirectory) / sizeof(char);
    wchar_t filePath[size];
    MultiByteToWideChar(CP_ACP, 0, currentDirectory, size, filePath, size);

    // concatenate file path and file name
    wchar_t* fileFullPath = new wchar_t[sizeof(filePath)+sizeof(fileName)+1];
    wcscpy(fileFullPath, filePath);
    wcscat(fileFullPath, fileName);
    delete[] fileFullPath;

Then, you can follow the example below to open the file.
http://twnin.blogspot.com/2012/03/win32-api-read-txt-file.html

2012年3月29日 星期四

iostream and iostream.h

<iostream> is encapsulated in namespace std, but <iostream.h> doesn't.

If you use #include <iostream> , you have to using namespace std.
If you use #include <iostream.h> , you won't use any namespace.

Reserving the iostream.h to compatible with previous source code.


Reference:

[Win API] Sample of reading txt file


It is just a simple example for reading text from .txt file using win32 api.


HANDLE hFile = CreateFile(dataFilePath, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
    return;
}

// read all characters from file.
DWORD bufferSize = GetFileSize(hFile, 0);
char* buffer = new char[bufferSize];
memset(buffer, '\0', bufferSize);

bool result = false;
DWORD bytesRead = 0;
result = !!ReadFile(hFile, &buffer, bufferSize, &bytesRead, 0);

delete[]  buffer;
CloseHandle(hFile);



Then, you can follow the example below to get file path.
http://twnin.blogspot.com/2012/03/get-current-directory-and-convert-to.html

2012年3月26日 星期一

[WebKit] HTML Parsing Flow

This is a simple note about the function flow of webkit data parsing. 
Assume webkit had gotten html from ResourceHandle, you can find previous article about data loading flow here. (http://twnin.blogspot.com/2012/03/webkit-loading-flow.html)

HTML Parsing
>> start from void DocumentWriter::endIfNotLoadingMainResource()
void DecodedDataDocumentParser::flush(DocumentWriter* writer)
void HTMLDocumentParser::append(const SegmentedString& source)
void HTMLDocumentParser::pumpTokenizerIfPossible(SynchronousMode mode)
void HTMLDocumentParser::pumpTokenizer(SynchronousMode mode)
node tree constructing loop
void HTMLSourceTracker::start(const HTMLInputStream& input, HTMLTokenizer* tokenizer, HTMLToken& token)
bool HTMLTokenizer::nextToken(SegmentedString& source, HTMLToken& token)
void HTMLSourceTracker::end(const HTMLInputStream& input, HTMLTokenizer* tokenizer, HTMLToken& token)
void HTMLTreeBuilder::constructTreeFromToken(HTMLToken& rawToken)
// Here omit detailed tree construction.
end loop
>> return to void DocumentWriter::endIfNotLoadingMainResource() 
void HTMLDocumentParser::finish()
void HTMLDocumentParser::attemptToEnd()
void HTMLDocumentParser::prepareToStopParsing()
void HTMLDocumentParser::attemptToRunDeferredScriptsAndEnd()
void HTMLDocumentParser::end()
void HTMLTreeBuilder::finished()
void Document::finishedParsing()
        bool Node::dispatchEvent(PassRefPtr<Event> event) // DOMContentLoadedEvent

void FrameLoader::finishedParsing()
        void WebFrameLoaderClient::dispatchDidFinishDocumentLoad()
        bool MessageSender::send(const U& message) // dispatchDidFinishDocumentLoad

>> return to void FrameLoader::finishedLoading()
        void WebFrameLoaderClient::dispatchDidLoadMainResource(DocumentLoader*)

>> return to head of call stack


So far, we establish a html node tree, then we are going to load content of DOM.

2012年3月23日 星期五

[WebKit] Page Loading Flow

This is a simple note about the function flow of standard opening a web. 
After I give a text of url on browser and summit it. Safari will dispatch a message to webkit.

Received Message From Web Page
void Connection::dispatchMessages()  // This is a infinite loop for listening message from browser
void Connection::dispatchMessage(IncomingMessage& message)
void WebProcess::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
void WebPage::didReceiveWebPageMessage(CoreIPC::Connection*, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)

URL Load
void WebPage::loadURLRequest(const ResourceRequest& request, const SandboxExtension::Handle& sandboxExtensionHandle)

Frame Load
void FrameLoader::load(const ResourceRequest& request, bool lockHistory)
void FrameLoader::load(const ResourceRequest& request, const SubstituteData& substituteData, bool lockHistory)
void FrameLoader::load(DocumentLoader* newDocumentLoader)
void FrameLoader::loadWithDocumentLoader(DocumentLoader* loader, FrameLoadType type, PassRefPtr<FormState> prpFormState)

Policy Check
void PolicyChecker::checkNavigationPolicy(const ResourceRequest& request, DocumentLoader* loader,  PassRefPtr<FormState> formState, NavigationPolicyDecisionFunction function, void* argument)
void WebFrameLoaderClient::dispatchDecidePolicyForNavigationAction(FramePolicyFunction function, const NavigationAction& navigationAction, const ResourceRequest& request, PassRefPtr<FormState> formState)
void PolicyChecker::continueAfterNavigationPolicy(PolicyAction policy)
After Policy Check
void FrameLoader::callContinueLoadAfterNavigationPolicy(void* argument,
    const ResourceRequest& request, PassRefPtr<FormState> formState, bool shouldContinue)
void FrameLoader::continueLoadAfterNavigationPolicy(const ResourceRequest&, PassRefPtr<FormState> formState, bool shouldContinue) 
Resource Load
void FrameLoader::continueLoadAfterWillSubmitForm()
bool DocumentLoader::startLoadingMainResource(unsigned long identifier)
bool MainResourceLoader::load(const ResourceRequest& r, const SubstituteData& substituteData)
bool MainResourceLoader::loadNow(ResourceRequest& r)
PassRefPtr<ResourceHandle> ResourceHandle::create(NetworkingContext* context, const ResourceRequest& request, ResourceHandleClient* client, bool defersLoading, bool shouldContentSniff)
bool ResourceHandle::start(NetworkingContext* context)  // It is depends on your platform and it get into ResourceHandleCFNet::start in my case.
>> return to head of call stack


The dll of ResourceHandleCFNet will connect to resource file to get html data.
After data is retrieved from network, the networking component will call back and come up with html data.

Data Receive 
static void ResourceHandleCFNet::didReceiveData(CFURLConnectionRef conn, CFDataRef data, CFIndex originalLength, const void* clientInfo)
void ResourceLoader::didReceiveData(ResourceHandle*, const char* data, int length, int encodedDataLength)
void MainResourceLoader::didReceiveData(const char* data, int length, long long encodedDataLength, bool allAtOnce)
void ResourceLoader::didReceiveData(const char* data, int length, long long encodedDataLength, bool allAtOnce)
void MainResourceLoader::addData(const char* data, int length, bool allAtOnce)
void ResourceLoader::addData(const char* data, int length, bool allAtOnce)
void DocumentLoader::receivedData(const char* data, int length)
void DocumentLoader::commitLoad(const char* data, int length)
void WebFrameLoaderClient::committedLoad(DocumentLoader* loader, const char* data, int length)
void DocumentLoader::commitData(const char* bytes, size_t length)
void DocumentWriter::addData(const char* bytes, size_t length)

>> return to head of call stack


Finish Loading
static void ResourceHandleCFNet::didFinishLoading(CFURLConnectionRef conn, const void* clientInfo)
void ResourceLoader::didFinishLoading(ResourceHandle*, double finishTime)
void MainResourceLoader::didFinishLoading(double finishTime)
void FrameLoader::finishedLoading()
void DocumentLoader::finishedLoading()
void FrameLoader::finishedLoadingDocument(DocumentLoader* loader)
void WebFrameLoaderClient::finishedLoading(DocumentLoader* loader)
void DocumentWriter::end()
void FrameLoader::didEndDocument()
void DocumentWriter::endIfNotLoadingMainResource()


So far, It had finished URL connect and data received, next step would be data parsing.

2012年3月16日 星期五

Convert between char *、wchar_t*、_bstr_t、CComBSTR、CString、basic_string and System.String


Reference: http://msdn.microsoft.com/zh-tw/library/ms235631(v=vs.80).aspx

Convert From char *

// convert_from_char.cpp
// compile with /clr /link comsuppw.lib

#include <iostream>
#include <stdlib.h>
#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"

using namespace std;
using namespace System;

int main()
{
    char *orig = "Hello, World!";
    cout << orig << " (char *)" << endl;

    // Convert to a wchar_t*
    size_t origsize = strlen(orig) + 1;
    const size_t newsize = 100;
    size_t convertedChars = 0;
    wchar_t wcstring[newsize];
    mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
    wcscat_s(wcstring, L" (wchar_t *)");
    wcout << wcstring << endl;

    // Convert to a _bstr_t
    _bstr_t bstrt(orig);
    bstrt += " (_bstr_t)";
    cout << bstrt << endl;

    // Convert to a CComBSTR
    CComBSTR ccombstr(orig);
    if (ccombstr.Append(L" (CComBSTR)") == S_OK)
    {
        CW2A printstr(ccombstr);
        cout << printstr << endl;
    }

    // Convert to a CString
    CString cstring(orig);
    cstring += " (CString)";
    cout << cstring << endl;

    // Convert to a basic_string
    string basicstring(orig);
    basicstring += " (basic_string)";
    cout << basicstring << endl;

    // Convert to a System::String
    String ^systemstring = gcnew String(orig);
    systemstring += " (System::String)";
    Console::WriteLine("{0}", systemstring);
    delete systemstring;
}

OutPut

Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)

Convert From wchar_t *

// convert_from_wchar_t.cpp
// compile with /clr /link comsuppw.lib

#include <iostream>
#include <stdlib.h>
#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"

using namespace std;
using namespace System;

int main()
{
    wchar_t *orig = L"Hello, World!";
    wcout << orig << L" (wchar_t *)" << endl;

    // Convert to a char*
    size_t origsize = wcslen(orig) + 1;
    const size_t newsize = 100;
    size_t convertedChars = 0;
    char nstring[newsize];
    wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE);
    strcat_s(nstring, " (char *)");
    cout << nstring << endl;

    // Convert to a _bstr_t
    _bstr_t bstrt(orig);
    bstrt += " (_bstr_t)";
    cout << bstrt << endl;

    // Convert to a CComBSTR
    CComBSTR ccombstr(orig);
    if (ccombstr.Append(L" (CComBSTR)") == S_OK)
    {
        CW2A printstr(ccombstr);
        cout << printstr << endl;
    }

    // Convert to a CString
    CString cstring(orig);
    cstring += " (CString)";
    cout << cstring << endl;

    // Convert to a basic_string
    wstring basicstring(orig);
    basicstring += L" (basic_string)";
    wcout << basicstring << endl;

    // Convert to a System::String
    String ^systemstring = gcnew String(orig);
    systemstring += " (System::String)";
    Console::WriteLine("{0}", systemstring);
    delete systemstring;
}

OutPut

Hello, World! (wchar_t *)
Hello, World! (char *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)

Convert From _bstr_t

// convert_from_bstr_t.cpp
// compile with /clr /link comsuppw.lib

#include <iostream>
#include <stdlib.h>
#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"

using namespace std;
using namespace System;

int main()
{
    _bstr_t orig("Hello, World!");
    wcout << orig << " (_bstr_t)" << endl;

    // Convert to a char*
    const size_t newsize = 100;
    char nstring[newsize];
    strcpy_s(nstring, (char *)orig);
    strcat_s(nstring, " (char *)");
    cout << nstring << endl;

    // Convert to a wchar_t*
    wchar_t wcstring[newsize];
    wcscpy_s(wcstring, (wchar_t *)orig);
    wcscat_s(wcstring, L" (wchar_t *)");
    wcout << wcstring << endl;

    // Convert to a CComBSTR
    CComBSTR ccombstr((char *)orig);
    if (ccombstr.Append(L" (CComBSTR)") == S_OK)
    {
        CW2A printstr(ccombstr);
        cout << printstr << endl;
    }

    // Convert to a CString
    CString cstring((char *)orig);
    cstring += " (CString)";
    cout << cstring << endl;

    // Convert to a basic_string
    string basicstring((char *)orig);
    basicstring += " (basic_string)";
    cout << basicstring << endl;

    // Convert to a System::String
    String ^systemstring = gcnew String((char *)orig);
    systemstring += " (System::String)";
    Console::WriteLine("{0}", systemstring);
    delete systemstring;
}

OutPut

Hello, World! (_bstr_t)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)

Convert From CComBSTR

// convert_from_ccombstr.cpp
// compile with /clr /link comsuppw.lib

#include <iostream>
#include <stdlib.h>
#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
#include "vcclr.h"

using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;

int main()
{
    CComBSTR orig("Hello, World!");
    CW2A printstr(orig);
    cout << printstr << " (CComBSTR)" << endl;

    // Convert to a char*
    const size_t newsize = 100;
    char nstring[newsize];
    CW2A tmpstr1(orig);
    strcpy_s(nstring, tmpstr1);
    strcat_s(nstring, " (char *)");
    cout << nstring << endl;

    // Convert to a wchar_t*
    wchar_t wcstring[newsize];
    wcscpy_s(wcstring, orig);
    wcscat_s(wcstring, L" (wchar_t *)");
    wcout << wcstring << endl;

    // Convert to a _bstr_t
    _bstr_t bstrt(orig);
    bstrt += " (_bstr_t)";
    cout << bstrt << endl;

    // Convert to a CString
    CString cstring(orig);
    cstring += " (CString)";
    cout << cstring << endl;

    // Convert to a basic_string
    wstring basicstring(orig);
    basicstring += L" (basic_string)";
    wcout << basicstring << endl;

    // Convert to a System::String
    String ^systemstring = gcnew String(orig);
    systemstring += " (System::String)";
    Console::WriteLine("{0}", systemstring);
    delete systemstring;
}

OutPut

Hello, World! (CComBSTR)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)

Convert From CString

// convert_from_cstring.cpp
// compile with /clr /link comsuppw.lib

#include <iostream>
#include <stdlib.h>
#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"

using namespace std;
using namespace System;

int main()
{
    CString orig("Hello, World!");
    wcout << orig << " (CString)" << endl;

    // Convert to a char*
    const size_t newsize = 100;
    char nstring[newsize];
    strcpy_s(nstring, orig);
    strcat_s(nstring, " (char *)");
    cout << nstring << endl;

    // Convert to a wchar_t*
    // You must first convert to a char * for this to work.
    size_t origsize = strlen(orig) + 1;
    size_t convertedChars = 0;
    wchar_t wcstring[newsize];
    mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
    wcscat_s(wcstring, L" (wchar_t *)");
    wcout << wcstring << endl;

    // Convert to a _bstr_t
    _bstr_t bstrt(orig);
    bstrt += " (_bstr_t)";
    cout << bstrt << endl;

    // Convert to a CComBSTR
    CComBSTR ccombstr(orig);
    if (ccombstr.Append(L" (CComBSTR)") == S_OK)
    {
        CW2A printstr(ccombstr);
        cout << printstr << endl;
    }

    // Convert to a basic_string
    string basicstring(orig);
    basicstring += " (basic_string)";
    cout << basicstring << endl;

    // Convert to a System::String
    String ^systemstring = gcnew String(orig);
    systemstring += " (System::String)";
    Console::WriteLine("{0}", systemstring);
    delete systemstring;
}

OutPut

Hello, World! (CString)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (basic_string)
Hello, World! (System::String)

Convert From basic_string

// convert_from_basic_string.cpp
// compile with /clr /link comsuppw.lib

#include <iostream>
#include <stdlib.h>
#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"

using namespace std;
using namespace System;

int main()
{
    string orig("Hello, World!");
    cout << orig << " (basic_string)" << endl;

    // Convert to a char*
    const size_t newsize = 100;
    char nstring[newsize];
    strcpy_s(nstring, orig.c_str());
    strcat_s(nstring, " (char *)");
    cout << nstring << endl;

    // Convert to a wchar_t*
    // You must first convert to a char * for this to work.
    size_t origsize = strlen(orig.c_str()) + 1;
    size_t convertedChars = 0;
    wchar_t wcstring[newsize];
    mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
    wcscat_s(wcstring, L" (wchar_t *)");
    wcout << wcstring << endl;

    // Convert to a _bstr_t
    _bstr_t bstrt(orig.c_str());
    bstrt += " (_bstr_t)";
    cout << bstrt << endl;

    // Convert to a CComBSTR
    CComBSTR ccombstr(orig.c_str());
    if (ccombstr.Append(L" (CComBSTR)") == S_OK)
    {
        CW2A printstr(ccombstr);
        cout << printstr << endl;
    }

    // Convert to a CString
    CString cstring(orig.c_str());
    cstring += " (CString)";
    cout << cstring << endl;

    // Convert to a System::String
    String ^systemstring = gcnew String(orig.c_str());
    systemstring += " (System::String)";
    Console::WriteLine("{0}", systemstring);
    delete systemstring;
}

Output

Hello, World! (basic_string)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (System::String)

Convert From System::String

// convert_from_system_string.cpp
// compile with /clr /link comsuppw.lib

#include <iostream>
#include <stdlib.h>
#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
#include "vcclr.h"

using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;

int main()
{
    String ^orig = gcnew String("Hello, World!");
    Console::WriteLine("{0} (System::String)", orig);

    pin_ptr<const wchar_t> wch = PtrToStringChars(orig);

    // Convert to a char*
    size_t origsize = wcslen(wch) + 1;
    const size_t newsize = 100;
    size_t convertedChars = 0;
    char nstring[newsize];
    wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
    strcat_s(nstring, " (char *)");
    cout << nstring << endl;

    // Convert to a wchar_t*
    wchar_t wcstring[newsize];
    wcscpy_s(wcstring, wch);
    wcscat_s(wcstring, L" (wchar_t *)");
    wcout << wcstring << endl;

    // Convert to a _bstr_t
    _bstr_t bstrt(wch);
    bstrt += " (_bstr_t)";
    cout << bstrt << endl;

    // Convert to a CComBSTR
    CComBSTR ccombstr(wch);
    if (ccombstr.Append(L" (CComBSTR)") == S_OK)
    {
        CW2A printstr(ccombstr);
        cout << printstr << endl;
    }

    // Convert to a CString
    CString cstring(wch);
    cstring += " (CString)";
    cout << cstring << endl;

    // Convert to a basic_string
    wstring basicstring(wch);
    basicstring += L" (basic_string)";
    wcout << basicstring << endl;

    delete orig;
}

Output

Hello, World! (System::String)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string)

2012年3月14日 星期三

LITTLE_ENDIAN and BIG_ENDIAN

The keywords LITTLE_ENDIAN and BIG_ENDIAN could specify store order in memory.


If there is a two byte data in memory, Ex: long d=0x12345678
It would be store as the order as below table.
big-endian     little-endian
0x0000    
0x12     
0x78
0x0001
0x34
0x56
0x0002
0x56
0x34
0x0003
0x78
0x12


What different ?


for some CPU:
  • INTEL X86、DEC VAX use LITTLE-ENDIAN
  • HP、IBM、MOTOROLA 68K use BIG-ENDIAN
  • POWERPC support both,called BI-ENDIAN


2012年3月1日 星期四

Breakpoint Invalid in Platform Building Project

If your process couldn't stop at breakpoint, please check the project's property.
Try to toggle on the 'Enable kernel debugger'