Changeset 28ee32b


Ignore:
Timestamp:
Dec 24, 2007, 2:53:11 AM (13 years ago)
Author:
Alejandro R. Sedeño <asedeno@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
762512d
Parents:
5bc0f68
Message:
UTF-8 - first pass

unicode changes:
* remove downstr() from text.c, replace on site with calls to g_utf8_strdown.
  In place downcasing is not a good idea, so the downstr() contract is unfulfillable.

* make owl_text_truncate_cols() and owl_fmtext_truncate_cols() understand character width.
  This may need more work. Some code duplication - see if we can refactor.

* stristr() rewritten to yse g_utf_casefold() instead of downstr(), and restructured to have a single return.

* only_whitespace() rewritten for unicode.

glib changes:
* rewrite owl_sprintf() in terms of g_strdup_vprintf()

WARNING: THIS IS NOT SAFE YET. Network data is not yet sanitized. Non
UTF-8 inputs may do horrible things to you. This phase is just
working on rendering.
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • fmtext.c

    rfa3290d r28ee32b  
    315315void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
    316316{
    317   char *ptr1, *ptr2, *last;
    318   int len, offset;
     317  char *ptr_s, *ptr_e, *ptr_c, *last;
     318  int col, cnt;
    319319
    320320  last=in->textbuff+in->textlen-1;
    321   ptr1=in->textbuff;
    322   while (ptr1<=last) {
    323     ptr2=strchr(ptr1, '\n');
    324     if (!ptr2) {
     321  ptr_s=in->textbuff;
     322  while (ptr_s<=last) {
     323    ptr_e=strchr(ptr_s, '\n');
     324    if (!ptr_e) {
    325325      /* but this shouldn't happen if we end in a \n */
    326326      break;
    327327    }
    328328   
    329     if (ptr2==ptr1) {
     329    if (ptr_e==ptr_s) {
    330330      owl_fmtext_append_normal(out, "\n");
    331       ptr1++;
     331      ptr_s++;
    332332      continue;
    333333    }
    334334
     335    col = 0;
     336    cnt = 0;
     337    ptr_c = ptr_s;
     338    while(col < bcol && ptr_c < ptr_e) {
     339      gunichar c = g_utf8_get_char(ptr_c);
     340      if (g_unichar_iswide(c)) {
     341        if (col + 2 > bcol) break;
     342        else col += 2;
     343      }
     344      else if (g_unichar_type(c) == G_UNICODE_NON_SPACING_MARK) ; /*do nothing*/
     345      /* We may need more special cases here... unicode spacing is hard. */
     346      else {
     347        if (col + 1 > bcol) break;
     348        else ++col;
     349      }
     350      ptr_c = g_utf8_next_char(ptr_c);
     351      if (col >= acol) ++cnt;
     352      if (col <= acol) ptr_s = ptr_c;
     353    }
     354    _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff);
     355    ptr_s=ptr_e+1;
     356   
     357#if 0
    335358    /* we need to check that we won't run over here */
    336359    len=bcol-acol;
    337     if (len > (ptr2-(ptr1+acol))) {
     360    if (len > (ptr_e-(ptr_s+acol))) {
    338361      /* the whole line fits with room to spare, don't take a full 'len' */
    339       len=ptr2-(ptr1+acol);
    340     }
    341     if (len>last-ptr1) {
     362      len=ptr_e-(ptr_s+acol);
     363    }
     364    if (len>last-ptr_s) {
    342365      /* the whole rest of the text fits with room to spare, adjust for it */
    343       len-=(last-ptr1);
     366      len-=(last-ptr_s);
    344367    }
    345368    if (len<=0) {
    346369      /* saftey check */
    347370      owl_fmtext_append_normal(out, "\n");
    348       ptr1=ptr2+1;
     371      ptr_s=ptr_e+1;
    349372      continue;
    350373    }
    351374
    352     offset=ptr1-in->textbuff;
     375    offset = ptr_s - in->textbuff;
    353376    _owl_fmtext_append_fmtext(out, in, offset+acol, offset+acol+len);
    354377
    355     ptr1=ptr2+1;
     378    ptr_s=ptr_e+1;
     379#endif
    356380  }
    357381}
  • functions.c

    r93ee554 r28ee32b  
    25982598  }
    25992599  /* downcase it */
    2600   downstr(filtname);
     2600  {
     2601    char *temp = g_utf8_strdown(filtname, -1);
     2602    if (temp) {
     2603      owl_free(filtname);
     2604      filtname = temp;
     2605    }
     2606  }
    26012607  /* turn spaces, single quotes, and double quotes into dots */
    26022608  owl_text_tr(filtname, ' ', '.');
  • logging.c

    rd0961fe r28ee32b  
    155155    to = owl_sprintf("jabber:%s", owl_message_get_recipient(m));
    156156  } else if (owl_message_is_type_aim(m)) {
     157    char *temp2;
    157158    temp = owl_aim_normalize_screenname(owl_message_get_recipient(m));
    158     downstr(temp);
    159     to = owl_sprintf("aim:%s", temp);
     159    temp2 = g_utf8_strdown(temp,-1);
     160    to = owl_sprintf("aim:%s", temp2);
     161    owl_free(temp2);
    160162    owl_free(temp);
    161163  } else {
     
    267269  } else if (owl_message_is_type_aim(m)) {
    268270    /* we do not yet handle chat rooms */
    269     char *normalto;
    270     normalto=owl_aim_normalize_screenname(owl_message_get_sender(m));
    271     downstr(normalto);
     271    char *normalto, *temp;
     272    temp = owl_aim_normalize_screenname(owl_message_get_sender(m));
     273    normalto = g_utf8_strdown(temp, -1);
    272274    from=frombuff=owl_sprintf("aim:%s", normalto);
    273275    owl_free(normalto);
     276    owl_free(temp);
    274277  } else if (owl_message_is_type_loopback(m)) {
    275278    from=frombuff=owl_strdup("loopback");
     
    290293
    291294  ch=frombuff[0];
    292   if (!isalnum(ch)) from="weird";
     295  if (!g_ascii_isalnum(ch)) from="weird";
    293296
    294297  for (i=0; i<len; i++) {
     
    299302
    300303  if (!personal) {
    301     if (strcmp(from, "weird")) downstr(from);
     304    if (strcmp(from, "weird")) {
     305      char* temp = g_utf8_strdown(frombuff, -1);
     306      if (temp) {
     307        owl_free(frombuff);
     308        from = frombuff = temp;
     309      }
     310    }
    302311  }
    303312
  • text.c

    r34509d5 r28ee32b  
    5050void owl_text_truncate_cols(char *out, char *in, int acol, int bcol)
    5151{
    52   char *ptr1, *ptr2, *tmpbuff, *last;
    53   int len;
    54 
     52  char *ptr_s, *ptr_e, *ptr_c, *tmpbuff, *last;
     53  int col, cnt;
     54 
    5555  tmpbuff=owl_malloc(strlen(in)+20);
    5656
    5757  strcpy(tmpbuff, "");
    5858  last=in+strlen(in)-1;
    59   ptr1=in;
    60   while (ptr1<last) {
    61     ptr2=strchr(ptr1, '\n');
    62     if (!ptr2) {
     59  ptr_s=in;
     60  while (ptr_s<last) {
     61    ptr_e=strchr(ptr_s, '\n');
     62    if (!ptr_e) {
    6363      /* but this shouldn't happen if we end in a \n */
    6464      break;
    6565    }
    6666   
    67     if (ptr2==ptr1) {
     67    if (ptr_e==ptr_s) {
    6868      strcat(tmpbuff, "\n");
    69       ptr1++;
    70       continue;
    71     }
    72 
     69      ptr_s++;
     70      continue;
     71    }
     72
     73    col = 0;
     74    cnt = 0;
     75    ptr_c = ptr_s;
     76    while(col < bcol && ptr_c < ptr_e) {
     77      gunichar c = g_utf8_get_char(ptr_c);
     78      if (g_unichar_iswide(c)) {
     79        if (col + 2 > bcol) break;
     80        else col += 2;
     81      }
     82      else if (g_unichar_type(c) == G_UNICODE_NON_SPACING_MARK) ; /*do nothing*/
     83      /* We may need more special cases here... unicode spacing is hard. */
     84      else {
     85        if (col + 1 > bcol) break;
     86        else ++col;
     87      }
     88      ptr_c = g_utf8_next_char(ptr_c);
     89      if (col >= acol) ++cnt;
     90      if (col <= acol) ptr_s = ptr_c;
     91    }
     92    strncat(tmpbuff, ptr_s, ptr_c - ptr_s - 1);
     93    strcat(tmpbuff, "\n");
     94    ptr_s = ptr_e + 1;
     95#if 0
    7396    /* we need to check that we won't run over here */
    74     if ( (ptr2-ptr1) < (bcol-acol) ) {
    75       len=ptr2-(ptr1+acol);
     97    if ( (ptr_e-ptr_s) < (bcol-acol) ) {
     98      len=ptr_e-(ptr_s+acol);
    7699    } else {
    77100      len=bcol-acol;
    78101    }
    79     if ((ptr1+len)>=last) {
    80       len-=last-(ptr1+len);
    81     }
    82 
    83     strncat(tmpbuff, ptr1+acol, len);
     102    if ((ptr_s+len)>=last) {
     103      len-=last-(ptr_s+len);
     104    }
     105
     106    strncat(tmpbuff, ptr_s+acol, len);
    84107    strcat(tmpbuff, "\n");
    85108
    86     ptr1=ptr2+1;
     109    ptr_s=ptr_e+1;
     110#endif
    87111  }
    88112  strcpy(out, tmpbuff);
     
    275299char *stristr(char *a, char *b)
    276300{
    277   char *x, *y, *ret;
    278 
    279   if ((x=owl_strdup(a))==NULL) return(NULL);
    280   if ((y=owl_strdup(b))==NULL) return(NULL);
    281   downstr(x);
    282   downstr(y);
    283   ret=strstr(x, y);
    284   if (ret==NULL) {
    285     owl_free(x);
    286     owl_free(y);
    287     return(NULL);
    288   }
    289   ret=ret-x+a;
    290   owl_free(x);
    291   owl_free(y);
     301  char *x, *y;
     302  char *ret = NULL;
     303  if ((x = g_utf8_casefold(a, -1)) != NULL) {
     304    if ((y = g_utf8_casefold(b, -1)) != NULL) {
     305      ret = strstr(x, y);
     306      if (ret != NULL) {
     307        ret = ret - x + a;
     308      }
     309      g_free(y);
     310    }
     311    g_free(x);
     312  }
    292313  return(ret);
    293314}
     
    296317int only_whitespace(char *s)
    297318{
    298   int i;
    299   for (i=0; s[i]; i++) {
    300     if (!isspace((int) s[i])) return(0);
     319  if (g_utf8_validate(s,-1,NULL)) {
     320    char *p;
     321    for(p = s; p[0]; p=g_utf8_next_char(p)) {
     322      if (!g_unichar_isspace(g_utf8_get_char(p))) return 0;
     323    }
     324  }
     325  else {
     326    int i;
     327    for (i=0; s[i]; i++) {
     328      if (!isspace((int) s[i])) return(0);
     329    }
    301330  }
    302331  return(1);
  • util.c

    r5bc0f68 r28ee32b  
    399399}
    400400
    401 /* downcase the string 'foo' */
    402 void downstr(char *foo)
    403 {
    404   int i;
    405   for (i=0; foo[i]!='\0'; i++) {
    406     foo[i]=tolower(foo[i]);
    407   }
    408 }
    409 
    410401/* Caller must free response.
    411402 * Takes in strings which are space-separated lists of tokens
     
    466457/* allocates memory and returns the string or null.
    467458 * caller must free the string.
    468  * from Linux sprintf man page.
    469459 */
    470460char *owl_sprintf(const char *fmt, ...)
    471461{
    472   int n, size = 100;
    473   char *p;
    474462  va_list ap;
    475   if ((p = owl_malloc (size)) == NULL) return (NULL);
    476   while (1) {
    477     /* Try to print in the allocated space. */
    478     va_start(ap, fmt);
    479     n = vsnprintf (p, size, fmt, ap);
    480     va_end(ap);
    481     /* If that worked, return the string. */
    482     if (n > -1 && n < size)
    483       return p;
    484     /* Else try again with more space. */
    485     if (n > -1)    /* glibc 2.1 */
    486       size = n+1; /* precisely what is needed */
    487     else           /* glibc 2.0 */
    488       size *= 2;  /* twice the old size */
    489     if ((p = owl_realloc (p, size)) == NULL)
    490       return NULL;
    491   }
    492 }
     463  char *ret = NULL;
     464  va_start(ap, fmt);
     465  ret = g_strdup_vprintf(fmt, ap);
     466  va_end(ap);
     467  return ret;
     468}
     469
    493470
    494471/* Return the owl color associated with the named color.  Return -1
Note: See TracChangeset for help on using the changeset viewer.