1 /********************************************************************* 2 Type -- Type information and basic operations. 3 This is part of GNU Astronomy Utilities (Gnuastro) package. 4 5 Original author: 6 Mohammad Akhlaghi <mohammad@akhlaghi.org> 7 Contributing author(s): 8 Copyright (C) 2015-2021, Free Software Foundation, Inc. 9 10 Gnuastro is free software: you can redistribute it and/or modify it 11 under the terms of the GNU General Public License as published by the 12 Free Software Foundation, either version 3 of the License, or (at your 13 option) any later version. 14 15 Gnuastro is distributed in the hope that it will be useful, but 16 WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with Gnuastro. If not, see <http://www.gnu.org/licenses/>. 22 **********************************************************************/ 23 #ifndef __GAL_TYPE_H__ 24 #define __GAL_TYPE_H__ 25 26 #include <limits.h> 27 #include <stdint.h> 28 29 #include <gsl/gsl_complex.h> 30 31 /* When we are within Gnuastro's building process, 'IN_GNUASTRO_BUILD' is 32 defined. In the build process, installation information (in particular 33 'GAL_CONFIG_SIZEOF_SIZE_T' that we need below) is kept in 34 'config.h'. When building a user's programs, this information is kept in 35 'gnuastro/config.h'. Note that all '.c' files in Gnuastro's source must 36 start with the inclusion of 'config.h' and that 'gnuastro/config.h' is 37 only created at installation time (not present during the building of 38 Gnuastro). */ 39 #ifndef IN_GNUASTRO_BUILD 40 #include <gnuastro/config.h> 41 #endif 42 43 44 45 /* C++ Preparations */ 46 #undef __BEGIN_C_DECLS 47 #undef __END_C_DECLS 48 #ifdef __cplusplus 49 # define __BEGIN_C_DECLS extern "C" { 50 # define __END_C_DECLS } 51 #else 52 # define __BEGIN_C_DECLS /* empty */ 53 # define __END_C_DECLS /* empty */ 54 #endif 55 /* End of C++ preparations */ 56 57 58 59 /* Actual header contants (the above were for the Pre-processor). */ 60 __BEGIN_C_DECLS /* From C++ preparations */ 61 62 63 64 65 66 /************************************************************* 67 ************** Constants *************** 68 *************************************************************/ 69 /* Macros to identify the type of data. */ 70 enum gal_types 71 { 72 GAL_TYPE_INVALID, /* Invalid (=0 by C standard). */ 73 74 GAL_TYPE_BIT, /* 1 bit */ 75 GAL_TYPE_UINT8, /* 8-bit unsigned integer. */ 76 GAL_TYPE_INT8, /* 8-bit signed integer. */ 77 GAL_TYPE_UINT16, /* 16-bit unsigned integer. */ 78 GAL_TYPE_INT16, /* 16-bit signed integer. */ 79 GAL_TYPE_UINT32, /* 32-bit unsigned integer. */ 80 GAL_TYPE_INT32, /* 32-bit signed integer. */ 81 GAL_TYPE_UINT64, /* 64-bit unsigned integer. */ 82 GAL_TYPE_INT64, /* 64-bit signed integer. */ 83 GAL_TYPE_FLOAT32, /* 32-bit single precision floating point. */ 84 GAL_TYPE_FLOAT64, /* 64-bit double precision floating point. */ 85 GAL_TYPE_COMPLEX32, /* Complex 32-bit floating point. */ 86 GAL_TYPE_COMPLEX64, /* Complex 64-bit floating point. */ 87 GAL_TYPE_STRING, /* String of characters. */ 88 GAL_TYPE_STRLL, /* Linked list of strings. */ 89 }; 90 91 92 93 /* Define system specific types. For example 'size_t' is 4 and 8 bytes on 94 32 and 64 bit systems respectively. In both cases, the standard defines 95 'size_t' to be unsigned. A similar case exists for 'long', but it is 96 signed. During './configure' the sizeof 'size_t' and 'long' were found 97 and are used to define an alias for these system specific types. 98 99 Note: we are not using 'else'. This is done because by any chance, if 100 the length of these types is not what is expected (4 or 8), then the 101 aliases are not defined and the compiler will crash. */ 102 #if GAL_CONFIG_SIZEOF_SIZE_T == 4 103 #define GAL_TYPE_SIZE_T GAL_TYPE_UINT32 104 #elif GAL_CONFIG_SIZEOF_SIZE_T == 8 105 #define GAL_TYPE_SIZE_T GAL_TYPE_UINT64 106 #endif 107 108 #if GAL_CONFIG_SIZEOF_LONG == 4 109 #define GAL_TYPE_LONG GAL_TYPE_INT32 110 #define GAL_TYPE_ULONG GAL_TYPE_UINT32 111 #elif GAL_CONFIG_SIZEOF_LONG == 8 112 #define GAL_TYPE_LONG GAL_TYPE_INT64 113 #define GAL_TYPE_ULONG GAL_TYPE_UINT64 114 #endif 115 116 #if GAL_CONFIG_SIZEOF_INT == 2 117 #define GAL_TYPE_INT GAL_TYPE_INT16 118 #define GAL_TYPE_UINT GAL_TYPE_UINT16 119 #elif GAL_CONFIG_SIZEOF_INT == 4 120 #define GAL_TYPE_INT GAL_TYPE_INT32 121 #define GAL_TYPE_UINT GAL_TYPE_UINT32 122 #endif 123 124 125 126 127 128 129 130 /************************************************************* 131 ************** General info *************** 132 *************************************************************/ 133 size_t 134 gal_type_sizeof(uint8_t type); 135 136 char * 137 gal_type_name(uint8_t type, int long_name); 138 139 uint8_t 140 gal_type_from_name(char *str); 141 142 void 143 gal_type_min(uint8_t type, void *in); 144 145 void 146 gal_type_max(uint8_t type, void *in); 147 148 int 149 gal_type_is_int(uint8_t type); 150 151 int 152 gal_type_is_list(uint8_t type); 153 154 int 155 gal_type_out(int first_type, int second_type); 156 157 158 159 160 161 /************************************************************* 162 ************** To/from string *************** 163 *************************************************************/ 164 165 char * 166 gal_type_bit_string(void *in, size_t size); 167 168 char * 169 gal_type_to_string(void *ptr, uint8_t type, int quote_if_str_has_space); 170 171 int 172 gal_type_from_string(void **out, char *string, uint8_t type); 173 174 void * 175 gal_type_string_to_number(char *string, uint8_t *type); 176 177 178 179 180 181 __END_C_DECLS /* From C++ preparations */ 182 183 #endif /* __GAL_TYPE_H__ */ 184