extime.c 2.73 KB
Newer Older
saeed's avatar
saeed committed
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
typedef struct{
  char *beg;
  int size;
}string;


static long long int pow10(long long int n){
  long long int d = 1;
  if (n > 0){
    for (long long int i=0; i < n;++i) d *= 10;
  }
  return d;
}

static long long int stoi_bg(char *beg, char *end){
  int sign = 0;
  long long int nc=0;
  char *temp = beg;

  while (temp<=end ){
    if ( *temp != ' ') nc++;
    temp++;
  }
  nc--;
  long long int d = 0;
  long long int c = 1;
  char *st = beg;
  while (st<=end ){
    if (*st == '-'){
      c = -1;
      nc--;
      st++;
      sign += 1;
      if (sign > 1 ) return 0;
    }
    else if (*st == '+'){
      c = 1;
      nc--;
      st++;
      sign += 1;
      if (sign > 1 ) return 0;
    }
    else if( *st != ' ') {
      d += ((int)(*st - '0')*pow10(nc));
      st++;
      nc--;
    }
    else {
      st++;
    }
  }

  return (c*d);
}


static int lenstr(char *str){
  int result = 0;
  if (str){
    while(*str != '\0'){
      result++;
      str++;
    }
  }
  return result;
}


static string find(char *str, char *sub){
  
  string result = {NULL,0};

  int lsub  = lenstr(sub);
  int count = 0;
  int found = 0;
  
  if (str && sub){
    char *t1  = str;
    char *t2  = sub;
    
    while(*t1 != '\0'){
      count = 0;
      if (*t1 == *t2){
	result.beg = t1;
	found = 1;
	count++;
	char *n1 = t1 + 1;
	char *n2 = t2 + 1;
	while(*n1 != '\0' && *n2 != '\0'){
	  if (*n1 != *n2){
	    found = 0;
	    result.beg = NULL;
	    break;
	  }
	  count++;
	  n1++;
	  n2++;
	}
	if (found){
	  break;
	}
      }
      t1++;
    }
  }
  if (count != lsub) {
    result.beg  = NULL;
    result.size = 0;
  }
  if (count == lsub && found){
    result.size = (count);
  }
  return result;
}
    
static string extime(char *filename, char *prefix, char *suffix){
  
  string result = {NULL,0};

  string pre = find(filename, prefix);
  string suf = find(filename, suffix);
  
  if (pre.beg && suf.beg ){
    result.beg  = pre.beg + pre.size;
    result.size = suf.beg - (pre.beg + pre.size);
  }
  
  return result;
}

static void show(string ss){
  if (ss.beg && ss.size){
    for (int i =0; i < ss.size;++i) printf("%c",*(ss.beg + i));
    //printf("\nbeg = %c %p \n",*(ss.beg),(void *)(ss.beg));
    //printf("end = %c %p \n",*(ss.beg + ss.size -1),(void *)(ss.beg + ss.size - 1));
  }
  printf("\n");
}

#if 0
int main(void){

  char *filename = "NS01_20150310_1-24H.nc";
  char *prefix   = "NS01_";
  char *suffix   = "_1-24H.nc";

  char *AA = "smhi_saqw8_sufm";
  char *BB = "saqw8"; 

  string name = find(AA,BB);
  show(name);
   
  
  string tid = extime(filename,prefix,suffix);
  show(tid);

  return 0;
  
}
#endif