Beispiel #1
0
/* 划分朋友圈。成功返回0, 否则返回-1.
 * 由于该函数对fidx写,并且在错误恢复时强烈依赖于fidx内部顺序。
 * 因此必须避免竞争条件。
 *
 * 朋友圈的定义:
 * A. 若集合中元素大于1, 则至少存在一个元素可访问其它所有元素。
 * B. 若集合中元素大于1,则每一元素至少可被另一元素访问。
 * C. 不同朋友圈无访问关系。
 *
 * 划分结果存储于UserNode结构div中,最大可划分32767个朋友圈。
 * div有效范围[1, 32767]
 */
static int div_friends() {
	short div, retv;
	short *tmp_div;
	int i, j;
	UserNode *punode;
	ght_iterator_t it;
	void *key;

	tmp_div = malloc(sizeof(short) * MAXUSERS);
	if(tmp_div == NULL)
		return -1;
	for(i = 0, punode = ght_first(fidx, &it, &key); punode;
			punode = ght_next(fidx, &it, &key), i++) {
		tmp_div[i] = punode->div;
		punode->div = 0;
	}
	div = 1;
	retv = 0;
	for(punode = ght_first(fidx, &it, &key); punode && div < 32767;
			punode = ght_next(fidx, &it, &key)) {
		if(punode->div <= 0) {
			retv = div_one(punode, div++, NO_PATH);
			if(retv == -1)
				break;
		}
	}
	if(retv == -1) { //recovery
		for(j = 0, punode = ght_first(fidx, &it, &key); punode && j < i;
		    punode = ght_next(fidx, &it, &key), j++) {
			punode->div = tmp_div[j];
		}
	}
	free(tmp_div);
	return -1;
}
Beispiel #2
0
static void query_adiv(char *userid) {
	ght_iterator_t it;
	void *key;
	int div, count;
	UserNode *punode;
	FILE *fp;
	char tmpf[256], fpath[256];

	sprintf(tmpf, FRIENDS_TMPDIR "/%s.div.tmp", userid);
	if((fp = fopen(tmpf, "w+")) == NULL)
		return;
	punode = ght_get(fidx, strlen(userid), userid);
	if(punode == NULL) {
		fprintf(fp, "Query return null result.");
		goto END_WITH_RESULT;
	}
	
	div = punode->div;
	count = 0;
	for(punode = ght_first(fidx, &it, &key); punode;
			punode = ght_next(fidx, &it, &key)) {
		if(div == punode->div && div > 0) {
			count++;
			fprintf(fp, "%s%c", punode->userid, 
				(count % 6 == 5) ? '\n' : '\t');
		}
	}
END_WITH_RESULT:
	fclose(fp);
	sprintf(fpath, FRIENDS_TMPDIR "/%s.div", userid);
	rename(tmpf, fpath);
	return;
}
Beispiel #3
0
void logfile_clear(logfile *log) {
  ght_iterator_t iterator;
  const void *p_key;
  void *p_e;
  for(p_e = ght_first(log->errortypes, &iterator, &p_key); p_e; p_e = ght_next(log->errortypes, &iterator, &p_key))
  {
    logerror_destroy((logerror *)p_e);

  }
  ght_finalize(log->errortypes);

  ght_hash_table_t *map = ght_create(256);
  if (map == NULL) {
    free(log);
    exit(EXIT_FAILURE);
  }
  log->errortypes = map;
  log->errors = NULL;
  log->filtered_errors = NULL;
  log->worstNewLine = 0;
  log->worstNewType = 0;
  log->filtered_entries = 0;
  log->empty_entries = 0;

}
Beispiel #4
0
int
bansiteop(struct in_addr *from)
{
	struct bansite *b;
	ght_iterator_t iterator;
	ght_hash_table_t *tmptable;
	void *key;
	static time_t last = 0;
	time_t now_t = time(NULL);
	FILE *fp;
	if (!bansitetable)
		bansitetable = ght_create(200);
	if (!bansitetable)
		return -1;
	if (from) {
		if ((b = ght_get(bansitetable, sizeof (*from), from))) {
			b->t = now_t;
			return 0;
		}
		b = malloc(sizeof (*b));
		if (!b)
			return -1;
		b->t = time(NULL);
		b->from = *from;
		if (ght_insert(bansitetable, b, sizeof (*from), from) < 0) {
			free(b);
			return -1;
		}
	}
	if (!from && now_t - last < 100)
		return 0;
	fp = fopen(MY_BBS_HOME "/bbstmpfs/dynamic/bansite", "w");
	if (!fp)
		return -1;
	last = now_t;
	tmptable = bansitetable;
	bansitetable = ght_create(200);
	if (!bansitetable) {
		bansitetable = tmptable;
		return -1;
	}
	for (b = ght_first(tmptable, &iterator, &key); b;
	     b = ght_next(tmptable, &iterator, &key)) {
		if (b->t < now_t - 6000) {
			free(b);
			continue;
		}
		fprintf(fp, "%s\n", inet_ntoa(b->from));
		if (ght_insert(bansitetable, b, sizeof (b->from), &b->from) < 0) {
			free(b);
		}
	}
	ght_finalize(tmptable);
	fclose(fp);
	return 0;
}
void TextResource_free(TextResource tr) {
	ght_iterator_t iterator;
	const void *p_key;
	void *p_e;

	for (p_e = ght_first(tr->parameters, &iterator, &p_key); p_e;
	     p_e = ght_next(tr->parameters, &iterator, &p_key)) {
		free(p_e);
	}


	ght_finalize(tr->parameters);

	for (p_e = ght_first(tr->strings, &iterator, &p_key); p_e;
	     p_e = ght_next(tr->strings, &iterator, &p_key)) {

		free(p_e);
	}

	ght_finalize(tr->strings);

	free(tr);
}
Beispiel #6
0
/* increase the distance value daily which means the relationship betwwen two
 * users are estranged. when the value is >= INITIAL_PATH, the pr will
 * be free() in order to minium memory usage.
 */
static void estrange_by_time() {
	ght_iterator_t iterator;
	void *key;
	UserNode *punode;

	for (punode = ght_first(fidx, &iterator, &key); punode;
			punode = ght_next(fidx, &iterator, &key))
	{
		apply_queue(punode->to, estrange_user, NULL);
		delete_node3(punode->to, del_invalid_path, NULL, 1);
		apply_queue(punode->from, estrange_user, NULL);
		delete_node3(punode->from, del_invalid_path, NULL, 1);
					
	}
	return;
}
Beispiel #7
0
void logfile_close(logfile *log) {
  ght_iterator_t iterator;
  const void *p_key;
  void *p_e;
  for(p_e = ght_first(log->errortypes, &iterator, &p_key); p_e; p_e = ght_next(log->errortypes, &iterator, &p_key))
  {
    logerror_destroy((logerror *)p_e);
  }
  ght_finalize(log->errortypes);
  
  if (log->file != NULL) {
    linereader_close(log->file);
  }
  destroyLogParser(log->logformat);
  free(log);
}
Beispiel #8
0
static int save_friends() {
	ght_iterator_t iterator;
	void *key;
	UserNode *punode;
	FILE *fp;
	char *filename = MY_BBS_HOME "/bbstmpfs/tmp/friends.dump.tmp";

	fp = fopen(filename, "w+");
	if(fp == NULL)
		return -1;
	for (punode = ght_first(fidx, &iterator, &key); punode;
			punode = ght_next(fidx, &iterator, &key))
	{
		if(queue_len(punode->to) > 0) {
			fprintf(fp, "#%s\n", punode->userid);
			apply_queue(punode->to, dump_user, fp);
		}
	}
	fclose(fp);
	crossfs_rename(filename, MY_BBS_HOME "/friends.dump");
	return 0;
}
Beispiel #9
0
int main(int argc, char *argv[])
{
    ght_hash_table_t *p_table;
    ght_iterator_t iterator;
    const void *p_key;
    int *p_data;

    /* Create a new hash table */
    if ( !(p_table = ght_create(128)) )
    {
        fprintf(stderr, "Could not create hash table!\n");
        return 1;
    }

    /* Insert a number of entries */
    insert_entry(p_table, 0, "zero");
    insert_entry(p_table, 1, "one");
    insert_entry(p_table, 2, "two");
    insert_entry(p_table, 3, "three");
    insert_entry(p_table, 4, "four");
    insert_entry(p_table, 5, "five");
    insert_entry(p_table, 6, "six");
    insert_entry(p_table, 7, "seven");
    insert_entry(p_table, 8, "eight");

    for (p_data = (int*)ght_first(p_table, &iterator, &p_key); p_data;
            p_data = (int*)ght_next(p_table, &iterator, &p_key))
    {
        /* Print out the entry */
        printf("%s: \t%d\n", (char*)p_key, *p_data);
        /* Free the data (the meta-data will be removed in ght_finalize below) */
        free(p_data);
    }

    /* Remove the hash table */
    ght_finalize(p_table);

    return 0;
}
Beispiel #10
0
 /*! Return the next entry in the hash table. This function should be used
  *  for iteration, and must be called after \c first().
  *
  * \return a pointer to the next entry (data) or nullptr if there are no more
  * entries.
  */
 void* next(iterator_t & iter, const void * & p_key) { return m_ht ? ght_first(m_ht, &iter, &p_key) : nullptr; }
Beispiel #11
0
/*
 * This is an example program that reads words from a text-file (a
 * book or something like that) and uses those as keys in a hash table
 * (the actual data stored is unimportant). The words are case
 * sensitive.
 *
 * After this, the program opens another text-file and tries to match
 * the words in that with the words stored in the table.
 *
 * The meaning with this program is to test the speed of the table and
 * to provide an example of its use.
 */
int main(int argc, char *argv[])
{
    FILE *p_file;
    char *p_dict_name;
    char *p_text_name;
    ght_hash_table_t *p_table;
    ght_iterator_t iterator;
    struct stat stat_struct;
    int i_found;
    int i_cnt;
    char *p_buf;
    char *p_tok;
    const void *p_key;
    void *p_e;


    /* Create a new hash table */
    if ( !(p_table = ght_create(1000)) )
    {
        return 1;
    }

    ght_set_rehash(p_table, TRUE);

    /* Parse the arguments */
    if (argc < 3)
    {
        printf("Usage: dict_example [-m|-t|-b] dictfile textfile\n\n"
                "Reads words from `dictfile' and looks up these words in `textfile'.\n"
                "Options:\n"
                "  -m  Use move-to-front heuristics\n"
                "  -t  Use transpose heuristics\n"
                "  -b  Use bounded buckets (use the hash table as a cache)\n"
              );
        return 0;
    }
    else if (argc > 3)
    {
        if(strcmp(argv[1], "-m") == 0)
            ght_set_heuristics(p_table, GHT_HEURISTICS_MOVE_TO_FRONT);
        else if(strcmp(argv[1], "-t") == 0)
            ght_set_heuristics(p_table, GHT_HEURISTICS_TRANSPOSE);
        else if (strcmp(argv[1], "-b") == 0)
        {
            /* Rehashing makes no sense in "cache" mode */
            ght_set_rehash(p_table, FALSE);
            ght_set_bounded_buckets(p_table, 3, bucket_free_callback);
        }
        p_dict_name = argv[2];
        p_text_name = argv[3];
    }
    else
    {
        /* 2 arguments */
        p_dict_name = argv[1];
        p_text_name = argv[2];
    }

    /* Open the dictionary file (first check its size) */
    if (stat(p_dict_name, &stat_struct) < 0)
    {
        perror("stat");
        return 1;
    }
    if (!(p_buf = (char*)malloc(stat_struct.st_size)))
    {
        perror("malloc");
        return 1;
    }

    /* Open the dictionary file and read that into the buffer. */
    if (! (p_file = fopen(p_dict_name, "r")) )
    {
        perror("fopen");
        return 1;
    }
    fread(p_buf, sizeof(char), stat_struct.st_size, p_file);
    fclose(p_file);

    /* For each word in the dictionary file, insert it into the hash table. */
    p_tok = strtok(p_buf, DELIMS);
    i_cnt = 0;
    i_found = 0;
    while (p_tok)
    {
        int *p_data;

        if ( !(p_data = (int*)malloc(sizeof(int))) )
        {
            perror("malloc");
            return 1;
        }
        *p_data = i_cnt++;

        /* Insert the word into the table */
        if (ght_insert(p_table,
                    p_data,
                    strlen(p_tok), p_tok) < 0)
            free(p_data); /* Could not insert the item (already exists), free it. */
        else
            i_found++;
        p_tok = strtok(NULL, DELIMS);
    }
    printf("Done reading %d unique words from the wordlist.\n"
            "Total number of words is %d.\n\n", i_found, i_cnt);

    free(p_buf);

    /* Check the size of the text file. */
    if (stat(p_text_name, &stat_struct) < 0)
    {
        perror("stat");
        return 1;
    }
    if (!(p_buf = (char*)malloc(stat_struct.st_size)))
    {
        perror("malloc");
        return 1;
    }

    /* Open the text file and read that into the buffer. */
    if (! (p_file = fopen(p_text_name, "r")) )
    {
        perror("fopen");
        return 1;
    }
    fread(p_buf, sizeof(char), stat_struct.st_size, p_file);
    fclose(p_file);

    /* For each word in the text file, check if it exists in the hash
       table. */
    p_tok = strtok(p_buf, DELIMS);
    i_cnt = 0;
    i_found = 0;
    while (p_tok)
    {  
        printf(" searching %s ;", p_tok);
        if (ght_get(p_table,
                    strlen(p_tok), p_tok))
        {
            i_found++;
        }
        i_cnt++;
        p_tok = strtok(NULL, DELIMS);
    }
    free(p_buf);

    printf("Found %d words out of %d words\n", i_found, i_cnt);

    /* Free the entry data in the table */
    for(p_e = ght_first(p_table, &iterator, &p_key); p_e; p_e = ght_next(p_table, &iterator, &p_key))
    {
        free(p_e);
    }

    /* Free the table */
    ght_finalize(p_table);

    return 0;
}
Beispiel #12
0
static void friends_top10() {
	ght_iterator_t it;
	void *key;
	UserNode *tmp;
	int i, j;
	TopSort *ts;
	FILE *fp;

	ts = calloc(MAXUSERS, sizeof(TopSort));
	if(ts == NULL)
		return;
	
	for(i = 0, tmp = ght_first(fidx, &it, &key); tmp;
			tmp = ght_next(fidx, &it, &key)) {
		strcpy(ts[i].userid, tmp->userid);
		ts[i].to_num = queue_len(tmp->to);
		ts[i].from_num = queue_len(tmp->from);
		ts[i].to_min = INITIAL_PATH;
		apply_queue(tmp->to, min_of_queue, &ts[i].to_min);
		apply_queue(tmp->from, sum_of_queue, &ts[i].from_dist);
		ts[i].from_dist /= (ts[i].from_num ? ts[i].from_num : 1);
		i++;
	}

	//十大大众情人,"from"队列长度
	qsort(ts, i, sizeof(TopSort), (void *)cmp_fromnum);
	fp = fopen(FRIENDS_TMPDIR "/top10.idolA.tmp", "w+");
	if(fp == NULL)
		goto END_FREE_TS;
	for(j = 0; j < 10 && j < i; j++) {
		if(ts[j].from_num == 0)
			break;
		fprintf(fp, "\t\t%12s\t%4d\n", ts[j].userid, ts[j].from_num);
	}
	fclose(fp);
	rename(FRIENDS_TMPDIR "/top10.idolA.tmp", FRIENDS_TMPDIR "/top10.idolA");

	//十大博爱粉丝,"to"队列长度
	qsort(ts, i, sizeof(TopSort), (void *)cmp_tonum);
	fp = fopen(FRIENDS_TMPDIR "/top10.fansA.tmp", "w+");
	if(fp == NULL)
		goto END_FREE_TS;
	for(j = 0; j < 10 && j < i; j++) {
		if(ts[j].to_num == 0)
			break;
		fprintf(fp, "\t\t%12s\t%4d\n", ts[j].userid, ts[j].to_num);
	}
	fclose(fp);
	rename(FRIENDS_TMPDIR "/top10.fansA.tmp", FRIENDS_TMPDIR "/top10.fansA");

	//十大实力偶像, "from"队列值平均距离
	qsort(ts, i, sizeof(TopSort), (void *)cmp_fromdist);
	fp = fopen(FRIENDS_TMPDIR "/top10.idolB.tmp", "w+");
	if(fp == NULL)
		goto END_FREE_TS;
	for(j = 0; j < 10 && j < i; j++) {
		if(ts[j].from_dist == 0)
			break;
		fprintf(fp, "\t\t%12s\t%4d\n", ts[j].userid, ts[j].from_dist);
	}
	fclose(fp);
	rename(FRIENDS_TMPDIR "/top10.idolB.tmp", FRIENDS_TMPDIR "/top10.idolB");

	
	//十大忠诚粉丝, "to"队列最小值
	qsort(ts, i, sizeof(TopSort), (void *)cmp_tomin);
	fp = fopen(FRIENDS_TMPDIR "/top10.fansB.tmp", "w+");
	if(fp == NULL)
		goto END_FREE_TS;
	for(j = 0; j < 10 && j < i; j++) {
		if(INITIAL_PATH-ts[j].to_min == 0)
			break;
		fprintf(fp, "\t\t%12s\t%4d\n", ts[j].userid, INITIAL_PATH-ts[j].to_min);
	}
	fclose(fp);
	rename(FRIENDS_TMPDIR "/top10.fansB.tmp", FRIENDS_TMPDIR "/top10.fansB");
	
	//十大人气圈子
	bzero(ts, sizeof(TopSort)*MAXUSERS);
	for(tmp = ght_first(fidx, &it, &key); tmp;
			tmp = ght_next(fidx, &it, &key)) {
		if(tmp->div > 0 && tmp->div < MAXUSERS) {
			if(ts[tmp->div].userid[0] == '\0') //first user as representive
				strcpy(ts[tmp->div].userid, tmp->userid);
			ts[tmp->div].div_num++;
		}
	}
	qsort(ts, MAXUSERS, sizeof(TopSort), (void *)cmp_divnum);
	fp = fopen(FRIENDS_TMPDIR "/top10.bigdiv.tmp", "w+");
	if(fp == NULL)
		goto END_FREE_TS;
	for(j = 0; j < 10 && ts[j].div_num > 0; j++)
		fprintf(fp, "\t\t%12s\t%4d\n", ts[j].userid, ts[j].div_num);
	fclose(fp);
	rename(FRIENDS_TMPDIR "/top10.bigdiv.tmp", FRIENDS_TMPDIR "/top10.bigdiv");

END_FREE_TS:
	free(ts);
	return;
}
Beispiel #13
0
/* 按照Dijkstra算法寻找从from到to的最短路径。对fidx只读。
 * 由于子进程执行完该函数很快结束,因此就不一一释放内存了。
 */
static void query_path(char *from, char *to, int distance)
{
	ght_iterator_t it;
	void *key;
	UserNode *pun_from, *pun_to, *tmp;
	PathNode *pn;
	pLinkList S;
	Path *dist, *ptr;
	int nu, i;
	PathParam pp;
	FILE *fp;
	char tmpf[256], fpath[256];

	pun_from = ght_get(fidx, strlen(from), from);
	if(pun_from == NULL)
		return;
	pun_to = ght_get(fidx, strlen(to), to);
	if(to == NULL)
		return;
	sprintf(tmpf, FRIENDS_TMPDIR "/%s.%s.path.tmp", from ,to);
	fp = fopen(tmpf, "w+");
	if(fp == NULL)
		return;
	//节省起见, 如果不在一个圈子或者还没有划分,就不找了
	if(pun_from->div != pun_to->div || pun_from->div == 0) {
		fprintf(fp, "Query return null result.");
		goto END_WITH_RESULT;
	}
	
	if((S = init_queue()) == NULL) //待查找集
		goto END_WITH_ERROR;
	nu = 1;
	for(tmp = ght_first(fidx, &it, &key); tmp;
			tmp = ght_next(fidx, &it, &key)) {
		if(tmp->div == pun_from->div && tmp != pun_from) {
			pn = malloc(sizeof(PathNode));
			if(pn == NULL)
				goto END_WITH_ERROR;
			pn->pun = tmp;
			pn->midx = pn->pidx = 0;
			if(insert_node(S, pn) == -1)
				goto END_WITH_ERROR;
			nu++;
		}
	}
	if(nu > 1024) {
		fprintf(fp, "朋友圈过于庞大,查询被拒绝。\n");
		goto END_WITH_RESULT;
	}

	dist = malloc(sizeof(Path)*nu);
	if(dist == NULL)
		goto END_WITH_ERROR;

	pn = malloc(sizeof(PathNode));
	if(pn == NULL)
		goto END_WITH_ERROR;
	pn->pun = pun_from;	//put "from" in dist[0]
	pn->midx = pn->pidx = 0;
	dist[0].me = pn;
	dist[0].prev = pn;
	dist[0].len = 0;	//myself
	pp.dist = dist;
	pp.curr = 1;	//curr=0 is "from"
	pp.distance = distance; //limited distance
	pp.nu = nu;	//num friends
	pp.pn = pn;
	apply_queue(S, init_dist, &pp); //init dist array
	
	while(!is_empty(S)) {	//S是未计算完毕的集合
		pp.pn = NULL;
		pp.len = INFINITY;
		apply_queue(S, find_min, &pp); //找出距离值最小的,放在pp.pn
		if(pp.pn == NULL)
			goto END_WITH_ERROR;
		apply_queue(S, renew_dist, &pp); //用pp.pn更新距离值
		delete_node2(S, (void*)pp.pn, 0); //只从队列中删除,pp.pn不释放
	}
	
	//find "to"
	for(i = 0; i < nu; i++) {
		if(dist[i].me->pun == pun_to)
			break;
	}
	if(i == nu)
		goto END_WITH_ERROR;
	ptr = &dist[i];
	for(i = 0; ptr->me && ptr->me->pun != pun_from && i < nu; i++) {
		fprintf(fp, "%s <--%c", ptr->me->pun->userid, (i%6==5) ? '\n' : ' ');
		ptr = &dist[ptr->prev->midx];
	}
	fprintf(fp, "%s", dist[0].me->pun->userid);
END_WITH_RESULT:
	fclose(fp);
	sprintf(fpath, FRIENDS_TMPDIR "/%s.%s.path", from ,to);
	rename(tmpf, fpath);
	return;
END_WITH_ERROR:
	fclose(fp);
	unlink(tmpf);
	return;
}
/* Rehash the hash table (i.e. change its size and reinsert all
 * items). This operation is slow and should not be used frequently.
 */
void ght_rehash(ght_hash_table_t *p_ht, unsigned int i_size)
{
  ght_hash_table_t *p_tmp;
  ght_iterator_t iterator;
  const void *p_key;
  void *p;
  unsigned int i;

  DEBUG_ASSERT(p_ht);

  /* Recreate the hash table with the new size */
  p_tmp = ght_create(i_size);
  DEBUG_ASSERT(p_tmp);

  /* Set the flags for the new hash table */
  ght_set_hash(p_tmp, p_ht->fn_hash);
  ght_set_alloc(p_tmp, p_ht->fn_alloc, p_ht->fn_free);
  ght_set_heuristics(p_tmp, GHT_HEURISTICS_NONE);
  ght_set_rehash(p_tmp, FALSE);

  /* Walk through all elements in the table and insert them into the temporary one. */
  for (p = ght_first(p_ht, &iterator, &p_key); p; p = ght_next(p_ht, &iterator, &p_key))
    {
      DEBUG_ASSERT(iterator.p_entry);

      /* Insert the entry into the new table */
      if (ght_insert(p_tmp,
		     iterator.p_entry->p_data,
		     iterator.p_entry->key.i_size, iterator.p_entry->key.p_key) < 0)
	{
	  LOG_ERROR("Out of memory error or entry already in hash table when rehashing (internal error)\n");
	}
    }

  /* Remove the old table... */
  for (i=0; i<p_ht->i_size; i++)
    {
      if (p_ht->pp_entries[i])
	{
	  /* Delete the entries in the bucket */
	  free_entry_chain (p_ht, p_ht->pp_entries[i]);
	  p_ht->pp_entries[i] = NULL;
	}
    }

  DefaultFreeFunction (p_ht->pp_entries);
  DefaultFreeFunction (p_ht->p_nr);

  /* ... and replace it with the new */
  p_ht->i_size = p_tmp->i_size;
  p_ht->i_size_mask = p_tmp->i_size_mask;
  p_ht->i_items = p_tmp->i_items;
  p_ht->pp_entries = p_tmp->pp_entries;
  p_ht->p_nr = p_tmp->p_nr;

  p_ht->p_oldest = p_tmp->p_oldest;
  p_ht->p_newest = p_tmp->p_newest;

  /* Clean up */
  p_tmp->pp_entries = NULL;
  p_tmp->p_nr = NULL;
  DefaultFreeFunction (p_tmp);
}
Beispiel #15
0
int main(int argc,char *argv[])
{
    int
	rc;

    char
	*k,
	*v,
	*line,
	buf[BUFSIZ];

    ght_hash_table_t
	*p_table=NULL;

    /* create hash table of size 256 */
    p_table=ght_create(256);
    if (p_table == NULL)
    {
	print_it("Error: Could not create hash table\n");
	return(1);
    }

    print_menu();
    for (;;)
    {
	print_prompt();


	line=fgets(buf,sizeof(buf)-1,stdin);
	if (line == NULL)
	    break;

	switch(*line)
	{
	    case 'i':
	    {
		k=read_data("Enter key: ");
		v=read_data("Enter value: ");
		if (k && v)
		{
		    /* insert to hash table */
		    rc=ght_insert(p_table,
			    v,
			    strlen(k),
			    k);
		    if (rc == 0)
		    {
			print_it("Inserted: Key=%s, Value=%s\n",
				k,v);
			free(k);
		    }
		    else
		    {
			print_it("Error: ght_insert() failed\n");
			(void) free((char *) k);
			(void) free((char *) v);
		    }
		}
		else
		{
		    if (k)
			(void) free((char *) k);
		    if (v)
			(void) free((char *) v);

		}

		break;
	    }

	    case 'r': /* replace */
	    {
		k=read_data("Enter key: ");
		v=read_data("Enter new value: ");
		if (k && v)
		{
		    char *
			 old_val;
		    /* Replace a key in the hash table */
		    old_val = (char*)ght_replace(p_table,
						 v,
						 strlen(k),
						 k);
		    if (old_val != NULL)
		    {
			print_it("Replaced: Key=%s, Old Value=%s, Value=%s\n",
				k,old_val, v);
			free(old_val);
			free(k);
		    }
		    else
		    {
			print_it("Error: ght_replace() failed\n");
			(void) free((char *) k);
			(void) free((char *) v);
		    }
		}
		else
		{
		    if (k)
			(void) free((char *) k);
		    if (v)
			(void) free((char *) v);

		}
		break;
	    }

	    case 'h':
	    {
		print_menu();
		break;
	    }

	    case 'n': /* number of elements */
	    {
		print_it("Number elements in hash table: %d\n",
			ght_size(p_table));
		break;
	    }

	    case 's': /* size of hash table */
	    {
		print_it("Hash table size: %d\n",
			ght_table_size(p_table));

		break;
	    }

	    case 't': /* dump */
	    {
		const void
		    *pk,
		    *pv;

		ght_iterator_t
		    iterator;

		for (pv=(char *) ght_first(p_table,&iterator, &pk);
		     pv;
		     pv=(char *) ght_next(p_table,&iterator, &pk))
		{
		    print_it("%s => %s\n",(char *) pk,(char *) pv);
		}
		break;
	    }

	    case 'd':
	    {
		k=read_data("Enter key to delete: ");
		if (k)
		{
		    v=ght_remove(p_table,
			    strlen(k),
			    k);
		    if (v)
		    {
			print_it("Removed %s => %s",k,v);
			(void) free((char *) v);
		    }
		    else
		    {
			print_it("Error: could not find data for key %s\n",
				k);
		    }
		    (void) free((char *) k);
		}

		break;
	    }

	    case 'l':
	    {
		k=read_data("Enter key: ");
		if (k)
		{
		    v=(char *) ght_get(p_table,strlen(k),k);
		    if (v)
		    {
			print_it("Found: %s => %s\n",k,v);
		    }
		    else
		    {
			print_it("No data found for key: %s\n",k);
		    }
		    (void) free((char *) k);

		}
		break;
	    }

	    case 'q':
	    {
		if (p_table)
		{
		    const void
			*pv,
			*pk;

		    ght_iterator_t
			iterator;

		    for (pv=(char *) ght_first(p_table,&iterator, &pk);
			 pv;
			 pv=(char *) ght_next(p_table,&iterator, &pk))
		    {
			(void) free((char *) pv);
		    }
		    ght_finalize(p_table);
		}

		return(0);
		break;
	    }

	    default:
	    {
		print_it("Unknown option\n");
		break;
	    }
	}
    }
    return(0);
}
Beispiel #16
0
Datei: main.c Projekt: 8l/insieme
int main()  {
	struct CLElemQMHS4 cLElemQMHS4;

	double* VecSig = 0;
	double* VecStr = 0;
	double* VecDsig = 0;
	double* VecDstr = 0;
	double* InvF = 0;
	double* InvFT = 0;
	double* G = 0;
	double* P11q = 0;
	double* P22 = 0;
	double* P21 = 0;
	double* T3 = 0;
	double* Fe1q = 0;
	double* Fe2 = 0;
	double* Fs = 0;
	double* ppu = 0;
	double* lBase = 0;
	double* XG = 0;
	size_t* nrOfOrder = 0;
	ght_hash_table_t* icoShellSections = 0; //new std::map<const char*, icoSSData* >
	ght_hash_table_t* elsets = 0; //new std::map<const char*, int* >;
	ght_hash_table_t* issSizes = 0;
	ght_hash_table_t* elsetSizes = 0;
	ght_hash_table_t* matMap = 0;

	constructCLElemQMHS4(&cLElemQMHS4);

	// set up path to files
	char path[256];
	
#ifdef WIN32
	sprintf(path, "C:/Users/klois/uni/OpenCore/save/");
#else
	sprintf(path, "/home/klaus/dpsnfs/save/");
#endif
	char filenameAddition[128];
	sprintf(filenameAddition, "before-1-0.5-");

	printf("Reading input data from files\n");
	size_t elements = setAll(path, filenameAddition, &VecSig, &VecStr, &VecDsig, &VecDstr, &InvF, &InvFT, &G, &P11q, &P22, &P21, &T3, &Fe1q, &Fe2, &Fs,
			&ppu, &lBase, &XG, &nrOfOrder, &icoShellSections, &elsets, &issSizes, &elsetSizes, &matMap, &cLElemQMHS4);

	double* Kt = (double*)malloc(24 * 24 * elements * sizeof(double));
	double* fin = (double*)malloc(24 * elements * sizeof(double));
	int* elementsIntex = (int*)malloc(elements * sizeof(int));
	int* numDOFperIndex = (int*)malloc(elements * sizeof(int));

	printf("Calling initCalcQMHS4firstTime\n");
	if(initCalcQMHS4firstTime(&cLElemQMHS4, 1024, 1) != 0) {
		printf("initCalcQMHS4firstTime FAILED!\n");
		return -1;
	}

	printf("Calling calcQMHS4\n");
	if(calcQMHS4(&cLElemQMHS4, Kt, fin, elementsIntex, numDOFperIndex)) {
		printf("calcQMHS4 FAILED!\n");
		return -1;
	}

	printf("Checking result\n");
//	sprintf(path, "/home/klaus/dpsnfs/save/");
	sprintf(filenameAddition, "after-1-0.5-");
	if(checkResult(path, filenameAddition, VecSig, VecStr, VecDsig, VecDstr, InvF, InvFT, G, P11q, P22, P21, T3, Fe1q, Fe2, Fs, elements) == 0)
		printf("\tResult is correct\n");

	printf("Writing output files\n");
//	sprintf(path, "/home/klaus/dpsnfs/save/gc/");
	sprintf(filenameAddition, "gc/c-");
	saveAll(path, filenameAddition, VecSig, VecStr, VecDsig, VecDstr, InvF, InvFT, G, P11q, P22, P21, T3, Fe1q, Fe2, Fs, elements, 1088);

	destructCLElemQMHS4(&cLElemQMHS4);

	free( VecSig );
	free( VecStr );
	free( VecDsig );
	free( VecDstr );
	free( InvF );
	free( InvFT );
	free( G );
	free( P11q );
	free( P22 );
	free( P21 );
	free( T3 );
	free( Fe1q );
	free( Fe2 );
	free( Fs );
	free( ppu );
	free( lBase );
	free( XG );
	free( nrOfOrder );

	free(Kt);
	free(fin);
	free(elementsIntex);
	free(numDOFperIndex);

	// take care of correctly removing double pointers
	void* tmpPtr = 0;
	void* tmpKey = 0;
	ght_iterator_t iterator;
	for(tmpPtr = ght_first(elsets, &iterator, (const void**)&tmpKey); tmpPtr;
			tmpPtr = ght_next(elsets, &iterator, (const void**)&tmpKey)) {
		free(*(int**)tmpPtr);
		free(tmpPtr);
	}
	ght_finalize(icoShellSections);
	ght_finalize(elsets);
	ght_finalize(issSizes);
	ght_finalize(elsetSizes);
	ght_finalize(matMap);

	return 0;
}