mcabber/src/utils.c
changeset 1122 648fe6f715a6
parent 1106 8e12137fab20
child 1167 9726c78a91f3
equal deleted inserted replaced
1121:1aeba5c19834 1122:648fe6f715a6
   484       *p = 0;
   484       *p = 0;
   485   }
   485   }
   486 }
   486 }
   487 
   487 
   488 //  ut_expand_tabs(text)
   488 //  ut_expand_tabs(text)
   489 // Expand tabs in string text.
   489 // Expand tabs and filter out some bad chars in string text.
   490 // If there is no tab in the string, a pointer to text is returned (be
   490 // If there is no tab and no bad chars in the string, a pointer to text
   491 // careful _not_ to free the pointer in this case).
   491 // is returned (be careful _not_ to free the pointer in this case).
   492 // If there are some tabs, a new string with expanded chars is returned; this
   492 // If there are some tabs or bad chars, a new string with expanded chars
   493 // is up to the caller to free this string after use.
   493 // and no bad chars is returned; this is up to the caller to free this
       
   494 // string after use.
   494 char *ut_expand_tabs(const char *text)
   495 char *ut_expand_tabs(const char *text)
   495 {
   496 {
   496   char *xtext;
   497   char *xtext;
   497   char *p, *q;
   498   char *p, *q;
   498   guint8 n=0;
   499   guint n = 0, bc = 0;
   499 
   500 
   500   xtext = (char*)text;
   501   xtext = (char*)text;
   501   for (p=xtext; *p; p++)
   502   for (p=xtext; *p; p++)
   502     if (*p == '\t') n++;
   503     if (*p == '\t')
   503 
   504       n++;
   504   if (!n)
   505     else if (*p == '\x0d')
       
   506       bc++;
       
   507   // XXX Are there other special chars we should filter out?
       
   508 
       
   509   if (!n && !bc)
   505     return (char*)text;
   510     return (char*)text;
   506 
   511 
   507   xtext = g_new(char, strlen(text) + 1 + 8*n);
   512   xtext = g_new(char, strlen(text) + 1 + 8*n);
   508   p = (char*)text;
   513   p = (char*)text;
   509   q = xtext;
   514   q = xtext;
   510   do {
   515   do {
   511     if (*p == '\t') {
   516     if (*p == '\t') {
   512       do { *q++ = ' '; } while ((q-xtext)%8);
   517       do { *q++ = ' '; } while ((q-xtext)%8);
   513     } else {
   518     } else if (*p != '\x0d') {
   514       *q++ = *p;
   519       *q++ = *p;
   515     }
   520     }
   516   } while (*p++);
   521   } while (*p++);
   517 
   522 
   518   return xtext;
   523   return xtext;