Class: MultiCompress::Dictionary
- Inherits:
-
Object
- Object
- MultiCompress::Dictionary
- Defined in:
- ext/multi_compress/multi_compress.c
Class Method Summary collapse
Instance Method Summary collapse
Constructor Details
#initialize(*args) ⇒ Object
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 |
# File 'ext/multi_compress/multi_compress.c', line 1953
static VALUE dict_initialize(int argc, VALUE *argv, VALUE self) {
VALUE raw, opts;
rb_scan_args(argc, argv, "1:", &raw, &opts);
StringValue(raw);
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
VALUE algo_sym = Qnil;
if (!NIL_P(opts)) {
algo_sym = rb_hash_aref(opts, ID2SYM(rb_intern("algo")));
}
d->algo = NIL_P(algo_sym) ? ALGO_ZSTD : sym_to_algo(algo_sym);
if (d->algo == ALGO_LZ4)
rb_raise(eUnsupportedError, "LZ4 does not support dictionaries");
d->size = RSTRING_LEN(raw);
d->data = ALLOC_N(uint8_t, d->size);
memcpy(d->data, RSTRING_PTR(raw), d->size);
return self;
}
|
Class Method Details
.load(*args) ⇒ Object
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 |
# File 'ext/multi_compress/multi_compress.c', line 2061
static VALUE dict_load(int argc, VALUE *argv, VALUE self) {
VALUE path, opts;
rb_scan_args(argc, argv, "1:", &path, &opts);
StringValue(path);
VALUE algo_sym = Qnil;
if (!NIL_P(opts)) {
algo_sym = rb_hash_aref(opts, ID2SYM(rb_intern("algo")));
}
compress_algo_t algo = NIL_P(algo_sym) ? ALGO_ZSTD : sym_to_algo(algo_sym);
if (algo == ALGO_LZ4)
rb_raise(eUnsupportedError, "LZ4 does not support dictionaries");
const char *cpath = RSTRING_PTR(path);
FILE *f = fopen(cpath, "rb");
if (!f)
rb_sys_fail(cpath);
fseek(f, 0, SEEK_END);
long file_size = ftell(f);
fseek(f, 0, SEEK_SET);
if (file_size <= 0) {
fclose(f);
rb_raise(eDataError, "dictionary file is empty: %s", cpath);
}
uint8_t *buf = ALLOC_N(uint8_t, file_size);
size_t read_bytes = fread(buf, 1, file_size, f);
fclose(f);
if ((long)read_bytes != file_size) {
xfree(buf);
rb_raise(eDataError, "failed to read dictionary: %s", cpath);
}
VALUE dict_obj = rb_obj_alloc(cDictionary);
dictionary_t *d;
TypedData_Get_Struct(dict_obj, dictionary_t, &dictionary_type, d);
d->algo = algo;
d->data = buf;
d->size = (size_t)file_size;
return dict_obj;
}
|
Instance Method Details
#algo ⇒ Object
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 |
# File 'ext/multi_compress/multi_compress.c', line 2124
static VALUE dict_algo(VALUE self) {
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
switch (d->algo) {
case ALGO_ZSTD:
return ID2SYM(rb_intern("zstd"));
case ALGO_LZ4:
return ID2SYM(rb_intern("lz4"));
case ALGO_BROTLI:
return ID2SYM(rb_intern("brotli"));
}
return Qnil;
}
|
#save(path) ⇒ Object
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 |
# File 'ext/multi_compress/multi_compress.c', line 2107
static VALUE dict_save(VALUE self, VALUE path) {
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
const char *cpath = StringValueCStr(path);
FILE *f = fopen(cpath, "wb");
if (!f)
rb_sys_fail(cpath);
size_t written = fwrite(d->data, 1, d->size, f);
fclose(f);
if (written != d->size)
rb_raise(eError, "failed to write dictionary to %s", cpath);
return path;
}
|
#size ⇒ Object
2138 2139 2140 2141 2142 |
# File 'ext/multi_compress/multi_compress.c', line 2138
static VALUE dict_size(VALUE self) {
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
return SIZET2NUM(d->size);
}
|