Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
numhash.h File Reference
#include <stdio.h>

Go to the source code of this file.

Typedefs

typedef struct numhash_table Numhash
 

Functions

Numhashnumhash_create (const char *url)
 
void numhash_destroy (Numhash *table)
 
int numhash_find_number (Numhash *table, Octstr *nro)
 
int numhash_find_key (Numhash *table, long long key)
 
long long numhash_get_key (Octstr *nro)
 
long long numhash_get_char_key (char *nro)
 
double numhash_hash_fill (Numhash *table, int *longest)
 
int numhash_size (Numhash *table)
 

Typedef Documentation

typedef struct numhash_table Numhash

Definition at line 94 of file numhash.h.

Function Documentation

Numhash* numhash_create ( const char *  url)

Definition at line 313 of file numhash.c.

References charset, debug(), error(), http_create_empty_headers(), http_destroy_headers(), http_get_real(), http_header_get_content_type(), HTTP_METHOD_GET, HTTP_OK, info(), lines, numhash_table::number_total, numhash_add_number(), NUMHASH_AUTO_HASH, numhash_init(), octstr_create, octstr_destroy(), octstr_get_cstr, octstr_str_compare(), type, url, and warning().

Referenced by create_onetrans(), create_oneuser(), init_smsbox(), smsc2_reload_lists(), and smsc2_start().

314 {
315  int loc, lines = 0;
316  List *request_headers, *reply_headers;
317  Octstr *url, *final_url, *reply_body;
318  Octstr *type, *charset;
319 
320  char *data, *ptr, numbuf[100];
321  int status;
322  Numhash *table;
323 
324  url = octstr_create(seek_url);
325  request_headers = http_create_empty_headers();
326  status = http_get_real(HTTP_METHOD_GET, url, request_headers, &final_url,
327  &reply_headers, &reply_body);
328  octstr_destroy(url);
329  octstr_destroy(final_url);
330  http_destroy_headers(request_headers);
331 
332  if (status != HTTP_OK) {
333  http_destroy_headers(reply_headers);
334  octstr_destroy(reply_body);
335  error(0, "Cannot load numhash!");
336  return NULL;
337  }
338  http_header_get_content_type(reply_headers, &type, &charset);
339  octstr_destroy(charset);
340  http_destroy_headers(reply_headers);
341 
342  if (octstr_str_compare(type, "text/plain") != 0) {
343  octstr_destroy(reply_body);
344  error(0, "Strange content type <%s> for numhash - expecting 'text/plain'"
345  ", operatiom fails", octstr_get_cstr(type));
346  octstr_destroy(type);
347  return NULL;
348  }
349  octstr_destroy(type);
350 
351  ptr = data = octstr_get_cstr(reply_body);
352  while(*ptr) {
353  if (*ptr == '\n') lines++;
354  ptr++;
355  }
356  debug("numhash", 0, "Total %d lines in %s", lines, seek_url);
357 
358  table = numhash_init(lines+10, NUMHASH_AUTO_HASH); /* automatic hash */
359 
360  /* now, parse the number information */
361 
362  lines = 0;
363 
364  while((ptr = strchr(data, '\n'))) { /* each line is ended with linefeed */
365  *ptr = '\0';
366  while(*data != '\0' && isspace(*data))
367  data++;
368  if (*data != '#') {
369  loc = 0;
370  while (*data != '\0') {
371  if (isdigit(*data))
372  numbuf[loc++] = *data;
373  else if (*data == ' ' || *data == '+' || *data == '-')
374  ;
375  else break;
376  data++;
377  }
378  if (loc) {
379  numbuf[loc] = '\0';
380  numhash_add_number(table, numbuf);
381  lines++;
382  }
383  else
384  warning(0, "Corrupted line '%s'", data);
385  }
386  data = ptr+1; /* next row... */
387  }
388  octstr_destroy(reply_body);
389 
390  info(0, "Read from <%s> total of %ld numbers", seek_url, table->number_total);
391  return table;
392 }
static Numhash * numhash_init(int max_numbers, int hash_size)
Definition: numhash.c:171
void error(int err, const char *fmt,...)
Definition: log.c:612
void info(int err, const char *fmt,...)
Definition: log.c:636
int type
Definition: smsc_cimd2.c:215
void http_header_get_content_type(List *headers, Octstr **type, Octstr **charset)
Definition: http.c:3202
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * charset
Definition: test_ota.c:68
static List * lines
Definition: mtbatch.c:88
long number_total
Definition: numhash.c:94
void http_destroy_headers(List *headers)
Definition: http.c:2856
Definition: http.h:142
List * http_create_empty_headers(void)
Definition: http.c:2849
void warning(int err, const char *fmt,...)
Definition: log.c:624
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
#define NUMHASH_AUTO_HASH
Definition: numhash.c:79
int http_get_real(int method, Octstr *url, List *request_headers, Octstr **final_url, List **reply_headers, Octstr **reply_body)
Definition: http.c:1806
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
int octstr_str_compare(const Octstr *ostr, const char *str)
Definition: octstr.c:971
static int numhash_add_number(Numhash *table, char *nro)
Definition: numhash.c:149
static Octstr * url
Definition: test_xmlrpc.c:84
Definition: list.c:102
void numhash_destroy ( Numhash table)

Definition at line 275 of file numhash.c.

References numhash_table::hash, and numhash_table::numbers.

Referenced by destroy_onetrans(), destroy_oneuser(), main(), smsc2_cleanup(), and smsc2_reload_lists().

276 {
277  if (table == NULL)
278  return;
279  gw_free(table->numbers);
280  gw_free(table->hash);
281  gw_free(table);
282 }
struct numhash_number * numbers
Definition: numhash.c:93
struct numhash_number ** hash
Definition: numhash.c:96
int numhash_find_key ( Numhash table,
long long  key 
)

Definition at line 228 of file numhash.c.

References numhash_table::hash, numhash_table::hash_size, numhash_number::key, and numhash_number::next.

Referenced by numhash_find_number().

229 {
230  struct numhash_number *ptr;
231 
232  ptr = table->hash[key % table->hash_size];
233  while (ptr) {
234  if (ptr->key == key) return 1;
235  ptr = ptr->next;
236  }
237  return 0; /* not found */
238 }
struct numhash_number * next
Definition: numhash.c:102
long long key
Definition: numhash.c:101
long hash_size
Definition: numhash.c:97
struct numhash_number ** hash
Definition: numhash.c:96
int numhash_find_number ( Numhash table,
Octstr nro 
)

Definition at line 218 of file numhash.c.

References numhash_number::key, numhash_find_key(), and numhash_get_key().

Referenced by bb_smscconn_receive(), blacklisted(), check_allowed_translation(), smsbox_req_handle(), smsc2_rout(), and whitelisted().

219 {
220  long long key = numhash_get_key(nro);
221  if (key < 0)
222  return key;
223 
224  return numhash_find_key(table, key);
225 }
long long key
Definition: numhash.c:101
int numhash_find_key(Numhash *table, long long key)
Definition: numhash.c:228
long long numhash_get_key(Octstr *nro)
Definition: numhash.c:242
long long numhash_get_char_key ( char *  nro)

Definition at line 257 of file numhash.c.

References numhash_number::key, and precision.

Referenced by numhash_add_number().

258 {
259  int len;
260  long long key;
261 
262  if (!nro) return -1;
263 
264  len = strlen(nro);
265 
266  if (len > precision)
267  key = strtoll(nro + len - precision, (char**) NULL, 10);
268  else
269  key = strtoll(nro, (char**) NULL, 10);
270 
271  return key;
272 }
long long key
Definition: numhash.c:101
static int precision
Definition: numhash.c:117
long long numhash_get_key ( Octstr nro)

Definition at line 242 of file numhash.c.

References numhash_number::key, octstr_get_cstr, octstr_len(), and precision.

Referenced by numhash_find_number().

243 {
244  long long key;
245 
246  if (!nro) return -1;
247 
248  if (octstr_len(nro) > precision)
249  key = strtoll(octstr_get_cstr(nro) + octstr_len(nro) - precision, (char**) NULL, 10);
250  else
251  key = strtoll(octstr_get_cstr(nro), (char**) NULL, 10);
252 
253  return key;
254 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
long long key
Definition: numhash.c:101
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
static int precision
Definition: numhash.c:117
double numhash_hash_fill ( Numhash table,
int *  longest 
)

Definition at line 285 of file numhash.c.

References numhash_table::hash, numhash_table::hash_size, and numhash_number::next.

286 {
287  int i, l, max = 0, tot = 0;
288  struct numhash_number *ptr;
289 
290  for (i=0; i < table->hash_size; i++)
291  if (table->hash[i]) {
292  tot++;
293  ptr = table->hash[i];
294  for (l=0; ptr->next; ptr = ptr->next)
295  l++;
296  if (l > max)
297  max = l;
298  }
299 
300  if (longest != NULL)
301  *longest = max;
302 
303  return (double)(tot*100.0/(table->hash_size));
304 }
struct numhash_number * next
Definition: numhash.c:102
long hash_size
Definition: numhash.c:97
struct numhash_number ** hash
Definition: numhash.c:96
int numhash_size ( Numhash table)

Definition at line 307 of file numhash.c.

References numhash_table::number_total.

308 {
309  return table->number_total;
310 }
long number_total
Definition: numhash.c:94
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.