Debian Codegraph @

fontforgeexe/wordlistparser.c

raw symbols SCIP
1 /******************************************************************************
2 *******************************************************************************
3 *******************************************************************************
4
5 Copyright (C) 2013 Ben Martin
6 Copyright 2014-2015, the FontForge Project Developers.
7
8 This file is part of FontForge.
9
10 FontForge is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation,either version 3 of the License, or
13 (at your option) any later version.
14
15 FontForge is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with FontForge. If not, see <http://www.gnu.org/licenses/>.
22
23 For more details see the COPYING.gplv3 file in the root directory of this
24 distribution.
25
26 *******************************************************************************
27 *******************************************************************************
28 ******************************************************************************/
29
30 #include <fontforge-config.h>
31
32 #include "fvfonts.h"
33 #include "ggadget.h"
34 #include "gwidget.h"
35 #include "uiinterface.h"
36 #include "ustring.h"
37 #include "wordlistparser.h"
38
39 #include <ctype.h>
40 #include <string.h>
41
42
43 const char* Wordlist_getSCName( SplineChar* sc )
44 {
45 /* printf("Wordlist_getSCName() sc->name:%s\n", sc->name ); */
46 /* printf("Wordlist_getSCName() sc->len :%zd\n", strlen(sc->name) ); */
47 /* printf("Wordlist_getSCName() is three:%d\n", strcmp( sc->name, "three" ) ); */
48
49 static char ret[ 1024 ];
50 int simple = false;
51 /* If the glyph is unencoded, we need to keep a slash before the name because
52 it doesn't correspond to the codepoint. */
53 if( sc->unicodeenc != -1 ) {
54 if( strlen( sc->name ) == 1 )
55 {
56 char ch = sc->name[0];
57
58 if( ch >= 'a' && ch <= 'z' )
59 simple = true;
60 else if( ch >= '0' && ch <= '9' )
61 simple = true;
62 if( ch >= 'A' && ch <= 'Z' )
63 simple = true;
64
65 if( simple )
66 {
67 strcpy( ret, sc->name );
68 return ret;
69 }
70 }
71
72 if( !strcmp( sc->name, "zero" ))
73 return "0";
74 if( !strcmp( sc->name, "one" ))
75 return "1";
76 if( !strcmp( sc->name, "two" ))
77 return "2";
78 if( !strcmp( sc->name, "three" ))
79 return "3";
80 if( !strcmp( sc->name, "four" ))
81 return "4";
82 if( !strcmp( sc->name, "five" ))
83 return "5";
84 if( !strcmp( sc->name, "six" ))
85 return "6";
86 if( !strcmp( sc->name, "seven" ))
87 return "7";
88 if( !strcmp( sc->name, "eight" ))
89 return "8";
90 if( !strcmp( sc->name, "nine" ))
91 return "9";
92 }
93
94 snprintf( ret, 1024, "/%s", sc->name );
95 return ret;
96 }
97
98 static SplineChar*
99 u_WordlistEscapedInputStringToRealString_readGlyphName(
100 SplineFont *sf, unichar_t* in, unichar_t* in_end,
101 unichar_t** updated_in, unichar_t* glyphname )
102 {
103 int startedWithBackSlash = (*in == '\\');
104 if( *in != '/' && *in != '\\' )
105 return 0;
106 bool startsWithBackSlash = *in == '\\';
107 // move over the delimiter that we know we are on
108 in++;
109 unichar_t* startpos = in;
110
111 // Get the largest possible 'glyphname' from the input stream.
112 memset( glyphname, '\0', PATH_MAX );
113 unichar_t* outname = glyphname;
114 while( *in != '/'
115 && ( !startsWithBackSlash || *in != '\\' )
116 && *in != ' ' && *in != ']' && in != in_end )
117 {
118 *outname = *in;
119 ++outname;
120 in++;
121 }
122 bool FullMatchEndsOnSpace = 0;
123 unichar_t* maxpos = in;
124 unichar_t* endpos = maxpos-1;
125 // TRACE("WordlistEscapedInputStringToRealString_readGlyphName(x1) -->:%s:<--\n", u_to_c(glyphname));
126
127 int loopCounter = 0;
128 int firstLookup = 1;
129 for( ; endpos >= startpos; endpos--, loopCounter++ )
130 {
131 // printf("WordlistEscapedInputStringToRealString_readGlyphName(trim loop top) gn:%s\n", u_to_c(glyphname) );
132 SplineChar* sc = 0;
133
134 if( startedWithBackSlash )
135 {
136 if( glyphname[0] == 'u' )
137 glyphname++;
138
139 unichar_t* endptr = 0;
140 long unicodepoint = u_strtoul( glyphname+1, &endptr, 16 );
141 TRACE("AAA glyphname:%s\n", u_to_c(glyphname+1) );
142 TRACE("AAA unicodepoint:%ld\n", unicodepoint );
143 sc = SFGetChar( sf, unicodepoint, 0 );
144 if( sc && endptr )
145 {
146 unichar_t* endofglyphname = glyphname + u_strlen(glyphname);
147 /* printf("glyphname:%p\n", glyphname ); */
148 /* printf("endptr:%p endofglyphname:%p\n", endptr, endofglyphname ); */
149 for( ; endptr < endofglyphname; endptr++ )
150 --endpos;
151 }
152 if( !sc )
153 {
154 // printf("WordlistEscapedInputStringToRealString_readGlyphName() no char found for backslashed unicodepoint:%ld\n", unicodepoint );
155 uc_strcpy(glyphname,"backslash");
156 sc = SFGetChar( sf, -1, u_to_c(glyphname) );
157 endpos = startpos;
158 }
159 }
160 else
161 {
162 if( uc_startswith( glyphname, "uni"))
163 {
164 unichar_t* endptr = 0, *tmp_gn;
165 int gn_len;
166 long unicodepoint = u_strtoul( glyphname+3, &endptr, 16 );
167 SplineChar* tmp = 0;
168 TRACE("uni prefix, codepoint: %ld\n", unicodepoint );
169 sc = SFGetChar( sf, unicodepoint, 0 );
170
171 /* When text is added after a glyphname with a period (such as "uni1234.alt"
172 * the other search heuristics will tend to interpret the period as a glyph
173 * and split the string. Here we search for the glyphname trimming off
174 * characters at the end in order to find the glyph
175 */
176 gn_len = u_strlen(glyphname);
177 tmp_gn = malloc((gn_len+1)*sizeof(unichar_t));
178 u_strncpy(tmp_gn, glyphname, gn_len);
179 for (int i = gn_len; i>0; i--) {
180 tmp_gn[i] = 0;
181 tmp = SFGetChar( sf, -1, u_to_c(tmp_gn) );
182 TRACE("looking for subst. char: %s\n", u_to_c(tmp_gn));
183 if (tmp != NULL) {
184 TRACE("have subst. char: %s\n", tmp->name ); break;
185 }
186 }
187 free(tmp_gn);
188
189 if (tmp != NULL){
190 sc = tmp;
191 } else {
192 if( sc && endptr )
193 {
194 unichar_t* endofglyphname = glyphname + u_strlen(glyphname);
195 //printf("endptr:%p endofglyphname:%p\n", endptr, endofglyphname );
196 for( ; endptr < endofglyphname; endptr++ ) --endpos;
197 }
198 }
199 }
200
201 if( firstLookup && glyphname[0] == '#' )
202 {
203 unichar_t* endptr = 0;
204 long unicodepoint = u_strtoul( glyphname+1, &endptr, 16 );
205 // printf("WordlistEscapedInputStringToRealString_readGlyphName() unicodepoint:%ld\n", unicodepoint );
206 sc = SFGetChar( sf, unicodepoint, 0 );
207 if( sc && endptr )
208 {
209 unichar_t* endofglyphname = glyphname + u_strlen(glyphname);
210 // printf("endptr:%p endofglyphname:%p\n", endptr, endofglyphname );
211 for( ; endptr < endofglyphname; endptr++ )
212 --endpos;
213 }
214 }
215 if( !sc )
216 {
217 // printf("WordlistEscapedInputStringToRealString_readGlyphName(getchar) gn:%s\n", glyphname );
218 sc = SFGetChar( sf, -1, u_to_c(glyphname) );
219 }
220 }
221
222 if( sc )
223 {
224 // printf("WordlistEscapedInputStringToRealString_readGlyphName(found!) gn:%s start:%p end:%p\n", glyphname, startpos, endpos );
225 if( !loopCounter && FullMatchEndsOnSpace )
226 {
227 endpos++;
228 }
229 *updated_in = endpos;
230 return sc;
231 }
232 if( glyphname[0] != '\0' )
233 glyphname[ u_strlen(glyphname)-1 ] = '\0';
234 }
235
236
237 *updated_in = endpos;
238
239 // printf("WordlistEscapedInputStringToRealString_readGlyphName(end) gn:%s\n", glyphname );
240 return 0;
241 }
242
243
244 int WordlistEscapedInputStringToRealString_getFakeUnicodeAsScUnicodeEnc( SplineChar *sc, void* udata )
245 {
246 return( sc->unicodeenc );
247 }
248
249
250 //
251 // If there is only one trailing slash, then remove it.
252 //
253 void WordlistTrimTrailingSingleSlash( unichar_t* txt )
254 {
255 int len = u_strlen(txt);
256 // printf("text changed, len :%d -1:%c -2:%c\n", len, txt[ len-1 ], txt[ len-2 ] );
257 if( len >= 1 )
258 {
259 if( len >= 2 && txt[ len-1 ]=='/' && txt[ len-2 ]=='/' )
260 {
261 // nothing
262 }
263 else
264 {
265 if( txt[ len-1 ]=='/' )
266 txt[ len-1 ] = '\0';
267 }
268 }
269 }
270
271
272
273 /************************************************************/
274 /************************************************************/
275 /************************************************************/
276
277 int WordListLine_countSelected( WordListLine wll )
278 {
279 int ret = 0;
280 for( ; wll->sc; wll++ ) {
281 ret += wll->isSelected;
282 }
283 return ret;
284 }
285
286 WordListLine WordListLine_end( WordListLine wll )
287 {
288 for( ; wll->sc; wll++ ) {
289 }
290 return wll;
291 }
292
293 int WordListLine_size( WordListLine wll )
294 {
295 int ret = 0;
296 for( ; wll->sc; wll++ ) {
297 ++ret;
298 }
299 return ret;
300 }
301
302
303 WordListLine WordlistEscapedInputStringToParsedDataComplex(
304 SplineFont* sf,
305 const unichar_t* input_const,
306 WordlistEscapedInputStringToRealString_getFakeUnicodeOfScFunc getUnicodeFunc,
307 void* udata )
308 {
309 unichar_t* input = u_copy( input_const );
310 int input_len = u_strlen(input);
311 WordListChar* ret = calloc( input_len+1, sizeof(WordListChar));
312 WordListChar* out = ret;
313 unichar_t* in = input;
314 unichar_t* in_end = input + input_len;
315 // trim comment and beyond from input
316 {
317 unichar_t* p = input;
318 while( p && p < in_end )
319 {
320 p = u_strchr( p, '#' );
321 if( p > input && *(p-1) == '/' )
322 {
323 p++;
324 continue;
325 }
326 if( p )
327 *p = '\0';
328 break;
329 }
330 }
331 in_end = input + u_strlen(input);
332
333 int addingGlyphsToSelected = 0;
334 int currentGlyphIndex = -1;
335 for ( ; in < in_end; in++ )
336 {
337 unichar_t ch = *in;
338 // TRACE("in:%p end:%p got char %d %c\n", in, in_end, ch, ch );
339 if( ch == '[' )
340 {
341 addingGlyphsToSelected = 1;
342 continue;
343 }
344 if( ch == ']' )
345 {
346 addingGlyphsToSelected = 0;
347 continue;
348 }
349 int isSelected = addingGlyphsToSelected;
350 currentGlyphIndex++;
351
352 if( ch == '/' || ch == '\\' )
353 {
354 // start of a glyph name
355 unichar_t glyphname[ PATH_MAX+1 ];
356 unichar_t* updated_in = 0;
357 SplineChar* sc = u_WordlistEscapedInputStringToRealString_readGlyphName( sf, in, in_end, &updated_in, glyphname );
358 if( sc )
359 {
360 in = updated_in;
361 int n = getUnicodeFunc( sc, udata );
362 if( n == -1 )
363 {
364 /*
365 * Okay, this probably means we've got an unencoded glyph (generally
366 * used for OpenType substitutions).
367 * Redeem the value from the SplineFont datamap instead of fetching from
368 * the Unicode identifier.
369 */
370 n = sf->map->backmap[sc->orig_pos];
371
372 /*
373 * Unencoded glyphs have special mappings in the SplineFont that
374 * start from 65536 (values beyond Unicode, 65535 being the reserved
375 * "frontier" value).
376 */
377 if ( (sf->map->enc->is_unicodebmp || sf->map->enc->is_unicodefull) && n < 65536 ) {
378 TRACE("ToRealString: backmapped position does not match Unicode encoding\n");
379 TRACE("orig_pos: %d, backmap: %d, attached unicode enc: %d\n", sc->orig_pos, n, sc->unicodeenc );
380 TRACE("ToRealString: INVALID CHAR POSITION, name: %s\n", sc->name );
381 }
382 }
383
384 out->sc = sc;
385 out->isSelected = isSelected;
386 out->currentGlyphIndex = currentGlyphIndex;
387 out->n = n;
388 out++;
389 /* out = utf8_idpb( out, n, 0 ); */
390 /* if( !out ) */
391 /* printf("ToRealString error on out\n"); */
392 continue;
393 }
394 }
395
396 /* If we reach this point, we're looking based on codepoint. */
397 SplineChar* sc = SFGetOrMakeChar( sf, (int)ch, 0 );
398 out->sc = sc;
399 out->isSelected = isSelected;
400 out->currentGlyphIndex = currentGlyphIndex;
401 out++;
402 }
403
404 free(input);
405 return(ret);
406 }
407
408 WordListLine WordlistEscapedInputStringToParsedData(
409 SplineFont* sf,
410 unichar_t* input_const )
411 {
412 WordListLine ret = WordlistEscapedInputStringToParsedDataComplex(
413 sf, input_const,
414 WordlistEscapedInputStringToRealString_getFakeUnicodeAsScUnicodeEnc, 0 );
415 return ret;
416 }
417
418
419
420
421 /************************************************************/
422 /************************************************************/
423 /************************************************************/
424
425 static bool WordlistLoadFileToGTextInfo_IsLineBreak( char ch )
426 {
427 return ch == '\n' || ch == '\r';
428 }
429 static bool WordlistLoadFileToGTextInfo_isLineAllWhiteSpace( char* buffer )
430 {
431 char* p = buffer;
432 for( ; *p; ++p )
433 {
434 if( !isspace( *p ))
435 return false;
436 }
437
438 return true;
439 }
440
441
442
443 GTextInfo** WordlistLoadFileToGTextInfoBasic( int words_max )
444 {
445 return WordlistLoadFileToGTextInfo( -1, words_max );
446 }
447
448
449 GTextInfo** WordlistLoadFileToGTextInfo( int type, int words_max )
450 {
451 GTextInfo **words = 0;
452 int cnt;
453 char buffer[PATH_MAX];
454 char *filename, *temp;
455
456 filename = gwwv_open_filename(type==-1 ? "File of Kerning Words":"File of glyphname lists",NULL,"*.txt",NULL);
457 if ( !filename )
458 {
459 return 0;
460 }
461 temp = utf82def_copy(filename);
462 GIOChannel* file = g_io_channel_new_file( temp, "r", 0 );
463 free(temp);
464 if ( !file )
465 {
466 ff_post_error("Could not open", "Could not open %s", filename );
467 return 0;
468 }
469 free(filename);
470
471 words = malloc( words_max * sizeof(GTextInfo *));
472
473 cnt = 0;
474 if ( type==-1 )
475 {
476 // Kerning words
477 while( true )
478 {
479 gsize len = 0;
480 gchar* buffer = 0;
481 GIOStatus status = g_io_channel_read_line( file, &buffer, &len, 0, 0 );
482
483 // printf("getline status:%d \n", status );
484 if( status != G_IO_STATUS_NORMAL )
485 break;
486
487 chomp(buffer);
488 if ( buffer[0]=='\0'
489 || WordlistLoadFileToGTextInfo_IsLineBreak(buffer[0])
490 || WordlistLoadFileToGTextInfo_isLineAllWhiteSpace( buffer ))
491 {
492 free(buffer);
493 continue;
494 }
495
496 words[cnt] = calloc(1,sizeof(GTextInfo));
497 words[cnt]->fg = words[cnt]->bg = COLOR_DEFAULT;
498 words[cnt]->text = (unichar_t *) utf82def_copy( buffer );
499 words[cnt++]->text_is_1byte = true;
500 free(buffer);
501 if( cnt >= words_max )
502 break;
503 }
504 }
505 else
506 {
507 // glyphname lists
508 strcpy(buffer,"​"); /* Zero width space: 0x200b, I use as a flag */
509 gsize bytes_read = 0;
510 while( G_IO_STATUS_NORMAL == g_io_channel_read_chars( file,
511 buffer+3,
512 sizeof(buffer)-3,
513 &bytes_read,
514 0 ))
515 {
516 if ( buffer[3]=='\n' || buffer[3]=='#' )
517 continue;
518 if ( cnt>1000-3 )
519 break;
520 if ( buffer[strlen(buffer)-1]=='\n' )
521 buffer[strlen(buffer)-1] = '\0';
522 words[cnt] = calloc(1,sizeof(GTextInfo));
523 words[cnt]->fg = words[cnt]->bg = COLOR_DEFAULT;
524 words[cnt]->text = (unichar_t *) copy( buffer );
525 words[cnt++]->text_is_1byte = true;
526 }
527 }
528
529 g_io_channel_shutdown( file, 1, 0 );
530 g_io_channel_unref( file );
531 if ( cnt!=0 )
532 {
533 words[cnt] = calloc(1,sizeof(GTextInfo));
534 words[cnt]->fg = words[cnt]->bg = COLOR_DEFAULT;
535 words[cnt++]->line = true;
536 words[cnt] = calloc(1,sizeof(GTextInfo));
537 words[cnt]->fg = words[cnt]->bg = COLOR_DEFAULT;
538 words[cnt]->text = (unichar_t *) copy( _("Load Word List...") );
539 words[cnt]->text_is_1byte = true;
540 words[cnt++]->userdata = (void *) -1;
541 words[cnt] = calloc(1,sizeof(GTextInfo));
542 words[cnt]->fg = words[cnt]->bg = COLOR_DEFAULT;
543 words[cnt]->text = (unichar_t *) copy( _("Load Glyph Name List...") );
544 words[cnt]->text_is_1byte = true;
545 words[cnt++]->userdata = (void *) -2;
546 words[cnt] = calloc(1,sizeof(GTextInfo));
547 }
548 else
549 {
550 free(words);
551 words = 0;
552 }
553 return words;
554 }
555
556
557 void Wordlist_MoveByOffset( GGadget* g, int* idx, int offset )
558 {
559 int cidx = *idx;
560 if ( cidx!=-1 )
561 {
562 int32_t len;
563 GGadgetGetList(g,&len);
564 /* We subtract 3 because: There are two lines saying "load * list" */
565 /* and then a line with a rule on it which we don't want access to */
566 if ( cidx+offset >=0 && cidx+offset<len-3 )
567 {
568 cidx += offset;
569 *idx = cidx;
570 GGadgetSelectOneListItem( g, cidx );
571 }
572 Wordlist_touch( g );
573 }
574 }
575
576 void Wordlist_touch( GGadget* g )
577 {
578 // Force any extra chars to be setup and drawn
579 GEvent e;
580 e.type=et_controlevent;
581 e.u.control.subtype = et_textchanged;
582 e.u.control.u.tf_changed.from_pulldown = GGadgetGetFirstListSelectedItem(g);
583 GGadgetDispatchEvent( g, &e );
584 }
585
586
587 void WordlistLoadToGTextInfo( GGadget* g, int* idx )
588 {
589 int words_max = 1024*128;
590 GTextInfo** words = WordlistLoadFileToGTextInfoBasic( words_max );
591 if( !words )
592 {
593 GGadgetSetTitle8(g,"");
594 return;
595 }
596
597 if( words[0] )
598 {
599 GGadgetSetList(g,words,true);
600 GGadgetSetTitle8(g,(char *) (words[0]->text));
601 GTextInfoArrayFree(words);
602 *idx = 0;
603 GGadgetSelectOneListItem( g, *idx );
604 Wordlist_touch( g );
605 return;
606 }
607 return;
608 }
609
610
611
612 unichar_t* Wordlist_selectionClear( SplineFont* sf, EncMap *map, unichar_t* txtu )
613 {
614 static unichar_t ret[ PATH_MAX ];
615 memset( ret, 0, sizeof(unichar_t) * PATH_MAX );
616
617 unichar_t *dst = ret;
618 const unichar_t *src_end = 0;
619 const unichar_t *src = 0;
620 src_end=txtu+u_strlen(txtu);
621 for ( src=txtu; src < src_end; ++src )
622 {
623 if( *src != '[' && *src != ']' )
624 {
625 *dst = *src;
626 dst++;
627 }
628 }
629
630 return ret;
631 }
632
633 unichar_t* Wordlist_selectionAdd( SplineFont* sf, EncMap *map, unichar_t* txtu, int offset )
634 {
635 int i = 0;
636 static unichar_t ret[ PATH_MAX ];
637 memset( ret, 0, sizeof(unichar_t) * PATH_MAX );
638
639 WordlistTrimTrailingSingleSlash( txtu );
640 WordListLine wll = WordlistEscapedInputStringToParsedData( sf, txtu );
641
642 for( i = 0; wll->sc; wll++, i++ )
643 {
644 SplineChar* sc = wll->sc;
645 int element_selected = wll->isSelected;
646
647 if( i == offset )
648 element_selected = 1;
649
650 if( element_selected )
651 {
652 int pos = map->backmap[ sc->orig_pos ];
653 TRACE("pos1:%d\n", pos );
654 TRACE("map:%d\n", map->map[ pos ] );
655 int gid = pos < 0 || pos >= map->enccount ? -2 : map->map[pos];
656 if( gid == -2 )
657 continue;
658 if( gid==-1 || !sf->glyphs[gid] )
659 sc = SFMakeChar( sf, map, pos );
660 else
661 sc = sf->glyphs[gid];
662 }
663
664
665 if( element_selected )
666 uc_strcat( ret, "[" );
667
668 /* uc_strcat( ret, "/" ); */
669 /* uc_strcat( ret, scarray[i]->name ); */
670 uc_strcat( ret, Wordlist_getSCName( sc ));
671
672 if( element_selected )
673 uc_strcat( ret, "]" );
674 }
675
676 return ret;
677 }
678
679
680 unichar_t* Wordlist_advanceSelectedCharsBy( SplineFont* sf, EncMap *map, unichar_t* txtu, int offset )
681 {
682 unichar_t original_data[ PATH_MAX ];
683 static unichar_t ret[ PATH_MAX ];
684 int i = 0;
685
686 u_strcpy( original_data, txtu );
687 TRACE("Wordlist_advanceSelectedCharsBy(1) %s\n", u_to_c( txtu ));
688 WordlistTrimTrailingSingleSlash( txtu );
689 WordListLine wll = WordlistEscapedInputStringToParsedData( sf, txtu );
690
691 int selectedCount = WordListLine_countSelected( wll );
692 if( !selectedCount )
693 wll->isSelected = 1;
694
695 memset( ret, 0, sizeof(unichar_t) * PATH_MAX );
696 for( i = 0; wll->sc; wll++, i++ )
697 {
698 SplineChar* sc = wll->sc;
699 int element_selected = wll->isSelected;
700
701 if( element_selected )
702 {
703 int pos = map->backmap[ sc->orig_pos ];
704 pos += offset;
705 int gid = pos < 0 || pos >= map->enccount ? -2 : map->map[pos];
706 if( gid == -2 )
707 {
708 // we can't find a glyph at the desired position.
709 // so instead of dropping it we just do not perform the operation
710 // on this char.
711 pos -= offset;
712 gid = pos < 0 || pos >= map->enccount ? -2 : map->map[pos];
713 if( gid == -2 )
714 {
715 // we can't go back manually!
716 u_strcpy( ret, original_data );
717 return ret;
718 }
719 }
720 if( gid==-1 || !sf->glyphs[gid] )
721 sc = SFMakeChar( sf, map, pos );
722 else
723 sc = sf->glyphs[gid];
724 }
725
726
727 if( element_selected )
728 uc_strcat( ret, "[" );
729
730 /* uc_strcat( ret, "/" ); */
731 /* uc_strcat( ret, scarray[i]->name ); */
732 uc_strcat( ret, Wordlist_getSCName( sc ));
733
734 if( element_selected )
735 uc_strcat( ret, "]" );
736 }
737
738 return ret;
739 }
740
741
742
743 /**
744 * haveSelection is set to true iff there is 1+ selections i txtu
745 */
746 static unichar_t* Wordlist_selectionStringOnly( unichar_t* txtu, int* haveSelection )
747 {
748 static unichar_t ret[ PATH_MAX ];
749 memset( ret, 0, sizeof(unichar_t) * PATH_MAX );
750 *haveSelection = 0;
751
752 int inSelection = 0;
753 unichar_t *dst = ret;
754 const unichar_t *src_end = 0;
755 const unichar_t *src = 0;
756 src_end=txtu+u_strlen(txtu);
757 for ( src=txtu; src < src_end; ++src )
758 {
759 if( *src == '[' )
760 {
761 inSelection = 1;
762 *haveSelection = 1;
763 continue;
764 }
765 if( *src == ']' )
766 {
767 inSelection = 0;
768 continue;
769 }
770
771 if( inSelection )
772 {
773 *dst = *src;
774 dst++;
775 }
776 }
777
778 return ret;
779 }
780
781
782 bool Wordlist_selectionsEqual( unichar_t* s1, unichar_t* s2 )
783 {
784 static unichar_t s1stripped[ PATH_MAX ];
785 static unichar_t s2stripped[ PATH_MAX ];
786 int s1HasSelection = 0;
787 int s2HasSelection = 0;
788
789 u_strcpy( s1stripped, Wordlist_selectionStringOnly( s1, &s1HasSelection ));
790 u_strcpy( s2stripped, Wordlist_selectionStringOnly( s2, &s2HasSelection ));
791
792 if( s1HasSelection && !s2HasSelection )
793 return false;
794 if( !s1HasSelection && s2HasSelection )
795 return false;
796
797 return !u_strcmp( s1stripped, s2stripped );
798 }
799
800
801 unichar_t* WordListLine_toustr( WordListLine wll )
802 {
803 unichar_t* ret = calloc( WordListLine_size(wll)+1, sizeof(unichar_t));
804 unichar_t* p = ret;
805 for( ; wll->sc; wll++, p++ ) {
806 *p = wll->sc->unicodeenc;
807 if (*p == -1) *p = wll->n;
808 }
809 return ret;
810 }
811
812
813
814
815