File: gap_match.c

package info (click to toggle)
gimp-gap 2.6.0%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,656 kB
  • ctags: 7,358
  • sloc: ansic: 119,801; sh: 3,890; makefile: 932; lisp: 97; pascal: 55
file content (466 lines) | stat: -rw-r--r-- 11,169 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* gap_match.c
 * 1998.10.14 hof (Wolfgang Hofer)
 *
 * GAP ... Gimp Animation Plugins
 *
 * This Module contains:
 * layername and layerstacknumber matching procedures
 *
 */
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* revision history:
 * gimp   1.3.20a;   2003/09/14  hof: gap_match_name increased limit of 256 bytes to 2048
 * gimp   1.1.29b;   2000/11/30  hof: used g_snprintf
 * version 0.97.00  1998.10.14  hof: - created module
 */
#include "config.h"

/* SYSTEM (UNIX) includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* GIMP includes */
#include "libgimp/gimp.h"

/* GAP includes */
#include "gap_match.h"

/* ---------------------------------
 * gap_match_string_is_empty
 * ---------------------------------
 */
int
gap_match_string_is_empty (const char *str)
{
  if(str == NULL)  return(TRUE);
  if(*str == '\0') return(TRUE);


  if(g_utf8_validate(str, -1, NULL))
  {
    while(*str != '\0')
    {
      gunichar uni_char;
      
      uni_char = g_utf8_get_char(str);
      
      if(!g_unichar_isspace(uni_char)) return(FALSE);
      str = g_utf8_find_next_char(str, NULL);
    }
    return(TRUE);
  }
  
  while(*str != '\0')
  {
    if(*str != ' ') return(FALSE);
    str++;
  }

  return(TRUE);
}  /* end gap_match_string_is_empty */


/* ---------------------------------
 * gap_match_substitute_framenr
 * ---------------------------------
 *    copy new_layername to buffer
 *    and substitute [####] by curr frame number
 */
void
gap_match_substitute_framenr (char *buffer, int buff_len, char *new_layername, long curr)
{
  int l_idx;
  int l_digits;
  int l_cpy;
  char  l_fmt_str[21];
  gboolean    utf8_compliant;
  const char *src_ptr;

  l_fmt_str[0] = '%';
  l_fmt_str[1] = '0';
  l_digits = 0;

  l_idx = 0;
  if(new_layername != NULL)
  {
    utf8_compliant = g_utf8_validate(new_layername, -1, NULL);

    while((l_idx < (buff_len-1)) && (*new_layername != '\0') )
    {
      l_cpy    = 1;
      switch(*new_layername)
      {
        case '[':
          if((new_layername[1] == '#') && (l_digits == 0))
          {
            l_cpy    = 0;
            l_digits = 1;
          }
          break;
        case '#':
          if(l_digits > 0)
          {
            l_cpy    = 0;
            l_digits++;
          }
          break;
        case ']':
          if(l_digits > 0)
          {
            l_digits--;
            g_snprintf(&l_fmt_str[2], sizeof(l_fmt_str) -2,  "%dd", l_digits);
            g_snprintf(&buffer[l_idx], buff_len - l_idx, l_fmt_str, (int)curr);
            l_idx += l_digits;
            l_digits = 0;
            l_cpy    = 0;
          }
          break;
        default:
          l_digits = 0;
          break;
      }
      
      src_ptr = new_layername;
      
      if (utf8_compliant)
      {
        new_layername = g_utf8_find_next_char(new_layername, NULL);
      }
      else
      {
        new_layername++;
      }

      if(l_cpy != 0)
      {
        while(src_ptr != new_layername)
        {
          buffer[l_idx] = (*src_ptr);
          src_ptr++;
          l_idx++;
        }
      }



    }
  }

  buffer[l_idx] = '\0';
}       /* end gap_match_substitute_framenr */


/* ---------------------------------
 * p_ascii_strup
 * ---------------------------------
 */
static gchar*
p_ascii_strup(const gchar *input_string)
{
  gchar *output_str;
  
  output_str = NULL;
  if(input_string != NULL)
  {
     gchar *str;
     
     output_str = g_strdup(input_string);
     str = output_str;
     while(*str != '\0')
     {
       *str = g_ascii_toupper(*str);
       str++;
     }
  }
  return (output_str);
}  /* end p_ascii_strup */


/* ---------------------------------
 * gap_match_number
 * ---------------------------------
 * match layer_idx (int) with pattern
 * pattern contains a list like that:
 *  "0, 3-4, 7, 10"
 */
int 
gap_match_number(gint32 layer_idx, const char *pattern)
{
   char        l_digit_buff[128];
   const char *l_ptr;
   int         l_idx;
   gint32      l_num, l_range_start;
   gboolean    utf8_compliant;

   utf8_compliant = g_utf8_validate(pattern, -1, NULL);
   l_idx = 0;
   l_num = -1; l_range_start = -1;
   l_ptr = pattern; 
   while(1 == 1)
   {
      if(g_ascii_isdigit(*l_ptr))
      {
         l_digit_buff[l_idx] = *l_ptr;   /* collect digits here */
         l_idx++;
      }
      else
      {
         if(l_idx > 0)
         {
            /* now we are one character past a number */
            l_digit_buff[l_idx] = '\0';
            l_num = atol(l_digit_buff);  /* scann the number */

            if(l_num == layer_idx)
            {
               return(TRUE);             /* matches number exactly */
            }

            if((l_range_start >= 0)
            && (layer_idx >= l_range_start) && (layer_idx <= l_num ))
            {
               return(TRUE);             /* matches given range */
            }

            l_range_start = -1;     /* disable number for opening a range */
            l_idx = 0;
          }

          switch(*l_ptr)
          {
            case '\0':
               return (FALSE);
               break;
            case ' ':
            case '\t':
               break;
            case ',':
               l_num = -1;              /* disable number for opening a range */
               break;
            case '-':
                 if (l_num >= 0)
                 {
                    l_range_start = l_num;  /* prev. number opens a range */
                 }
                 break;
            default:
               /* found illegal characters */
               /* return (FALSE); */
               l_num = -1;             /* disable number for opening a range */
               l_range_start = -1;     /* disable number for opening a range */

               break;
          }
      }
      
      if(utf8_compliant)
      {
        l_ptr = g_utf8_find_next_char(l_ptr, NULL);
      }
      else
      {
        l_ptr++;
      }
   }   /* end while */

   return(FALSE);
}       /* end gap_match_number */


/* ---------------------------------
 * p_match_name
 * ---------------------------------
 * simple string matching.
 * since this procedure checks only for equal strings
 * it uses strcmp and strncmp regerdless if utf8 complient or not.
 * if dealing with utf8 compliant strings, iterating 
 * takes care that utf8 character can have more than one byte.
 *
 * returns FALSE for non matching string
 */
static int 
p_match_name(const char *layername, const char *pattern, gint32 mode, gboolean utf8_compliant)
{
   int l_llen;
   int l_plen;
   const char *uni_ptr;

   switch (mode)
   {
      case GAP_MTCH_EQUAL:
           if (0 == strcmp(layername, pattern))
           {
             return(TRUE);
           }
           break;
      case GAP_MTCH_START:
           l_plen = strlen(pattern);
           if (0 == strncmp(layername, pattern, l_plen))
           {
             return(TRUE);
           }
           break;
      case GAP_MTCH_END:
      case GAP_MTCH_ANYWHERE:
           l_llen = strlen(layername);
           l_plen = strlen(pattern);
           uni_ptr = layername;
           while (l_llen >= l_plen)
           {
             /* printf("GAP_MTCH :%s: llen:%d \n", uni_ptr, (int)l_llen); */
             
             if((l_llen == l_plen) || (mode == GAP_MTCH_ANYWHERE))
             {
               if(0 == strncmp(uni_ptr, pattern, l_plen))
               {
                 return(TRUE);
               }
             }

             if(utf8_compliant)
             {
               uni_ptr = g_utf8_find_next_char(uni_ptr, NULL);
             }
             else
             {
               uni_ptr++;
             }
             l_llen = strlen(uni_ptr);
           }
           break;
      default:
           break;

   }

   return (FALSE);

}  /* end p_match_name */


/* ---------------------------------
 * gap_match_name
 * ---------------------------------
 * simple stringmatching without wildcards
 * return TRUE if layername matches pattern according to specified mode
 * NULL pointers never match (even if compared with NULL)
 */
int 
gap_match_name(const char *layername, const char *pattern, gint32 mode, gint32 case_sensitive)
{
   int l_rc;
   const gchar *l_name_ptr;
   const gchar *l_patt_ptr;
   gchar  *l_name_buff;
   gchar  *l_patt_buff;
   gboolean utf8_compliant;

   if(pattern == NULL)   return (FALSE);
   if(layername == NULL) return (FALSE);

   l_name_buff = NULL;
   l_patt_buff = NULL;


   /* check utf8 compliance for both layername and pattern */
   utf8_compliant = FALSE;
   if(g_utf8_validate(layername, -1, NULL))
   {
     utf8_compliant = g_utf8_validate(pattern, -1, NULL);
   }


   if(case_sensitive)
   {
     /* case sensitive can compare on the originals */
     l_name_ptr = layername;
     l_patt_ptr = pattern;
   }
   else
   {
     if(utf8_compliant)
     {
       /* ignore case by converting everything to UPPER before compare */
       l_name_buff = g_utf8_strup(layername, -1);
       l_patt_buff = g_utf8_strup(pattern, -1);

     }
     else
     {
       l_name_buff = p_ascii_strup(layername);
       l_patt_buff = p_ascii_strup(pattern);
     }
     l_name_ptr = l_name_buff;
     l_patt_ptr = l_patt_buff;
   }

   l_rc = p_match_name(l_name_ptr, l_patt_ptr, mode, utf8_compliant);
   
   if(l_name_buff)
   {
     g_free(l_name_buff);
   }
   
   if(l_patt_buff)
   {
     g_free(l_patt_buff);
   }


   return (l_rc);

}  /* end gap_match_name */


/* ---------------------------------
 * gap_match_layer
 * ---------------------------------
 */
int 
gap_match_layer(gint32 layer_idx, const char *layername, const char *pattern,
                  gint32 mode, gint32 case_sensitive, gint32 invert,
                  gint nlayers, gint32 layer_id)
{
   int l_rc;
   switch(mode)
   {
     case GAP_MTCH_NUMBERLIST:
          l_rc = gap_match_number(layer_idx, pattern);
          break;
     case GAP_MTCH_INV_NUMBERLIST:
          l_rc = gap_match_number((nlayers -1) - layer_idx, pattern);
          break;
     case GAP_MTCH_ALL_VISIBLE:
          l_rc = gimp_drawable_get_visible(layer_id);
          break;
     default:
          l_rc = gap_match_name(layername, pattern, mode, case_sensitive);
          break;
   }

   if(invert == TRUE)
   {
      if(l_rc == FALSE)  { return(TRUE); }
      return(FALSE);
   }
   return (l_rc);
}  /* end gap_match_layer */