Subversion Repositories wimsdev

Rev

Rev 14716 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14716 Rev 15588
Line 543... Line 543...
543
 
543
 
544
/* Check whether parentheses are balanced in a given string.
544
/* Check whether parentheses are balanced in a given string.
545
 * Returns 0 if OK.
545
 * Returns 0 if OK.
546
 */
546
 */
547
/* style=0: simple check. style<>0: strong check. */
547
/* style=0: simple check. style<>0: strong check. */
548
int check_parentheses(char *p, int style)
548
static int _check_parentheses(char *p, char *q, int style)
549
{
549
{
550
  int i,j,k;
550
  int i,j = q-p, k;
551
  j=strlen(p);
-
 
552
  if(j>=MAX_LINELEN) return 65535;
-
 
553
  if(style!=0) {
551
  if(style!=0) {
554
    char buf[MAX_LINELEN+1];
-
 
555
    char *pp, *pe, c;
552
    char *pp, *pe, c;
556
    for(pp=p;pp<p+j;pp++) {
553
    for(pp=p;pp<p+j;pp++) {
557
      switch (*pp) {
554
      switch (*pp) {
558
        case ')':
555
        case ')':
559
        case ']':
556
        case ']':
Line 561... Line 558...
561
        case '(': c=')'; goto find;
558
        case '(': c=')'; goto find;
562
        case '[': c=']'; goto find;
559
        case '[': c=']'; goto find;
563
        case '{': c='}';
560
        case '{': c='}';
564
        find: {
561
        find: {
565
          pe=find_matching(pp+1,c);
562
          pe=find_matching(pp+1,c);
566
          if(pe==NULL) return 1;
563
          if(pe==NULL||pe>q) return 1;
567
          memcpy(buf,pp+1,pe-pp-1);
-
 
568
          buf[pe-pp-1]=0;
-
 
569
          if((k=check_parentheses(buf,1))!=0) return k;
564
              if((k=_check_parentheses(pp+1,pe-1,1))!=0) return k;
570
          else pp=pe;
565
          else pp=pe;
571
        }
566
        }
572
        default: break;
567
        default: break;
573
      }
568
      }
574
    }
569
    }
Line 577... Line 572...
577
  for(i=k=0;i<j && k>=0;i++) {
572
  for(i=k=0;i<j && k>=0;i++) {
578
    if(*(p+i)=='(') k++;
573
    if(*(p+i)=='(') k++;
579
    if(*(p+i)==')') k--;
574
    if(*(p+i)==')') k--;
580
  }
575
  }
581
  return k;
576
  return k;
-
 
577
}
-
 
578
 
-
 
579
int check_parentheses(char *p, int style)
-
 
580
{
-
 
581
  int j=strlen(p);
-
 
582
  if(j>=MAX_LINELEN) return 65535;
-
 
583
  return _check_parentheses(p, p+j, style);
582
}
584
}
583
 
585
 
584
/* Strip enclosing pairs of parentheses */
586
/* Strip enclosing pairs of parentheses */
585
void strip_enclosing_par(char *p)
587
void strip_enclosing_par(char *p)
586
{
588
{