~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to system/lib/libcxx/support/win32/support.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
//
9
9
//===----------------------------------------------------------------------===//
10
10
 
11
 
#include <support/win32/support.h>
12
 
#include <stdarg.h> // va_start, va_end
13
 
#include <stddef.h> // size_t
14
 
#include <stdlib.h> // malloc
15
 
#include <stdio.h>  // vsprintf, vsnprintf
16
 
#include <string.h> // strcpy, wcsncpy
17
 
 
18
 
int asprintf(char **sptr, const char *__restrict fmt, ...)
 
11
#include <cstdarg> // va_start, va_end
 
12
#include <cstddef> // size_t
 
13
#include <cstdlib> // malloc
 
14
#include <cstdio>  // vsprintf, vsnprintf
 
15
#include <cstring> // strcpy, wcsncpy
 
16
#include <cwchar>  // mbstate_t
 
17
#include <memory> // unique_ptr
 
18
 
 
19
namespace { // Private
 
20
 
 
21
    struct free_deleter { 
 
22
        inline void operator()(char* p) { free(p); } 
 
23
    }; 
 
24
}
 
25
// Some of these functions aren't standard or if they conform, the name does not.
 
26
 
 
27
int asprintf(char **sptr, const char *__restrict format, ...)
19
28
{
20
29
    va_list ap;
21
 
    va_start(ap, fmt);
22
 
    int result = vasprintf(sptr, fmt, ap);
 
30
    va_start(ap, format);
 
31
    int result;
 
32
#ifndef _LIBCPP_NO_EXCEPTIONS
 
33
    try {
 
34
#endif
 
35
        result = vasprintf(sptr, format, ap);
 
36
#ifndef _LIBCPP_NO_EXCEPTIONS
 
37
    } catch( ... ) {
 
38
        va_end(ap);
 
39
        throw;
 
40
    }
 
41
#endif
23
42
    va_end(ap);
24
43
    return result;
25
44
}
26
 
int vasprintf( char **sptr, const char *__restrict fmt, va_list ap )
 
45
 
 
46
// Like sprintf, but when return value >= 0 it returns a pointer to a malloc'd string in *sptr.
 
47
// If return >= 0, use free to delete *sptr.
 
48
int vasprintf( char **sptr, const char *__restrict format, va_list ap )
27
49
{
28
50
    *sptr = NULL;
29
 
    int count = vsnprintf( *sptr, 0, fmt, ap );
30
 
    if( (count >= 0) && ((*sptr = (char*)malloc(count+1)) != NULL) )
31
 
    {
32
 
        vsprintf( *sptr, fmt, ap );
33
 
        sptr[count] = '\0';
 
51
    int count = _vsnprintf( NULL, 0, format, ap ); // Query the buffer size required.
 
52
    if( count >= 0 ) {
 
53
        std::unique_ptr<char, free_deleter> p( static_cast<char*>(malloc(count+1)) );
 
54
        if ( ! p )
 
55
            return -1;
 
56
        if ( vsnprintf( p.get(), count+1, format, ap ) == count ) // We should have used exactly what was required.
 
57
            *sptr = p.release();
 
58
        else // Otherwise something is wrong, likely a bug in vsnprintf. If so free the memory and report the error.
 
59
            return -1; // Pointer will get automaticlaly deleted.
34
60
    }
35
61
 
36
62
    return count;
37
63
}
38
64
 
39
 
// FIXME: use wcrtomb and avoid copy
40
 
// use mbsrtowcs which is available, first copy first nwc elements of src
 
65
// Returns >= 0: the number of wide characters found in the multi byte sequence src (of src_size_bytes),
 
66
// that fit in the buffer dst (of max_dest_chars elements size). The count returned excludes the null terminator.
 
67
// When dst is NULL, no characters are copied and no "out" parameters are updated.
 
68
// Returns (size_t) -1: an incomplete sequence encountered.
 
69
// Leaves *src pointing the next character to convert or NULL if a null character was converted from *src.
41
70
size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
42
 
                   size_t nmc, size_t len, mbstate_t *__restrict ps )
 
71
                   size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps )
43
72
{
44
 
    char* local_src = new char[nmc+1];
45
 
    char* nmcsrc = local_src;
46
 
    strncpy( nmcsrc, *src, nmc );
47
 
    nmcsrc[nmc] = '\0';
48
 
    const size_t result = mbsrtowcs( dst, const_cast<const char **>(&nmcsrc), len, ps );
49
 
    // propagate error
50
 
    if( nmcsrc == NULL )
51
 
        *src = NULL;
52
 
    delete[] local_src;
53
 
    return result;
 
73
    const size_t terminated_sequence = static_cast<size_t>(0);
 
74
    //const size_t invalid_sequence = static_cast<size_t>(-1);
 
75
    const size_t incomplete_sequence = static_cast< size_t>(-2);
 
76
 
 
77
    size_t dest_converted = 0;
 
78
    size_t source_converted = 0;
 
79
    size_t source_remaining = src_size_bytes;
 
80
    size_t result = 0;
 
81
    bool have_result = false;
 
82
 
 
83
    while ( source_remaining ) {
 
84
        if ( dst && dest_converted >= max_dest_chars )
 
85
            break;
 
86
        // Converts one multi byte character.
 
87
        // if result > 0, it's the size in bytes of that character.
 
88
        // othewise if result is zero it indicates the null character has been found.
 
89
        // otherwise it's an error and errno may be set.
 
90
        size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps );
 
91
        // Don't do anything to change errno from here on.
 
92
        if ( char_size > 0 ) {
 
93
            source_remaining -= char_size;
 
94
            source_converted += char_size;
 
95
            ++dest_converted;
 
96
            continue;
 
97
        }
 
98
        result = char_size;
 
99
        have_result = true;
 
100
        break;
 
101
    }
 
102
    if ( dst ) {
 
103
        if ( have_result && result == terminated_sequence )
 
104
            *src = NULL;
 
105
        else
 
106
            *src += source_converted;
 
107
    }
 
108
    if ( have_result && result != terminated_sequence && result != incomplete_sequence )
 
109
        return static_cast<size_t>(-1);
 
110
 
 
111
    return dest_converted;
54
112
}
55
 
// FIXME: use wcrtomb and avoid copy
56
 
// use wcsrtombs which is available, first copy first nwc elements of src
 
113
 
 
114
// Converts max_source_chars from the wide character buffer pointer to by *src,
 
115
// into the multi byte character sequence buffer stored at dst which must be dst_size_bytes bytes in size.
 
116
// Returns >= 0: the number of bytes in the sequence sequence converted frome *src, excluding the null terminator.
 
117
// Returns size_t(-1) if an error occurs, also sets errno.
 
118
// If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst and no "out" parameters are updated.
57
119
size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
58
 
                   size_t nwc, size_t len, mbstate_t *__restrict ps )
 
120
                   size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
59
121
{
60
 
    wchar_t* local_src = new wchar_t[nwc];
61
 
    wchar_t* nwcsrc = local_src;
62
 
    wcsncpy(nwcsrc, *src, nwc);
63
 
    nwcsrc[nwc] = '\0';
64
 
    const size_t result = wcsrtombs( dst, const_cast<const wchar_t **>(&nwcsrc), len, ps );
65
 
    // propogate error
66
 
    if( nwcsrc == NULL )
67
 
        *src = NULL;
68
 
    delete[] nwcsrc;
69
 
    return result;
 
122
    //const size_t invalid_sequence = static_cast<size_t>(-1);
 
123
 
 
124
    size_t source_converted = 0;
 
125
    size_t dest_converted = 0;
 
126
    size_t dest_remaining = dst_size_bytes;
 
127
    size_t char_size = 0;
 
128
    const errno_t no_error = ( errno_t) 0;
 
129
    errno_t result = ( errno_t ) 0;
 
130
    bool have_result = false;
 
131
    bool terminator_found = false;
 
132
 
 
133
    while ( source_converted != max_source_chars ) {
 
134
        if ( ! dest_remaining )
 
135
            break;
 
136
        wchar_t c = (*src)[source_converted];
 
137
        if ( dst )
 
138
            result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
 
139
        else
 
140
            result = wcrtomb_s( &char_size, NULL, 0, c, ps);
 
141
        // If result is zero there is no error and char_size contains the size of the multi-byte-sequence converted.
 
142
        // Otherwise result indicates an errno type error.
 
143
        if ( result == no_error ) {
 
144
            if ( c == L'\0' ) {
 
145
                terminator_found = true;
 
146
                break;
 
147
            }
 
148
            ++source_converted;
 
149
            if ( dst )
 
150
                dest_remaining -= char_size;
 
151
            dest_converted += char_size;
 
152
            continue;
 
153
        }
 
154
        have_result = true;
 
155
        break;
 
156
    }
 
157
    if ( dst ) {
 
158
        if ( terminator_found )
 
159
            *src = NULL;
 
160
        else
 
161
            *src = *src + source_converted;
 
162
    }
 
163
    if ( have_result && result != no_error ) {
 
164
        errno = result;
 
165
        return static_cast<size_t>(-1);
 
166
    }
 
167
 
 
168
    return dest_converted;
70
169
}