/* 对给定的字符串queryCharStr(不一定以0结尾),输出查找到的字符串resultStr(类型为StrPtrLen),区分大小写,最后返回值是查找到的字符串 */
char *StrPtrLen::FindStringCase(char *queryCharStr, StrPtrLen *resultStr, Bool16 caseSensitive) const
{
    // Be careful about exiting this method from the middle. This routine deletes allocated memory at the end.
    // 

	/* make sure the initial condition of output StrPtrLen class  */
	/* 为了存储输出字符串,必须先清空并重置 */
    if (resultStr)
        resultStr->Set(NULL,0);

	/* make sure that the query string no null */
    Assert (NULL != queryCharStr);
    if (NULL == queryCharStr) return NULL;

	/* make sure that the two member of output StrPtrLen class is in their initial condition */
	/* NOTE WE CAN ACCESS DIRECTLY THE TWO MEMBERS because they are not capsulted in StrPtrLen class */
    if (NULL == Ptr) return NULL;
    if (0 == Len) return NULL;
    
    /* what is queryStr()? */
	/* 创建类StrPtrLen的对象并初始化,下面的成员变量就以它为主 */
    StrPtrLen queryStr(queryCharStr);

	/* the temporary variables used below */
	/* the sign of whether append 0 terminated */
    char *editSource = NULL;/* 可能的新建的字符指针 */
    char *resultChar = NULL;/* 最后查找的结果字符串 */

	/* the last char in the source string with length Len */
    char lastSourceChar = Ptr[Len -1];
    
	/* 假如入参字符串的最末字符非零,就在后面补一个零 */
    if (lastSourceChar != 0) //first need to modify for termination,see GetAsCString(). 
    {   editSource = NEW char[Len + 1]; 
	// Ptr could be a static string so make a copy
        ::memcpy( editSource, Ptr, Len );
		/* the next char of the last char of a string is set to 0 */
        editSource[Len] = 0; // this won't work on static strings so we are modifying a new string here
    }
char *StrPtrLen::FindStringCase(char *queryCharStr, StrPtrLen *resultStr, Bool16 caseSensitive) const
{
    // Be careful about exiting this method from the middle. This routine deletes allocated memory at the end.
    // 

    if (resultStr)
        resultStr->Set(NULL,0);

    Assert (NULL != queryCharStr);
    if (NULL == queryCharStr) return NULL;
    if (NULL == Ptr) return NULL;
    if (0 == Len) return NULL;
    

    StrPtrLen queryStr(queryCharStr);
    char *editSource = NULL;
    char *resultChar = NULL;
    char lastSourceChar = Ptr[Len -1];
    
    if (lastSourceChar != 0) // need to modify for termination. 
    {   editSource = NEW char[Len + 1]; // Ptr could be a static string so make a copy
        ::memcpy( editSource, Ptr, Len );
        editSource[Len] = 0; // this won't work on static strings so we are modifing a new string here
    }
Example #3
0
void run_httpsend(bmp2png_buf_desc* pngbuf){
    std::string queryStr("/bin/db/host_parser.cgi");
    for(int i = 0; i < 8; i++){
        queryStr.append(i == 0 ? "?p" : "&p");
        queryStr.push_back('1' + i);
        queryStr.append(".rank=");
        
        TCHAR rankText[2];
        char cRankText[6];
        GetDlgItemText(hDlg, IDC_EDIT2+i, rankText, sizeof(rankText));
        WideCharToMultiByte(CP_THREAD_ACP, 0, rankText, 2, cRankText, 6, NULL, NULL);
        queryStr.append(cRankText);
    }
    
    int queryTextLen = MultiByteToWideChar(CP_THREAD_ACP, 0, (char*) queryStr.c_str(), queryStr.length(), NULL, 0) + 1;
    TCHAR* queryText = new TCHAR[queryTextLen];
    ZeroMemory(queryText, sizeof(TCHAR)*queryTextLen);
    MultiByteToWideChar(CP_THREAD_ACP, 0, (char*)queryStr.c_str(), queryStr.length(), queryText, queryTextLen);

    /* WININET初期化 */
    HINTERNET hInternet = InternetOpen(_T("WININET Sample Program"),
        INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

    /* サーバへ接続 */
    HINTERNET hHttpSession = InternetConnect(hInternet,
        _T("pokonori.fous.jp"), INTERNET_DEFAULT_HTTP_PORT,
        NULL, NULL,
        INTERNET_SERVICE_HTTP,
        0, 0);

    /* HTTP要求の作成 */
    HINTERNET hHttpRequest = HttpOpenRequest(hHttpSession,
        _T("POST"), queryText, NULL, NULL,
        NULL, 0, 0);
    delete[] queryText;

    BOOL res;
    TCHAR* h1 = _T("Content-Type: multipart/form-data; boundary=z--aoeutils--z");
    res = HttpAddRequestHeaders(hHttpRequest, h1, _tcslen(h1), HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);

    const char* b1 = 
        "Content-Type: multipart/form-data; boundary=z--aoeutils--z\x0D\x0A"
        "--z--aoeutils--z\x0D\x0A"
        "Content-Disposition: form-data; name=\"host1\"; filename=\"host1.png\"\x0D\x0A"
        "Content-Type: image/png\x0D\x0A\x0D\x0A";
    const char* b3 = "\x0D\x0A--z--aoeutils--z--\x0D\x0A";
    SIZE_T req_size = strlen(b1) + pngbuf->length + strlen(b3);
    BYTE* req_data = (BYTE*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, req_size);
    CopyMemory(req_data, b1, strlen(b1));
    CopyMemory(req_data + strlen(b1), pngbuf->hptr, pngbuf->length);
    CopyMemory(req_data + strlen(b1) + pngbuf->length, b3, strlen(b3));

    res = HttpSendRequest(hHttpRequest, NULL, 0, req_data, req_size);
    assert(res);
    HeapFree(GetProcessHeap(), 0, req_data);

    DWORD resBufSize = 8192;
    BYTE* resBuf = (BYTE*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, resBufSize);

    std::string resString;
    /* コンテンツの内容を取得・表示 */
    while(1){
        DWORD readSize;
        res = InternetReadFile(hHttpRequest, resBuf, resBufSize, &readSize);
        if(res == 1 && readSize == 0) break;
        assert(res);
        resString.append((char*) resBuf, readSize);
    }
    int resTextLen = MultiByteToWideChar(CP_THREAD_ACP, 0, (char*)resString.c_str(), resString.length(), NULL, 0) + 1;
    TCHAR* resText = new TCHAR[resTextLen];
    ZeroMemory(resText, sizeof(TCHAR)*resTextLen);
    MultiByteToWideChar(CP_THREAD_ACP, 0, (char*)resString.c_str(), resString.length(), resText, resTextLen);
    HWND editCtl = GetDlgItem(hDlg, IDC_EDIT1);
    SetDlgItemText(hDlg, IDC_EDIT1, resText);
    delete[] resText;

    /* 後処理 */
    HeapFree(GetProcessHeap(), 0, resBuf);
    InternetCloseHandle(hHttpRequest);
    InternetCloseHandle(hHttpSession);
    InternetCloseHandle(hInternet);
}