kotoraさんのブログ-net_test

前記事のプログラムの入力をURL形式に変更してみました。
それに加えて、出力漢字コード変換を入れてみた。


動作部分
private: System::Void button1_Click(
System::Object^ sender, System::EventArgs^ e) {
int line = 0;
char work[RCV_BUFF_SIZE+1] = "";
char buff[RCV_BUFF_SIZE+1] = "";
char *host;
char *root = "/";
char *path;
int port;
int rt = 0;

host = get_host(textBox1->Text);
path = get_path(textBox1->Text);
if(path == NULL) {
path = root;
}
port = get_port(textBox1->Text);
if(host != NULL) {
//接続
if(!TCPConnect(host, port)) {
rt = -1;
} else {
//送信
sprintf_s(buff, sizeof(buff),
"GET %s HTTP/1.0\n\n", path);
if(!TCPSend(buff)) {
rt = -2;
}
//読み出し
if(!TCPRead(work,(int)sizeof(work))) {
rt = -3;
}
// 漢字コード変換に外部 nkf32.dll を使用
SetNkfOption("-s -Lw");
NkfConvert(buff, work);
}
TCPClose();
} else {
rt = -4;
}
if(rt == 0) {
textBox3->Text = gcnew String(buff);
} else {
if(host != NULL) {
sprintf_s(buff, sizeof(buff),
"ERROR: rtcode = %d\r\nHOST: %s\r\nPATH: %s\r\n",
rt, host, path);
} else {
sprintf_s(buff, sizeof(buff),
"ERROR: rtcode = %d\r\n", rt);
}
textBox3->Text = gcnew String(buff);
}
if(host != NULL) { free(host); }
if(path != root) { free(path); }
}

char *conv_char_ptr(System::String^ str) {
pin_ptr<const wchar_t> wch = PtrToStringChars(str);
char *buf = (char *)malloc(HOST_NAME_LEN+1);

sprintf_s(buf, (HOST_NAME_LEN+1), "%S", wch);
return buf;
}

char *get_host(System::String^ str) {
String^ pattern = "http://(?<url>[^/^:]*)";
Regex^ regex = gcnew Regex( pattern );
Match^ match = regex->Match( str );
char *host;

if(match->;Success == true) {
host = conv_char_ptr(match->Groups["url"]->Value);
} else {
host = NULL;
}
return host;
}

char *get_path(System::String^ str) {
String^ pattern = "http://[^/]*(?<path>/.*)";
Regex^ regex = gcnew Regex( pattern );
Match^ match = regex->Match( str );
char *path;

if(match->Success == true) {
path = conv_char_ptr(match->Groups["path"]->Value);
} else {
path = NULL;
}
return path;
}

int get_port(System::String^ str) {
String^ pattern = "http://[^/^:]*:(?<port>[0-9]+)";
Regex^ regex = gcnew Regex( pattern );
Match^ match = regex->Match( str );
int port = 80;

if(match->Success == true) {
port = Convert::ToInt32(match->Groups["port"]->Value);
}
return port;
}