[PATCH 1/2] lib/util: Move strncols to util.c
Samuel Mendoza-Jonas
sam.mj at au1.ibm.com
Thu Aug 27 16:38:46 AEST 2015
Signed-off-by: Samuel Mendoza-Jonas <sam.mj at au1.ibm.com>
---
lib/util/util.c | 30 ++++++++++++++++++++++++++++++
lib/util/util.h | 1 +
ui/ncurses/nc-boot-editor.c | 32 +-------------------------------
3 files changed, 32 insertions(+), 31 deletions(-)
diff --git a/lib/util/util.c b/lib/util/util.c
index a18926c..a6deae7 100644
--- a/lib/util/util.c
+++ b/lib/util/util.c
@@ -15,8 +15,12 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#define _XOPEN_SOURCE
+
#include <string.h>
#include <assert.h>
+#include <stdlib.h>
+#include <wchar.h>
#include <util/util.h>
@@ -47,3 +51,29 @@ void mac_str(uint8_t *mac, unsigned int maclen, char *buf, unsigned int buflen)
return;
}
+
+/* Return the number of columns required to display a localised string */
+int strncols(const char *str)
+{
+ int wlen, ncols;
+ wchar_t *wstr;
+
+ wlen = mbstowcs(NULL, str, 0);
+ if (wlen <= 0)
+ return wlen;
+
+ wstr = malloc(sizeof(wchar_t) * wlen + 1);
+ if (!wstr)
+ return -1;
+
+ wlen = mbstowcs(wstr, str, wlen);
+ if (wlen <= 0) {
+ free(wstr);
+ return wlen;
+ }
+
+ ncols = wcswidth(wstr, wlen);
+
+ free(wstr);
+ return ncols;
+}
diff --git a/lib/util/util.h b/lib/util/util.h
index 39966d0..5cb42c4 100644
--- a/lib/util/util.h
+++ b/lib/util/util.h
@@ -50,6 +50,7 @@
do { (void)sizeof(char[(x)?1:-1]); } while (0)
void mac_str(uint8_t *mac, unsigned int maclen, char *buf, unsigned int buflen);
+int strncols(const char *str);
#endif /* UTIL_H */
diff --git a/ui/ncurses/nc-boot-editor.c b/ui/ncurses/nc-boot-editor.c
index 274bd9d..4438f4b 100644
--- a/ui/ncurses/nc-boot-editor.c
+++ b/ui/ncurses/nc-boot-editor.c
@@ -22,11 +22,11 @@
#include <assert.h>
#include <string.h>
-#include <stdlib.h>
#include "log/log.h"
#include "talloc/talloc.h"
#include "i18n/i18n.h"
+#include "util/util.h"
#include "nc-boot-editor.h"
#include "nc-widgets.h"
@@ -524,36 +524,6 @@ void boot_editor_update(struct boot_editor *boot_editor,
pad_refresh(boot_editor);
}
-/* Return the number of columns required to display a localised string */
-static int strncols(const char *str)
-{
- int i, wlen, ncols = 0;
- wchar_t *wstr;
-
- wlen = mbstowcs(NULL, str, 0);
- if (wlen <= 0)
- return wlen;
-
- wstr = malloc(sizeof(wchar_t) * wlen + 1);
- if (!wstr)
- return -1;
-
- wlen = mbstowcs(wstr, str, wlen);
- if (wlen <= 0) {
- free(wstr);
- return wlen;
- }
-
- /* Processing each character individually lets us use the same
- * check for all languages */
- for (i = 0; i < wlen; i++) {
- ncols += wcwidth(wstr[i]);
- }
-
- free(wstr);
- return ncols;
-}
-
struct boot_editor *boot_editor_init(struct cui *cui,
struct pmenu_item *item,
const struct system_info *sysinfo,
--
2.5.0
More information about the Petitboot
mailing list