Beispiel #1
0
wi_integer_t wi_file_write_bytes(wi_file_t *file, const void *buffer, wi_uinteger_t length) {
    wi_uinteger_t   offset;
    wi_integer_t    bytes;
    
    _WI_FILE_ASSERT_OPEN(file);
    
    offset = 0;
    
    while(offset < length) {
        bytes = write(file->fd, buffer + offset, length - offset);
        
        if(bytes > 0) {
            offset += bytes;
            file->offset += bytes;
        } else {
            if(bytes == 0) {
                return offset;
            } else {
                if(errno == EINTR) {
                    continue;
                } else {
                    wi_error_set_errno(errno);
                    
                    return -1;
                }
            }
        }
    }
    
    return offset;
}
Beispiel #2
0
wi_string_t * wi_file_read_to_string(wi_file_t *file, wi_string_t *separator) {
	wi_string_t		*string, *totalstring = NULL;
	wi_uinteger_t	index, length;
	
	_WI_FILE_ASSERT_OPEN(file);
	
	while((string = wi_file_read(file, WI_FILE_BUFFER_SIZE))) {
		if(!totalstring)
			totalstring = wi_string_init(wi_string_alloc());
		
		index = wi_string_index_of_string(string, separator, 0);
		
		if(index == WI_NOT_FOUND) {
			wi_string_append_string(totalstring, string);
		} else {
			length = wi_string_length(string);
			
			wi_string_delete_characters_from_index(string, index);
			wi_string_append_string(totalstring, string);

			wi_file_seek(file, wi_file_offset(file) - length + index + 1);
			
			break;
		}
	}
	
	return wi_autorelease(totalstring);
}
Beispiel #3
0
wi_data_t * wi_file_read_to_end_of_file(wi_file_t *file) {
    wi_mutable_data_t   *data;
    char                buffer[WI_FILE_BUFFER_SIZE];
    wi_integer_t        bytes;
    
    _WI_FILE_ASSERT_OPEN(file);
    
    data = wi_data_init_with_capacity(wi_mutable_data_alloc(), WI_FILE_BUFFER_SIZE);
    
    while(true) {
        bytes = wi_file_read_bytes(file, buffer, sizeof(buffer));
        
        if(bytes <= 0)
            break;

        wi_mutable_data_append_bytes(data, buffer, bytes);
    }

    if(bytes < 0) {
        wi_release(data);
        
        data = NULL;
    }
    
    wi_runtime_make_immutable(data);
    
    return wi_autorelease(data);
}
Beispiel #4
0
wi_data_t * wi_file_read(wi_file_t *file, wi_uinteger_t length) {
    wi_mutable_data_t   *data;
    char                buffer[WI_FILE_BUFFER_SIZE];
    wi_integer_t        bytes = -1;
    
    _WI_FILE_ASSERT_OPEN(file);
    
    data = wi_data_init_with_capacity(wi_mutable_data_alloc(), length);
    
    while(length > 0) {
        bytes = wi_file_read_bytes(file, buffer, WI_MIN(sizeof(buffer), length));
        
        if(bytes <= 0)
            break;
        
        wi_mutable_data_append_bytes(data, buffer, bytes);
        
        length -= bytes;
    }
    
    if(bytes < 0) {
        wi_release(data);
        
        data = NULL;
    }
    
    wi_runtime_make_immutable(data);

    return wi_autorelease(data);
}
Beispiel #5
0
void wi_file_seek(wi_file_t *file, wi_file_offset_t offset) {
	off_t		r;
	
	_WI_FILE_ASSERT_OPEN(file);
	
	r = lseek(file->fd, (off_t) offset, SEEK_SET);
	
	if(r >= 0)
		file->offset = r;
}
Beispiel #6
0
wi_boolean_t wi_file_truncate(wi_file_t *file, wi_file_offset_t offset) {
	_WI_FILE_ASSERT_OPEN(file);
	
	if(ftruncate(file->fd, offset) < 0) {
		wi_error_set_errno(errno);
		
		return false;
	}
	
	return true;
}
Beispiel #7
0
wi_file_offset_t wi_file_seek_to_end_of_file(wi_file_t *file) {
	off_t		r;
	
	_WI_FILE_ASSERT_OPEN(file);
	
	r = lseek(file->fd, 0, SEEK_END);
	
	if(r >= 0)
		file->offset = r;

	return file->offset;
}
Beispiel #8
0
int32_t wi_file_write(wi_file_t *file, wi_string_t *fmt, ...) {
	wi_string_t		*string;
	int				bytes;
	va_list			ap;
	
	_WI_FILE_ASSERT_OPEN(file);
	
	va_start(ap, fmt);
	string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap);
	va_end(ap);
	
	bytes = wi_file_write_buffer(file, wi_string_cstring(string), wi_string_length(string));
	
	wi_release(string);
	
	return bytes;
}
Beispiel #9
0
wi_string_t * wi_file_read(wi_file_t *file, wi_uinteger_t length) {
	wi_mutable_string_t		*string;
	char					buffer[WI_FILE_BUFFER_SIZE];
	wi_integer_t			bytes = -1;
	
	_WI_FILE_ASSERT_OPEN(file);
	
	string = wi_string_init_with_capacity(wi_mutable_string_alloc(), length);
	
	while(length > sizeof(buffer)) {
		bytes = wi_file_read_buffer(file, buffer, sizeof(buffer));
		
		if(bytes <= 0)
			goto end;
		
		wi_mutable_string_append_bytes(string, buffer, bytes);
		
		length -= bytes;
	}
	
	if(length > 0) {
		bytes = wi_file_read_buffer(file, buffer, sizeof(buffer));
		
		if(bytes <= 0)
			goto end;
		
		wi_mutable_string_append_bytes(string, buffer, bytes);
	}
	
end:
	if(bytes <= 0) {
		wi_release(string);
		
		string = NULL;
	}
	
	wi_runtime_make_immutable(string);

	return wi_autorelease(string);
}
Beispiel #10
0
wi_string_t * wi_file_read_to_string(wi_file_t *file, wi_string_t *separator) {
	wi_string_t		*string;
	uint32_t		index, length;
	
	_WI_FILE_ASSERT_OPEN(file);

	while((string = wi_file_read(file, WI_FILE_BUFFER_SIZE))) {
		length = wi_string_length(string);
		index = wi_string_index_of_string(string, separator, 0);
		
		if(index == WI_NOT_FOUND) {
			if(length < WI_FILE_BUFFER_SIZE)
				return string;
		} else {
			wi_string_delete_characters_from_index(string, index);
			
			wi_file_seek(file, wi_file_offset(file) - length + index + 1);
			
			return string;
		}
	}
	
	return NULL;
}
Beispiel #11
0
wi_file_offset_t wi_file_seek_to_end_of_file(wi_file_t *file) {
	_WI_FILE_ASSERT_OPEN(file);
	
	return (wi_file_offset_t) lseek(file->fd, 0, SEEK_END);
}
Beispiel #12
0
void wi_file_seek(wi_file_t *file, wi_file_offset_t offset) {
	_WI_FILE_ASSERT_OPEN(file);
	
	lseek(file->fd, (off_t) offset, SEEK_SET);
}