Skip to content

Commit

Permalink
ECC-1820: Local static functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramn committed May 8, 2024
1 parent 915935d commit d397f58
Show file tree
Hide file tree
Showing 31 changed files with 773 additions and 535 deletions.
63 changes: 39 additions & 24 deletions src/accessor/grib_accessor_class_ascii.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,73 @@

#include "grib_accessor_class_ascii.h"

grib_accessor_class_ascii_t _grib_accessor_class_ascii{"ascii"};
grib_accessor_class_ascii_t _grib_accessor_class_ascii{ "ascii" };
grib_accessor_class* grib_accessor_class_ascii = &_grib_accessor_class_ascii;


void grib_accessor_class_ascii_t::init(grib_accessor* a, const long len, grib_arguments* arg){
void grib_accessor_class_ascii_t::init(grib_accessor* a, const long len, grib_arguments* arg)
{
grib_accessor_class_gen_t::init(a, len, arg);
a->length = len;
Assert(a->length >= 0);
}

int grib_accessor_class_ascii_t::value_count(grib_accessor* a, long* count){
int grib_accessor_class_ascii_t::value_count(grib_accessor* a, long* count)
{
*count = 1;
return 0;
}

size_t grib_accessor_class_ascii_t::string_length(grib_accessor* a){
size_t grib_accessor_class_ascii_t::string_length(grib_accessor* a)
{
return a->length;
}

void grib_accessor_class_ascii_t::dump(grib_accessor* a, grib_dumper* dumper){
void grib_accessor_class_ascii_t::dump(grib_accessor* a, grib_dumper* dumper)
{
grib_dump_string(dumper, a, NULL);
}

int grib_accessor_class_ascii_t::get_native_type(grib_accessor* a){
int grib_accessor_class_ascii_t::get_native_type(grib_accessor* a)
{
return GRIB_TYPE_STRING;
}

int grib_accessor_class_ascii_t::unpack_string(grib_accessor* a, char* val, size_t* len){
size_t i = 0;
int grib_accessor_class_ascii_t::unpack_string(grib_accessor* a, char* val, size_t* len)
{
grib_handle* hand = grib_handle_of_accessor(a);
const size_t alen = a->length;

if (*len < (alen + 1)) {
const char* cclass_name = a->cclass->name;
grib_context_log(a->context, GRIB_LOG_ERROR,
"%s: Buffer too small for %s. It is %zu bytes long (len=%zu)",
cclass_name, a->name, alen+1, *len);
cclass_name, a->name, alen + 1, *len);
*len = alen + 1;
return GRIB_BUFFER_TOO_SMALL;
}

size_t i = 0;
for (i = 0; i < alen; i++)
val[i] = hand->buffer->data[a->offset + i];
val[i] = 0;
*len = i;
*len = i;
return GRIB_SUCCESS;
}

int grib_accessor_class_ascii_t::pack_string(grib_accessor* a, const char* val, size_t* len){
size_t i = 0;
int grib_accessor_class_ascii_t::pack_string(grib_accessor* a, const char* val, size_t* len)
{
grib_handle* hand = grib_handle_of_accessor(a);
const size_t alen = a->length;
if (len[0] > (alen + 1)) {
grib_context_log(a->context, GRIB_LOG_ERROR,
"pack_string: Wrong size (%zu) for %s, it contains %ld values", len[0], a->name, a->length + 1);
"pack_string: Wrong size (%zu) for %s, it contains %ld values",
len[0], a->name, a->length + 1);
len[0] = 0;
return GRIB_BUFFER_TOO_SMALL;
}

for (i = 0; i < alen; i++) {
for (size_t i = 0; i < alen; i++) {
if (i < len[0])
hand->buffer->data[a->offset + i] = val[i];
else
Expand All @@ -80,17 +87,20 @@ int grib_accessor_class_ascii_t::pack_string(grib_accessor* a, const char* val,
return GRIB_SUCCESS;
}

int grib_accessor_class_ascii_t::pack_long(grib_accessor* a, const long* v, size_t* len){
int grib_accessor_class_ascii_t::pack_long(grib_accessor* a, const long* v, size_t* len)
{
grib_context_log(a->context, GRIB_LOG_ERROR, "Should not pack %s as long (It's a string)", a->name);
return GRIB_NOT_IMPLEMENTED;
}

int grib_accessor_class_ascii_t::pack_double(grib_accessor* a, const double* v, size_t* len){
int grib_accessor_class_ascii_t::pack_double(grib_accessor* a, const double* v, size_t* len)
{
grib_context_log(a->context, GRIB_LOG_ERROR, "Should not pack %s as double (It's a string)", a->name);
return GRIB_NOT_IMPLEMENTED;
}

int grib_accessor_class_ascii_t::unpack_long(grib_accessor* a, long* v, size_t* len){
int grib_accessor_class_ascii_t::unpack_long(grib_accessor* a, long* v, size_t* len)
{
char val[1024] = {0,};
size_t l = sizeof(val);
size_t i = 0;
Expand All @@ -116,12 +126,14 @@ int grib_accessor_class_ascii_t::unpack_long(grib_accessor* a, long* v, size_t*
return GRIB_SUCCESS;
}

int grib_accessor_class_ascii_t::unpack_double(grib_accessor* a, double* v, size_t* len){
int grib_accessor_class_ascii_t::unpack_double(grib_accessor* a, double* v, size_t* len)
{
char val[1024];
size_t l = sizeof(val);
char* last = NULL;

int err = a->unpack_string(val, &l); if (err) return err;
int err = a->unpack_string(val, &l);
if (err) return err;

*v = strtod(val, &last);

Expand All @@ -135,23 +147,26 @@ int grib_accessor_class_ascii_t::unpack_double(grib_accessor* a, double* v, size
return GRIB_NOT_IMPLEMENTED;
}

int grib_accessor_class_ascii_t::compare(grib_accessor* a, grib_accessor* b){
int grib_accessor_class_ascii_t::compare(grib_accessor* a, grib_accessor* b)
{
int retval = 0;
char* aval = 0;
char* bval = 0;
int err = 0;

size_t alen = a->length+1;
size_t blen = b->length+1;
size_t alen = a->length + 1;
size_t blen = b->length + 1;

if (alen != blen)
return GRIB_COUNT_MISMATCH;

aval = (char*)grib_context_malloc(a->context, alen * sizeof(char));
bval = (char*)grib_context_malloc(b->context, blen * sizeof(char));

err = a->unpack_string(aval, &alen); if (err) return err;
err = b->unpack_string(bval, &blen); if (err) return err;
err = a->unpack_string(aval, &alen);
if (err) return err;
err = b->unpack_string(bval, &blen);
if (err) return err;

retval = GRIB_SUCCESS;
if (!STR_EQUAL(aval, bval))
Expand Down
58 changes: 36 additions & 22 deletions src/accessor/grib_accessor_class_bitmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@

#include "grib_accessor_class_bitmap.h"

grib_accessor_class_bitmap_t _grib_accessor_class_bitmap{"bitmap"};
grib_accessor_class_bitmap_t _grib_accessor_class_bitmap{ "bitmap" };
grib_accessor_class* grib_accessor_class_bitmap = &_grib_accessor_class_bitmap;



void compute_size(grib_accessor* a){
static void compute_size(grib_accessor* a)
{
long slen = 0;
long off = 0;
grib_handle* hand = grib_handle_of_accessor(a);
Expand Down Expand Up @@ -51,7 +50,8 @@ void compute_size(grib_accessor* a){
Assert(a->length >= 0);
}

void grib_accessor_class_bitmap_t::init(grib_accessor* a, const long len, grib_arguments* arg){
void grib_accessor_class_bitmap_t::init(grib_accessor* a, const long len, grib_arguments* arg)
{
grib_accessor_class_bytes_t::init(a, len, arg);
grib_accessor_bitmap_t* self = (grib_accessor_bitmap_t*)a;
grib_handle* hand = grib_handle_of_accessor(a);
Expand All @@ -65,24 +65,29 @@ void grib_accessor_class_bitmap_t::init(grib_accessor* a, const long len, grib_a
compute_size(a);
}

long grib_accessor_class_bitmap_t::next_offset(grib_accessor* a){
return a->byte_offset() + a->byte_count();}
long grib_accessor_class_bitmap_t::next_offset(grib_accessor* a)
{
return a->byte_offset() + a->byte_count();
}

void grib_accessor_class_bitmap_t::dump(grib_accessor* a, grib_dumper* dumper){
void grib_accessor_class_bitmap_t::dump(grib_accessor* a, grib_dumper* dumper)
{
long len = 0;
char label[1024];

a->value_count(&len);
snprintf(label, 1024, "Bitmap of %ld values", len);
snprintf(label, sizeof(label), "Bitmap of %ld values", len);
grib_dump_bytes(dumper, a, label);
}

int grib_accessor_class_bitmap_t::unpack_long(grib_accessor* a, long* val, size_t* len){
int grib_accessor_class_bitmap_t::unpack_long(grib_accessor* a, long* val, size_t* len)
{
long pos = a->offset * 8;
long tlen = 0;
const grib_handle* hand = grib_handle_of_accessor(a);

int err = a->value_count(&tlen); if (err)
int err = a->value_count(&tlen);
if (err)
return err;

if (*len < tlen) {
Expand All @@ -99,13 +104,15 @@ int grib_accessor_class_bitmap_t::unpack_long(grib_accessor* a, long* val, size_
}

template <typename T>
int unpack(grib_accessor* a, T* val, size_t* len){
int unpack(grib_accessor* a, T* val, size_t* len)
{
static_assert(std::is_floating_point<T>::value, "Requires floating points numbers");
long pos = a->offset * 8;
long tlen;
grib_handle* hand = grib_handle_of_accessor(a);

int err = a->value_count(&tlen); if (err)
int err = a->value_count(&tlen);
if (err)
return err;

if (*len < tlen) {
Expand All @@ -121,40 +128,47 @@ int unpack(grib_accessor* a, T* val, size_t* len){
return GRIB_SUCCESS;
}

int grib_accessor_class_bitmap_t::unpack_double(grib_accessor* a, double* val, size_t* len){
int grib_accessor_class_bitmap_t::unpack_double(grib_accessor* a, double* val, size_t* len)
{
return unpack<double>(a, val, len);
}

int grib_accessor_class_bitmap_t::unpack_float(grib_accessor* a, float* val, size_t* len){
int grib_accessor_class_bitmap_t::unpack_float(grib_accessor* a, float* val, size_t* len)
{
return unpack<float>(a, val, len);
}

int grib_accessor_class_bitmap_t::unpack_double_element(grib_accessor* a, size_t idx, double* val){
int grib_accessor_class_bitmap_t::unpack_double_element(grib_accessor* a, size_t idx, double* val)
{
long pos = a->offset * 8;

pos += idx;
*val = (double)grib_decode_unsigned_long(grib_handle_of_accessor(a)->buffer->data, &pos, 1);

return GRIB_SUCCESS;
}
int grib_accessor_class_bitmap_t::unpack_double_element_set(grib_accessor* a, const size_t* index_array, size_t len, double* val_array){
for (size_t i=0; i<len; ++i) {
int grib_accessor_class_bitmap_t::unpack_double_element_set(grib_accessor* a, const size_t* index_array, size_t len, double* val_array)
{
for (size_t i = 0; i < len; ++i) {
unpack_double_element(a, index_array[i], val_array + i);
}
return GRIB_SUCCESS;
}

void grib_accessor_class_bitmap_t::update_size(grib_accessor* a, size_t s){
void grib_accessor_class_bitmap_t::update_size(grib_accessor* a, size_t s)
{
a->length = s;
}

size_t grib_accessor_class_bitmap_t::string_length(grib_accessor* a){
size_t grib_accessor_class_bitmap_t::string_length(grib_accessor* a)
{
return a->length;
}

int grib_accessor_class_bitmap_t::unpack_string(grib_accessor* a, char* val, size_t* len){
int grib_accessor_class_bitmap_t::unpack_string(grib_accessor* a, char* val, size_t* len)
{
grib_handle* hand = grib_handle_of_accessor(a);
const size_t l = a->length;
const size_t l = a->length;

if (*len < l) {
const char* cclass_name = a->cclass->name;
Expand Down
33 changes: 19 additions & 14 deletions src/accessor/grib_accessor_class_bits_per_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@

#include "grib_accessor_class_bits_per_value.h"

grib_accessor_class_bits_per_value_t _grib_accessor_class_bits_per_value{"bits_per_value"};
grib_accessor_class_bits_per_value_t _grib_accessor_class_bits_per_value{ "bits_per_value" };
grib_accessor_class* grib_accessor_class_bits_per_value = &_grib_accessor_class_bits_per_value;


void grib_accessor_class_bits_per_value_t::init(grib_accessor* a, const long l, grib_arguments* args){
void grib_accessor_class_bits_per_value_t::init(grib_accessor* a, const long l, grib_arguments* args)
{
grib_accessor_class_long_t::init(a, l, args);
int n = 0;
int n = 0;
grib_accessor_bits_per_value_t* self = (grib_accessor_bits_per_value_t*)a;
self->values = grib_arguments_get_name(grib_handle_of_accessor(a), args, n++);
self->bits_per_value = grib_arguments_get_name(grib_handle_of_accessor(a), args, n++);
self->values = grib_arguments_get_name(grib_handle_of_accessor(a), args, n++);
self->bits_per_value = grib_arguments_get_name(grib_handle_of_accessor(a), args, n++);
a->flags |= GRIB_ACCESSOR_FLAG_FUNCTION;
a->length = 0;
}

int grib_accessor_class_bits_per_value_t::unpack_long(grib_accessor* a, long* val, size_t* len){
int ret = 0;
int grib_accessor_class_bits_per_value_t::unpack_long(grib_accessor* a, long* val, size_t* len)
{
grib_accessor_bits_per_value_t* self = (grib_accessor_bits_per_value_t*)a;
grib_handle* h = grib_handle_of_accessor(a);

int ret = 0;
grib_handle* h = grib_handle_of_accessor(a);

if ((ret = grib_get_long_internal(h, self->bits_per_value, val)) != GRIB_SUCCESS)
return ret;
Expand All @@ -37,13 +40,15 @@ int grib_accessor_class_bits_per_value_t::unpack_long(grib_accessor* a, long* va
return ret;
}

int grib_accessor_class_bits_per_value_t::pack_long(grib_accessor* a, const long* val, size_t* len){
double* values = NULL;
size_t size = 0;
int ret = 0;
int grib_accessor_class_bits_per_value_t::pack_long(grib_accessor* a, const long* val, size_t* len)
{
grib_accessor_bits_per_value_t* self = (grib_accessor_bits_per_value_t*)a;
grib_context* c = a->context;
grib_handle* h = grib_handle_of_accessor(a);

double* values = NULL;
size_t size = 0;
int ret = 0;
grib_context* c = a->context;
grib_handle* h = grib_handle_of_accessor(a);

if ((ret = grib_get_size(h, self->values, &size)) != GRIB_SUCCESS)
return ret;
Expand Down
Loading

0 comments on commit d397f58

Please sign in to comment.