Exemplo n.º 1
0
/*
 *	Load a file and return a database pointer.
 *	If file is NULL, create an empty database.
 */
DLLEXP struct pdb* pdb_load(char* file) {
	struct pdb* dbptr;
	struct pdb_node_types_t* rntptr;
	int line = 1;
	FILE* fptr = NULL;
	
	dbptr = (struct pdb*)malloc(sizeof(struct pdb));
	if (!dbptr)
		return NULL;
	
	if (file) {
		fptr = pdb_open_file(file);
		if (!fptr)
			return NULL;
	}
	
	/*
	 *	Initialize pdb structure.
	 */
	dbptr->altered = 0;
	dbptr->last_write = time(NULL);
	dbptr->write_interval = PDB_DEFAULT_WRITE_INTERVAL;
	dbptr->settings = PDB_DEFAULT_SETTINGS;
	#ifdef PDB_USING_THREADS
		dbptr->mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
		pthread_mutex_init(dbptr->mutex, NULL);
	#endif
	
	if (file) {
		dbptr->file = (char*)malloc(sizeof(char) * (strlen(file) + 1));
		strcpy(dbptr->file, file);
	} else
		dbptr->file = NULL;
	
	rntptr = pdb_get_type_info(ROOT_NODE_TYPE);
	if (!rntptr) {
		ERROR("Root node type is invalid.");
		free(dbptr->file);
		free(dbptr);
		pdb_close_file(fptr);
		return NULL;
	}		
	dbptr->data = (*rntptr->create_cb)(NULL, NULL, NULL);
	
	/*
	 *	Load database (if file was specified).
	 */
	if (file) {
		pdb_standard_load_node(fptr, dbptr->data, NULL, &line);
		pdb_close_file(fptr);
	}
	
	return dbptr;
}
Exemplo n.º 2
0
/*
 *	Callback for loading a hash node.
 */
int pdb_load_hash_node_cb(FILE* fptr, struct pdb_node_t* pptr, char** tok_arr,
	int* line) {

	char b;
	int i;
	struct pdb_node_types_t* tiptr = pdb_get_type_info(HASH_NODE_TYPE);
		
	/*
	 *	Move to the next non-white character.
	 */
	while (!feof(fptr)) {
		FPEEK(&b, fptr);
		if (!is_whitespace(b))
			break;
		if (b == '\n')
			*line++;
		fgetc(fptr);
	}
	/*
	 *	Skip over the opening token.
	 */
	for (i = 0; i < (signed)strlen(tiptr->open_token); ++i)
		fgetc(fptr);
	
	/*
	 *	Get the size of the hash table.
	 */
	if (!fscanf(fptr, "%i;", &i)) {
		char* e = va(NULL,
			"Unable to load hash from file -- no size (line %i).\n", *line);
		ERROR(e);
		free(e);
		return 0;
	}
	
	pptr->data = (void*)hash_create(i);

	return pdb_standard_load_node(fptr, pptr, tok_arr, line);
}
Exemplo n.º 3
0
/*
 *	Callback for loading a list node.
 */
int pdb_load_list_node_cb(FILE* fptr, struct pdb_node_t* pptr, char** tok_arr,
	int* line) {

	return pdb_standard_load_node(fptr, pptr, tok_arr, line);
}