2012-08-05 23:01:50 +02:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-04 02:17:56 +02:00
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2012-08-05 23:01:50 +02:00
|
|
|
*
|
|
|
|
* string.c: Define an i3String type to automagically handle UTF-8/UCS-2
|
|
|
|
* conversions. Some font backends need UCS-2 (X core fonts),
|
|
|
|
* others want UTF-8 (Pango).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-06-10 19:01:05 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2012-08-05 23:01:50 +02:00
|
|
|
#include "libi3.h"
|
|
|
|
|
|
|
|
struct _i3String {
|
|
|
|
char *utf8;
|
|
|
|
xcb_char2b_t *ucs2;
|
|
|
|
size_t num_glyphs;
|
|
|
|
size_t num_bytes;
|
2015-10-13 09:29:50 +02:00
|
|
|
bool pango_markup;
|
2012-08-05 23:01:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build an i3String from an UTF-8 encoded string.
|
|
|
|
* Returns the newly-allocated i3String.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
i3String *i3string_from_utf8(const char *from_utf8) {
|
2015-08-03 11:50:13 +02:00
|
|
|
i3String *str = scalloc(1, sizeof(i3String));
|
2012-08-05 23:01:50 +02:00
|
|
|
|
|
|
|
/* Get the text */
|
|
|
|
str->utf8 = sstrdup(from_utf8);
|
|
|
|
|
|
|
|
/* Compute and store the length */
|
|
|
|
str->num_bytes = strlen(str->utf8);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:45:34 +01:00
|
|
|
/*
|
|
|
|
* Build an i3String from an UTF-8 encoded string in Pango markup.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
i3String *i3string_from_markup(const char *from_markup) {
|
|
|
|
i3String *str = i3string_from_utf8(from_markup);
|
|
|
|
|
|
|
|
/* Set the markup flag */
|
2015-10-13 09:29:50 +02:00
|
|
|
str->pango_markup = true;
|
2015-02-12 20:45:34 +01:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-08-05 23:01:50 +02:00
|
|
|
/*
|
|
|
|
* Build an i3String from an UTF-8 encoded string with fixed length.
|
|
|
|
* To be used when no proper NUL-terminaison is available.
|
|
|
|
* Returns the newly-allocated i3String.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
i3String *i3string_from_utf8_with_length(const char *from_utf8, size_t num_bytes) {
|
2015-08-03 11:50:13 +02:00
|
|
|
i3String *str = scalloc(1, sizeof(i3String));
|
2012-08-05 23:01:50 +02:00
|
|
|
|
|
|
|
/* Copy the actual text to our i3String */
|
2015-08-03 11:50:13 +02:00
|
|
|
str->utf8 = scalloc(num_bytes + 1, 1);
|
2012-08-05 23:01:50 +02:00
|
|
|
strncpy(str->utf8, from_utf8, num_bytes);
|
|
|
|
str->utf8[num_bytes] = '\0';
|
|
|
|
|
|
|
|
/* Store the length */
|
|
|
|
str->num_bytes = num_bytes;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:45:34 +01:00
|
|
|
/*
|
|
|
|
* Build an i3String from an UTF-8 encoded string in Pango markup with fixed
|
|
|
|
* length.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
i3String *i3string_from_markup_with_length(const char *from_markup, size_t num_bytes) {
|
|
|
|
i3String *str = i3string_from_utf8_with_length(from_markup, num_bytes);
|
|
|
|
|
|
|
|
/* set the markup flag */
|
2015-10-13 09:29:50 +02:00
|
|
|
str->pango_markup = true;
|
2015-02-12 20:45:34 +01:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-08-05 23:01:50 +02:00
|
|
|
/*
|
|
|
|
* Build an i3String from an UCS-2 encoded string.
|
|
|
|
* Returns the newly-allocated i3String.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
i3String *i3string_from_ucs2(const xcb_char2b_t *from_ucs2, size_t num_glyphs) {
|
2015-08-03 11:50:13 +02:00
|
|
|
i3String *str = scalloc(1, sizeof(i3String));
|
2012-08-05 23:01:50 +02:00
|
|
|
|
|
|
|
/* Copy the actual text to our i3String */
|
2015-08-03 11:50:13 +02:00
|
|
|
str->ucs2 = scalloc(num_glyphs, sizeof(xcb_char2b_t));
|
|
|
|
memcpy(str->ucs2, from_ucs2, num_glyphs * sizeof(xcb_char2b_t));
|
2012-08-05 23:01:50 +02:00
|
|
|
|
|
|
|
/* Store the length */
|
|
|
|
str->num_glyphs = num_glyphs;
|
|
|
|
|
|
|
|
str->utf8 = NULL;
|
|
|
|
str->num_bytes = 0;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-03-21 21:33:53 +01:00
|
|
|
/**
|
|
|
|
* Copies the given i3string.
|
|
|
|
* Note that this will not free the source string.
|
|
|
|
*/
|
|
|
|
i3String *i3string_copy(i3String *str) {
|
2015-03-21 21:36:15 +01:00
|
|
|
i3String *copy = i3string_from_utf8(i3string_as_utf8(str));
|
2015-10-13 09:29:50 +02:00
|
|
|
copy->pango_markup = str->pango_markup;
|
2015-03-21 21:36:15 +01:00
|
|
|
return copy;
|
2015-03-21 21:33:53 +01:00
|
|
|
}
|
|
|
|
|
2012-08-05 23:01:50 +02:00
|
|
|
/*
|
|
|
|
* Free an i3String.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void i3string_free(i3String *str) {
|
|
|
|
if (str == NULL)
|
|
|
|
return;
|
|
|
|
free(str->utf8);
|
|
|
|
free(str->ucs2);
|
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void i3string_ensure_utf8(i3String *str) {
|
|
|
|
if (str->utf8 != NULL)
|
|
|
|
return;
|
|
|
|
if ((str->utf8 = convert_ucs2_to_utf8(str->ucs2, str->num_glyphs)) != NULL)
|
|
|
|
str->num_bytes = strlen(str->utf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void i3string_ensure_ucs2(i3String *str) {
|
|
|
|
if (str->ucs2 != NULL)
|
|
|
|
return;
|
|
|
|
str->ucs2 = convert_utf8_to_ucs2(str->utf8, &str->num_glyphs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the UTF-8 encoded version of the i3String.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const char *i3string_as_utf8(i3String *str) {
|
|
|
|
i3string_ensure_utf8(str);
|
|
|
|
return str->utf8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the UCS-2 encoded version of the i3String.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const xcb_char2b_t *i3string_as_ucs2(i3String *str) {
|
|
|
|
i3string_ensure_ucs2(str);
|
|
|
|
return str->ucs2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the number of bytes (UTF-8 encoded) in an i3String.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
size_t i3string_get_num_bytes(i3String *str) {
|
|
|
|
i3string_ensure_utf8(str);
|
|
|
|
return str->num_bytes;
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:45:34 +01:00
|
|
|
/*
|
|
|
|
* Whether the given i3String is in Pango markup.
|
|
|
|
*/
|
|
|
|
bool i3string_is_markup(i3String *str) {
|
2015-10-13 09:29:50 +02:00
|
|
|
return str->pango_markup;
|
2015-02-12 20:45:34 +01:00
|
|
|
}
|
|
|
|
|
2015-03-24 07:27:38 +01:00
|
|
|
/*
|
|
|
|
* Set whether the i3String should use Pango markup.
|
|
|
|
*/
|
2015-10-13 09:29:50 +02:00
|
|
|
void i3string_set_markup(i3String *str, bool pango_markup) {
|
|
|
|
str->pango_markup = pango_markup;
|
2015-03-24 07:27:38 +01:00
|
|
|
}
|
|
|
|
|
2015-06-10 19:01:05 +02:00
|
|
|
/*
|
|
|
|
* Escape pango markup characters in the given string.
|
|
|
|
*/
|
|
|
|
i3String *i3string_escape_markup(i3String *str) {
|
|
|
|
const char *text = i3string_as_utf8(str);
|
2015-09-05 16:21:16 +02:00
|
|
|
char *escaped = g_markup_escape_text(text, -1);
|
|
|
|
i3String *result = i3string_from_utf8(escaped);
|
|
|
|
free(escaped);
|
|
|
|
return result;
|
2015-06-10 19:01:05 +02:00
|
|
|
}
|
|
|
|
|
2012-08-05 23:01:50 +02:00
|
|
|
/*
|
|
|
|
* Returns the number of glyphs in an i3String.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
size_t i3string_get_num_glyphs(i3String *str) {
|
|
|
|
i3string_ensure_ucs2(str);
|
|
|
|
return str->num_glyphs;
|
|
|
|
}
|