source: util.c @ 14acbbd

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 14acbbd was e30ed92, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
skiptokens: Handle quotes more correctly. This fixes ctl's bug about not being able to bind the ' key. In addition, add some tests for skiptokens.
  • Property mode set to 100644
File size: 17.5 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
[5145235]4#include <unistd.h>
[7d4fbcd]5#include <ctype.h>
[f36222f]6#include <pwd.h>
[7d4fbcd]7
[e19eb97]8void sepbar(const char *in)
[e4eebe8]9{
[7d4fbcd]10  char buff[1024];
11  WINDOW *sepwin;
[3eb599d]12  const owl_messagelist *ml;
[9e5c9f3]13  const owl_view *v;
[7d4fbcd]14  int x, y, i;
[e19eb97]15  const char *foo, *appendtosepbar;
[7d4fbcd]16
17  sepwin=owl_global_get_curs_sepwin(&g);
18  ml=owl_global_get_msglist(&g);
19  v=owl_global_get_current_view(&g);
20
21  werase(sepwin);
22  wattron(sepwin, A_REVERSE);
[c15bbfb]23  if (owl_global_is_fancylines(&g)) {
24    whline(sepwin, ACS_HLINE, owl_global_get_cols(&g));
25  } else {
26    whline(sepwin, '-', owl_global_get_cols(&g));
27  }
[7d4fbcd]28
[73624b4]29  if (owl_global_is_sepbar_disable(&g)) {
30    getyx(sepwin, y, x);
31    wmove(sepwin, y, owl_global_get_cols(&g)-1);
32    return;
33  }
34
[7d4fbcd]35  wmove(sepwin, 0, 2); 
36
37  if (owl_messagelist_get_size(ml)==0) {
38    strcpy(buff, " (-/-) ");
39  } else {
[1c6c4d3]40    snprintf(buff, 1024, " (%i/%i/%i) ", owl_global_get_curmsg(&g)+1,
[7d4fbcd]41            owl_view_get_size(v),
42            owl_messagelist_get_size(ml));
43  }
44  waddstr(sepwin, buff);
45
46  foo=owl_view_get_filtname(v);
[330bcec]47  if (strcmp(foo, owl_global_get_view_home(&g))) wattroff(sepwin, A_REVERSE);
[7d4fbcd]48  waddstr(sepwin, " ");
49  waddstr(sepwin, owl_view_get_filtname(v));
50  waddstr(sepwin, " ");
[330bcec]51  if (strcmp(foo, owl_global_get_view_home(&g))) wattron(sepwin, A_REVERSE);
[7d4fbcd]52
53  if (owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
54    getyx(sepwin, y, x);
55    wmove(sepwin, y, x+2);
56    wattron(sepwin, A_BOLD);
57    waddstr(sepwin, " <truncated> ");
58    wattroff(sepwin, A_BOLD);
59  }
60
61  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
62  if ((i != -1) &&
63      (i < owl_view_get_size(v)-1)) {
64    getyx(sepwin, y, x);
65    wmove(sepwin, y, x+2);
66    wattron(sepwin, A_BOLD);
67    waddstr(sepwin, " <more> ");
68    wattroff(sepwin, A_BOLD);
69  }
70
71  if (owl_global_get_rightshift(&g)>0) {
72    getyx(sepwin, y, x);
73    wmove(sepwin, y, x+2);
[1c6c4d3]74    snprintf(buff, 1024, " right: %i ", owl_global_get_rightshift(&g));
[7d4fbcd]75    waddstr(sepwin, buff);
76  }
77
[4b660cc]78  if (owl_global_is_zaway(&g) || owl_global_is_aaway(&g)) {
[7d4fbcd]79    getyx(sepwin, y, x);
80    wmove(sepwin, y, x+2);
81    wattron(sepwin, A_BOLD);
82    wattroff(sepwin, A_REVERSE);
[4b660cc]83    if (owl_global_is_zaway(&g) && owl_global_is_aaway(&g)) {
84      waddstr(sepwin, " AWAY ");
85    } else if (owl_global_is_zaway(&g)) {
86      waddstr(sepwin, " Z-AWAY ");
87    } else if (owl_global_is_aaway(&g)) {
88      waddstr(sepwin, " A-AWAY ");
89    }
[7d4fbcd]90    wattron(sepwin, A_REVERSE);
91    wattroff(sepwin, A_BOLD);
92  }
93
94  if (owl_global_get_curmsg_vert_offset(&g)) {
95    getyx(sepwin, y, x);
96    wmove(sepwin, y, x+2);
97    wattron(sepwin, A_BOLD);
98    wattroff(sepwin, A_REVERSE);
99    waddstr(sepwin, " SCROLL ");
100    wattron(sepwin, A_REVERSE);
101    wattroff(sepwin, A_BOLD);
102  }
103 
104  if (in) {
105    getyx(sepwin, y, x);
106    wmove(sepwin, y, x+2);
107    waddstr(sepwin, in);
108  }
109
110  appendtosepbar = owl_global_get_appendtosepbar(&g);
111  if (appendtosepbar && *appendtosepbar) {
112    getyx(sepwin, y, x);
113    wmove(sepwin, y, x+2);
114    waddstr(sepwin, " ");
115    waddstr(sepwin, owl_global_get_appendtosepbar(&g));
116    waddstr(sepwin, " ");
117  }
118
119  getyx(sepwin, y, x);
120  wmove(sepwin, y, owl_global_get_cols(&g)-1);
121   
122  wattroff(sepwin, A_BOLD);
123  wattroff(sepwin, A_REVERSE);
124  wnoutrefresh(sepwin);
125}
126
[e19eb97]127char **atokenize(const char *buffer, const char *sep, int *i)
[e4eebe8]128{
[7d4fbcd]129  /* each element of return must be freed by user */
130  char **args;
131  char *workbuff, *foo;
132  int done=0, first=1, count=0;
133
134  workbuff=owl_malloc(strlen(buffer)+1);
135  memcpy(workbuff, buffer, strlen(buffer)+1);
136
137  args=NULL;
138  while (!done) {
139    if (first) {
140      first=0;
[4d86e06]141      foo=strtok(workbuff, sep);
[7d4fbcd]142    } else {
[4d86e06]143      foo=strtok(NULL, sep);
[7d4fbcd]144    }
145    if (foo==NULL) {
146      done=1;
147    } else {
[4d86e06]148      args=owl_realloc(args, sizeof(char *) * (count+1));
[7d4fbcd]149      args[count]=owl_malloc(strlen(foo)+1);
150      strcpy(args[count], foo);
151      count++;
152    }
153  }
154  *i=count;
155  owl_free(workbuff);
156  return(args);
157}
158
[e19eb97]159const char *skiptokens(const char *buff, int n) {
[e30ed92]160  /* skips n tokens and returns where that would be. */
161  char quote = 0;
[7d4fbcd]162  while (*buff && n>0) {
163      while (*buff == ' ') buff++;
[e30ed92]164      while (*buff && (quote || *buff != ' ')) {
165        if(quote) {
166          if(*buff == quote) quote = 0;
167        } else if(*buff == '"' || *buff == '\'') {
168          quote = *buff;
169        }
170        buff++;
[7d4fbcd]171      }
172      while (*buff == ' ') buff++;
173      n--;
174  }
175  return buff;
176}
177
[f36222f]178/* Return a "nice" version of the path.  Tilde expansion is done, and
179 * duplicate slashes are removed.  Caller must free the return.
180 */
[e19eb97]181char *owl_util_makepath(const char *in)
[f36222f]182{
183  int i, j, x;
184  char *out, user[MAXPATHLEN];
185  struct passwd *pw;
186
187  out=owl_malloc(MAXPATHLEN+1);
188  out[0]='\0';
189  j=strlen(in);
190  x=0;
191  for (i=0; i<j; i++) {
192    if (in[i]=='~') {
193      if ( (i==(j-1)) ||          /* last character */
194           (in[i+1]=='/') ) {     /* ~/ */
195        /* use my homedir */
196        pw=getpwuid(getuid());
197        if (!pw) {
198          out[x]=in[i];
199        } else {
200          out[x]='\0';
201          strcat(out, pw->pw_dir);
202          x+=strlen(pw->pw_dir);
203        }
204      } else {
205        /* another user homedir */
206        int a, b;
207        b=0;
208        for (a=i+1; i<j; a++) {
209          if (in[a]==' ' || in[a]=='/') {
210            break;
211          } else {
212            user[b]=in[a];
213            i++;
214            b++;
215          }
216        }
217        user[b]='\0';
218        pw=getpwnam(user);
219        if (!pw) {
[8766d44]220          out[x]=in[i];
[f36222f]221        } else {
222          out[x]='\0';
223          strcat(out, pw->pw_dir);
224          x+=strlen(pw->pw_dir);
225        }
226      }
227    } else if (in[i]=='/') {
228      /* check for a double / */
229      if (i<(j-1) && (in[i+1]=='/')) {
230        /* do nothing */
231      } else {
232        out[x]=in[i];
233        x++;
234      }
235    } else {
236      out[x]=in[i];
237      x++;
238    }
239  }
240  out[x]='\0';
241  return(out);
242}
243
[e4eebe8]244void atokenize_free(char **tok, int nels)
245{
[7d4fbcd]246  int i;
247  for (i=0; i<nels; i++) {
248    owl_free(tok[i]);
249  }
250  owl_free(tok);
251}
252
253
[e4eebe8]254void owl_parsefree(char **argv, int argc)
255{
[7d4fbcd]256  int i;
257
258  if (!argv) return;
259 
260  for (i=0; i<argc; i++) {
261    if (argv[i]) owl_free(argv[i]);
262  }
263  owl_free(argv);
264}
265
[e19eb97]266char **owl_parseline(const char *line, int *argc)
[e4eebe8]267{
[7d4fbcd]268  /* break a command line up into argv, argc.  The caller must free
269     the returned values.  If there is an error argc will be set to
270     -1, argv will be NULL and the caller does not need to free
271     anything */
272
273  char **argv;
274  int i, len, between=1;
275  char *curarg;
276  char quote;
277
278  argv=owl_malloc(sizeof(char *));
279  len=strlen(line);
280  curarg=owl_malloc(len+10);
281  strcpy(curarg, "");
282  quote='\0';
283  *argc=0;
284  for (i=0; i<len+1; i++) {
285    /* find the first real character */
286    if (between) {
287      if (line[i]==' ' || line[i]=='\t' || line[i]=='\0') {
288        continue;
289      } else {
290        between=0;
291        i--;
292        continue;
293      }
294    }
295
296    /* deal with a quote character */
297    if (line[i]=='"' || line[i]=="'"[0]) {
298      /* if this type of quote is open, close it */
299      if (quote==line[i]) {
300        quote='\0';
301        continue;
302      }
303
304      /* if no quoting is open then open with this */
305      if (quote=='\0') {
306        quote=line[i];
307        continue;
308      }
309
310      /* if another type of quote is open then treat this as a literal */
311      curarg[strlen(curarg)+1]='\0';
312      curarg[strlen(curarg)]=line[i];
313      continue;
314    }
315
316    /* if it's not a space or end of command, then use it */
317    if (line[i]!=' ' && line[i]!='\t' && line[i]!='\n' && line[i]!='\0') {
318      curarg[strlen(curarg)+1]='\0';
319      curarg[strlen(curarg)]=line[i];
320      continue;
321    }
322
323    /* otherwise, if we're not in quotes, add the whole argument */
324    if (quote=='\0') {
325      /* add the argument */
326      argv=owl_realloc(argv, sizeof(char *)*((*argc)+1));
327      argv[*argc]=owl_malloc(strlen(curarg)+2);
328      strcpy(argv[*argc], curarg);
329      *argc=*argc+1;
330      strcpy(curarg, "");
331      between=1;
332      continue;
333    }
334
335    /* if it is a space and we're in quotes, then use it */
336    curarg[strlen(curarg)+1]='\0';
337    curarg[strlen(curarg)]=line[i];
338  }
339
[d524c83]340  owl_free(curarg);
[95caa16]341
[7d4fbcd]342  /* check for unbalanced quotes */
343  if (quote!='\0') {
344    owl_parsefree(argv, *argc);
345    *argc=-1;
346    return(NULL);
347  }
348
349  return(argv);
350}
351
[de03334]352/* caller must free the return */
[5b85d19]353char *owl_util_minutes_to_timestr(int in)
[de03334]354{
[f1e629d]355  int days, hours;
[de03334]356  long run;
357  char *out;
358
[5b85d19]359  run=in;
[de03334]360
[5b85d19]361  days=run/1440;
362  run-=days*1440;
363  hours=run/60;
364  run-=hours*60;
[de03334]365
366  if (days>0) {
[6eaf35b]367    out=owl_sprintf("%i d %2.2i:%2.2li", days, hours, run);
[de03334]368  } else {
[6eaf35b]369    out=owl_sprintf("    %2.2i:%2.2li", hours, run);
[de03334]370  }
371  return(out);
372}
373
[42abb10]374/* hooks for doing memory allocation et. al. in owl */
375
[e4eebe8]376void *owl_malloc(size_t size)
377{
[34509d5]378  return(g_malloc(size));
[7d4fbcd]379}
380
[e4eebe8]381void owl_free(void *ptr)
382{
[34509d5]383  g_free(ptr);
[7d4fbcd]384}
385
[e4eebe8]386char *owl_strdup(const char *s1)
387{
[34509d5]388  return(g_strdup(s1));
[7d4fbcd]389}
390
[e4eebe8]391void *owl_realloc(void *ptr, size_t size)
392{
[34509d5]393  return(g_realloc(ptr, size));
[7d4fbcd]394}
395
[e4eebe8]396/* allocates memory and returns the string or null.
397 * caller must free the string.
398 */
399char *owl_sprintf(const char *fmt, ...)
400{
[1c6c4d3]401  va_list ap;
[28ee32b]402  char *ret = NULL;
403  va_start(ap, fmt);
404  ret = g_strdup_vprintf(fmt, ap);
405  va_end(ap);
406  return ret;
[1c6c4d3]407}
408
[28ee32b]409
[12c35df]410/* Return the owl color associated with the named color.  Return -1
411 * if the named color is not available
412 */
[e19eb97]413int owl_util_string_to_color(const char *color)
[e4eebe8]414{
[c2c5c77]415  int c;
[7d4fbcd]416  if (!strcasecmp(color, "black")) {
417    return(OWL_COLOR_BLACK);
418  } else if (!strcasecmp(color, "red")) {
419    return(OWL_COLOR_RED);
420  } else if (!strcasecmp(color, "green")) {
421    return(OWL_COLOR_GREEN);
422  } else if (!strcasecmp(color, "yellow")) {
423    return(OWL_COLOR_YELLOW);
424  } else if (!strcasecmp(color, "blue")) {
425    return(OWL_COLOR_BLUE);
426  } else if (!strcasecmp(color, "magenta")) {
427    return(OWL_COLOR_MAGENTA);
428  } else if (!strcasecmp(color, "cyan")) {
429    return(OWL_COLOR_CYAN);
430  } else if (!strcasecmp(color, "white")) {
431    return(OWL_COLOR_WHITE);
432  } else if (!strcasecmp(color, "default")) {
433    return(OWL_COLOR_DEFAULT);
434  }
[c2c5c77]435  c = atoi(color);
436  if (c >= -1 && c < COLORS) {
437    return(c);
438  }
[601733d]439  return(OWL_COLOR_INVALID);
[7d4fbcd]440}
441
[e4eebe8]442/* Return a string name of the given owl color */
[e19eb97]443const char *owl_util_color_to_string(int color)
[e4eebe8]444{
[7d4fbcd]445  if (color==OWL_COLOR_BLACK)   return("black");
446  if (color==OWL_COLOR_RED)     return("red");
447  if (color==OWL_COLOR_GREEN)   return("green");
448  if (color==OWL_COLOR_YELLOW)  return("yellow");
449  if (color==OWL_COLOR_BLUE)    return("blue");
450  if (color==OWL_COLOR_MAGENTA) return("magenta");
451  if (color==OWL_COLOR_CYAN)    return("cyan");
452  if (color==OWL_COLOR_WHITE)   return("white");
453  if (color==OWL_COLOR_DEFAULT) return("default");
454  return("Unknown color");
455}
[e1c4636]456
[e4eebe8]457/* Get the default tty name.  Caller must free the return */
458char *owl_util_get_default_tty()
459{
[e19eb97]460  const char *tmp;
[65b2173]461  char *out;
[61e79a9]462
463  if (getenv("DISPLAY")) {
464    out=owl_strdup(getenv("DISPLAY"));
[5145235]465  } else if ((tmp=ttyname(fileno(stdout)))!=NULL) {
466    out=owl_strdup(tmp);
[61e79a9]467    if (!strncmp(out, "/dev/", 5)) {
468      owl_free(out);
[5145235]469      out=owl_strdup(tmp+5);
[61e79a9]470    }
471  } else {
[5145235]472    out=owl_strdup("unknown");
[61e79a9]473  }
474  return(out);
475}
476
477
[e4eebe8]478/* Animation hack */
479void owl_hack_animate()
480{
[3eb599d]481  const owl_messagelist *ml;
[4b464a4]482  owl_message *m;
483  owl_fmtext *fm;
[e19eb97]484  const char *text, *ptr;
[4b464a4]485  int place;
486
487  /* grab the first message and make sure its id is 0 */
488  ml=owl_global_get_msglist(&g);
489  m=owl_messagelist_get_element(ml, 0);
490  if (!m) return;
491  if (owl_message_get_id(m)!=0) return;
492
493  fm=owl_message_get_fmtext(m);
494  text=owl_fmtext_get_text(fm);
495
496  ptr=strstr(text, "OvO");
497  if (ptr) {
498    place=ptr-text;
499    owl_fmtext_set_char(fm, place, '-');
500    owl_fmtext_set_char(fm, place+2, '-');
501
502    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
[a15a84f]503    if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
504      owl_popwin_refresh(owl_global_get_popwin(&g));
505      /* TODO: this is a broken kludge */
506      if (owl_global_get_viewwin(&g)) {
507        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
508      }
509    }
[4b464a4]510    owl_global_set_needrefresh(&g);
511    return;
512  }
513
514  ptr=strstr(text, "-v-");
515  if (ptr) {
516    place=ptr-text;
517    owl_fmtext_set_char(fm, place, 'O');
518    owl_fmtext_set_char(fm, place+2, 'O');
519
520    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
[a15a84f]521    if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
522      owl_popwin_refresh(owl_global_get_popwin(&g));
523      /* TODO: this is a broken kludge */
524      if (owl_global_get_viewwin(&g)) {
525        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
526      }
527    }
[4b464a4]528    owl_global_set_needrefresh(&g);
529    return;
530  }
531}
[e1c4636]532
[e4eebe8]533/* strip leading and trailing new lines.  Caller must free the
534 * return.
535 */
[e19eb97]536char *owl_util_stripnewlines(const char *in)
[e4eebe8]537{
[7e3e00a]538 
539  char  *tmp, *ptr1, *ptr2, *out;
540
541  ptr1=tmp=owl_strdup(in);
542  while (ptr1[0]=='\n') {
543    ptr1++;
544  }
545  ptr2=ptr1+strlen(ptr1)-1;
[1bb1e67]546  while (ptr2>ptr1 && ptr2[0]=='\n') {
[7e3e00a]547    ptr2[0]='\0';
548    ptr2--;
549  }
550
551  out=owl_strdup(ptr1);
552  owl_free(tmp);
553  return(out);
554}
555
[38cf544c]556/* Delete the line matching "line" from the named file.  If no such
557 * line is found the file is left intact.  If backup==1 then create a
558 * backupfile containing the original contents.  This is an
559 * inefficient impelementation which reads the entire file into
560 * memory.
561 */
[e19eb97]562void owl_util_file_deleteline(const char *filename, const char *line, int backup)
[38cf544c]563{
564  char buff[LINE], *text;
[fa4562c]565  char *backupfilename=NULL;
[e6449bc]566  FILE *file, *backupfile=NULL;
[38cf544c]567  int size, newline;
568
569  /* open the file for reading */
570  file=fopen(filename, "r");
571  if (!file) {
[836ea3a3]572    owl_function_error("Error opening file %s", filename);
[38cf544c]573    return;
574  }
[e6449bc]575
[38cf544c]576  /* open the backup file for writing */
577  if (backup) {
578    backupfilename=owl_sprintf("%s.backup", filename);
579    backupfile=fopen(backupfilename, "w");
580    if (!backupfile) {
[836ea3a3]581      owl_function_error("Error opening file %s for writing", backupfilename);
[378fa14]582      owl_free(backupfilename);
[e97c4a30]583      fclose(file);
[38cf544c]584      return;
585    }
586  }
587
588  /* we'll read the entire file into memory, minus the line we don't want and
589   * and at the same time create the backup file if necessary
590   */
591  text=owl_malloc(LINE);
592  strcpy(text, "");
593  size=LINE;
594  while (fgets(buff, LINE, file)!=NULL) {
595    /* strip the newline */
596    newline=0;
597    if (buff[strlen(buff)-1]=='\n') {
598      buff[strlen(buff)-1]='\0';
599      newline=1;
600    }
601   
602    /* if we don't match the line, add to saved text in memory */
603    if (strcasecmp(buff, line)) {
604      size+=LINE;
605      text=owl_realloc(text, size);
606      strcat(text, buff);
607      if (newline) strcat(text, "\n");
608    }
609
610    /* write to backupfile if necessary */
[6a50af2]611    if (backup) {
612      fputs(buff, backupfile);
613      if (newline) fputs("\n", backupfile);
614    }
[38cf544c]615  }
[65ad073]616  if (backup) fclose(backupfile);
[38cf544c]617  fclose(file);
618
619  /* now rewrite the original file from memory */
620  file=fopen(filename, "w");
621  if (!file) {
[836ea3a3]622    owl_function_error("WARNING: Error opening %s for writing.  Use %s to restore.", filename, backupfilename);
[38cf544c]623    owl_function_beep();
[e97c4a30]624  } else {
625    fputs(text, file);
626    fclose(file);
[38cf544c]627  }
628
[e97c4a30]629  if (backup)
630    owl_free(backupfilename);
631  owl_free(text);
[38cf544c]632}
633
[fe67f1f]634int owl_util_max(int a, int b)
635{
636  if (a>b) return(a);
637  return(b);
638}
639
640int owl_util_min(int a, int b)
641{
642  if (a<b) return(a);
643  return(b);
644}
645
[7a20e4c]646/* Return the base class or instance from a zephyr class, by removing
647   leading `un' or trailing `.d'.
[be5aa09]648   The caller is responsible for freeing the allocated string.
[7a20e4c]649*/
[e19eb97]650char * owl_util_baseclass(const char * class)
[7a20e4c]651{
[59916e8]652  char *start, *end;
653
[fa4562c]654  while(!strncmp(class, "un", 2)) {
655    class += 2;
[7a20e4c]656  }
[f166580]657
[fa4562c]658  start = owl_strdup(class);
[59916e8]659  end = start + strlen(start) - 1;
[04166e9]660  while(end > start && *end == 'd' && *(end-1) == '.') {
[7a20e4c]661    end -= 2;
662  }
663  *(end + 1) = 0;
[59916e8]664
[f166580]665  return start;
[7a20e4c]666}
667
[e19eb97]668const char * owl_get_datadir()
[5376a95]669{
[e19eb97]670  const char * datadir = getenv("BARNOWL_DATA_DIR");
[5376a95]671  if(datadir != NULL)
[7bf51d5]672    return datadir;
[5376a95]673  return DATADIR;
674}
675
676/* Strips format characters from a valid utf-8 string. Returns the
677   empty string if 'in' does not validate. */
[7f6a8a2]678char * owl_strip_format_chars(const char *in)
[5376a95]679{
680  char *r;
681  if (g_utf8_validate(in, -1, NULL)) {
[7f6a8a2]682    const char *s, *p;
[5376a95]683    r = owl_malloc(strlen(in)+1);
684    r[0] = '\0';
685    s = in;
686    p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
687    while(p) {
688      /* If it's a format character, copy up to it, and skip all
689         immediately following format characters. */
[c1522ec]690      if (owl_fmtext_is_format_char(g_utf8_get_char(p))) {
[5376a95]691        strncat(r, s, p-s);
692        p = g_utf8_next_char(p);
[c1522ec]693        while (p && owl_fmtext_is_format_char(g_utf8_get_char(p))) {
[5376a95]694          p = g_utf8_next_char(p);
695        }
696        s = p;
697        p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
698      }
699      else {
700        p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8);
701      }
702    }
703    if (s) strcat(r,s);
704  }
705  else {
706    r = owl_strdup("");
707  }
708  return r;
709}
710
711/* If in is not UTF-8, convert from ISO-8859-1. We may want to allow
712 * the caller to specify an alternative in the future. We also strip
713 * out characters in Unicode Plane 16, as we use that plane internally
714 * for formatting.
715 */
[7f6a8a2]716char * owl_validate_or_convert(const char *in)
[5376a95]717{
[6201646]718  if (g_utf8_validate(in, -1, NULL)) {
[5376a95]719    return owl_strip_format_chars(in);
720  }
721  else {
[6201646]722    return g_convert(in, -1,
[5376a95]723                     "UTF-8", "ISO-8859-1",
724                     NULL, NULL, NULL);
725  }
[89f5338]726}
[4b17a6c]727/*
728 * Validate 'in' as UTF-8, and either return a copy of it, or an empty
729 * string if it is invalid utf-8.
[7b1d048]730 */
[7f6a8a2]731char * owl_validate_utf8(const char *in)
[7b1d048]732{
733  char *out;
734  if (g_utf8_validate(in, -1, NULL)) {
[4b17a6c]735    out = owl_strdup(in);
736  } else {
[7b1d048]737    out = owl_strdup("");
738  }
739  return out;
740}
[89f5338]741
[84027015]742/* This is based on _extract() and _isCJ() from perl's Text::WrapI18N */
743int owl_util_can_break_after(gunichar c)
744{
745 
746  if (c == ' ') return 1;
747  if (c >= 0x3000 && c <= 0x312f) {
748    /* CJK punctuations, Hiragana, Katakana, Bopomofo */
749    if (c == 0x300a || c == 0x300c || c == 0x300e ||
750        c == 0x3010 || c == 0x3014 || c == 0x3016 ||
751        c == 0x3018 || c == 0x301a)
752      return 0;
753    return 1;
754  }
755  if (c >= 0x31a0 && c <= 0x31bf) {return 1;}  /* Bopomofo */
756  if (c >= 0x31f0 && c <= 0x31ff) {return 1;}  /* Katakana extension */
757  if (c >= 0x3400 && c <= 0x9fff) {return 1;}  /* Han Ideogram */
758  if (c >= 0xf900 && c <= 0xfaff) {return 1;}  /* Han Ideogram */
759  if (c >= 0x20000 && c <= 0x2ffff) {return 1;}  /* Han Ideogram */
760  return 0;
761}
Note: See TracBrowser for help on using the repository browser.