Code

the updated sources
[ncmpc.git] / src / support.c
index 90ecb8b0d444a5b11df1395887b36521952f81ec..847178d9c32e5938e831ecc8f00587bd4d7203da 100644 (file)
@@ -35,24 +35,13 @@ extern void screen_status_printf(char *format, ...);
 
 static gboolean noconvert = TRUE;
 
-char *
-trim(char *str)
+size_t
+my_strlen(char *str)
 {
-  char *end;
-
-  if( str==NULL )
-    return NULL;
-
-  while( IS_WHITESPACE(*str) )
-    str++;
-
-  end=str+strlen(str)-1;
-  while( end>str && IS_WHITESPACE(*end) )
-    {
-      *end = '\0';
-      end--;
-    }
-  return str;
+  if( g_utf8_validate(str,-1,NULL) )
+    return g_utf8_strlen(str,-1);
+  else
+    return strlen(str);
 }
 
 char *
@@ -73,8 +62,8 @@ remove_trailing_slash(char *path)
 char *
 lowerstr(char *str)
 {
-  size_t i;
-  size_t len = strlen(str);
+  gsize i;
+  gsize len = strlen(str);
 
   if( str==NULL )
     return NULL;
@@ -117,11 +106,12 @@ strcasestr(const char *haystack, const char *needle)
 }
 #endif /* HAVE_STRCASESTR */
 
+// FIXME: utf-8 length
 char *
 strscroll(char *str, char *separator, int width, scroll_state_t *st)
 {
-  char *tmp, *buf;
-  size_t len;
+  gchar *tmp, *buf;
+  gsize len, size;
 
   if( st->offset==0 )
     {
@@ -130,20 +120,32 @@ strscroll(char *str, char *separator, int width, scroll_state_t *st)
     }
   
   /* create a buffer containing the string and the separator */
-  tmp = g_malloc(strlen(str)+strlen(separator)+1);
-  strcpy(tmp, str);
-  strcat(tmp, separator);
-  len = strlen(tmp);
+  size = strlen(str)+strlen(separator)+1;
+  tmp = g_malloc(size);
+  g_strlcpy(tmp, str, size);
+  g_strlcat(tmp, separator, size);
+  len = my_strlen(tmp);
 
   if( st->offset >= len )
     st->offset = 0;
   
   /* create the new scrolled string */
-  buf = g_malloc(width+1);
-  strncpy(buf, tmp+st->offset, width);
-  if( strlen(buf) < width )
-    strncat(buf, tmp, width-strlen(buf));
-
+  size = width+1;
+  if (g_utf8_validate(tmp, -1, NULL) )
+    {
+      int ulen;
+      buf = g_malloc(size*6);// max length of utf8 char is 6
+      g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size);
+      if( (ulen = g_utf8_strlen(buf, -1)) < width )
+       g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
+    }
+  else
+    {
+      buf = g_malloc(size);
+      g_strlcpy(buf, tmp+st->offset, size);
+      if( strlen(buf) < width )
+       g_strlcat(buf, tmp, size);
+    }
   if( time(NULL)-st->t >= 1 )
     {
       st->t = time(NULL);
@@ -154,7 +156,6 @@ strscroll(char *str, char *separator, int width, scroll_state_t *st)
   
 }
 
-
 void
 charset_init(gboolean disable)
 {