2014-08-01 12:43:14 -07:00
|
|
|
#include "runtime/string_input.h"
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
const char *string;
|
|
|
|
|
size_t position;
|
|
|
|
|
size_t length;
|
|
|
|
|
} TSStringInput;
|
|
|
|
|
|
|
|
|
|
const char *ts_string_input_read(void *d, size_t *bytes_read) {
|
|
|
|
|
TSStringInput *data = (TSStringInput *)d;
|
|
|
|
|
if (data->position >= data->length) {
|
|
|
|
|
*bytes_read = 0;
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
size_t previous_position = data->position;
|
|
|
|
|
data->position = data->length;
|
|
|
|
|
*bytes_read = data->position - previous_position;
|
|
|
|
|
return data->string + previous_position;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
int ts_string_input_seek(void *d, TSLength position) {
|
2014-08-01 12:43:14 -07:00
|
|
|
TSStringInput *data = (TSStringInput *)d;
|
2014-09-26 16:15:07 -07:00
|
|
|
data->position = position.bytes;
|
|
|
|
|
return (position.bytes < data->length);
|
2014-08-01 12:43:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSInput ts_string_input_make(const char *string) {
|
|
|
|
|
TSStringInput *data = malloc(sizeof(TSStringInput));
|
|
|
|
|
data->string = string;
|
|
|
|
|
data->position = 0;
|
|
|
|
|
data->length = strlen(string);
|
2015-07-27 18:29:48 -07:00
|
|
|
return (TSInput){.data = (void *)data,
|
|
|
|
|
.read_fn = ts_string_input_read,
|
|
|
|
|
.seek_fn = ts_string_input_seek,
|
|
|
|
|
.release_fn = free };
|
2014-08-01 12:43:14 -07:00
|
|
|
}
|