Esempio n. 1
0
FX_WINOLEAUTAPI_(FX_BSTR) FX_SysAllocStringA( const char* pSrc )
{
    // check for null argument
    // if NULL, defer to wide character version
    if ( !pSrc ) 
        return FX_SysAllocString( 0 );

    // get length of the wchar_t string
    mbstate_t state;
    memset( &state, 0, sizeof(state) );
    char const* tmp = pSrc;
    size_t wl = mbsrtowcs( NULL, &tmp, 0, &state );

    // create the buffer
    FX_BSTR bstr = FX_SysAllocStringLen( 0, (uint16_t)wl );
    if ( !bstr ) return 0;

    // copy the buffer
    memset( &state, 0, sizeof(state) );
    tmp = pSrc;
    size_t wl2 = mbsrtowcs( const_cast<wchar_t*>(bstr), &tmp, wl, &state );
    assert( wl == wl2 );
    wl2 = wl2; //avoid warning in release mode
    return bstr;
}
Esempio n. 2
0
void test_mbsrtowcs(mbstate_t* ps) {
  wchar_t out[4];

  const char* valid = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
  ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
  ASSERT_EQ(L'A', out[0]);
  ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
  ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
  ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
  // Check that valid has advanced to the next unread character.
  ASSERT_EQ('e', *valid);

  wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
  ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
  ASSERT_EQ(L'e', out[0]);
  ASSERT_EQ(L'f', out[1]);
  ASSERT_EQ(L'\0', out[2]);
  // Check that we didn't clobber the rest of out.
  ASSERT_EQ(L'x', out[3]);
  // Check that valid has advanced to the end of the string.
  ASSERT_EQ(nullptr, valid);

  const char* invalid = "A" "\xc2\x20" "ef";
  ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
  EXPECT_EQ(EILSEQ, errno);
  ASSERT_EQ('\xc2', *invalid);

  const char* incomplete = "A" "\xc2";
  ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
  EXPECT_EQ(EILSEQ, errno);
  ASSERT_EQ('\xc2', *incomplete);
}
Esempio n. 3
0
	wstring StringConverter::ToWide (const string &str, bool noThrow)
	{
		try
		{
			mbstate_t mbState;
			Memory::Zero (&mbState, sizeof (mbState));
			const char *src = str.c_str();

			size_t size = mbsrtowcs (nullptr, &src, 0, &mbState);
			if (size == (size_t) -1)
				throw StringConversionFailed (SRC_POS);

			vector <wchar_t> buf (size + 1);
			Memory::Zero (&mbState, sizeof (mbState));

			if ((size = mbsrtowcs (&buf[0], &src, buf.size(), &mbState)) == (size_t) -1)
				throw StringConversionFailed (SRC_POS);

			wstring s;
			s.insert (s.begin(), buf.begin(), buf.begin() + size);
			return s;
		}
		catch (...)
		{
			if (noThrow)
				return L"";
			throw;
		}
	}
Esempio n. 4
0
int
main (void)
{
  unsigned char buf[6] = { 0x25,  0xe2, 0x82, 0xac,  0xce, 0xbb };
  mbstate_t state;
  const char *src;
  wchar_t wc = 42;
  size_t n;
  int result = 0;
  const char *used_locale;

  setlocale (LC_CTYPE,"de_DE.UTF-8");
  /* Double check.  */
  used_locale = setlocale (LC_CTYPE, NULL);
  printf ("used locale: \"%s\"\n", used_locale);
  result = strcmp (used_locale, "de_DE.UTF-8");

  memset (&state, '\0', sizeof (state));

  src = (const char *) buf;
  show (mbsrtowcs (&wc, &src, 1, &state), 1, 37, buf + 1);
  show (mbsrtowcs (&wc, &src, 1, &state), 1, 8364, buf + 4);
  show (mbsrtowcs (&wc, &src, 1, &state), 1, 955, buf + 6);

  return result;
}
Esempio n. 5
0
std::wstring Utils::widenString(const std::string& s)
{
    // Set LC_CTYPE according to the environnement variable, for mbsrtowcs().
    system::TemporaryLocale locale(LC_CTYPE, "");

    const char * src = s.c_str();
    // Call mbsrtowcs() once to find out the length of the converted string.
    size_t length = mbsrtowcs(NULL, &src, 0, NULL);
    if (length == size_t(-1)) {
        int error = errno;
        msg_warning("Utils::widenString()") << strerror(error);
        return L"";
    }

    // Call mbsrtowcs() again with a correctly sized buffer to actually do the conversion.
    wchar_t * buffer = new wchar_t[length + 1];
    length = mbsrtowcs(buffer, &src, length + 1, NULL);
    if (length == size_t(-1)) {
        int error = errno;
        msg_warning("Utils::widenString()") << strerror(error);
        delete[] buffer;
        return L"";
    }

    if (src != NULL) {
        msg_warning("Utils::widenString()") << "Conversion failed (\"" << s << "\")";
        delete[] buffer;
        return L"";
    }

    std::wstring result(buffer);
    delete[] buffer;
    return result;
}
Esempio n. 6
0
// Print a string in multibyte format
static void mbprint(const char* msg) {
    size_t len = mbsrtowcs(NULL, &msg, 0, NULL);
    if(len == (size_t)-1) {
        error(EXIT_FAILURE, errno, _("conversion to multibyte string failed"));
    }
    wchar_t* mb_msg = xmalloc((len + 1) * sizeof(wchar_t));
    mbsrtowcs(mb_msg, &msg, len + 1, NULL);
    wprintf(L"%ls\n", mb_msg);
    free(mb_msg);
}
Esempio n. 7
0
bool ll_register_foreign_func(LibLoader *ll,
                              char *lisp_func,
                              char *c_func)
{
    PrimFunc *func;

    func = (PrimFunc *)LIBRARY_GETSYM(ll->_prev_handle, c_func);

#ifndef WIN32
    {
        char *error;

        if((error = dlerror()) != NULL) {
            OUTPUT_ERR("problem finding symbol: %s\n", error);
            return false;
        }
    }
#else
    if(func == NULL) {
        OUTPUT_ERR("problem opening function: %s in library\n", c_func);
        return false;
    }
#endif

    if(strcmp(ll->_mod_name, "None") == 0) {
        /* need to first convert to String */
        String tmp_w[MAX_FUNCTION_LEN];
        mbsrtowcs((wchar_t *)tmp_w,
                  (const char **)&lisp_func,
                  MAX_FUNCTION_LEN,
                  NULL);

        REGISTER_FUNC("foreign", (wchar_t*)tmp_w, func);
    } else {
        char tmp[MAX_FUNCTION_LEN];
        char *tmp_p = tmp;
        String tmp_w[MAX_FUNCTION_LEN];

        snprintf(tmp_p, MAX_FUNCTION_LEN,
                 "%s:%s", ll->_mod_name, lisp_func);

        /* need to first convert to String */
        mbsrtowcs((wchar_t*)tmp_w,
                  (const char **)&tmp_p,
                  MAX_FUNCTION_LEN,
                  NULL);

        REGISTER_FUNC("foreign", (wchar_t*)tmp_w, func);
    }
    
    return true;
}
Esempio n. 8
0
static wchar_t *
towcs(const char *s)
{
	wchar_t *wcs;
	size_t n;

	if ((n = mbsrtowcs(NULL, &s, 0, NULL)) == (size_t)-1)
		return (NULL);
	if ((wcs = malloc((n + 1) * sizeof(*wcs))) == NULL)
		return (NULL);
	mbsrtowcs(wcs, &s, n + 1, NULL);
	return (wcs);
}
Esempio n. 9
0
int
main(int argc, char *argv[])
{
	int ch, rval, seq;
	wchar_t *warg;
	const char *arg;
	size_t len;

	setlocale(LC_CTYPE, "");

	seq = 0;
	while ((ch = getopt(argc, argv, "d:s")) != -1)
		switch(ch) {
		case 'd':
			arg = optarg;
			len = mbsrtowcs(NULL, &arg, 0, NULL);
			if (len == (size_t)-1)
				err(1, "delimiters");
			warg = malloc((len + 1) * sizeof(*warg));
			if (warg == NULL)
				err(1, NULL);
			arg = optarg;
			len = mbsrtowcs(warg, &arg, len + 1, NULL);
			if (len == (size_t)-1)
				err(1, "delimiters");
			delimcnt = tr(delim = warg);
			break;
		case 's':
			seq = 1;
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (*argv == NULL)
		usage();
	if (!delim) {
		delimcnt = 1;
		delim = tab;
	}

	if (seq)
		rval = sequential(argv);
	else
		rval = parallel(argv);
	exit(rval);
}
Esempio n. 10
0
static int
int_mbscoll(const char *s1, const char *s2, int icase)
{ size_t l1 = strlen(s1);
  size_t l2 = strlen(s2);
  wchar_t *w1;
  wchar_t *w2;
  int ml1, ml2;
  mbstate_t mbs;
  int rc;

#if HAVE_ALLOCA
  if ( l1 < 1024 && (w1 = alloca(sizeof(wchar_t)*(l1+1))) )
  { ml1 = FALSE;
  } else
#endif
  { w1 = PL_malloc_atomic(sizeof(wchar_t)*(l1+1));
    ml1 = TRUE;
  }
#if HAVE_ALLOCA
  if ( l2 < 1024 && (w2 = alloca(sizeof(wchar_t)*(l2+1))) )
  { ml2 = FALSE;
  } else
#endif
  { w2 = PL_malloc_atomic(sizeof(wchar_t)*(l2+1));
    ml2 = TRUE;
  }

  memset(&mbs, 0, sizeof(mbs));
  if ( mbsrtowcs(w1, &s1, l1+1, &mbs) == (size_t)-1 )
  { rc = -2;
    goto out;
  }
  if ( mbsrtowcs(w2, &s2, l2+1, &mbs) == (size_t)-1 )
  { rc = 2;
    goto out;
  }
  if ( icase )
  { wstolower(w1, l1);
    wstolower(w2, l2);
  }

  rc = wcscoll(w1, w2);

out:
  if ( ml1 ) PL_free(w1);
  if ( ml2 ) PL_free(w2);

  return rc;
}
Esempio n. 11
0
File: str.c Progetto: DirtYiCE/uim
static int byte2width2(char *str, int n)
{
  int width;
  int str_byte;
  char save_char;
  char *save_str;
  wchar_t *wcstr;
  int nr_wchars;
  
  assert(str != NULL);

  if (n <= 0) {
    return 0;
  }

  str_byte = strlen(str);
  if (str_byte == 0) {
    return 0;
  }

  if (n > str_byte) {
    n = str_byte;
  }

  wcstr = uim_malloc(sizeof(wchar_t) * str_byte);

  save_str = str;

  save_char = str[n];
  str[n] = '\0';
  nr_wchars = mbsrtowcs(wcstr, (const char **)&str, str_byte, NULL);
  save_str[n] = save_char;

  if ((size_t)nr_wchars != (size_t)(-1)) {
    width = wcswidth(wcstr, nr_wchars);
  } else {
    mbsrtowcs(wcstr, (const char **)&str, 1, NULL);
    /* strを最後まで変換するとNULLになる */
    assert(str != NULL);
    save_char = str[0];
    str[0] = '\0';
    width = strwidth(save_str);
    str[0] = save_char;
  }
  free(wcstr);
  assert(width >= 0);
  return width;
}
Esempio n. 12
0
int main()
{
   size_t result;

   char mbstring[ ] = "This is originally a multibyte string.\n";
   const char *mbsptr = mbstring;

   wchar_t widestring[256] = { L'\0' };

   mbstate_t state;
   memset( &state, '\0', sizeof state );

   printf( "The current locale is %s.\n", setlocale( LC_CTYPE, "" ));

   result = mbsrtowcs( widestring, &mbsptr, 256, &state );
   if ( result == (size_t)-1 )
   {
      fputs( "Encoding error in multibyte string", stderr );
      return -1;
   }
   else
   {
      printf( "Converted %u multibyte characters. The result:\n", result );
      printf( "%ls", widestring );
   }
   return 0;
}
Esempio n. 13
0
ATF_TC_BODY(positional_wide, tc)
{

	swprintf(wbuf1, nitems(wbuf1),
	    L"|xx %1$s %2$s %3$s %4$s\n"
	    "|xx %5$s %6$s %7$s %8$s\n"
	    "|xx %9$s %10$s %11$s %12$s\n"
	    "|xx %13$s %14$s %15$s %16$s\n"
	    "|xx %17$s %18$s %19$s %20$s\n"
	    "|xx %21$s %22$s %23$s %24$s\n"
	    "|xx %25$s %26$s %27$s %28$s\n"
	    "|xx %29$s %30$s %31$s %32$s\n"
	    "|xx %33$s %34$s %35$s %36$s\n"
	    "|xx %37$s %38$s %39$s %40$s\n"
	    "|xx %41$s %42$s %43$s %44$s\n"
	    "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
	    "01", "02", "03", "04", "05", "06",
	    "07", "08", "09", "10", "11", "12",
	    "13", "14", "15", "16", "17", "18",
	    "19", "20", "21", "22", "23", "24",
	    "25", "26", "27", "28", "29", "30",
	    "31", "32", "33", "34", "35", "36",
	    "37", "38", "39", "40", "41", "42",
	    "43", "44", 45, -1L, 1LL, -1, 1LL
	    );
	temp = correct;
	mbsrtowcs(wbuf2, &temp, nitems(wbuf2), NULL);
	ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
	    "buffers didn't match");
}
Esempio n. 14
0
static CH *
utf8_to_local (char *lc_ctype, char *string)
{
  mbstate_t ps;
  size_t len;
  char *local;
  const char *p;
  wchar_t *wcs = NULL;
  char *old_ctype = NULL;

  local = pinentry_utf8_to_local (lc_ctype, string);
  if (!local)
    return NULL;

  old_ctype = strdup (setlocale (LC_CTYPE, NULL));
  setlocale (LC_CTYPE, lc_ctype? lc_ctype : "");

  p = local;
  memset (&ps, 0, sizeof(mbstate_t));
  len = mbsrtowcs (NULL, &p, strlen (string), &ps);
  if (len == (size_t)-1)
    {
      free (local);
      goto leave;
    }
  wcs = calloc (len + 1, sizeof(wchar_t));
  if (!wcs)
    {
      free (local);
      goto leave;
    }

  p = local;
  memset (&ps, 0, sizeof(mbstate_t));
  mbsrtowcs (wcs, &p, len, &ps);

  free (local);

 leave:
  if (old_ctype)
    {
      setlocale (LC_CTYPE, old_ctype);
      free (old_ctype);
    }

  return wcs;
}
Esempio n. 15
0
size_t _RTL_FUNC mbstowcs(wchar_t *pwcs,const char *mbs, size_t n)
{
    size_t rv ;
    rv = mbsrtowcs(pwcs,&mbs,n,&__getRtlData()->mbstowcs_st);
    if (rv == (size_t)-2)
        rv = (size_t)-1;
    if ((int)rv >=0 && rv != n)
        pwcs[rv] = 0 ;
    return rv ;
}
Esempio n. 16
0
ATF_TC_BODY(positional_precision_wide, tc)
{

	swprintf(wbuf1, sizeof buf, L"%2$.*4$s %2$.*3$s %1$s",
		 "BSD", "bsd", 2, 1);
	temp = correct2;
	mbsrtowcs(wbuf2, &temp, nitems(wbuf2), NULL);
	ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
	    "buffers didn't match");
}
Esempio n. 17
0
/**
 * Convert a list of utf8 chars to wide-chars.  The reason for doing
 * this is kind-of dorky: its so that we can easily find,
 * character-by-character, if a given character is a quotation mark
 * or a bullet.  This works only because the quotation marks and
 * bullets are exactly one (wide) character in length. I would like
 * it better if we didn't do this wide-char conversion, since wide-chars
 * are badly-behaved in crazy locales, and on MS Windows.
 */
static bool afdict_to_wide(Dictionary afdict, int classno)
{
	Afdict_class * ac;
	wchar_t * wqs;
	mbstate_t mbs;
	size_t i;
	int w;
	dyn_str * qs;
	const char *pqs;

	ac = AFCLASS(afdict, classno);
	if (0 == ac->length) return true;

	qs = dyn_str_new();
	for (i = 0; i < ac->length; i++)
		dyn_strcat(qs, ac->string[i]);

	/*
	 * Convert utf8 to wide chars before use.
	 * In case of error the result is undefined.
	 */
	pqs = qs->str;
	memset(&mbs, 0, sizeof(mbs));
	w = mbsrtowcs(NULL, &pqs, 0, &mbs);
	if (0 > w)
	{
		prt_error("Error: Affix dictionary: %s: "
		          "Invalid utf8 character\n", afdict_classname[classno]);
		return false;
	}

	/* Store the wide char version at the AFCLASS entry. */
	ac->mem_elems =  sizeof(*wqs) * (w+1); /* bytes here, but we don't care */
	ac->string = malloc(ac->mem_elems);
	wqs = (wchar_t *)ac->string;
	pqs = qs->str;
	(void)mbsrtowcs(wqs, &pqs, w, &mbs);
	wqs[w] = L'\0';

	dyn_str_delete(qs);

	return true;
}
Esempio n. 18
0
/* Convert multi-byte sequence to wide characters.  Change invalid UTF-8
 * sequences to '?'.  'dest' can be NULL as in mbstowcs().
 * If 'invalid_char' is not NULL it will be set to 1 if an invalid character
 * appears in the string, otherwise 0. */
static size_t xmbstowcs (wchar_t *dest, const char *src, size_t len,
		int *invalid_char)
{
	mbstate_t ps;
	size_t count = 0;

	assert (src != NULL);
	assert (!dest || len > 0);

	memset (&ps, 0, sizeof(ps));

	if (dest)
		memset (dest, 0, len * sizeof(wchar_t));

	if (invalid_char)
		*invalid_char = 0;

	while (src && (len || !dest)) {
		size_t res;

		res = mbsrtowcs (dest, &src, len, &ps);
		if (res != (size_t)-1) {
			count += res;
			src = NULL;
		}
		else {
			size_t converted;

			src++;
			if (dest) {
				converted = wcslen (dest);
				dest += converted;
				count += converted;
				len -= converted;

				if (len > 1) {
					*dest = L'?';
					dest++;
					*dest = L'\0';
					len--;
				}
				else
					*(dest - 1) = L'\0';
			}
			else
				count++;
			memset (&ps, 0, sizeof(ps));

			if (invalid_char)
				*invalid_char = 1;
		}
	}

	return count;
}
Esempio n. 19
0
wchar_t* ModuleMbToTCharDup(const char*  str)
{
	int    len = ModuleMbStrLen(str);
	wchar_t* tstr = (wchar_t*) malloc((len + 1) * sizeof(wchar_t));
	if (tstr)
	{
		mbsrtowcs(tstr, &str, len + 1, NULL);
		return tstr;
	}
	return NULL;
}
Esempio n. 20
0
/**
 * Find display width for longest argument string.
 * @param opt		option(s)
 * @param translation_domain	translation domain
 * @return		display width
 */
static size_t maxArgWidth(const struct poptOption * opt,
		       /*@null@*/ const char * translation_domain)
	/*@*/
{
    size_t max = 0;
    size_t len = 0;
    const char * s;
    
    if (opt != NULL)
    while (opt->longName || opt->shortName || opt->arg) {
	if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
	    if (opt->arg)	/* XXX program error */
	    len = maxArgWidth(opt->arg, translation_domain);
	    if (len > max) max = len;
	} else if (!(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
	    len = sizeof("  ")-1;
	    if (opt->shortName != '\0') len += sizeof("-X")-1;
	    if (opt->shortName != '\0' && opt->longName) len += sizeof(", ")-1;
	    if (opt->longName) {
		len += ((opt->argInfo & POPT_ARGFLAG_ONEDASH)
			? sizeof("-")-1 : sizeof("--")-1);
		len += strlen(opt->longName);
	    }

	    s = getArgDescrip(opt, translation_domain);

#ifdef POPT_WCHAR_HACK
	    /* XXX Calculate no. of display characters. */
	    if (s) {
		const char * scopy = s;
		mbstate_t t;
		size_t n;

/*@-boundswrite@*/
		memset ((void *)&t, '\0', sizeof (t));	/* In initial state.  */
/*@=boundswrite@*/
		/* Determine number of characters.  */
		n = mbsrtowcs (NULL, &scopy, strlen(scopy), &t);
		len += sizeof("=")-1 + n;
	    }
#else
	    if (s)
		len += sizeof("=")-1 + strlen(s);
#endif

	    if (opt->argInfo & POPT_ARGFLAG_OPTIONAL) len += sizeof("[]")-1;
	    if (len > max) max = len;
	}

	opt++;
    }
    
    return max;
}
Esempio n. 21
0
void test_mbsrtowcs(mbstate_t* ps) {
  wchar_t out[4];

  const char* valid = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
  ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
  ASSERT_EQ(L'A', out[0]);
  ASSERT_EQ(0x00a2, out[1]);
  ASSERT_EQ(0x20ac, out[2]);
  ASSERT_EQ(0x24b62, out[3]);
  ASSERT_EQ('e', *valid);

  const char* invalid = "A" "\xc2\x20" "ef";
  ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
  EXPECT_EQ(EILSEQ, errno);
  ASSERT_EQ('\xc2', *invalid);

  const char* incomplete = "A" "\xc2";
  ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
  EXPECT_EQ(EILSEQ, errno);
  ASSERT_EQ('\xc2', *incomplete);
}
Esempio n. 22
0
Array make_string(const char* s) {
	size_t len = strlen(s);
	wchar_t ws[len + 2];
	
	mbstate_t mbs;
	bzero(&mbs, sizeof(mbs));
	
	if(mbsrtowcs(ws, &s, len + 1, &mbs) == (size_t)-1)
		error(L"Wide-string encoding error");
	
	return make_wstring(ws);
}
Esempio n. 23
0
size_t
mbstowcs(
  wchar_t *pwcs,
  const char *s,
  size_t n
  )
{

  /* pwcs may be NULL */
  /* s may be NULL */

  return mbsrtowcs(pwcs, &s, n, NULL);
}
Esempio n. 24
0
/*--------------------------------------------------------------------------*/
wchar_t *to_wide_string(const char *_UTFStr)
{
    wchar_t *_buf = NULL;
    size_t pszLen = 0;
    char *psz = _UTFStr;
    mbstate_t ps;

    if (_UTFStr == NULL)
    {
        return NULL;
    }

    memset (&ps, 0x00, sizeof(ps));
    pszLen = mbsrtowcs(NULL, (const char**)&psz, 0, &ps);

    if ( pszLen == (size_t)(-1) )
    {
        return NULL;
    }

    _buf = (wchar_t*)MALLOC((pszLen + 1) * sizeof(wchar_t));
    if (_buf == NULL)
    {
        return NULL;
    }

    pszLen = mbsrtowcs(_buf, (const char**)&psz, (int)strlen(psz), &ps);

    if ( pszLen == (size_t)(-1) )
    {
        FREE(_buf);
        return NULL;
    }
    else
    {
        _buf[pszLen] = L'\0';
    }
    return _buf;
}
Esempio n. 25
0
wchar_t* string2wstring(const char* str)
{
  if (str)
  {
    wchar_t* s;
    size_t l = strlen(str);
    s = (wchar_t*)malloc(sizeof(wchar_t)*(l+1));
    memset(s,0,(l+1)*sizeof(wchar_t));
    mbsrtowcs(s,&str,l,NULL);
    return(s);
  }
  return((wchar_t*)NULL);
}
Esempio n. 26
0
size_t mbsnrtowcs(wchar_t *wcs, const char **src, size_t n, size_t wn, mbstate_t *st)
{
	size_t l, cnt=0, n2;
	wchar_t *ws, wbuf[256];
	const char *s = *src;

	if (!wcs) ws = wbuf, wn = sizeof wbuf / sizeof *wbuf;
	else ws = wcs;

	/* making sure output buffer size is at most n/4 will ensure
	 * that mbsrtowcs never reads more than n input bytes. thus
	 * we can use mbsrtowcs as long as it's practical.. */

	while ( s && wn && ( (n2=n/4)>=wn || n2>32 ) ) {
		if (n2>=wn) n2=wn;
		n -= n2;
		l = mbsrtowcs(ws, &s, n2, st);
		if (!(l+1)) {
			cnt = l;
			wn = 0;
			break;
		}
		if (ws != wbuf) {
			ws += l;
			wn -= l;
		}
		cnt += l;
	}
	if (s) while (wn && n) {
		l = mbrtowc(ws, s, n, st);
		if (l+2<=2) {
			if (!(l+1)) {
				cnt = l;
				break;
			}
			if (!l) {
				s = 0;
				break;
			}
			/* have to roll back partial character */
			*(unsigned *)st = 0;
			break;
		}
		s += l; n -= l;
		/* safe - this loop runs fewer than sizeof(wbuf)/8 times */
		ws++; wn--;
		cnt++;
	}
	if (wcs) *src = s;
	return cnt;
}
Esempio n. 27
0
static void
h_wctomb(const struct test *t, char tc)
{
	wchar_t wcs[16 + 2];
	char buf[128];
	char cs[MB_LEN_MAX];
	const char *pcs;
	char *str;
	mbstate_t st;
	mbstate_t *stp = NULL;
	size_t sz, ret, i;

	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);

	(void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL);
	(void)printf("Checking sequence: \"%s\"\n", buf);

	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
	(void)printf("Using locale: %s\n", str);

	if (tc == TC_WCRTOMB_ST) {
		(void)memset(&st, 0, sizeof(st));
		stp = &st;
	}

	wcs[t->wclen] = L'X'; /* poison */
	pcs = t->data;
	sz = mbsrtowcs(wcs, &pcs, t->wclen + 2, NULL);
	ATF_REQUIRE_EQ_MSG(sz, t->wclen, "mbsrtowcs() returned: "
		"%zu, expected: %zu", sz, t->wclen);
	ATF_REQUIRE_EQ(wcs[t->wclen], 0);

	for (i = 0; i < t->wclen + 1; i++) {
		if (tc == TC_WCTOMB)
			ret = wctomb(cs, wcs[i]);
		else
			ret = wcrtomb(cs, wcs[i], stp);

		if (ret == t->mblen[i])
			continue;

		(void)printf("At position %zd:\n", i);
		(void)printf("  expected: %zd\n", t->mblen[i]);
		(void)printf("  got     : %zd\n", ret);
		atf_tc_fail("Test failed");
		/* NOTREACHED */
	}

	(void)printf("Ok.\n");
}
Esempio n. 28
0
static wchar_t *
ls_to_wcs(const char *in, const wchar_t *on_error)
{ wchar_t buf[LSTR_MAX];
  mbstate_t state;

  memset(&state, 0, sizeof(state));
  mbsrtowcs(buf, &in, LSTR_MAX, &state);
  if ( in == NULL )
  { return wcsdup(buf);
  } else
  { Sdprintf("Illegal locale string: %s\n", in);
    return wcsdup(on_error);
  }
}
std::wstring
MakeWideString(const char* input)
{
  wchar_t* tmp;
  size_t l = strlen(input);
  tmp = new wchar_t[l + 1];
  memset(tmp, 0, (l + 1) * sizeof(wchar_t));
  const char* mbr = input;
  mbsrtowcs(tmp, &mbr, l, NULL);
  std::wstring str(tmp);
  delete [] tmp;

  return str;
}
Esempio n. 30
0
static PyObject *extfunc_InfoWindowWrite(PyObject *self, PyObject *args) {
	const char *str;
	wchar_t buffer[1024];

	if (!PyArg_ParseTuple(args, "s", &str))
		return NULL;

	memset(buffer, 0, sizeof(buffer));
	if (mbsrtowcs (buffer, &str, sizeof(buffer)/sizeof(wchar_t), NULL) == -1)
		wcscpy(buffer, L"[wide character conversion failed]");

	Melder_print (buffer);

	return Py_BuildValue("");
}