hedgefund

generics_c_auto_detect

Dec 18th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | Software | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. typedef enum {
  7.     INT,
  8.     DOUBLE,
  9.     CHAR,
  10.     STRING,
  11.     UNKNOWN
  12. } DataType;
  13.  
  14. typedef struct {
  15.     union {
  16.         int i;
  17.         double d;
  18.         char c;
  19.         char* s;
  20.     } data;
  21.     DataType type;
  22. } Data;
  23.  
  24. typedef struct {
  25.     Data x;
  26.     Data y;
  27. } Point;
  28.  
  29. // Function to detect data type
  30. DataType detect_type(void *value, char *input) {
  31.     if (isdigit(input[0]) || (input[0] == '-' && isdigit(input[1]))) {
  32.         if (strchr(input, '.')) {
  33.             return DOUBLE;
  34.         }
  35.         return INT;
  36.     } else if (strlen(input) == 1) {
  37.         return CHAR;
  38.     } else if (strchr(input, '\"')) {
  39.         // Assuming strings are wrapped in quotes
  40.         return STRING;
  41.     }
  42.     return UNKNOWN;
  43. }
  44.  
  45. // Function to set data based on detected type
  46. void set_data(Data *data, char *input) {
  47.     switch(detect_type(NULL, input)) {
  48.         case INT:
  49.             data->data.i = atoi(input);
  50.             data->type = INT;
  51.             break;
  52.         case DOUBLE:
  53.             data->data.d = atof(input);
  54.             data->type = DOUBLE;
  55.             break;
  56.         case CHAR:
  57.             data->data.c = input[0];
  58.             data->type = CHAR;
  59.             break;
  60.         case STRING:
  61.             data->data.s = strdup(input + 1);  // Skip the opening quote
  62.             data->data.s[strlen(data->data.s) - 1] = '\0';  // Remove the closing quote
  63.             data->type = STRING;
  64.             break;
  65.         default:
  66.             data->type = UNKNOWN;
  67.             break;
  68.     }
  69. }
  70.  
  71. void print_data(Data data) {
  72.     switch (data.type) {
  73.         case INT:
  74.             printf("%d", data.data.i);
  75.             break;
  76.         case DOUBLE:
  77.             printf("%f", data.data.d);
  78.             break;
  79.         case CHAR:
  80.             printf("%c", data.data.c);
  81.             break;
  82.         case STRING:
  83.             printf("%s", data.data.s);
  84.             break;
  85.         default:
  86.             printf("Unknown type");
  87.     }
  88. }
  89.  
  90. void print_point(Point p) {
  91.     printf("p.x = ");
  92.     print_data(p.x);
  93.     printf(", p.y = ");
  94.     print_data(p.y);
  95.     printf("\n");
  96. }
  97.  
  98. Point mixup(Point self, Point other) {
  99.     Point result;
  100.     result.x = self.x;
  101.     result.y = other.y;
  102.     return result;
  103. }
  104.  
  105. int main() {
  106.     Point p1;
  107.     set_data(&p1.x, "5");
  108.     set_data(&p1.y, "10.4");
  109.  
  110.     Point p2;
  111.     set_data(&p2.x, "\"Hello\"");  // Quotes needed for string detection
  112.     set_data(&p2.y, "c");
  113.  
  114.     Point p3 = mixup(p1, p2);
  115.     print_point(p3);
  116.  
  117.     // Clean up allocated string memory
  118.     if (p2.x.type == STRING) {
  119.         free(p2.x.data.s);
  120.     }
  121.  
  122.     return 0;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment