source: PSPA/madxPSPA/src/mad_range.c @ 478

Last change on this file since 478 was 430, checked in by touze, 11 years ago

import madx-5.01.00

File size: 1.6 KB
Line 
1#include "madx.h"
2
3void
4get_bracket_range(char* string, char lb, char rb, int* rs, int* re)
5  /* find bracket range in string outside quotes (brackets are lb and rb) */
6{
7  int i, toggle = 0, level = 0, length = strlen(string);
8  char quote = ' ';
9  *rs = *re = -1;
10  for (i = 0; i < length; i++)
11  {
12    if (toggle)
13    {
14      if (string[i] == quote)  toggle = 0;
15    }
16    else if(string[i] == '\'' || string[i] == '\"')
17    {
18      quote = string[i]; toggle = 1;
19    }
20    else if (string[i] == lb)
21    {
22      if (level++ == 0) *rs = i;
23    }
24    else if (string[i] == rb)
25    {
26      *re = i;
27      if (--level == 0) return;
28    }
29  }
30  *rs = -1;
31}
32
33void
34get_bracket_t_range(char* toks[], char lb, char rb, int start, int end, int* rs, int* re)
35  /* find bracket range in token list (brackets are lb and rb) */
36{
37  int i, level = 0;
38  *rs = *re = start - 1;
39  for (i = start; i <= end; i++)
40  {
41    if (*toks[i] == lb)
42    {
43      if (level++ == 0) *rs = i;
44    }
45    else if (*toks[i] == rb)
46    {
47      *re = i;
48      if (--level == 0) return;
49    }
50  }
51  *rs = start - 1;
52}
53
54void
55remove_range(char* string, char* s1, char* s2)
56  /* remove portion s1...s2 (included) in string */
57{
58  char* ps1 = strstr(string, s1);
59  char* ps2 = strstr(string, s2);
60  if (ps1 != NULL && ps2 != NULL && ps1 < ps2)
61  {
62    ps2++; ps2++;
63    while (*ps2 != '\0')  *ps1++ = *ps2++;
64    *ps1 = '\0';
65  }
66}
67
68void
69remove_upto(char* string, char* s1)
70  /* removes portion from start up to s2 (included) in string */
71{
72  char* ps1 = strstr(string, s1);
73  if (ps1 != NULL)
74  {
75    ps1++; ps1++;
76    while (*ps1 != '\0') *string++ = *ps1++;
77    *string = '\0';
78  }
79}
80
81
Note: See TracBrowser for help on using the repository browser.