Loading...
Searching...
No Matches
g2cio.c File Reference

File I/O functions for the g2c library. More...

`#include "grib2_int.h"`

Go to the source code of this file.

## Macros

#define | BITSHIFT_15 15
| 15 bits.
#define | BITSHIFT_31 31
| 31 bits.
#define | BITSHIFT_63 63
| 63 bits.
#define | BITSHIFT_7 7
| 7 bits.

## Functions

int | g2c_file_io (FILE *f, int write, int g2ctype, void *var)
| Read or write a big-endian integer type to an open file, with conversion between native and big-endian format.
int | g2c_file_io_byte (FILE *f, int write, char *var)
| Read or write a big-endian signed byte to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.
int | g2c_file_io_int (FILE *f, int write, int *var)
| Read or write a big-endian 4-byte signed int to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.
int | g2c_file_io_longlong (FILE *f, int write, long long *var)
| Read or write a big-endian signed long long to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.
int | g2c_file_io_short (FILE *f, int write, short *var)
| Read or write a big-endian signed short to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.
int | g2c_file_io_template (FILE *f, int rw_flag, int map, long long int *template_value)
| Read or write a big-endian 4-byte int or unsigned int from or to an open file, with conversion between native and big-endian format, and handling of GRIB negative numbers.
int | g2c_file_io_ubyte (FILE *f, int write, unsigned char *var)
| Read or write a big-endian unsigned byte to an open GRIB2 file, with conversion between native and big-endian format.
int | g2c_file_io_uint (FILE *f, int write, unsigned int *var)
| Read or write a big-endian 4-byte unsigned int to an open GRIB2 file, with conversion between native and big-endian format.
int | g2c_file_io_ulonglong (FILE *f, int write, unsigned long long *var)
| Read or write a big-endian unsigned long long to an open GRIB2 file, with conversion between native and big-endian format.
int | g2c_file_io_ushort (FILE *f, int write, unsigned short *var)
| Read or write a big-endian unsigned short to an open GRIB2 file, with conversion between native and big-endian format.

## Detailed Description

File I/O functions for the g2c library.

Author
  Ed Hartnett

Date
  Nov 11, 2022

Definition in file g2cio.c.

## Macro Definition Documentation

## BITSHIFT_15

#define BITSHIFT_15 15

15 bits.

Definition at line 10 of file g2cio.c.

## BITSHIFT_31

#define BITSHIFT_31 31

31 bits.

Definition at line 11 of file g2cio.c.

## BITSHIFT_63

#define BITSHIFT_63 63

63 bits.

Definition at line 12 of file g2cio.c.

## BITSHIFT_7

#define BITSHIFT_7 7

7 bits.

Definition at line 9 of file g2cio.c.

## Function Documentation

## g2c_file_io()

int g2c_file_io | ( | FILE * | f,
|  | int | write,
|  | int | g2ctype,
|  | void * | var
| ) |  |

Read or write a big-endian integer type to an open file, with conversion between native and big-endian format.

GRIB2 handles negative numbers in a special way. Instead of storing two-compliments, like every other programmer and computing organization in the world, GRIB2 flips the first bit, then stores the rest of the int as an unsigned number in the remaining 31 bits. How exciting!

This function takes the excitement out of GRIB2 negative numbers.

Parameters

  f | Pointer to the open FILE.
  write | Non-zero if function should write, otherwise function will read.
  g2ctype | The type to be read or written.
  var | Pointer to the int to be written, or pointer to the storage that gets the int read.

Returns
  - :: G2C_NOERROR No error.
  - :: G2C_EINVAL Invalid input.
  - :: G2C_EFILE Error reading/writing file.

Author
  Ed Hartnett

Date
  11/7/22

Definition at line 41 of file g2cio.c.

References BITSHIFT_15, BITSHIFT_31, BITSHIFT_63, BITSHIFT_7, EIGHT_BYTES, FOUR_BYTES, G2C_BYTE, G2C_EBADTYPE, G2C_EFILE, G2C_EINVAL, G2C_INT, G2C_INT64, G2C_NOERROR, G2C_SHORT, G2C_UBYTE, G2C_UINT, G2C_UINT64, G2C_USHORT, hton64, ntoh64, ONE_BYTE, and TWO_BYTES.

Referenced by g2c_file_io_byte(), g2c_file_io_int(), g2c_file_io_longlong(), g2c_file_io_short(), g2c_file_io_ubyte(), g2c_file_io_uint(), g2c_file_io_ulonglong(), and g2c_file_io_ushort().

## g2c_file_io_byte()

int g2c_file_io_byte | ( | FILE * | f,
|  | int | write,
|  | char * | var
| ) |  |

Read or write a big-endian signed byte to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.

Parameters

  f | Pointer to the open GRIB2 FILE.
  write | Non-zero to write, zero to read.
  var | Pointer to the byte.

Returns
  - :: G2C_NOERROR No error.
  - :: G2C_EINVAL Invalid input.
  - :: G2C_EFILE Error reading/writing file.

Author
  Ed Hartnett 11/13/22

Definition at line 316 of file g2cio.c.

References G2C_BYTE, and g2c_file_io().

Referenced by g2c_file_io_template().

## g2c_file_io_int()

int g2c_file_io_int | ( | FILE * | f,
|  | int | write,
|  | int * | var
| ) |  |

Read or write a big-endian 4-byte signed int to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.

Parameters

  f | Pointer to the open GRIB2 FILE.
  write | Non-zero to write, zero to read.
  var | Pointer to the int.

Returns
  - :: G2C_NOERROR No error.
  - :: G2C_EINVAL Invalid input.
  - :: G2C_EFILE Error reading/writing file.

Author
  Ed Hartnett 11/7/22

Definition at line 230 of file g2cio.c.

References g2c_file_io(), and G2C_INT.

Referenced by g2c_file_io_template().

## g2c_file_io_longlong()

int g2c_file_io_longlong | ( | FILE * | f,
|  | int | write,
|  | long long * | var
| ) |  |

Read or write a big-endian signed long long to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.

Parameters

  f | Pointer to the open GRIB2 FILE.
  write | Non-zero to write, zero to read.
  var | Pointer to the long long.

Returns
  - :: G2C_NOERROR No error.
  - :: G2C_EINVAL Invalid input.
  - :: G2C_EFILE Error reading/writing file.

Author
  Ed Hartnett 11/13/22

Definition at line 359 of file g2cio.c.

References g2c_file_io(), and G2C_INT64.

## g2c_file_io_short()

int g2c_file_io_short | ( | FILE * | f,
|  | int | write,
|  | short * | var
| ) |  |

Read or write a big-endian signed short to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.

Parameters

  f | Pointer to the open GRIB2 FILE.
  write | Non-zero to write, zero to read.
  var | Pointer to the short.

Returns
  - :: G2C_NOERROR No error.
  - :: G2C_EINVAL Invalid input.
  - :: G2C_EFILE Error reading/writing file.

Author
  Ed Hartnett 11/13/22

Definition at line 273 of file g2cio.c.

References g2c_file_io(), and G2C_SHORT.

Referenced by g2c_file_io_template(), g2c_rw_section1_metadata(), g2c_start_index_record(), and g2c_start_index_record_lf().

## g2c_file_io_template()

int g2c_file_io_template | ( | FILE * | f,
|  | int | rw_flag,
|  | int | map,
|  | long long int * | template_value
| ) |  |

Read or write a big-endian 4-byte int or unsigned int from or to an open file, with conversion between native and big-endian format, and handling of GRIB negative numbers.

This is for template values.

With template values, if the map value is negative, then the template value may be negative.

Parameters

  f | Pointer to the open FILE.
  rw_flag | Non-zero if function should write, otherwise function will read.
  map | The map value for this template item.
  template_value | Pointer to the template value to be written, or pointer to the storage that gets the templage value read.

Returns
  - :: G2C_NOERROR No error.
  - :: G2C_EINVAL Invalid input.
  - :: G2C_EFILE Error reading/writing file.

Author
  Ed Hartnett 11/7/22

Definition at line 408 of file g2cio.c.

References FOUR_BYTES, G2C_EBADTEMPLATE, g2c_file_io_byte(), g2c_file_io_int(), g2c_file_io_short(), g2c_file_io_ubyte(), g2c_file_io_uint(), g2c_file_io_ushort(), G2C_NOERROR, ONE_BYTE, and TWO_BYTES.

Referenced by g2c_rw_section3_metadata(), g2c_rw_section4_metadata(), and g2c_rw_section5_metadata().

## g2c_file_io_ubyte()

int g2c_file_io_ubyte | ( | FILE * | f,
|  | int | write,
|  | unsigned char * | var
| ) |  |

Read or write a big-endian unsigned byte to an open GRIB2 file, with conversion between native and big-endian format.

Parameters

  f | Pointer to the open GRIB2 FILE.
  write | Non-zero to write, zero to read.
  var | Pointer to the unsigned byte.

Returns
  - :: G2C_NOERROR No error.
  - :: G2C_EINVAL Invalid input.
  - :: G2C_EFILE Error reading/writing file.

Author
  Ed Hartnett 11/13/22

Definition at line 337 of file g2cio.c.

References g2c_file_io(), and G2C_UBYTE.

Referenced by g2c_file_io_template(), g2c_open_index(), g2c_rw_section1_metadata(), g2c_rw_section3_metadata(), g2c_rw_section6_metadata(), g2c_start_index1_record(), g2c_start_index_record(), g2c_start_index_record_lf(), g2c_write_index(), and read_msg_metadata().

## g2c_file_io_uint()

int g2c_file_io_uint | ( | FILE * | f,
|  | int | write,
|  | unsigned int * | var
| ) |  |

Read or write a big-endian 4-byte unsigned int to an open GRIB2 file, with conversion between native and big-endian format.

Parameters

  f | Pointer to the open GRIB2 FILE.
  write | Non-zero to write, zero to read.
  var | Pointer to the unsigned int.

Returns
  - :: G2C_NOERROR No error.
  - :: G2C_EINVAL Invalid input.
  - :: G2C_EFILE Error reading/writing file.

Author
  Ed Hartnett 11/7/22

Definition at line 251 of file g2cio.c.

References g2c_file_io(), and G2C_UINT.

Referenced by g2c_file_io_template(), g2c_open_index(), g2c_rw_section1_metadata(), g2c_rw_section3_metadata(), g2c_rw_section5_metadata(), g2c_start_index1_record(), g2c_start_index_record(), g2c_start_index_record_lf(), g2c_write_index(), and read_msg_metadata().

## g2c_file_io_ulonglong()

int g2c_file_io_ulonglong | ( | FILE * | f,
|  | int | write,
|  | unsigned long long * | var
| ) |  |

Read or write a big-endian unsigned long long to an open GRIB2 file, with conversion between native and big-endian format.

Parameters

  f | Pointer to the open GRIB2 FILE.
  write | Non-zero to write, zero to read.
  var | Pointer to the unsigned long long.

Returns
  - :: G2C_NOERROR No error.
  - :: G2C_EINVAL Invalid input.
  - :: G2C_EFILE Error reading/writing file.

Author
  Ed Hartnett 11/13/22

Definition at line 380 of file g2cio.c.

References g2c_file_io(), and G2C_UINT64.

Referenced by g2c_start_index_record(), and g2c_start_index_record_lf().

## g2c_file_io_ushort()

int g2c_file_io_ushort | ( | FILE * | f,
|  | int | write,
|  | unsigned short * | var
| ) |  |

Read or write a big-endian unsigned short to an open GRIB2 file, with conversion between native and big-endian format.

Parameters

  f | Pointer to the open GRIB2 FILE.
  write | Non-zero to write, zero to read.
  var | Pointer to the unsigned short.

Returns
  - :: G2C_NOERROR No error.
  - :: G2C_EINVAL Invalid input.
  - :: G2C_EFILE Error reading/writing file.

Author
  Ed Hartnett 11/13/22

Definition at line 294 of file g2cio.c.

References g2c_file_io(), and G2C_USHORT.

Referenced by g2c_file_io_template(), g2c_rw_section3_metadata(), g2c_rw_section4_metadata(), and g2c_rw_section5_metadata().