Work on Windows port by Ruben Van Boxem

llvm-svn: 140384
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: dbe8111948d372a6f7445e86a0ac985717fbe1bc
diff --git a/src/locale.cpp b/src/locale.cpp
index 8f1e4e1..388660d 100644
--- a/src/locale.cpp
+++ b/src/locale.cpp
@@ -19,7 +19,11 @@
 #include "cstring"
 #include "cwctype"
 #include "__sso_allocator"
+#if _WIN32
+#include <locale.h>
+#else // _WIN32
 #include <langinfo.h>
+#endif // _!WIN32
 #include <stdlib.h>
 
 #ifdef _LIBCPP_STABLE_APPLE_ABI
@@ -5568,16 +5572,29 @@
         __frac_digits_ = lc->int_frac_digits;
     else
         __frac_digits_ = base::do_frac_digits();
+#if _WIN32
+    if (lc->p_sign_posn == 0)
+#else // _WIN32
     if (lc->int_p_sign_posn == 0)
+#endif //_WIN32
         __positive_sign_ = "()";
     else
         __positive_sign_ = lc->positive_sign;
+#if _WIN32
+    if(lc->n_sign_posn == 0)
+#else // _WIN32
     if (lc->int_n_sign_posn == 0)
+#endif // _WIN32
         __negative_sign_ = "()";
     else
         __negative_sign_ = lc->negative_sign;
+#if _WIN32
+    __init_pat(__pos_format_, lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn);
+    __init_pat(__neg_format_, lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn);
+#else
     __init_pat(__pos_format_, lc->int_p_cs_precedes, lc->int_p_sep_by_space, lc->int_p_sign_posn);
     __init_pat(__neg_format_, lc->int_n_cs_precedes, lc->int_n_sep_by_space, lc->int_n_sign_posn);
+#endif // _WIN32
 }
 
 template<>
@@ -5698,7 +5715,11 @@
         __frac_digits_ = lc->int_frac_digits;
     else
         __frac_digits_ = base::do_frac_digits();
+#if _WIN32
+    if (lc->p_sign_posn == 0)
+#else // _WIN32
     if (lc->int_p_sign_posn == 0)
+#endif // _WIN32
         __positive_sign_ = L"()";
     else
     {
@@ -5714,7 +5735,11 @@
         wbe = wbuf + j;
         __positive_sign_.assign(wbuf, wbe);
     }
+#if _WIN32
+    if (lc->n_sign_posn == 0)
+#else // _WIN32
     if (lc->int_n_sign_posn == 0)
+#endif // _WIN32
         __negative_sign_ = L"()";
     else
     {
@@ -5730,8 +5755,13 @@
         wbe = wbuf + j;
         __negative_sign_.assign(wbuf, wbe);
     }
+#if _WIN32
+    __init_pat(__pos_format_, lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn);
+    __init_pat(__neg_format_, lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn);
+#else // _WIN32
     __init_pat(__pos_format_, lc->int_p_cs_precedes, lc->int_p_sep_by_space, lc->int_p_sign_posn);
     __init_pat(__neg_format_, lc->int_n_cs_precedes, lc->int_n_sep_by_space, lc->int_n_sign_posn);
+#endif // _WIN32
 }
 
 void __do_nothing(void*) {}
diff --git a/src/string.cpp b/src/string.cpp
index 7e651a1..1f58e36 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -11,6 +11,9 @@
 #include "cstdlib"
 #include "cwchar"
 #include "cerrno"
+#if _WIN32
+#include "support/win32/support.h"
+#endif // _WIN32
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
diff --git a/src/support/win32/support.cpp b/src/support/win32/support.cpp
index 7033809..db80063 100644
--- a/src/support/win32/support.cpp
+++ b/src/support/win32/support.cpp
@@ -8,11 +8,17 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <stddef.h> // size_t
+#include <stdlib.h> // malloc
+#include <stdio.h>  // vsprintf, vsnprintf
+#include <string.h> // strcpy, wcsncpy
+#include <wchar.h>  // mbstate_t
+
 int vasprintf( char **sptr, const char *__restrict__ fmt, va_list ap )
 {
-    *sptr = NULL
+    *sptr = NULL;
     int count = vsnprintf( *sptr, 0, fmt, ap );
-    if( (count >= 0) && ((*sptr = malloc(count+1)) != NULL) )
+    if( (count >= 0) && ((*sptr = (char*)malloc(count+1)) != NULL) )
     {
         vsprintf( *sptr, fmt, ap );
         sptr[count] = '\0';
@@ -20,3 +26,36 @@
 
     return count;
 }
+
+// FIXME: use wcrtomb and avoid copy
+// use mbsrtowcs which is available, first copy first nwc elements of src
+size_t mbsnrtowcs( wchar_t *__restrict__ dst, const char **__restrict__ src,
+                   size_t nmc, size_t len, mbstate_t *__restrict__ ps )
+{
+    char* local_src = new char[nmc+1];
+    char* nmcsrc = local_src;
+    strncpy( nmcsrc, *src, nmc );
+    nmcsrc[nmc] = '\0';
+    const size_t result = mbsrtowcs( dst, const_cast<const char **>(&nmcsrc), len, ps );
+    // propagate error
+    if( nmcsrc == NULL )
+        *src = NULL;
+    delete[] local_src;
+    return result;
+}
+// FIXME: use wcrtomb and avoid copy
+// use wcsrtombs which is available, first copy first nwc elements of src
+size_t wcsnrtombs( char *__restrict__ dst, const wchar_t **__restrict__ src,
+                   size_t nwc, size_t len, mbstate_t *__restrict__ ps )
+{
+    wchar_t* local_src = new wchar_t[nwc];
+    wchar_t* nwcsrc = local_src;
+    wcsncpy(nwcsrc, *src, nwc);
+    nwcsrc[nwc] = '\0';
+    const size_t result = wcsrtombs( dst, const_cast<const wchar_t **>(&nwcsrc), len, ps );
+    // propogate error
+    if( nwcsrc == NULL )
+        *src = NULL;
+    delete[] nwcsrc;
+    return result;
+}
diff --git a/src/thread.cpp b/src/thread.cpp
index 4ccff32..b07f8f8 100644
--- a/src/thread.cpp
+++ b/src/thread.cpp
@@ -12,7 +12,9 @@
 #include "vector"
 #include "future"
 #include <sys/types.h>
+#if !_WIN32
 #include <sys/sysctl.h>
+#endif // _WIN32
 
 _LIBCPP_BEGIN_NAMESPACE_STD