can-eth-gw-utils Utilities  0.1
A bidirectional CAN to Ethernet Gateway (Utilities)
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Translation

Functions

char * enum2str (int value, const struct enums *enums, size_t size, int max)
 Returns the textual representation of an numeric identifier (enum) More...
 
char * flags2str (uint32_t bits, const struct flags *flags, size_t size)
 Convert Flags in ist textual represenation. More...
 

Detailed Description

Function Documentation

char* enum2str ( int  value,
const struct enums enums,
size_t  size,
int  max 
)

Returns the textual representation of an numeric identifier (enum)

Parameters
valuethe numeric identifier (value) of an enum
enumsA array with the textual representation of the int value. The Last entry in the array bust be {0}, because the function will stop here. The index af an array entry must be the same as param value.
sizethe size of the retuned char pointer. Must be big enough for the largest entry in the array.
Return values
NULLif not found
Returns
A char pointer with the textual representation of value ending with \0.

Definition at line 173 of file netlink.c.

References enums::name.

Referenced by nl_cb_list_entry().

174 {
175  char *str = malloc(size+1); /* +1 for \0 */
176  str[0] = '\0';
177 
178  /* Iterate until the end of value or the end of enums array */
179  for (int i = 0; i <= max && enums[i].name != 0; ++i) {
180  if (i == value) {
181  strcat(str, enums[i].name);
182  return str;
183  }
184  }
185 
186  free(str);
187  return NULL;
188 }

Here is the caller graph for this function:

char * flags2str ( uint32_t  bits,
const struct flags flags,
size_t  size 
)

Convert Flags in ist textual represenation.

Parameters
bitsThe bits variable which will be checked with the highest bit
flagsAn Array with its textual representation the index of the one array entry must be the same as the position in param bits from the left. The Last entry in the array bust be {0}, because the function will stop here.
sizethe size of the retuned char pointer. Must be big enough for all flags names plus 2 for < and > plus 1 for ',' after each flag plus 1 for \0
Returns
a char pointer in the form <Flag1,Flag2,Flag3,...> if the flags flag1, flag2, flag3, ... are set. The Pointer has a ending \0.

Definition at line 117 of file netlink.c.

Referenced by nl_cb_list_entry().

118 {
119  char *str = malloc(size);
120  str[0] = '<';
121  str[1] = '\0';
122 
123  /* Iterate until the end of bits or thh end of flags array */
124  for (int i = 0; i < sizeof(uint32_t) * 8 && flags[i].name != 0; ++i) {
125  // extract the i-th bit
126  int b = ((bits >> i) & 1);
127  // b will be 1 if i-th bit is set, 0 otherwise
128 
129  if (b == 1) {
130  strcat(str, flags[i].name);
131  strcat(str, ",");
132  }
133  }
134  int len = strlen(str);
135 
136  if (len <= 1) {
137  strcat(str, ">");
138  } else {
139  str[len-1] = '>'; /* replace ',' with '>' */
140  }
141 
142  return str;
143 }

Here is the caller graph for this function: