Esempio n. 1
0
/**
 * Walk down the tree from the given node following the given path
 * @param st the suffixtree in question
 * @param v the node to start from its children
 * @param p the path to walk down and then free
 * @return a position corresponding to end
 */
static pos *walk_down( suffixtree *st, node *v, path *p )
{
    pos *q=NULL;
    int start = path_start( p );
    int len = path_len( p );
    v = node_find_child( v, st->str, st->str[start] );
    while ( len > 0 )
    {
        if ( len <= node_len(v) )
        {
            q = pos_create();
            q->loc = node_start(v)+len-1;
            q->v = v;
            break;
        }
        else
        {
            start += node_len(v);
            len -= node_len(v);
            v = node_find_child( v, st->str, st->str[start] );
        }
    }
    path_dispose( p );
    return q;
}
Esempio n. 2
0
word_t* word_alloc(char* w, path_t* p){
  word_t* tmp;
  int wsize=sizeof(char)*(strlen(w)+1);

  if ((tmp = malloc(sizeof(word_t))) == NULL){
    perror("alloc word");
    return NULL;
  }

  if ((tmp->word = malloc(wsize)) == NULL){
    free(tmp);
    perror("alloc word string");
    return NULL;
  }
  strncpy(tmp->word, w, wsize);
  if ((tmp->path = path_copy(p)) == NULL){
    free(tmp->word);
    free(tmp);
    perror("alloc word path");
    return NULL;
  }
  tmp->pathlen = path_len(tmp->path);

  return tmp;
}
Esempio n. 3
0
int
hwgfs_generate_path(
	hwgfs_handle_t		de,
	char			*path,
	int			buflen)
{
	struct dentry		*hwgfs_root;
	int			len;
	char			*path_orig = path;

	if (unlikely(de == NULL))
		return -EINVAL;

	hwgfs_root = hwgfs_vfsmount->mnt_sb->s_root;
	if (unlikely(de == hwgfs_root))
		return -EINVAL;

	spin_lock(&dcache_lock);
	len = path_len(de, hwgfs_root);
	if (len > buflen) {
		spin_unlock(&dcache_lock);
		return -ENAMETOOLONG;
	}

	path += len - 1;
	*path = '\0';

	for (;;) {
		path -= de->d_name.len;
		memcpy(path, de->d_name.name, de->d_name.len);
		de = de->d_parent;
		if (de == hwgfs_root)
			break;
		*(--path) = '/';
	}
		
	spin_unlock(&dcache_lock);
	BUG_ON(path != path_orig);
	return 0;
}
Esempio n. 4
0
int64_t compress(const char* in_filename, const char* out_filename, uint32_t dict_size, uint32_t ht_size, uint8_t flags) {

	struct bitio		*bd = bstdout;
	struct dictionary	*d = NULL;
	struct stat			file_stat;
	time_t				t;
	FILE				*fin = stdin;
	char				*md5_str;
	int					c, read_count = 0;
	uint8_t				bits, initial_bits;
	uint32_t			bitMask, cur, next_record, y;
	uint64_t			filesize = 0;
	unsigned char		*md5;


	if (out_filename != NULL && in_filename != NULL && strcmp(in_filename, out_filename) == 0) {
		errno = EINVAL;
		goto error;
	}

	if (in_filename != NULL) {
		fin = fopen(in_filename, "r");
		if (fin == NULL)
			goto error;
	}

	if (out_filename != NULL) {
		bd = bitio_open(out_filename, 'w');
		if (bd == NULL)
			goto error;
	}

	//write metadata
	if (flags & META_DICT_SIZE)
		if (meta_write(bd, META_DICT_SIZE, &dict_size, sizeof(dict_size)) < 0)
			goto error;

	if (flags & META_MD5) {
		if (fin != stdin) {
			int md5_size;

			md5 = compute_digest(fin, "md5", &md5_size);
			if (meta_write(bd, META_MD5, md5, md5_size) < 0)
				goto error;
			md5_str = sprinth(md5, md5_size);
			PRINT(1, "md5sum:\t\t\t%s\n", md5_str);
			free(md5);
			free(md5_str);
		}
		else
			PRINT(1, "md5sum:\t\t\tNot availabe when reading from stdin\n");
	}

	if ((flags & META_NAME) && in_filename != NULL) { //don't put META_NAME if input = stdin
		c = path_len(in_filename);
		if (meta_write(bd, META_NAME, (void*)&in_filename[c], strlen(in_filename) - c + 1) < 0)
			goto error;
	}

	if ((flags & META_TIMESTAMP) && in_filename != NULL) { //don't put META_TIMESTAMP if input = stdin
		fstat(fileno(fin), &file_stat);
		t = file_stat.st_mtime;
		if (meta_write(bd, META_TIMESTAMP, &t, sizeof(t)) < 0)
			goto error;
	}

	if (meta_finalize(bd) < 0)
		goto error;

	d = dict_new(dict_size, 1, ht_size, NUM_SYMBOLS);

	if (d == NULL)
		goto error;

	next_record = dict_init(d);
	initial_bits = 0;
	bitMask = 1;
	while (bitMask < next_record) {
		bitMask <<= 1;
		initial_bits++;
	}
	bits = initial_bits;
	bitMask = 1 << bits;
	
	cur = ROOT_NODE;
	for(;;) {
  		c = fgetc(fin);
		if (c == EOF) {

			//emit last word
			if (emit(bd, cur, bits) < 0)
				goto error;

			//emit EOF
			dict_lookup(d, ROOT_NODE, EOF_SYMBOL, &y);

			if (emit(bd, y, bits) < 0)
				goto error;

			break;
		}
		
		filesize++;

		if (VERBOSE_LEVEL > 0 && ++read_count >= COUNT_THRESHOLD) {
			read_count = 0;
			PRINT(1, ".");
		}

		if (!dict_lookup(d, cur, (uint16_t) c, &y)) { //node not found

			if (emit(bd, cur, bits) < 0)
				goto error;

			dict_fill(d, y, cur, (uint16_t) c, next_record++);
			if (next_record & bitMask) {
				bitMask <<= 1;
				bits++;
			}

			if (next_record == dict_size) {
				next_record = dict_reinit(d);
				bits = initial_bits;
				bitMask = 1 << bits;
			}

			// search again starting from last unmatched symbol
			dict_lookup(d, ROOT_NODE, (uint16_t) c, &y);
		}

		cur = dict_next(d, y);
	}

	PRINT(1, "\nCompression Finished\n\n");
	dict_delete(d);
	bitio_flush(bd);
	if (bd != bstdout)
		bitio_close(bd);
	if (fin != NULL)
		fclose(fin);
	return filesize;

error:
	PRINT(1, "\n");
	dict_delete(d);
	bitio_flush(bd);
	if (bd != bstdout)
		bitio_close(bd);
	if (fin != NULL)
		fclose(fin);
	return -1;
}
Esempio n. 5
0
int danger(abstract_heapt *heap) {
  return path_len(heap, p, null_ptr) == 1;
}
Esempio n. 6
0
int init(abstract_heapt *heap) {
  return path_len(heap, p, null_ptr) == 1;
}