Thread: Multiple read types

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    14

    Question Multiple read types

    I'm trying to develop a function that could return different data type e.g. byte, word, dword etc.

    How do i get started on doing this?

    A simple example would be something like this:-

    Code:
    void multi_read(int feature_id)
    {
     /* Data type to be supported */
     boolean result0;
     byte result1;
     word result2;
     dword result3;
     int64 result4;
     byte result5[2][100];
    
     switch(feature_id)
     {
      case 0:
      /* Boolean */
      /* Some processing */
        result0 = TRUE;
        return result0;
      break;
      case 1:
      /* Byte */
      /* Some processing */
        result1 = 250;
        return result1;
      break;
      case 2:
      /* Word */
      /* Some processing */
        result2 = 40000;
        return result2;
      break;
      case 3:
      /* Dword */
      /* Some processing */
        result3 = 125000;
        return result3;
      break;
      case 4:
      /* Int64 */
      /* Some processing */
        result4 = 5000000;
        return result4;
      break;
      case 5:
      /* Array */
      /* Some processing */
        strcpy(result5[0],"Hello World!");
        strcat(result5[0],"\0");
        strcpy(result5[1],"My_Version_ID");
        strcat(result5[1],"\0");
        return &result5[1];
      break;
      default:
      /* Some processing */
      break;
     }
     return FALSE;
    }
    Thanks in advance for any ideas/guides.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    by the way, I put a void in the function type but I know that needs to be something else... Also, if possible, I'm trying to avoid casting so that means whoever call the function can simply use it directly
    e.g.
    Code:
    boolean result;
    dword value;
    result = multi_read(MY_NEW_FEATURE);
    /* Some processing */
    value = multi_read(FEATURE_X_VALUE);

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    I would do something along the lines of this:

    Code:
    #define FEAT_BOOLEAN 0
    #define FEAT_BYTE 1
    #define FEAT_WORD 2
    #define FEAT_DWORD 3
    #define FEAT_INT64 4
    
    typedef struct tagFeatureData {
        void* pAnyData;
        unsigned int Type;
    } FeatureData;
    
    void multi_read(int feature_id, FeatureData* fd) {
        switch(feature_id) {
            case 1:
            {
                bool whatever = false;
                fd->pAnyData = (void*)&whatever;
                fd->Type = FEAT_BOOLEAN;
                break;
            }
            /* ... */
        }
    }
    I'm not quite sure about that cast though. I'm more used to C++'s templates.

    Edit: I think Dave Sinkula's idea is better.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    14

    Thumbs up

    I'm now looking into Dave's idea.. and will try to get a simple program running based on it. Too bad the link didn't have an example, but at least I got something to start with.

    Will see how it goes. Thanks for the fast respond guys.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    typedef struct tagFeatureData {
        enum { BOOLEAN, BYTE, WORD } code;
        union {
            bool dat_boolean;
            BYTE dat_byte;
            WORD dat_word;
            /* ... */
        };
    } FeatureData;
    
    FeatureData* multi_read(int feature_id) {
        // You may want to check that line, I've never used malloc()
        FeatureData* fd = (FeatureData*)malloc(sizeof(FeatureData));
        if(fd != NULL) {
            switch(feature_id) {
                case 1:
                    fd->dat_code =  BOOLEAN;
                    fd->dat_boolean = 1;
                    break;
            }
            return fd;
        }
    }
    
    int main() {
        FeatureData* fd = multi_read(1);
        if(fd->code == BOOLEAN)
            printf("%d", fd->dat_boolean);
        free(fd);
    }

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    Okie, I realise there's one setback for the above idea:-

    Code:
    FeatureData* fd = multi_read(1);
    if(fd->code == BOOLEAN)
        printf("%d", fd->dat_boolean);
    Since the multi_read() returns the struct, I still need an extra line of checking to determine what data type is in use for that feature. I could use this idea with some design change though to my solution.

    Still, I would like to simply achieve the initial design having the multi_read() returning the value directly, with whatever checking/processing done in multi_read(), so the caller just invoke the function and can either use the value read directly or store it elsewhere. e.g.
    Code:
    boolean result;
    dword value;
    if( multi_read(MY_NEW_FEATURE_ENABLE) == TRUE)
    {
      value = multi_read(NEW_FEATURE_X_VALUE);
    }
    Any ideas?

    Edit: I'm not being picky, but it will be nice if the caller of multi_read() need not do anything additional beside just calling the function and getting their value. Like I said, its a design choice, but just wondering if this is achievable in C.
    Another constraint I have is that I cannot use memory related function calls e.g. malloc, realloc etc.
    Last edited by 7force; 06-30-2006 at 03:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accepting Multiple Types of Input
    By Junior89 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2006, 11:25 PM
  2. read multiple lines from a file
    By YankeePride13 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 10:30 PM
  3. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  4. Replies: 1
    Last Post: 05-01-2003, 02:52 PM
  5. Returning multiple types w/o a struct
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2003, 11:04 PM