Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    r7b1d048 r89f5338  
    399399}
    400400
     401/* downcase the string 'foo' */
     402void downstr(char *foo)
     403{
     404  int i;
     405  for (i=0; foo[i]!='\0'; i++) {
     406    foo[i]=tolower(foo[i]);
     407  }
     408}
     409
    401410/* Caller must free response.
    402411 * Takes in strings which are space-separated lists of tokens
     
    437446void *owl_malloc(size_t size)
    438447{
    439   return(g_malloc(size));
     448  return(malloc(size));
    440449}
    441450
    442451void owl_free(void *ptr)
    443452{
    444   g_free(ptr);
     453  free(ptr);
    445454}
    446455
    447456char *owl_strdup(const char *s1)
    448457{
    449   return(g_strdup(s1));
     458  return(strdup(s1));
    450459}
    451460
    452461void *owl_realloc(void *ptr, size_t size)
    453462{
    454   return(g_realloc(ptr, size));
     463  return(realloc(ptr, size));
    455464}
    456465
    457466/* allocates memory and returns the string or null.
    458467 * caller must free the string.
     468 * from Linux sprintf man page.
    459469 */
    460470char *owl_sprintf(const char *fmt, ...)
    461471{
     472  int n, size = 100;
     473  char *p;
    462474  va_list ap;
    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 
     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}
    470493
    471494/* Return the owl color associated with the named color.  Return -1
     
    753776}
    754777
    755 char * owl_get_datadir()
    756 {
    757   char * datadir = getenv("BARNOWL_DATA_DIR");
    758   if(datadir != NULL)
    759     return strchr(datadir, '=') + 1;
    760   return DATADIR;
    761 }
    762 
    763 /* Strips format characters from a valid utf-8 string. Returns the
    764    empty string if 'in' does not validate. */
    765 char * owl_strip_format_chars(char *in)
    766 {
    767   char *r;
    768   if (g_utf8_validate(in, -1, NULL)) {
    769     char *s, *p;
    770     r = owl_malloc(strlen(in)+1);
    771     r[0] = '\0';
    772     s = in;
    773     p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
    774     while(p) {
    775       /* If it's a format character, copy up to it, and skip all
    776          immediately following format characters. */
    777       if (_owl_fmtext_is_format_char(g_utf8_get_char(p))) {
    778         strncat(r, s, p-s);
    779         p = g_utf8_next_char(p);
    780         while (p && _owl_fmtext_is_format_char(g_utf8_get_char(p))) {
    781           p = g_utf8_next_char(p);
    782         }
    783         s = p;
    784         p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
    785       }
    786       else {
    787         p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8);
    788       }
    789     }
    790     if (s) strcat(r,s);
    791   }
    792   else {
    793     r = owl_strdup("");
    794   }
    795   return r;
    796 }
    797 
    798 /* If in is not UTF-8, convert from ISO-8859-1. We may want to allow
    799  * the caller to specify an alternative in the future. We also strip
    800  * out characters in Unicode Plane 16, as we use that plane internally
    801  * for formatting.
    802  */
    803 char * owl_validate_or_convert(char *in)
    804 {
    805   if (g_utf8_validate(in, -1, NULL)) {
    806     return owl_strip_format_chars(in);
    807   }
    808   else {
    809     return g_convert(in, -1,
    810                      "UTF-8", "ISO-8859-1",
    811                      NULL, NULL, NULL);
    812   }
    813 }
    814 /* Attempts to convert 'in' to ISO-8859-1. Returns that if possible,
    815    else returns UTF-8.
    816  */
    817 char * owl_get_iso_8859_1_if_possible(char *in)
    818 {
    819   char *out;
    820   if (g_utf8_validate(in, -1, NULL)) {
    821     out = g_convert(in, -1,
    822                     "ISO-8859-1", "UTF-8",
    823                     NULL, NULL, NULL);
    824     if (!out) {
    825       out = owl_strdup(in);
    826     }
    827   }
    828   else {
    829     out = owl_strdup("");
    830   }
    831   return out;
     778char * owl_get_datadir() {
     779    char * datadir = getenv("BARNOWL_DATA_DIR");
     780    if(datadir != NULL)
     781        return strchr(datadir, '=') + 1;
     782    return DATADIR;
    832783}
    833784
Note: See TracChangeset for help on using the changeset viewer.