Mapper
sensor_search.c
Go to the documentation of this file.
1 /*****************************************************************************\
2 * bs_hbook5() *
3 *******************************************************************************
4 * Bs_hb5() performs a binary search on a sorted file of handbook5 structures.*
5 * The sorted file of max_recs structures is on the RAMDISK in the *
6 * /tmp/HANDBOOK5.DAT file. Bs_hb5() tries to match a requested id with the *
7 * actual ids that are in the HANDBOOK5.DAT file. The requested ID is the *
8 * hb5_pc argument. Hb5_pc is a contatenation of Handbook 5 ID and Parameter *
9 * Code. If a matching record is found in the HANDBOOK5.DAT file, the matching*
10 * record is returned. Otherwise, the return value is a handbook5 structure *
11 * that contains the value CNOT_USED. *
12 * Andy Morin - CNRFC 10/10/92 *
13 * Converted to QNX 4.2 Wayne Martin - CNRFC 01/30/95 *
14 \*****************************************************************************/
15 
16 #include "prototypes.h"
17 
18 #define NO 0x00
19 #define YES 0x01
20 #define LMAX 17L
21 
22 int sensor_search(char *name, struct database *dbase,struct sensor_rec *sensor_rec) {
23 
24  size_t name_size; /* Number of chars in req string to compare */
25  unsigned long int lower, upper; /* Lower and upper bounds in HANDBOOK5.DAT file */
26  signed long int i; /* Current record position to be tested */
27  struct handbook5 hb5; /* Record to be compared */
28  signed long offset; /* Offset of record in HANDBOOK5.DAT */
29  size_t rec_size;
30  size_t erv, rv;
31  char error_msg[100];
32  struct error_message err;
33  int m,isave,string_rv;
34  char dumbuf[50];
35 
36  /* assign defaults */
37  rec_size = (size_t)sizeof(struct handbook5);
38  erv = rec_size;
39  lower = 0L;
40  upper = dbase->recs_used -1L;
41 
42  /* return NOT_USED immediately if no ID's in list to be searched */
43  if(dbase->recs_used < 1L) {
44  return(INVALID);
45  }
46 
47  name_size = strlen(name);
48 
49  m=0;
50 
51  for(;;) {
52 
53  i=(upper+lower)/2L;
54  offset = (signed long int)i * rec_size;
55 
56  rv = dbread(NTHANDBOOK5, offset, rec_size, 1L, &hb5);
57  if(rv != erv) {
58  sprintf(error_msg, "Read error THANDBOOK5,offset=%ld", offset);
59  mk_err_msg("bs_hb5()", error_msg, &err);
60  offset = dbsize(NERRORLOG);
61  dbwrite(NERRORLOG, offset, sizeof(struct error_message), 1L, &err);
62  exit(EXIT_FAILURE);
63  }
64 
65 
66  if(hb5.id[0]==0)
67  strcpy(hb5.id," ");
68 
69  if(hb5.pc[0]==0)
70  strcpy(hb5.pc," ");
71 
72  if(name_size > 5) {
73 
74  strcpy(dumbuf,hb5.id);
75  strcat(dumbuf,hb5.pc);
76 
77  }
78 
79  else
80  strcpy(dumbuf,hb5.id);
81 
82  if(((string_rv=strncmp(name,dumbuf,name_size))==0 &&
83  (name_size <= 5 && name_size==strlen(hb5.id))) ||
84  ((string_rv=strncmp(name,dumbuf,name_size))==0 &&
85  name_size > 5)) {
86 
87  strcpy(sensor_rec[m].pc,hb5.pc);
88  sensor_rec[m].index=hb5.dbk_ptr;
89  m++;
90 
91  if( name_size > 5)
92  return(m);
93 
94  isave=i;
95  i++;
96  while(i<dbase->recs_used) {
97 
98  offset = (signed long int)i * rec_size;
99  rv = dbread(NTHANDBOOK5, offset, rec_size, 1L, &hb5.id[0]);
100  if(rv != erv) {
101  sprintf(error_msg, "Read error THANDBOOK5,offset=%ld", offset);
102  mk_err_msg("bs_hb5()", error_msg, &err);
103  offset = dbsize(NERRORLOG);
104  dbwrite(NERRORLOG, offset, sizeof(struct error_message), 1L, &err);
105  exit(EXIT_FAILURE);
106 
107  }
108 
109  if((string_rv=strncmp(hb5.id,name,name_size))==0 &&
110  name_size==strlen(hb5.id)) {
111 
112  strcpy(sensor_rec[m].pc,hb5.pc);
113  sensor_rec[m].index=hb5.dbk_ptr;
114  m++;
115  i++;
116 
117  }
118 
119  else
120  break;
121 
122  }
123 
124  i=isave;
125  i--;
126  while(i>=0L) {
127 
128  offset = (signed long int)i * rec_size;
129  rv = dbread(NTHANDBOOK5, offset, rec_size, 1L, &hb5.id[0]);
130  if(rv != erv) {
131  sprintf(error_msg, "Read error THANDBOOK5,offset=%ld", offset);
132  mk_err_msg("bs_hb5()", error_msg, &err);
133  offset = dbsize(NERRORLOG);
134  dbwrite(NERRORLOG, offset, sizeof(struct error_message), 1L, &err);
135  exit(EXIT_FAILURE);
136 
137  }
138 
139  if((string_rv=strncmp(hb5.id,name,name_size))==0 &&
140  name_size==strlen(hb5.id)) {
141 
142 
143  strcpy(sensor_rec[m].pc,hb5.pc);
144  sensor_rec[m].index=hb5.dbk_ptr;
145  m++;
146  i--;
147 
148  }
149 
150  else
151  break;
152 
153  }
154 
155  break;
156 
157  }
158 
159 
160  else {
161 
162  if(string_rv > 0)
163  lower=i+1L;
164 
165  else
166  upper=i-1L;
167 
168  if(upper < lower || upper==NOT_USED)
169  return(0);
170 
171  }
172 
173 }
174 
175 
176 return(m);
177 
178 }
char name[35][30]
Definition: borshef.c:10
static int i
unsigned long int dbwrite(unsigned long int file_num, signed long int offset, unsigned long int rec_size, unsigned long int num_recs, void *buf)
Definition: dbwrite.c:22
unsigned long int dbread(unsigned long int file_num, signed long int offset, unsigned long int rec_size, unsigned long int num_recs, void *buf)
Definition: dbread.c:22
#define NERRORLOG
Definition: database.h:584
signed long int dbsize(unsigned long int file_num)
Definition: dbsize.c:13
void mk_err_msg(char *pname, char *errmsg, struct error_message *err)
Definition: mk_err_msg.c:29
#define NTHANDBOOK5
Definition: database.h:578
#define NOT_USED
Definition: database.h:110
#define INVALID
Definition: dbsearch.h:5
int isave
Definition: edit_stations.c:8
sprintf(fbuf,"/usr/mapper/nexrad/ngrid.%02d-%02d-%02d-%02d", year, month, day, hour)
struct database dbase
Definition: mapper.c:121
int sensor_search(char *name, struct database *dbase, struct sensor_rec *sensor_rec)
Definition: sensor_search.c:22
unsigned long int recs_used
Definition: database.h:223
unsigned long int dbk_ptr
Definition: database.h:275
char pc[PC_LEN]
Definition: database.h:274
char id[HB5_ID_LEN]
Definition: database.h:273
unsigned long index
Definition: misc.h:26