~ubuntu-branches/ubuntu/trusty/libc++/trusty

« back to all changes in this revision

Viewing changes to libcxx/src/support/win32/support.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-02 19:43:49 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20130902194349-top6vl3nq767uhdm
Tags: 1.0~svn189766-1
New snapshot release

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
 
 
18
// Some of these functions aren't standard or if they conform, the name does not.
 
19
 
 
20
int asprintf(char **sptr, const char *__restrict format, ...)
19
21
{
20
22
    va_list ap;
21
 
    va_start(ap, fmt);
22
 
    int result = vasprintf(sptr, fmt, ap);
 
23
    va_start(ap, format);
 
24
    int result;
 
25
    result = vasprintf(sptr, format, ap);
23
26
    va_end(ap);
24
27
    return result;
25
28
}
26
29
 
27
 
// Like sprintf, but when return value >= 0 it returns a pointer to a malloc'd string in *sptr.
 
30
// Like sprintf, but when return value >= 0 it returns
 
31
// a pointer to a malloc'd string in *sptr.
28
32
// If return >= 0, use free to delete *sptr.
29
 
int vasprintf( char **sptr, const char *__restrict fmt, va_list ap )
 
33
int vasprintf( char **sptr, const char *__restrict format, va_list ap )
30
34
{
31
35
    *sptr = NULL;
32
 
    int count = vsnprintf( NULL, 0, fmt, ap ); // Query the buffer size required.
33
 
    if( count >= 0 ) {
34
 
        char* p = static_cast<char*>(malloc(count+1)); // Allocate memory for it and the terminator.
35
 
        if ( p == NULL )
36
 
            return -1;
37
 
        if ( vsnprintf( p, count+1, fmt, ap ) == count ) // We should have used exactly what was required.
38
 
            *sptr = p;
39
 
        else { // Otherwise something is wrong, likely a bug in vsnprintf. If so free the memory and report the error.
40
 
            free(p);
41
 
            return -1;
42
 
        }
 
36
    // Query the count required.
 
37
    int count = _vsnprintf( NULL, 0, format, ap );
 
38
    if (count < 0)
 
39
        return count;
 
40
    size_t buffer_size = static_cast<size_t>(count) + 1;
 
41
    char* p = static_cast<char*>(malloc(buffer_size));
 
42
    if ( ! p )
 
43
        return -1;
 
44
    // If we haven't used exactly what was required, something is wrong.
 
45
    // Maybe bug in vsnprintf. Report the error and return.
 
46
    if (_vsnprintf(p, buffer_size, format, ap) != count) {
 
47
        free(p);
 
48
        return -1;
43
49
    }
44
 
 
 
50
    // All good. This is returning memory to the caller not freeing it.
 
51
    *sptr = p;
45
52
    return count;
46
53
}
47
54
 
48
 
// Returns >= 0: the number of wide characters found in the multi byte sequence src (of src_size_bytes),
49
 
// that fit in the buffer dst (of max_dest_chars elements size). The count returned excludes the null terminator.
50
 
// When dst is NULL, no characters are copied and no "out" parameters are updated.
 
55
// Returns >= 0: the number of wide characters found in the 
 
56
// multi byte sequence src (of src_size_bytes), that fit in the buffer dst 
 
57
// (of max_dest_chars elements size). The count returned excludes the
 
58
// null terminator. When dst is NULL, no characters are copied 
 
59
// and no "out" parameters are updated.
51
60
// Returns (size_t) -1: an incomplete sequence encountered.
52
 
// Leaves *src pointing the next character to convert or NULL if a null character was converted from *src.
 
61
// Leaves *src pointing the next character to convert or NULL 
 
62
// if a null character was converted from *src.
53
63
size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
54
64
                   size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps )
55
65
{
95
105
}
96
106
 
97
107
// Converts max_source_chars from the wide character buffer pointer to by *src,
98
 
// into the multi byte character sequence buffer stored at dst which must be dst_size_bytes bytes in size.
99
 
// Returns >= 0: the number of bytes in the sequence sequence converted frome *src, excluding the null terminator.
 
108
// into the multi byte character sequence buffer stored at dst which must be
 
109
// dst_size_bytes bytes in size.
 
110
// Returns >= 0: the number of bytes in the sequence sequence 
 
111
// converted frome *src, excluding the null terminator.
100
112
// Returns size_t(-1) if an error occurs, also sets errno.
101
 
// If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst and no "out" parameters are updated.
 
113
// If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst 
 
114
// and no "out" parameters are updated.
102
115
size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
103
116
                   size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
104
117
{
120
133
        if ( dst )
121
134
            result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
122
135
        else
123
 
            result = wcrtomb_s( &char_size, NULL, 0, c, ps); 
124
 
        // If result is zero there is no error and char_size contains the size of the multi-byte-sequence converted.
 
136
            result = wcrtomb_s( &char_size, NULL, 0, c, ps);
 
137
        // If result is zero there is no error and char_size contains the 
 
138
        // size of the multi-byte-sequence converted.
125
139
        // Otherwise result indicates an errno type error.
126
140
        if ( result == no_error ) {
127
141
            if ( c == L'\0' ) {