GophHub - go4retro/tcpser/src/phone_book.c


Raw File

    1	#include <stdio.h>
    2	#include <string.h>
    3	#include "phone_book.h"
    4	#include "debug.h"
    5	
    6	char phone_book[PH_BOOK_SIZE][2][PH_ENTRY_SIZE];
    7	int size = 0;
    8	
    9	int pb_init() {
   10	  size = 0;
   11	  return 0;
   12	}
   13	
   14	int pb_add(char* from, char* to) {
   15	  LOG_ENTER();
   16	  if(size < PH_BOOK_SIZE
   17	     && from != NULL
   18	     && to != NULL
   19	     && strlen(from) > 0
   20	     && strlen(to) > 0
   21	    ) {
   22	    // should really trim spaces.
   23	    strncpy(phone_book[size][0], from, PH_ENTRY_SIZE);
   24	    strncpy(phone_book[size][1], to, PH_ENTRY_SIZE);
   25	    size++;
   26	    LOG_EXIT();
   27	    return 0;
   28	  }
   29	  LOG_EXIT();
   30	  return -1;
   31	}
   32	
   33	int pb_search(char *number, char *address) {
   34	  int i=0;
   35	
   36	  LOG_ENTER();
   37	  strcpy(address, number);
   38	  for(i = 0; i < size; i++) {
   39	    LOG(LOG_INFO, "Searching entry %d of %d", i, size);
   40	    if(strcmp(phone_book[i][0], number) == 0) {
   41	      LOG(LOG_INFO, "Found a match for '%s': '%s'", number, phone_book[i][1]);
   42	      strncpy(address, phone_book[i][1], PH_ENTRY_SIZE);
   43	      break;
   44	    }
   45	  }
   46	  LOG_EXIT();
   47	  return 0;
   48	}
   49	

Generated by GNU Enscript 1.6.6, and GophHub 1.3.