Пример #1
0
static address_expansion_value_t *address_expansion_value_read(FILE *f) {
    if (f == NULL) return NULL;

    address_expansion_value_t *value = address_expansion_value_new();

    if (!file_read_uint32(f, &value->components)) {
        goto exit_expansion_value_created;
    }

    uint32_t num_expansions;

    if (!file_read_uint32(f, &num_expansions)) {
        goto exit_expansion_value_created;
    }

    address_expansion_t expansion;

    for (size_t i = 0; i < num_expansions; i++) {
        if (!address_expansion_read(f, &expansion)) {
            goto exit_expansion_value_created;
        }
        address_expansion_array_push(value->expansions, expansion);
    }

    return value;

exit_expansion_value_created:
    address_expansion_value_destroy(value);
    return NULL;
}
Пример #2
0
graph_t *graph_read(FILE *f) {
    graph_t *g = malloc(sizeof(graph_t));
    if (g == NULL) return NULL;

    g->indptr = NULL;
    g->indices = NULL;

    if (!file_read_uint32(f, &g->m) ||
        !file_read_uint32(f, &g->n) ||
        !file_read_uint8(f, (uint8_t *)&g->fixed_rows)) {
        goto exit_graph_allocated;
    }

    uint64_t len_indptr;

    if (!file_read_uint64(f, &len_indptr)) {
        goto exit_graph_allocated;
    }

    uint32_array *indptr = uint32_array_new_size(len_indptr);
    if (indptr == NULL) {
        goto exit_graph_allocated;
    }

    for (int i = 0; i < len_indptr; i++) {
        if (!file_read_uint32(f, indptr->a + i)) {
            goto exit_graph_allocated;
        }
    }
    indptr->n = (size_t)len_indptr;

    g->indptr = indptr;

    uint64_t len_indices;

    if (!file_read_uint64(f, &len_indices)) {
        goto exit_graph_allocated;
    }

    uint32_array *indices = uint32_array_new_size(len_indices);
    if (indices == NULL) {
        goto exit_graph_allocated;
    }

    for (int i = 0; i < len_indices; i++) {
        if (!file_read_uint32(f, indices->a + i)) {
            goto exit_graph_allocated;
        }
    }

    indices->n = (size_t)len_indices;

    g->indices = indices;
    
    return g;

exit_graph_allocated:
    graph_destroy(g);
    return NULL;
}
Пример #3
0
static bool address_expansion_read(FILE *f, address_expansion_t *expansion) {
    if (f == NULL) return false;


    if (!file_read_uint32(f, (uint32_t *)&expansion->canonical_index)) {
        return false;
    }

    uint32_t language_len;

    if (!file_read_uint32(f, &language_len)) {
        return false;
    }

    if (!file_read_chars(f, expansion->language, language_len)) {
        return false;
    }

    if (!file_read_uint32(f, (uint32_t *)&expansion->num_dictionaries)) {
        return false;
    }

    for (size_t i = 0; i < expansion->num_dictionaries; i++) {
        if (!file_read_uint16(f, (uint16_t *)expansion->dictionary_ids + i)) {
            return false;
        }
    }

    if (!file_read_uint32(f, &expansion->address_components)) {
        return false;
    }

    if (!file_read_uint8(f, (uint8_t *)&expansion->separable)) {
        return false;
    }

    return true;
}
Пример #4
0
RawSound* wav_load(const char* filename) {
	assert(filename);

	FileHandle wav_file = file_open(filename);

	uint chunk_id = file_read_uint32(wav_file);
	if(chunk_id != ('R'+('I'<<8)+('F'<<16)+('F'<<24)))
		LOG_ERROR("Bad file type");
	file_read_uint32(wav_file);
	uint wave_id = file_read_uint32(wav_file);
	if(wave_id != ('W'+('A'<<8)+('V'<<16)+('E'<<24)))
		LOG_ERROR("Bad wave id");

	// Format chunk
	uint format_id = file_read_uint32(wav_file);
	if(format_id != ('f'+('m'<<8)+('t'<<16)+(' '<<24)))
		LOG_ERROR("Not format chunk");
	uint format_length __attribute__ ((unused)) = file_read_uint32(wav_file);
	assert(format_length == 0x10);
	format_length = 0; // To prevent unused warning
	uint16 format_code __attribute__((unused)) = file_read_uint16(wav_file);
	assert(format_code == 0x01);
	format_code = 0; // To prevent unused warning
	uint16 n_channels = file_read_uint16(wav_file);
	assert(n_channels == 1 || n_channels == 2);
	uint32 sample_rate = file_read_uint32(wav_file);
	file_read_uint32(wav_file);
	file_read_uint16(wav_file);
	uint16 bits_per_sample = file_read_uint16(wav_file);
	assert(bits_per_sample == 8 || bits_per_sample == 16);

	// Data chunk
	// https://projects.developer.nokia.com/svn/matchempoker/tags/v1.1.0/qt_build/src/GEAudioBuffer.cpp
	uint32 data_id = 1635017060;
    uint32 current_field = file_read_uint32(wav_file);
    while(current_field != data_id) // Skip till "data" is found
        current_field = file_read_uint32(wav_file);

    uint data_size = file_read_uint32(wav_file);

	if(data_size < 1)
        LOG_ERROR("Data id is not \'data\'");
	void* data = MEM_ALLOC(data_size);
	file_read(wav_file, data, data_size);

	// Fill sound struct
	RawSound* result = (RawSound*)MEM_ALLOC(sizeof(RawSound));
	result->frequency = sample_rate;
	result->bits = bits_per_sample;
	result->channels = n_channels;
	result->size = data_size;
	result->data = data;

	// If 16 bits per sample and we're on big-endian cpu, swap bytes
	uint16 endian_test = 0x0011;
	if(bits_per_sample == 16 && endian_test != endian_swap2(endian_test)) {
		size_t i; for(i = 0; i < data_size; i += 2) {
			uint16* sample = (uint16*)(result->data + i);
			*sample = endian_swap2(*sample);
		}
	}

	return result;
}
Пример #5
0
NavMesh ai_load_navmesh(FileHandle file) {
	assert(file);

	NavMesh r;

	r.n_nodes = file_read_uint32(file);
	assert(r.n_nodes > 3 && r.n_nodes < 1024);

	size_t n = r.n_nodes;

	r.navpoints = (Vector2*)MEM_ALLOC(sizeof(Vector2)*n);
	r.neighbour_count = (uint*)MEM_ALLOC(sizeof(uint)*n);
	r.neighbours_start = (uint*)MEM_ALLOC(sizeof(uint)*n);
	r.distance = (float*)MEM_ALLOC(sizeof(float)*n*n);
	r.radius = (float*)MEM_ALLOC(sizeof(float)*n);
	r.nn_grid_start = (uint*)MEM_ALLOC(sizeof(uint)*nn_grid_cells);
	r.nn_grid_count = (uint*)MEM_ALLOC(sizeof(uint)*nn_grid_cells);

	for(uint i = 0; i < n; ++i) {
		r.navpoints[i].x = file_read_float(file);
		r.navpoints[i].y = file_read_float(file);
	}

	uint n_neighbours = 0;
	for(uint i = 0; i < n; ++i) {
		r.neighbour_count[i] = file_read_uint32(file);
		n_neighbours += r.neighbour_count[i];
	}

	r.neighbours_start[0] = 0;
	for(uint i = 1; i < n; ++i)
		r.neighbours_start[i] = 
			r.neighbours_start[i-1] + r.neighbour_count[i-1];

	r.neighbours = MEM_ALLOC(sizeof(uint) * n_neighbours);
	for(uint i = 0; i < n_neighbours; ++i)
		r.neighbours[i] = file_read_uint32(file);

	for(uint i = 0; i < n*n; ++i)
		r.distance[i] = file_read_float(file);

	for(uint i = 0; i < n; ++i)
		r.radius[i] = file_read_float(file);

	uint n_nn_grid_nodes = 0;
	for(uint i = 0; i <	nn_grid_cells; ++i) {
		r.nn_grid_count[i] = file_read_uint32(file);
		n_nn_grid_nodes += r.nn_grid_count[i];
	}

	r.nn_grid = MEM_ALLOC(sizeof(uint) * n_nn_grid_nodes);
	r.nn_grid_start[0] = 0;
	for(uint i = 1; i < nn_grid_cells; ++i)
		r.nn_grid_start[i] = r.nn_grid_start[i-1] + r.nn_grid_count[i-1];

	for(uint i = 0; i < n_nn_grid_nodes; ++i)
		r.nn_grid[i] = file_read_uint32(file);

	r.vis_bitmap = MEM_ALLOC(sizeof(uint) * vis_bitmap_size); 
	for(uint i = 0; i < vis_bitmap_size; ++i)
		r.vis_bitmap[i] = file_read_uint32(file);

	return r;	
}
Пример #6
0
bool address_dictionary_read(FILE *f) {
    if (address_dict != NULL) return false;

    uint32_t signature;

    if (!file_read_uint32(f, &signature) || signature != ADDRESS_DICTIONARY_SIGNATURE) {
        return false;
    }

    address_dict = malloc(sizeof(address_dictionary_t));
    if (address_dict == NULL) return false;

    uint32_t canonical_str_len;

    if (!file_read_uint32(f, &canonical_str_len)) {
        goto exit_address_dict_created;
    }

    char_array *array = char_array_new_size(canonical_str_len);

    if (array == NULL) {
        goto exit_address_dict_created;
    }

    if (!file_read_chars(f, array->a, canonical_str_len)) {
        char_array_destroy(array);
        goto exit_address_dict_created;
    }

    array->n = canonical_str_len;

    address_dict->canonical = cstring_array_from_char_array(array);

    uint32_t num_values;

    if (!file_read_uint32(f, &num_values)) {
        goto exit_address_dict_created;
    }

    address_dict->values = address_expansion_value_array_new_size(num_values);

    for (uint32_t i = 0; i < num_values; i++) {
        address_expansion_value_t *value = address_expansion_value_read(f);
        if (value == NULL) {
            goto exit_address_dict_created;
        }
        address_expansion_value_array_push(address_dict->values, value);
    }

    address_dict->trie = trie_read(f);

    if (address_dict->trie == NULL) {
        goto exit_address_dict_created;
    }

    return true;

exit_address_dict_created:
    address_dictionary_destroy(address_dict);
    return false;
}