예제 #1
0
int main(int argc, char **argv)
{
    st_manager holder;

    /*options */
    char c;
    int disp_f = 0;     /* shows patterns in coloured text */
    int num_f = 0;      /* displays the number of occurences */
    int ind_f = 0;      /* lists all the indices[from 0] of occurence of pattern */

    if (argc < 3) {
        print_usage(argv[0]);
        return EXIT_FAILURE;
    }

    FILE *inp_file = fopen(argv[1], "r");
    char *pattern = argv[2];

    /* parse arguments */
    while ((c = getopt(argc, argv, "dni")) != -1)
        switch(c)
        {
            case 'd':
                disp_f = 1;
                break;
            case 'n':
                num_f = 1;
                break;
            case 'i':
                ind_f = 1;
                break;
            default:
                break;

        }

    /*
    FILE *inp_file = fopen(argv[1], "r");
    char *pattern = argv[2];
    */

    holder = pat_search(inp_file, pattern);
    
    if (num_f) { printf("occurences: %d \n", holder.num_occur); }

    if (disp_f) {
        //rewind file as it has already been read
        fseek(inp_file, 0, SEEK_SET);
        display_pattern(inp_file, pattern, holder);
    }

    if (ind_f) {
        list_indices(holder);
    }

    fclose(inp_file);

    return EXIT_SUCCESS;
}
예제 #2
0
/** Test if a particular key exists in a map.
 * \param im the map.
 * \param key the key to look up.
 * \return true if the key is present, false otherwise.
 */
bool
im_exists(intmap *im, im_key key)
{
  patricia *node;

  if (!im->root)
    return 0;
  node = pat_search(im->root, key, -1);
  return node->key == key;
}
예제 #3
0
/* Returns the node matching the key or its prefix */
static patricia *
pat_search(patricia *node, im_key key, int bit)
{
  assert(node);

  if (node->bit <= bit)
    return node;
  else
    return pat_search(node->links[digit(key, node->bit)], key, node->bit);
}
예제 #4
0
/** Look up an element in the map.
 * \param im the map.
 * \param key the key to look up.
 * \returns the value of the data stored under that key, or NULL
 */
void *
im_find(intmap *im, im_key key)
{
  patricia *node;

  if (!im->root)
    return NULL;

  node = pat_search(im->root, key, -1);

  if (node->key == key)
    return node->data;
  else
    return NULL;
}
예제 #5
0
int main(int argc, char **argv)
{
    if (argc < 2) {
        fprintf(stderr, "wrong usage\n");
        return EXIT_FAILURE;
    }

    FILE *inp_file = fopen(argv[1], "r");
    char *pattern = argv[2];

    int occurs = pat_search(inp_file, pattern);
    printf("OCCURS: %d \n", occurs);

    fclose(inp_file);

    return EXIT_SUCCESS;
}
예제 #6
0
/** Insert a new element into the map.
 * \param im the map to insert into.
 * \param key the key
 * \param data Pointer to data to store under this key.
 * \return true on success, false on failure (Usually a duplicate key)
 */
bool
im_insert(intmap *im, im_key key, void *data)
{
  patricia *here, *newnode, *prev = NULL;
  int bit, prevbit = 0;

  newnode = slab_malloc(intmap_slab, im->root);
  newnode->key = key;
  newnode->data = data;
  newnode->links[0] = newnode->links[1] = newnode;

  /* First key added to tree */
  if (!im->root) {
    newnode->bit = 0;
    im->root = newnode;
    im->count += 1;
    assert(im->count == 1);
    return true;
  }

  here = pat_search(im->root, key, -1);
  if (here && here->key == key) {
    /* Duplicate key fails */
    slab_free(intmap_slab, newnode);
    return false;
  }

  /* Not a duplicate, so key and here->key /will/ differ in at least one
     bit. No need to make sure that bit doesn't go > 31 */
  for (bit = 0; digit(key, bit) == digit(here->key, bit); bit++) ;

  newnode->bit = bit;

  if (bit < im->root->bit) {
    newnode->links[digit(im->root->key, bit)] = im->root;
    im->root = newnode;
    im->count += 1;
    assert(im->count > 1);
    return true;
  }

  for (here = im->root;;) {
    int i;
    if (here->bit == bit) {
      newnode->bit = bit + 1;
      here->links[digit(key, bit)] = newnode;
      im->count += 1;
      assert(im->count > 1);
      return true;
    }
    if (here->bit > bit || (prev && here->bit <= prev->bit)) {
      prev->links[prevbit] = newnode;
      newnode->links[digit(here->key, bit)] = here;
      im->count += 1;
      assert(im->count > 1);
      return true;
    }

    prev = here;
    i = digit(key, here->bit);
    here = prev->links[i];
    prevbit = i;
  }

  /* Not reached */
  assert(0);
  return false;
}
예제 #7
0
int
main(int argc, char **argv)
{
	struct ptree *phead;
	struct ptree *p,*pfind;
	struct ptree_mask *pm;
	FILE *fp;
	char line[128];
	char addr_str[16];
	struct in_addr addr;
	unsigned long mask=0xffffffff;
	float time;

	if (argc<2) {
		printf("Usage: %s <TCP stream>\n", argv[0]);
		exit(-1);
	}
	/*
	 * Open file of IP addresses and masks.
	 * Each line looks like:
	 *    10.0.3.4 0xffff0000
	 */
	if ((fp = fopen(argv[1], "r")) == NULL) {
		printf("File %s doesn't seem to exist\n",argv[1]);
		exit(0);
	}

	/*
	 * Initialize the Patricia trie by doing the following:
	 *   1. Assign the head pointer a default route/default node
	 *   2. Give it an address of 0.0.0.0 and a mask of 0x00000000
	 *      (matches everything)
	 *   3. Set the bit position (p_b) to 0.
	 *   4. Set the number of masks to 1 (the default one).
	 *   5. Point the head's 'left' and 'right' pointers to itself.
	 * NOTE: This should go into an intialization function.
	 */
	phead = (struct ptree *)malloc(sizeof(struct ptree));
	if (!phead) {
		perror("Allocating p-trie node");
		exit(0);
	}
	bzero(phead, sizeof(*phead));
	phead->p_m = (struct ptree_mask *)malloc(
			sizeof(struct ptree_mask));
	if (!phead->p_m) {
		perror("Allocating p-trie mask data");
		exit(0);
	}
	bzero(phead->p_m, sizeof(*phead->p_m));
	pm = phead->p_m;
	pm->pm_data = (struct MyNode *)malloc(sizeof(struct MyNode));
	if (!pm->pm_data) {
		perror("Allocating p-trie mask's node data");
		exit(0);
	}
	bzero(pm->pm_data, sizeof(*pm->pm_data));
	/*******
	 *
	 * Fill in default route/default node data here.
	 *
	 *******/
	phead->p_mlen = 1;
	phead->p_left = phead->p_right = phead;


	/*
	 * The main loop to insert nodes.
	 */
	while (fgets(line, 128, fp)) {
		/*
		 * Read in each IP address and mask and convert them to
		 * more usable formats.
		 */
		sscanf(line, "%f %d", &time, (unsigned int *)&addr);
		//inet_aton(addr_str, &addr);

		/*
		 * Create a Patricia trie node to insert.
		 */
		p = (struct ptree *)malloc(sizeof(struct ptree));
		if (!p) {
			perror("Allocating p-trie node");
			exit(0);
		}
		bzero(p, sizeof(*p));

		/*
		 * Allocate the mask data.
		 */
		p->p_m = (struct ptree_mask *)malloc(
				sizeof(struct ptree_mask));
		if (!p->p_m) {
			perror("Allocating p-trie mask data");
			exit(0);
		}
		bzero(p->p_m, sizeof(*p->p_m));

		/*
		 * Allocate the data for this node.
		 * Replace 'struct MyNode' with whatever you'd like.
		 */
		pm = p->p_m;
		pm->pm_data = (struct MyNode *)malloc(sizeof(struct MyNode));
		if (!pm->pm_data) {
			perror("Allocating p-trie mask's node data");
			exit(0);
		}
		bzero(pm->pm_data, sizeof(*pm->pm_data));

		/*
		 * Assign a value to the IP address and mask field for this
		 * node.
		 */
		p->p_key = addr.s_addr;		/* Network-byte order */
		p->p_m->pm_mask = htonl(mask);

		pfind=pat_search(addr.s_addr,phead);
		//printf("%08x %08x %08x\n",p->p_key, addr.s_addr, p->p_m->pm_mask);
		//if(pfind->p_key==(addr.s_addr&pfind->p_m->pm_mask))
		if(pfind->p_key==addr.s_addr)
		{
			printf("%f %08x: ", time, addr.s_addr);
			printf("Found.\n");
		}
		else
		{
			/*
		 	* Insert the node.
		 	* Returns the node it inserted on success, 0 on failure.
		 	*/
			//printf("%08x: ", addr.s_addr);
			//printf("Inserted.\n");
			p = pat_insert(p, phead);
		}
		if (!p) {
			fprintf(stderr, "Failed on pat_insert\n");
			exit(0);
		}
	}

	exit(1);
}