Example #1
0
int xwaddnstr (WINDOW *win, const char *str, const int n)
{
	int res, width, inv_char;
	wchar_t *ucs;
	char *mstr, *lstr;
	size_t size, num_chars;

	assert (n > 0);
	assert (str != NULL);

	mstr = iconv_str (iconv_desc, str);

	size = xmbstowcs (NULL, mstr, -1, NULL) + 1;
	ucs = (wchar_t *)xmalloc (sizeof(wchar_t) * size);
	xmbstowcs (ucs, mstr, size, &inv_char);
	width = wcswidth (ucs, WIDTH_MAX);

	if (width == -1) {
		size_t clidx;
		for (clidx = 0; clidx < size - 1; clidx++) {
			if (wcwidth (ucs[clidx]) == -1)
				ucs[clidx] = L'?';
		}
		width = wcswidth (ucs, WIDTH_MAX);
		inv_char = 1;
	}

	if (width > n) {
		while (width > n)
			width -= wcwidth (ucs[--size]);
		ucs[size] = L'\0';
	}

	num_chars = wcstombs (NULL, ucs, 0);
	lstr = (char *)xmalloc (num_chars + 1);

	if (inv_char)
		wcstombs (lstr, ucs, num_chars + 1);
	else
		snprintf (lstr, num_chars + 1, "%s", mstr);

	res = waddstr (win, lstr);

	free (ucs);
	free (lstr);
	free (mstr);
	return res;
}
Example #2
0
int
tst_wcswidth (FILE *fp, int debug_flg)
{
  TST_DECL_VARS (int);
  wchar_t *ws;
  int n;

  TST_DO_TEST (wcswidth)
  {
    TST_HEAD_LOCALE (wcswidth, S_WCSWIDTH);
    TST_DO_REC (wcswidth)
    {
      TST_GET_ERRET (wcswidth);
      ws = TST_INPUT (wcswidth).ws;
      n = TST_INPUT (wcswidth).n;
      ret = wcswidth (ws, n);

      if (debug_flg)
	{
	  fprintf (stderr, "wcswidth: [ %d ] : ret = %d\n", rec + 1, ret);
	}

      TST_IF_RETURN (S_WCSWIDTH)
      {
      };
    }
  }

  return err_count;
}
Example #3
0
int ustrwid(wchar_t const *s, int charset)
{
    char buf[256];
    int wid, len = ustrlen(s);
    charset_state state = CHARSET_INIT_STATE;

    wid = 0;

    while (len > 0) {
	int err;
	wchar_t const *s_orig;

	err = 0;
	s_orig = s;
        charset_from_unicode(&s, &len, buf, lenof(buf), charset, &state, &err);
	wid += wcswidth(s_orig, s - s_orig);
	if (err) {
	    assert(len > 0 && *s);
	    s++;
	    len--;
	}
    }

    return wid;
}
Example #4
0
/* Return a malloc()ed string containing the tail of 'str' up to a
 * maximum of 'len' characters (in columns occupied on the screen). */
char *xstrtail (const char *str, const int len)
{
	wchar_t *ucs;
	wchar_t *ucs_tail;
	size_t size;
	int width;
	char *tail;

	assert (str != NULL);
	assert (len > 0);

	size = xmbstowcs(NULL, str, -1, NULL) + 1;
	ucs = (wchar_t *)xmalloc (sizeof(wchar_t) * size);
	xmbstowcs (ucs, str, size, NULL);
	ucs_tail = ucs;

	width = wcswidth (ucs, WIDTH_MAX);
	assert (width >= 0);

	while (width > len)
		width -= wcwidth (*ucs_tail++);

	size = wcstombs (NULL, ucs_tail, 0) + 1;
	tail = (char *)xmalloc (size);
	wcstombs (tail, ucs_tail, size);

	free (ucs);

	return tail;
}
Example #5
0
size_t utils::strwidth(const std::string& str) {
	std::wstring wstr = str2wstr(str);
	int width = wcswidth(wstr.c_str(), wstr.length());
	if (width < 1) // a non-printable character found?
		return wstr.length(); // return a sane width (which might be larger than necessary)
	return width; // exact width
}
Example #6
0
int
utf8_strlen_screen (const char *string)
{
    int length, num_char;
    wchar_t *wstring;
    
    if (!string)
        return 0;
    
    if (!local_utf8)
        return utf8_strlen (string);
    
    num_char = mbstowcs (NULL, string, 0) + 1;
    wstring = malloc ((num_char + 1) * sizeof (wstring[0]));
    if (!wstring)
        return utf8_strlen (string);
    
    if (mbstowcs (wstring, string, num_char) == (size_t)(-1))
    {
        free (wstring);
        return utf8_strlen (string);
    }
    
    length = wcswidth (wstring, num_char);
    free (wstring);
    return length;
}
Example #7
0
/*
 * Return the cursor position of the cursor in the line.
 * Note 1: This function doesn't return the cursor position in the whole command line.
 * A line is every wide characters separated by L'\n' or L'\0'.
 * Note 2 : This function return the number of column used.
 * It may not correspond to the number of characters.
 */
static int sizeOfOneLineInTerm(wchar_t * CommandLine, unsigned int cursorLocation)
{
    unsigned int beginningOfLine = cursorLocation;

    int sizeOfLineInTerm = 0;

    wchar_t saveLastWChar = 0;

    /* Character under cursor saved */
    saveLastWChar = CommandLine[cursorLocation];
    /* Set the end of the line to the cursor position */
    CommandLine[cursorLocation] = L'\0';
    /* Looking for the beginning of the line (L'\n' or beginning of the command line) */
    while (CommandLine[beginningOfLine] != L'\n' && beginningOfLine)
    {
        beginningOfLine--;
    }
    /* If the line is the first of the command, prompt size must be added */
    if (!beginningOfLine)
    {
        sizeOfLineInTerm += printPrompt(NOWRITE_PROMPT);
    }
    /* If L'\n' is found, the line start to the next character */
    if (CommandLine[beginningOfLine] == L'\n')
    {
        beginningOfLine++;
    }
    /* Set the beginning of the line */
    CommandLine += beginningOfLine;
    /* Get the number of column used by the line */
    sizeOfLineInTerm += wcswidth(CommandLine, wcslen(CommandLine));
    /* Previously saved character put back in the command line */
    CommandLine[cursorLocation - beginningOfLine] = saveLastWChar;
    return sizeOfLineInTerm;
}
Example #8
0
/* Moves cursor to the end of command-line on Ctrl+E and End. */
static void
cmd_end(key_info_t key_info, keys_info_t *keys_info)
{
	input_stat.index = input_stat.len;
	input_stat.curs_pos = input_stat.prompt_wid +
			wcswidth(input_stat.line, (size_t)-1);
	update_cursor();
}
Example #9
0
size_t wideLength(const std::wstring &ws)
{
	int len = wcswidth(ws.c_str(), -1);
	if (len < 0)
		return ws.length();
	else
		return len;
}
Example #10
0
static int wis_2cols(wchar_t c)
{
    wchar_t buf[256];

    buf[0] = c;
    buf[1] = 0;

    return wcswidth(buf, 10) == 2;
}
Example #11
0
size_t
gcc_gettext_width (const char *msgstr)
{
  size_t nwcs = mbstowcs (0, msgstr, 0);
  wchar_t *wmsgstr = alloca ((nwcs + 1) * sizeof (wchar_t));

  mbstowcs (wmsgstr, msgstr, nwcs + 1);
  return wcswidth (wmsgstr, nwcs);
}
Example #12
0
int getpadding(int len, char *str)
{
	wchar_t wbuffer[64];
	if (!cfg.utflocale) {
		return len;
	}
	if ((int)mbstowcs(wbuffer, str, 64) < 0) {
		return len;
	}
	return len + ((int)strlen(str) - wcswidth(wbuffer, 64));
}
Example #13
0
File: base.c Project: chazu/stfl
unsigned int stfl_print_richtext(struct stfl_widget *w, WINDOW *win, unsigned int y, unsigned int x, const wchar_t * text, unsigned int width, const wchar_t * style_normal, int has_focus)
{
	const wchar_t *p = text;
	unsigned int retval = 0;

	unsigned int end_col = x + width;

	while (*p) {
		unsigned int len = compute_len_from_width(p, end_col - x);
		const wchar_t *p1 = wcschr(p, L'<');
		if (NULL == p1) {
			mvwaddnwstr(win, y, x, p, len);
			retval += len;
			break;
		} else {
			const wchar_t *p2 = wcschr(p1 + 1, L'>');

			if (len > (p1 - p))
				len = p1 - p;
			mvwaddnwstr(win, y, x, p, len);
			retval += len;
			x += wcswidth(p, len);

			if (p2) {
				wchar_t stylename[p2 - p1];
				wmemcpy(stylename, p1 + 1, p2 - p1 - 1);
				stylename[p2 - p1 - 1] = L'\0';
				if (wcscmp(stylename,L"")==0) {
					mvwaddnwstr(win, y, x, L"<", 1);
					retval += 1;
					++x;
				} else if (wcscmp(stylename, L"/")==0) {
					stfl_style(win, style_normal);
				} else {
					wchar_t lookup_stylename[128];
					const wchar_t * style;
					if (has_focus)
						swprintf(lookup_stylename, sizeof(lookup_stylename)/sizeof(*lookup_stylename), L"style_%ls_focus", stylename);
					else
						swprintf(lookup_stylename, sizeof(lookup_stylename)/sizeof(*lookup_stylename), L"style_%ls_normal", stylename);
					style = stfl_widget_getkv_str(w, lookup_stylename, L"");
					stfl_style(win, style);
				}
				p = p2 + 1;
			} else {
				break;
			}
		}
	}
	return retval;
}
Example #14
0
static void
init_hdr_cols(enum be_fmt be_fmt, struct hdr_info *hdr)
{
	struct col_info *col = hdr->cols;
	size_t i;

	col[1].col_name = _("Active");
	col[2].col_name = _("Mountpoint");
	col[3].col_name = _("Space");
	col[4].col_name = _("Policy");
	col[5].col_name = _("Created");
	col[6].col_name = NULL;

	switch (be_fmt) {
	case BE_FMT_ALL:
		col[0].col_name = _("BE/Dataset/Snapshot");
		break;
	case BE_FMT_DATASET:
		col[0].col_name = _("BE/Dataset");
		break;
	case BE_FMT_SNAPSHOT:
		col[0].col_name = _("BE/Snapshot");
		col[1].col_name = NULL;
		col[2].col_name = NULL;
		break;
	case BE_FMT_DEFAULT:
	default:
		col[0].col_name = _("BE");
	}

	for (i = 0; i < NUM_COLS; i++) {
		const char *name = col[i].col_name;
		col[i].width = 0;

		if (name != NULL) {
			wchar_t wname[128];
			size_t sz = mbstowcs(wname, name, sizeof (wname) /
			    sizeof (wchar_t));
			if (sz > 0) {
				int wcsw = wcswidth(wname, sz);
				if (wcsw > 0)
					col[i].width = wcsw;
				else
					col[i].width = sz;
			} else {
				col[i].width = strlen(name);
			}
		}
	}
}
Example #15
0
/* render a row */
static void table_render_row (Table* t, int rownum, int ncols, wchar_t** s)
{
        wchar_t** row = t->rows[rownum];
        size_t len = 1, i;
        size_t newsize;

        assert(t);
        assert(s != NULL);

        for (i = 0; i < ncols; ++i)
                len += t->widths[i] + wcslen(DELIMITER);

        len += wcslen(COLSUFFIX);

        newsize = (wcslen(*s) + len + 1) * sizeof(wchar_t);
        *s = xrealloc (*s, newsize);

        for (i = 0; i < ncols; ++i)
        {
                wcscat (*s, row[i]);
                if (ncols <= i + 1)
                        break;

                int j;
                int nspaces = max(t->widths[i] - wcswidth(row[i], MAX_WIDTH),
                                  0);
                wchar_t* pad = xmalloc ((nspaces + 1) * sizeof(*pad));

                for (j = 0; j < nspaces; ++j)
                       pad[j] = L' ';

                pad[nspaces] = L_('\0');

                wcscat (*s, pad);
                if (i + 1 < ncols)
                        wcscat (*s, DELIMITER);

                free (pad);
        }

        /* Remove any trailing blanks.  */
        wchar_t *p = *s;
        size_t k = wcslen (p);
        while (k && p[k-1] == L_(' '))
                --k;
        p[k] = L_('\0');


        wcscat (*s, COLSUFFIX);
}
Example #16
0
static void
set_searching_status (action_find_res_wnd_t *__res_wnd,
                      const wchar_t *__action, const wchar_t *__path)
{
  wchar_t format[1024], path[1024];
  size_t width;

  swprintf (format, BUF_LEN (format), L"%ls: %%ls", __action);

  width = wcswidth (format, wcslen (format));
  fit_dirname (__path,
               __res_wnd->window->position.width - 6 - width, path);

  set_status (__res_wnd, _(format), path);
}
Example #17
0
File: str.c Project: 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;
}
Example #18
0
/* Return the number of columns the string occupies when displayed. */
size_t strwidth (const char *s)
{
	wchar_t *ucs;
	size_t size;
	size_t width;

	assert (s != NULL);

	size = xmbstowcs (NULL, s, -1, NULL) + 1;
	ucs = (wchar_t *)xmalloc (sizeof(wchar_t) * size);
	xmbstowcs (ucs, s, size, NULL);
	width = wcswidth (ucs, WIDTH_MAX);
	free (ucs);

	return width;
}
Example #19
0
/* Returns number of character positions allocated by the string on the
 * screen.  On issues will try to do the best, to make visible at least
 * something. */
static size_t
get_width_on_screen(const char str[])
{
	size_t length = (size_t)-1;
	wchar_t *const wide = to_wide(str);
	if(wide != NULL)
	{
		length = wcswidth(wide, (size_t)-1);
		free(wide);
	}
	if(length == (size_t)-1)
	{
		length = get_normal_utf8_string_length(str);
	}
	return length;
}
Example #20
0
int
utf8_strlen_screen (const char *string)
{
    int length, num_char;
    wchar_t *alloc_wstring, *ptr_wstring, wstring[4+2];

    if (!string || !string[0])
        return 0;

    if (!local_utf8)
        return utf8_strlen (string);

    alloc_wstring = NULL;

    if (!string[1] || !string[2] || !string[3] || !string[4])
    {
        /* optimization for max 4 chars: no malloc */
        num_char = 4 + 1;
        ptr_wstring = wstring;
    }
    else
    {
        num_char = mbstowcs (NULL, string, 0) + 1;
        alloc_wstring = malloc ((num_char + 1) * sizeof (alloc_wstring[0]));
        if (!alloc_wstring)
            return utf8_strlen (string);
        ptr_wstring = alloc_wstring;
    }

    if (mbstowcs (ptr_wstring, string, num_char) != (size_t)(-1))
    {
        length = wcswidth (ptr_wstring, num_char);
        /*
         * ensure the size is always >= 1, to prevent any display bug
         * (for example size of UTF-8 char U+26C4 is -1, why? mystery...)
         */
        if (length < 1)
            length = 1;
    }
    else
        length = utf8_strlen (string);

    if (alloc_wstring)
        free (alloc_wstring);

    return length;
}
Example #21
0
static void
add_id(struct idcache **ic, char *name, unsigned long int id, int *width)
{
	struct idcache *nc, *x;
	int w = 0;

	nc = calloc(1, sizeof(*nc));
	if (!nc)
		goto alloc_err;
	nc->id = id;

	if (name) {
#ifdef HAVE_WIDECHAR
		wchar_t wc[LOGIN_NAME_MAX + 1];

		if (mbstowcs(wc, name, LOGIN_NAME_MAX) > 0) {
			wc[LOGIN_NAME_MAX] = '\0';
			w = wcswidth(wc, LOGIN_NAME_MAX);
		}
		else
#endif
			w = strlen(name);
	}
	/* note, we ignore names with non-printable widechars */
	if (w > 0)
		nc->name = strdup(name);
	else if (asprintf(&nc->name, "%lu", id) == -1)
		nc->name = NULL;
	if (!nc->name)
		goto alloc_err;

	for (x = *ic; x && x->next; x = x->next);

	/* add 'nc' at end of the 'ic' list */
	if (x)
		x->next = nc;
	else
		*ic = nc;
	if (w <= 0)
		w = strlen(nc->name);
	*width = *width < w ? w : *width;

	return;
alloc_err:
	err(EXIT_FAILURE, _("out of memory?"));
}
Example #22
0
static void wt_textview_prepare(struct stfl_widget *w, struct stfl_form *f)
{
	struct stfl_widget *c = w->first_child;
	
	w->min_w = 1;
	w->min_h = 5;

	if (c)
		w->allow_focus = 1;

	while (c) {
		const wchar_t * text = stfl_widget_getkv_str(c, L"text", L"");
		int len = wcswidth(text, wcslen(text));
		w->min_w = len > w->min_w ? len : w->min_w;
		c = c->next_sibling;
	}
}
Example #23
0
/*
 * pr_attime --
 *	Print the time since the user logged in.
 */
int
pr_attime(time_t *started, time_t *now)
{
	static wchar_t buf[256];
	struct tm tp, tm;
	time_t diff;
	const wchar_t *fmt;
	int len, width, offset = 0;

	tp = *localtime(started);
	tm = *localtime(now);
	diff = *now - *started;

	/* If more than a week, use day-month-year. */
	if (diff > 86400 * 7)
		fmt = L"%d%b%y";

	/* If not today, use day-hour-am/pm. */
	else if (tm.tm_mday != tp.tm_mday ||
		 tm.tm_mon != tp.tm_mon ||
		 tm.tm_year != tp.tm_year) {
	/* The line below does not take DST into consideration */
	/* else if (*now / 86400 != *started / 86400) { */
		fmt = use_ampm ? L"%a%I%p" : L"%a%H";
	}

	/* Default is hh:mm{am,pm}. */
	else {
		fmt = use_ampm ? L"%l:%M%p" : L"%k:%M";
	}

	(void)wcsftime(buf, sizeof(buf), fmt, &tp);
	len = wcslen(buf);
	width = wcswidth(buf, len);
	xo_attr("since", "%lu", (unsigned long) *started);
	xo_attr("delta", "%lu", (unsigned long) diff);
	if (len == width)
		xo_emit("{:login-time/%-7.7ls/%ls}", buf);
	else if (width < 7)
	        xo_emit("{:login-time/%ls}%.*s", buf, 7 - width, "      ");
	else {
		xo_emit("{:login-time/%ls}", buf);
		offset = width - 7;
	}
	return (offset);
}
Example #24
0
static size_t string_length(const char *s)
{
	int len;
	wchar_t *wcstr;

	if(!s || s[0] == '\0') {
		return 0;
	}
	/* len goes from # bytes -> # chars -> # cols */
	len = strlen(s) + 1;
	wcstr = calloc(len, sizeof(wchar_t));
	len = mbstowcs(wcstr, s, len);
	len = wcswidth(wcstr, len);
	free(wcstr);

	return len;
}
/* render a row */
static void table_render_row (Table* t, int rownum, int ncols, wchar_t** s)
{
        wchar_t** row = t->rows[rownum];
        int len = 1, i;
        size_t newsize;

        assert(t);
        assert(s != NULL);
        
        for (i = 0; i < ncols; ++i)
                len += t->widths[i] + wcslen(DELIMITER);

        len += wcslen(COLSUFFIX);

        newsize = (wcslen(*s) + len + 1) * sizeof(wchar_t);
        *s = realloc (*s, newsize);

        for (i = 0; i < ncols; ++i)
        {
                int j;
                int nspaces = max(t->widths[i] - wcswidth(row[i], MAX_WIDTH),
                                  0);
                wchar_t* pad = malloc ( (nspaces + 1) * sizeof(wchar_t) );

                for (j = 0; j < nspaces; ++j)
                       pad[j] = L' '; 

#ifdef ENABLE_NLS
                pad[nspaces] = L'\0';
#else
                pad[nspaces] = '\0';
#endif
                
                wcscat (*s, row[i]);
                wcscat (*s, pad);
                if (i + 1 < ncols) 
                        wcscat (*s, DELIMITER);

                free (pad);
        }

        wcscat (*s, COLSUFFIX);
}
TInt CTestLibcwchar::wcswidth2L(  )
    {
       
	wchar_t *ws = L"test case";
	int retval;
	
	retval = wcswidth(ws,9);

	INFO_PRINTF2(_L("wcswidth2 result is %d"),retval);

	if(retval != 9)
		{
		return KErrGeneral;
		}
	else
		{
		return KErrNone;
		}
    }
Example #27
0
File: str.c Project: DirtYiCE/uim
int strwidth(const char *str)
{
  int width;
  wchar_t *wcstr;
  int nr_wchars;

  assert(str != NULL);

  nr_wchars = str2wcstr(str, &wcstr);

  if (nr_wchars == 0) {
    return 0;
  }

  width = wcswidth(wcstr, nr_wchars);
  assert(width != -1);
  free(wcstr);
  return width;
}
TInt CTestLibcwchar::wcswidth4L(  )
    {
       
	wchar_t *ws = L"";
	int retval;
	
	retval = wcswidth(ws,5);

	INFO_PRINTF2(_L("wcswidth4 result is %d"),retval);
	
	if(retval != 0)
		{
		return KErrGeneral;
		}
	else
		{
		return KErrNone;
		}
    }
Example #29
0
size_t utils::wcswidth_stfl(const std::wstring& str, size_t size) {
	size_t reduce_count = 0;
	size_t len = std::min(str.length(), size);
	if (len > 1) {
		for (size_t idx=0;idx<len-1;++idx) {
			if (str[idx] == L'<' && str[idx+1] != L'>') {
				reduce_count += 3;
				idx += 3;
			}
		}
	}

	int width = wcswidth(str.c_str(), size);
	if (width < 0) {
		LOG(LOG_ERROR, "oh, oh, wcswidth just failed"); // : %ls", str.c_str());
		return str.length() - reduce_count;
	}

	return width - reduce_count;
}
Example #30
0
int strwid(char const *s, int charset)
{
    wchar_t buf[256];
    int wid, len = strlen(s);
    charset_state state = CHARSET_INIT_STATE;

    wid = 0;

    while (len > 0) {
	int ret;

	ret = charset_to_unicode(&s, &len, buf, lenof(buf),
				 charset, &state, NULL, 0);

	if (ret > 0)
	    wid += wcswidth(buf, ret);
    }

    return wid;
}