replay get form stewgate

Dependencies:   EthernetNetIf NetServices mbed

Fork of HTTPClientExample by Donatien Garnier

Files at this revision

API Documentation at this revision

Comitter:
nameless129
Date:
Sat Aug 31 07:55:46 2013 +0000
Parent:
3:5964d831654c
Commit message:
reply get from stewgate

Changed in this revision

HTTPClient.lib Show diff for this revision Revisions of this file
HTTPClientExample.cpp Show annotated file Show diff for this revision Revisions of this file
NetServices.lib Show annotated file Show diff for this revision Revisions of this file
utf8tosjis.c Show annotated file Show diff for this revision Revisions of this file
utf8tosjis.h Show annotated file Show diff for this revision Revisions of this file
--- a/HTTPClient.lib	Thu Aug 05 15:12:04 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/donatien/code/HTTPClient/#d97a4fc01c86
--- a/HTTPClientExample.cpp	Thu Aug 05 15:12:04 2010 +0000
+++ b/HTTPClientExample.cpp	Sat Aug 31 07:55:46 2013 +0000
@@ -1,38 +1,67 @@
+#pragma import __use_all_ctype
+
 #include "mbed.h"
 #include "EthernetNetIf.h"
 #include "HTTPClient.h"
+#include "utf8tosjis.h"
+#include <string.h>
 
 EthernetNetIf eth;
 HTTPClient http;
-  
+Serial pc(USBTX, USBRX); // tx, rx
+
 int main() {
+    char  buf_s[256];
+    char  buf_u[256];
+
+    printf("Setting up...\r\n");
+    EthernetErr ethErr = eth.setup();
+    if(ethErr)
+    {
+        printf("Error %d in setup.\r\n", ethErr);
+        return -1;
+    }
+    printf("Setup OK\r\n");
+
+    HTTPText txt;
+    HTTPMap token;
+    token["_t"] = "";//input token
+    //token["msg"] = "test";  
+
+    HTTPResult r = http.post("http://stewgate-u.appspot.com/api/last_mention/", token, &txt);
 
-  printf("Setting up...\n");
-  EthernetErr ethErr = eth.setup();
-  if(ethErr)
-  {
-    printf("Error %d in setup.\n", ethErr);
-    return -1;
-  }
-  printf("Setup OK\n");
-  
-  HTTPText txt;
-  
-  HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt);
-  if(r==HTTP_OK)
-  {
-    printf("Result :\"%s\"\n", txt.gets()); 
-  }
-  else
-  {
-    printf("Error %d\n", r);
-  }
-  
-  while(1)
-  {
-  
-  }
-  
-  return 0;
-  
+    if(r==HTTP_OK)
+    {
+      printf("Result :\"%s\"\r\n", txt.gets()); 
+    }
+    else
+    {
+      printf("Error %d\r\n", r);
+    }
+
+    strcpy(buf_u, txt.gets());  
+    size_t sz;
+    // assuming UTF-8 is NULL terminated
+    sz = strlen(buf_u);
+    
+    printf("\r\nUTF-8:\r\n");
+    for(int i=0; i<sz; i++) {
+        printf("0x%02x ", buf_u[i]);
+    }
+
+    int len = utf8tosjis(buf_u, sz, buf_s, sizeof(buf_s));
+
+    printf("\r\nSJIS:\r\n");
+    if (len != -1) {
+        for(int i=0; i<len; i++) {
+            printf("0x%02x ", buf_s[i]);
+        }
+        printf("\r\n");
+    }
+    printf("%s", buf_s);
+    
+    while(1)
+    {
+    }
+    return 0;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NetServices.lib	Sat Aug 31 07:55:46 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/segundo/code/NetServices/#4e2468d7d5cb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utf8tosjis.c	Sat Aug 31 07:55:46 2013 +0000
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <locale.h>
+#include <cwchar>
+
+int utf8tosjis(const char* utfBuffer, int utfBufLen, char* sjisBuffer, int sjisBufLen)
+{
+    wchar_t wstr[256];
+    int         i, wi;
+    wchar_t     wc;
+    mbstate_t   state = {0};
+    size_t      ret;
+    char *      current_locale;
+    i = wi = 0;
+
+    current_locale = setlocale(LC_CTYPE, "UTF-8");
+    if (current_locale == NULL)
+        return 0;
+
+    while (1) {
+        ret = mbrtowc(&wc, utfBuffer+i, 3, &state);
+        if (ret == (size_t)-2 || ret == (size_t)-1) {
+            printf("\nThere was a problem decoding the multibyte string.\n");
+            return ret;
+        } else if (ret == 0) {
+            break;          /* we hit \0, end of string */
+        } else {
+            i += ret;
+            wstr[wi++] = wc;
+        }
+    }
+    wstr[wi] = L'\0';
+    
+    current_locale = setlocale(LC_CTYPE, "SJIS");
+    if (current_locale == NULL)
+        return 0;
+
+    ret = wcstombs(sjisBuffer, wstr, sjisBufLen);
+
+    return ret;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utf8tosjis.h	Sat Aug 31 07:55:46 2013 +0000
@@ -0,0 +1,6 @@
+#ifndef __UTF8TOSJIS_H_
+#define __UTF8TOSJIS_H_
+
+extern "C" int utf8tosjis(const char* utfBuffer, int utfBufLen, char* sjisBuffer, int sjisBufLen);
+
+#endif
\ No newline at end of file