source: util.c @ 3dcccba

barnowl_perlaim
Last change on this file since 3dcccba was c453ada, checked in by Geoffrey Thomas <geofft@mit.edu>, 16 years ago
Remove aim.c. buddylist.c, buddy.c, libfaim, and everything that uses them.
  • Property mode set to 100644
File size: 20.7 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <ctype.h>
6#include <pwd.h>
7
8static const char fileIdent[] = "$Id$";
9
10void sepbar(char *in)
11{
12  char buff[1024];
13  WINDOW *sepwin;
14  owl_messagelist *ml;
15  owl_view *v;
16  int x, y, i;
17  char *foo, *appendtosepbar;
18
19  sepwin=owl_global_get_curs_sepwin(&g);
20  ml=owl_global_get_msglist(&g);
21  v=owl_global_get_current_view(&g);
22
23  werase(sepwin);
24  wattron(sepwin, A_REVERSE);
25  if (owl_global_is_fancylines(&g)) {
26    whline(sepwin, ACS_HLINE, owl_global_get_cols(&g));
27  } else {
28    whline(sepwin, '-', owl_global_get_cols(&g));
29  }
30
31  if (owl_global_is_sepbar_disable(&g)) {
32    getyx(sepwin, y, x);
33    wmove(sepwin, y, owl_global_get_cols(&g)-1);
34    return;
35  }
36
37  wmove(sepwin, 0, 2); 
38
39  if (owl_messagelist_get_size(ml)==0) {
40    strcpy(buff, " (-/-) ");
41  } else {
42    snprintf(buff, 1024, " (%i/%i/%i) ", owl_global_get_curmsg(&g)+1,
43            owl_view_get_size(v),
44            owl_messagelist_get_size(ml));
45  }
46  waddstr(sepwin, buff);
47
48  foo=owl_view_get_filtname(v);
49  if (strcmp(foo, owl_global_get_view_home(&g))) wattroff(sepwin, A_REVERSE);
50  waddstr(sepwin, " ");
51  waddstr(sepwin, owl_view_get_filtname(v));
52  waddstr(sepwin, " ");
53  if (strcmp(foo, owl_global_get_view_home(&g))) wattron(sepwin, A_REVERSE);
54
55  if (owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
56    getyx(sepwin, y, x);
57    wmove(sepwin, y, x+2);
58    wattron(sepwin, A_BOLD);
59    waddstr(sepwin, " <truncated> ");
60    wattroff(sepwin, A_BOLD);
61  }
62
63  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
64  if ((i != -1) &&
65      (i < owl_view_get_size(v)-1)) {
66    getyx(sepwin, y, x);
67    wmove(sepwin, y, x+2);
68    wattron(sepwin, A_BOLD);
69    waddstr(sepwin, " <more> ");
70    wattroff(sepwin, A_BOLD);
71  }
72
73  if (owl_global_get_rightshift(&g)>0) {
74    getyx(sepwin, y, x);
75    wmove(sepwin, y, x+2);
76    snprintf(buff, 1024, " right: %i ", owl_global_get_rightshift(&g));
77    waddstr(sepwin, buff);
78  }
79
80  /* TODO add perl hooks for sepbar away, etc. status */
81  if (owl_global_is_zaway(&g)) {
82    getyx(sepwin, y, x);
83    wmove(sepwin, y, x+2);
84    wattron(sepwin, A_BOLD);
85    wattroff(sepwin, A_REVERSE);
86    if (owl_global_is_zaway(&g)) {
87      waddstr(sepwin, " AWAY ");
88    } /* else if... */
89    wattron(sepwin, A_REVERSE);
90    wattroff(sepwin, A_BOLD);
91  }
92
93  if (owl_global_get_curmsg_vert_offset(&g)) {
94    getyx(sepwin, y, x);
95    wmove(sepwin, y, x+2);
96    wattron(sepwin, A_BOLD);
97    wattroff(sepwin, A_REVERSE);
98    waddstr(sepwin, " SCROLL ");
99    wattron(sepwin, A_REVERSE);
100    wattroff(sepwin, A_BOLD);
101  }
102 
103  if (in) {
104    getyx(sepwin, y, x);
105    wmove(sepwin, y, x+2);
106    waddstr(sepwin, in);
107  }
108
109  appendtosepbar = owl_global_get_appendtosepbar(&g);
110  if (appendtosepbar && *appendtosepbar) {
111    getyx(sepwin, y, x);
112    wmove(sepwin, y, x+2);
113    waddstr(sepwin, " ");
114    waddstr(sepwin, owl_global_get_appendtosepbar(&g));
115    waddstr(sepwin, " ");
116  }
117
118  getyx(sepwin, y, x);
119  wmove(sepwin, y, owl_global_get_cols(&g)-1);
120   
121  wattroff(sepwin, A_BOLD);
122  wattroff(sepwin, A_REVERSE);
123  wnoutrefresh(sepwin);
124}
125
126
127void pophandler_quit(int ch)
128{
129  if (ch=='q') {
130    owl_popwin_close(owl_global_get_popwin(&g));
131  }
132}
133
134char **atokenize(char *buffer, char *sep, int *i)
135{
136  /* each element of return must be freed by user */
137  char **args;
138  char *workbuff, *foo;
139  int done=0, first=1, count=0;
140
141  workbuff=owl_malloc(strlen(buffer)+1);
142  memcpy(workbuff, buffer, strlen(buffer)+1);
143
144  args=NULL;
145  while (!done) {
146    if (first) {
147      first=0;
148      foo=(char *)strtok(workbuff, sep);
149    } else {
150      foo=(char *)strtok(NULL, sep);
151    }
152    if (foo==NULL) {
153      done=1;
154    } else {
155      args=(char **)owl_realloc(args, sizeof(char *) * (count+1));
156      args[count]=owl_malloc(strlen(foo)+1);
157      strcpy(args[count], foo);
158      count++;
159    }
160  }
161  *i=count;
162  owl_free(workbuff);
163  return(args);
164}
165
166char *skiptokens(char *buff, int n) {
167  /* skips n tokens and returns where that would be.
168   * TODO: handle quotes more sanely. */
169 
170  int inquotes=0;
171  while (*buff && n>0) {
172      while (*buff == ' ') buff++;
173      while (*buff && (inquotes || *buff != ' ')) { 
174        if (*buff == '"' || *buff == '\'') inquotes=!inquotes;
175        buff++;
176      }
177      while (*buff == ' ') buff++;
178      n--;
179  }
180  return buff;
181}
182
183/* Return a "nice" version of the path.  Tilde expansion is done, and
184 * duplicate slashes are removed.  Caller must free the return.
185 */
186char *owl_util_makepath(char *in)
187{
188  int i, j, x;
189  char *out, user[MAXPATHLEN];
190  struct passwd *pw;
191
192  out=owl_malloc(MAXPATHLEN+1);
193  out[0]='\0';
194  j=strlen(in);
195  x=0;
196  for (i=0; i<j; i++) {
197    if (in[i]=='~') {
198      if ( (i==(j-1)) ||          /* last character */
199           (in[i+1]=='/') ) {     /* ~/ */
200        /* use my homedir */
201        pw=getpwuid(getuid());
202        if (!pw) {
203          out[x]=in[i];
204        } else {
205          out[x]='\0';
206          strcat(out, pw->pw_dir);
207          x+=strlen(pw->pw_dir);
208        }
209      } else {
210        /* another user homedir */
211        int a, b;
212        b=0;
213        for (a=i+1; i<j; a++) {
214          if (in[a]==' ' || in[a]=='/') {
215            break;
216          } else {
217            user[b]=in[a];
218            i++;
219            b++;
220          }
221        }
222        user[b]='\0';
223        pw=getpwnam(user);
224        if (!pw) {
225          out[x]=in[i];
226        } else {
227          out[x]='\0';
228          strcat(out, pw->pw_dir);
229          x+=strlen(pw->pw_dir);
230        }
231      }
232    } else if (in[i]=='/') {
233      /* check for a double / */
234      if (i<(j-1) && (in[i+1]=='/')) {
235        /* do nothing */
236      } else {
237        out[x]=in[i];
238        x++;
239      }
240    } else {
241      out[x]=in[i];
242      x++;
243    }
244  }
245  out[x]='\0';
246  return(out);
247}
248
249void atokenize_free(char **tok, int nels)
250{
251  int i;
252  for (i=0; i<nels; i++) {
253    owl_free(tok[i]);
254  }
255  owl_free(tok);
256}
257
258
259void owl_parsefree(char **argv, int argc)
260{
261  int i;
262
263  if (!argv) return;
264 
265  for (i=0; i<argc; i++) {
266    if (argv[i]) owl_free(argv[i]);
267  }
268  owl_free(argv);
269}
270
271char **owl_parseline(char *line, int *argc)
272{
273  /* break a command line up into argv, argc.  The caller must free
274     the returned values.  If there is an error argc will be set to
275     -1, argv will be NULL and the caller does not need to free
276     anything */
277
278  char **argv;
279  int i, len, between=1;
280  char *curarg;
281  char quote;
282
283  argv=owl_malloc(sizeof(char *));
284  len=strlen(line);
285  curarg=owl_malloc(len+10);
286  strcpy(curarg, "");
287  quote='\0';
288  *argc=0;
289  for (i=0; i<len+1; i++) {
290    /* find the first real character */
291    if (between) {
292      if (line[i]==' ' || line[i]=='\t' || line[i]=='\0') {
293        continue;
294      } else {
295        between=0;
296        i--;
297        continue;
298      }
299    }
300
301    /* deal with a quote character */
302    if (line[i]=='"' || line[i]=="'"[0]) {
303      /* if this type of quote is open, close it */
304      if (quote==line[i]) {
305        quote='\0';
306        continue;
307      }
308
309      /* if no quoting is open then open with this */
310      if (quote=='\0') {
311        quote=line[i];
312        continue;
313      }
314
315      /* if another type of quote is open then treat this as a literal */
316      curarg[strlen(curarg)+1]='\0';
317      curarg[strlen(curarg)]=line[i];
318      continue;
319    }
320
321    /* if it's not a space or end of command, then use it */
322    if (line[i]!=' ' && line[i]!='\t' && line[i]!='\n' && line[i]!='\0') {
323      curarg[strlen(curarg)+1]='\0';
324      curarg[strlen(curarg)]=line[i];
325      continue;
326    }
327
328    /* otherwise, if we're not in quotes, add the whole argument */
329    if (quote=='\0') {
330      /* add the argument */
331      argv=owl_realloc(argv, sizeof(char *)*((*argc)+1));
332      argv[*argc]=owl_malloc(strlen(curarg)+2);
333      strcpy(argv[*argc], curarg);
334      *argc=*argc+1;
335      strcpy(curarg, "");
336      between=1;
337      continue;
338    }
339
340    /* if it is a space and we're in quotes, then use it */
341    curarg[strlen(curarg)+1]='\0';
342    curarg[strlen(curarg)]=line[i];
343  }
344
345  owl_free(curarg);
346
347  /* check for unbalanced quotes */
348  if (quote!='\0') {
349    owl_parsefree(argv, *argc);
350    *argc=-1;
351    return(NULL);
352  }
353
354  return(argv);
355}
356
357/* caller must free the return */
358char *owl_util_minutes_to_timestr(int in)
359{
360  int days, hours;
361  long run;
362  char *out;
363
364  run=in;
365
366  days=run/1440;
367  run-=days*1440;
368  hours=run/60;
369  run-=hours*60;
370
371  if (days>0) {
372    out=owl_sprintf("%i d %2.2i:%2.2i", days, hours, run);
373  } else {
374    out=owl_sprintf("    %2.2i:%2.2i", hours, run);
375  }
376  return(out);
377}
378
379/* return the index of the last char before a change from the first one */
380int owl_util_find_trans(char *in, int len)
381{
382  int i;
383  for (i=1; i<len; i++) {
384    if (in[i] != in[0]) return(i-1);
385  }
386  return(i);
387}
388
389int owl_util_find_trans_short(short *in, int len)
390{
391  int i;
392  for (i=1; i<len; i++) {
393    if (in[i] != in[0]) return(i-1);
394  }
395  return(i);
396}
397
398/* Caller must free response.
399 * Takes in strings which are space-separated lists of tokens
400 * and returns a single string containing no token more than once.
401 * If prohibit is non-null, no token may start with a character
402 * in prohibit.
403 */
404char *owl_util_uniq(char *A, char *B, char *prohibit)
405{
406 
407  char *cat, **tok;
408  int toklen, i, j, first=1;
409  cat = owl_malloc(strlen(A)+strlen(B)+3);
410  strcpy(cat, A);
411  strcat(cat, " ");
412  strcat(cat, B);
413  tok = atokenize(cat, " ", &toklen);
414  strcpy(cat, "");
415  for (i=0; i<toklen; i++) {
416    int dup=0;
417    for (j=0; j<i; j++) {
418      if (!strcmp(tok[i], tok[j])) dup=1;
419    }
420    if (!dup && (!prohibit || !strchr(prohibit, tok[i][0]))) {
421      if (!first) {
422        strcat(cat, " ");
423      }
424      first=0;
425      strcat(cat, tok[i]);
426    }
427  }
428  atokenize_free(tok, toklen);
429  return(cat);
430}
431
432/* hooks for doing memory allocation et. al. in owl */
433
434void *owl_malloc(size_t size)
435{
436  return(g_malloc(size));
437}
438
439void owl_free(void *ptr)
440{
441  g_free(ptr);
442}
443
444char *owl_strdup(const char *s1)
445{
446  return(g_strdup(s1));
447}
448
449void *owl_realloc(void *ptr, size_t size)
450{
451  return(g_realloc(ptr, size));
452}
453
454/* allocates memory and returns the string or null.
455 * caller must free the string.
456 */
457char *owl_sprintf(const char *fmt, ...)
458{
459  va_list ap;
460  char *ret = NULL;
461  va_start(ap, fmt);
462  ret = g_strdup_vprintf(fmt, ap);
463  va_end(ap);
464  return ret;
465}
466
467
468/* Return the owl color associated with the named color.  Return -1
469 * if the named color is not available
470 */
471int owl_util_string_to_color(char *color)
472{
473  int c;
474  if (!strcasecmp(color, "black")) {
475    return(OWL_COLOR_BLACK);
476  } else if (!strcasecmp(color, "red")) {
477    return(OWL_COLOR_RED);
478  } else if (!strcasecmp(color, "green")) {
479    return(OWL_COLOR_GREEN);
480  } else if (!strcasecmp(color, "yellow")) {
481    return(OWL_COLOR_YELLOW);
482  } else if (!strcasecmp(color, "blue")) {
483    return(OWL_COLOR_BLUE);
484  } else if (!strcasecmp(color, "magenta")) {
485    return(OWL_COLOR_MAGENTA);
486  } else if (!strcasecmp(color, "cyan")) {
487    return(OWL_COLOR_CYAN);
488  } else if (!strcasecmp(color, "white")) {
489    return(OWL_COLOR_WHITE);
490  } else if (!strcasecmp(color, "default")) {
491    return(OWL_COLOR_DEFAULT);
492  }
493  c = atoi(color);
494  if (c >= -1 && c < COLORS) {
495    return(c);
496  }
497  return(-1);
498}
499
500/* Return a string name of the given owl color */
501char *owl_util_color_to_string(int color)
502{
503  if (color==OWL_COLOR_BLACK)   return("black");
504  if (color==OWL_COLOR_RED)     return("red");
505  if (color==OWL_COLOR_GREEN)   return("green");
506  if (color==OWL_COLOR_YELLOW)  return("yellow");
507  if (color==OWL_COLOR_BLUE)    return("blue");
508  if (color==OWL_COLOR_MAGENTA) return("magenta");
509  if (color==OWL_COLOR_CYAN)    return("cyan");
510  if (color==OWL_COLOR_WHITE)   return("white");
511  if (color==OWL_COLOR_DEFAULT) return("default");
512  return("Unknown color");
513}
514
515/* Get the default tty name.  Caller must free the return */
516char *owl_util_get_default_tty()
517{
518  char *out, *tmp;
519
520  if (getenv("DISPLAY")) {
521    out=owl_strdup(getenv("DISPLAY"));
522  } else if ((tmp=ttyname(fileno(stdout)))!=NULL) {
523    out=owl_strdup(tmp);
524    if (!strncmp(out, "/dev/", 5)) {
525      owl_free(out);
526      out=owl_strdup(tmp+5);
527    }
528  } else {
529    out=owl_strdup("unknown");
530  }
531  return(out);
532}
533
534
535/* Animation hack */
536void owl_hack_animate()
537{
538  owl_messagelist *ml;
539  owl_message *m;
540  owl_fmtext *fm;
541  char *text, *ptr;
542  int place;
543
544  /* grab the first message and make sure its id is 0 */
545  ml=owl_global_get_msglist(&g);
546  m=owl_messagelist_get_element(ml, 0);
547  if (!m) return;
548  if (owl_message_get_id(m)!=0) return;
549
550  fm=owl_message_get_fmtext(m);
551  text=owl_fmtext_get_text(fm);
552
553  ptr=strstr(text, "OvO");
554  if (ptr) {
555    place=ptr-text;
556    owl_fmtext_set_char(fm, place, '-');
557    owl_fmtext_set_char(fm, place+2, '-');
558
559    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
560    if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
561      owl_popwin_refresh(owl_global_get_popwin(&g));
562      /* TODO: this is a broken kludge */
563      if (owl_global_get_viewwin(&g)) {
564        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
565      }
566    }
567    owl_global_set_needrefresh(&g);
568    return;
569  }
570
571  ptr=strstr(text, "-v-");
572  if (ptr) {
573    place=ptr-text;
574    owl_fmtext_set_char(fm, place, 'O');
575    owl_fmtext_set_char(fm, place+2, 'O');
576
577    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
578    if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
579      owl_popwin_refresh(owl_global_get_popwin(&g));
580      /* TODO: this is a broken kludge */
581      if (owl_global_get_viewwin(&g)) {
582        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
583      }
584    }
585    owl_global_set_needrefresh(&g);
586    return;
587  }
588}
589
590/* strip leading and trailing new lines.  Caller must free the
591 * return.
592 */
593char *owl_util_stripnewlines(char *in)
594{
595 
596  char  *tmp, *ptr1, *ptr2, *out;
597
598  ptr1=tmp=owl_strdup(in);
599  while (ptr1[0]=='\n') {
600    ptr1++;
601  }
602  ptr2=ptr1+strlen(ptr1)-1;
603  while (ptr2>ptr1 && ptr2[0]=='\n') {
604    ptr2[0]='\0';
605    ptr2--;
606  }
607
608  out=owl_strdup(ptr1);
609  owl_free(tmp);
610  return(out);
611}
612
613/* Delete the line matching "line" from the named file.  If no such
614 * line is found the file is left intact.  If backup==1 then create a
615 * backupfile containing the original contents.  This is an
616 * inefficient impelementation which reads the entire file into
617 * memory.
618 */
619void owl_util_file_deleteline(char *filename, char *line, int backup)
620{
621  char buff[LINE], *text;
622  char *backupfilename="";
623  FILE *file, *backupfile=NULL;
624  int size, newline;
625
626  /* open the file for reading */
627  file=fopen(filename, "r");
628  if (!file) {
629    owl_function_error("Error opening file %s", filename);
630    return;
631  }
632
633  /* open the backup file for writing */
634  if (backup) {
635    backupfilename=owl_sprintf("%s.backup", filename);
636    backupfile=fopen(backupfilename, "w");
637    if (!backupfile) {
638      owl_function_error("Error opening file %s for writing", backupfilename);
639      owl_free(backupfilename);
640      return;
641    }
642    owl_free(backupfilename);
643  }
644
645  /* we'll read the entire file into memory, minus the line we don't want and
646   * and at the same time create the backup file if necessary
647   */
648  text=owl_malloc(LINE);
649  strcpy(text, "");
650  size=LINE;
651  while (fgets(buff, LINE, file)!=NULL) {
652    /* strip the newline */
653    newline=0;
654    if (buff[strlen(buff)-1]=='\n') {
655      buff[strlen(buff)-1]='\0';
656      newline=1;
657    }
658   
659    /* if we don't match the line, add to saved text in memory */
660    if (strcasecmp(buff, line)) {
661      size+=LINE;
662      text=owl_realloc(text, size);
663      strcat(text, buff);
664      if (newline) strcat(text, "\n");
665    }
666
667    /* write to backupfile if necessary */
668    if (backup) {
669      fputs(buff, backupfile);
670      if (newline) fputs("\n", backupfile);
671    }
672  }
673  if (backup) fclose(backupfile);
674  fclose(file);
675
676  /* now rewrite the original file from memory */
677  file=fopen(filename, "w");
678  if (!file) {
679    owl_function_error("WARNING: Error opening %s for writing.  Use %s to restore.", filename, backupfilename);
680    owl_function_beep();
681    owl_free(line);
682    return;
683  }
684
685  fputs(text, file);
686  fclose(file);
687}
688
689/* add the string 'str' to the list 'list' of strings, only if it
690 * is not already present
691 */
692void owl_util_list_add_unique_string(owl_list *list, char *str)
693{
694  int i, j;
695
696  j=owl_list_get_size(list);
697  for (i=0; i<j; i++) {
698    if (!strcmp(str, owl_list_get_element(list, i))) return;
699  }
700  owl_list_append_element(list, owl_strdup(str));
701}
702
703int owl_util_common_strings_in_lists(owl_list *a, owl_list *b)
704{
705  int i, j, x, y;
706
707  j=owl_list_get_size(a);
708  for (i=0; i<j; i++) {
709    y=owl_list_get_size(b);
710    for (x=0; x<y; x++) {
711      if (!strcmp(owl_list_get_element(a, i), owl_list_get_element(b, x))) return(1);
712    }
713  }
714  return(0);
715}
716
717int owl_util_max(int a, int b)
718{
719  if (a>b) return(a);
720  return(b);
721}
722
723int owl_util_min(int a, int b)
724{
725  if (a<b) return(a);
726  return(b);
727}
728
729/* Return the base class or instance from a zephyr class, by removing
730   leading `un' or trailing `.d'.
731   The caller is responsible for freeing the allocated string.
732*/
733char * owl_util_baseclass(char * class)
734{
735  char *start, *end;
736
737  start = class;
738  while(!strncmp(start, "un", 2)) {
739    start += 2;
740  }
741
742  start = owl_strdup(start);
743  end = start + strlen(start) - 1;
744  while(end > start && *end == 'd' && *(end-1) == '.') {
745    end -= 2;
746  }
747  *(end + 1) = 0;
748
749  return start;
750}
751
752char * owl_get_datadir()
753{
754  char * datadir = getenv("BARNOWL_DATA_DIR");
755  if(datadir != NULL)
756    return strchr(datadir, '=') + 1;
757  return DATADIR;
758}
759
760/* Strips format characters from a valid utf-8 string. Returns the
761   empty string if 'in' does not validate. */
762char * owl_strip_format_chars(char *in)
763{
764  char *r;
765  if (g_utf8_validate(in, -1, NULL)) {
766    char *s, *p;
767    r = owl_malloc(strlen(in)+1);
768    r[0] = '\0';
769    s = in;
770    p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
771    while(p) {
772      /* If it's a format character, copy up to it, and skip all
773         immediately following format characters. */
774      if (owl_fmtext_is_format_char(g_utf8_get_char(p))) {
775        strncat(r, s, p-s);
776        p = g_utf8_next_char(p);
777        while (p && owl_fmtext_is_format_char(g_utf8_get_char(p))) {
778          p = g_utf8_next_char(p);
779        }
780        s = p;
781        p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
782      }
783      else {
784        p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8);
785      }
786    }
787    if (s) strcat(r,s);
788  }
789  else {
790    r = owl_strdup("");
791  }
792  return r;
793}
794
795/* If in is not UTF-8, convert from ISO-8859-1. We may want to allow
796 * the caller to specify an alternative in the future. We also strip
797 * out characters in Unicode Plane 16, as we use that plane internally
798 * for formatting.
799 */
800char * owl_validate_or_convert(char *in)
801{
802  if (g_utf8_validate(in, -1, NULL)) {
803    return owl_strip_format_chars(in);
804  }
805  else {
806    return g_convert(in, -1,
807                     "UTF-8", "ISO-8859-1",
808                     NULL, NULL, NULL);
809  }
810}
811/* Attempts to convert 'in' to ISO-8859-1. Returns that if possible,
812   else returns UTF-8.
813 */
814char * owl_get_iso_8859_1_if_possible(char *in)
815{
816  char *out;
817  if (g_utf8_validate(in, -1, NULL)) {
818    out = g_convert(in, -1,
819                    "ISO-8859-1", "UTF-8",
820                    NULL, NULL, NULL);
821    if (!out) {
822      out = owl_strdup(in);
823    }
824  }
825  else {
826    out = owl_strdup("");
827  }
828  return out;
829}
830
831/* This is based on _extract() and _isCJ() from perl's Text::WrapI18N */
832int owl_util_can_break_after(gunichar c)
833{
834 
835  if (c == ' ') return 1;
836  if (c >= 0x3000 && c <= 0x312f) {
837    /* CJK punctuations, Hiragana, Katakana, Bopomofo */
838    if (c == 0x300a || c == 0x300c || c == 0x300e ||
839        c == 0x3010 || c == 0x3014 || c == 0x3016 ||
840        c == 0x3018 || c == 0x301a)
841      return 0;
842    return 1;
843  }
844  if (c >= 0x31a0 && c <= 0x31bf) {return 1;}  /* Bopomofo */
845  if (c >= 0x31f0 && c <= 0x31ff) {return 1;}  /* Katakana extension */
846  if (c >= 0x3400 && c <= 0x9fff) {return 1;}  /* Han Ideogram */
847  if (c >= 0xf900 && c <= 0xfaff) {return 1;}  /* Han Ideogram */
848  if (c >= 0x20000 && c <= 0x2ffff) {return 1;}  /* Han Ideogram */
849  return 0;
850}
851
852/**************************************************************************/
853/************************* REGRESSION TESTS *******************************/
854/**************************************************************************/
855
856#ifdef OWL_INCLUDE_REG_TESTS
857
858#include "test.h"
859
860int owl_util_regtest(void)
861{
862  int numfailed=0;
863
864  printf("# BEGIN testing owl_util\n");
865
866  FAIL_UNLESS("owl_util_substitute 1",
867              !strcmp("foo", owl_text_substitute("foo", "", "Y")));
868  FAIL_UNLESS("owl_text_substitute 2",
869              !strcmp("fYZYZ", owl_text_substitute("foo", "o", "YZ")));
870  FAIL_UNLESS("owl_text_substitute 3",
871              !strcmp("foo", owl_text_substitute("fYZYZ", "YZ", "o")));
872  FAIL_UNLESS("owl_text_substitute 4",
873              !strcmp("/u/foo/meep", owl_text_substitute("~/meep", "~", "/u/foo")));
874
875  FAIL_UNLESS("skiptokens 1", 
876              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
877  FAIL_UNLESS("skiptokens 2", 
878              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
879
880  FAIL_UNLESS("owl_util_uniq 1", 
881              !strcmp("foo bar x", owl_util_uniq("foo", "bar x", "-")));
882  FAIL_UNLESS("owl_util_uniq 2", 
883              !strcmp("foo bar x", owl_util_uniq("foo", "bar -y x", "-")));
884  FAIL_UNLESS("owl_util_uniq 3", 
885              !strcmp("meep foo bar", owl_util_uniq("meep foo", "bar foo meep", "-")));
886
887  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
888  printf("# END testing owl_util (%d failures)\n", numfailed);
889  return(numfailed);
890}
891
892#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.