IT Community - Software Programming, Web Development and Technical Support

How can I implement opaque (abstract) data types in C?

This is a discussion on How can I implement opaque (abstract) data types in C? within the C and C++ Programming forums, part of the Software Development category; Hi, How can I implement opaque (abstract) data types in C? Thanks, Prasath.K...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-18-2007, 02:47 AM
prasath prasath is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: Chennai
Posts: 173
prasath is on a distinguished road
Cool How can I implement opaque (abstract) data types in C?

Hi,
How can I implement opaque (abstract) data types in C?

Thanks,
Prasath.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-30-2007, 04:24 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Exclamation Re: How can I implement opaque (abstract) data types in C?

Hi prasath,

Hope this Code explains what you are searching for,
Code:
#include <stdio.h>
 #ifdef UWOCSD
  int fclose(FILE *stream); /* library doesn't declare fclose()  lose, lose! */
 #endif
#include "types.h"
#include "proto.h"
#include "stream.h"


/* Prototypes for local functions */
static 
  FILE * open_file(const char * proc, const char * fname, const char * mode);
static
  bool close_file(const char * proc, const char * fname, FILE * handle);

/* Exported (program global) opaque type for using functions defined here */
File_t Stream = {
  &open_file, &close_file};


/* Module global variable */
   static int open_files = 0;  


/* 
   --- Functions using the opaque datatype --- 
 
   open()  : given a filename it returns a file handle
   close() : given a file handle it closes the associated stream

*/

/*
 **********
 * open() *
 **********
 PRE:  `fname' holds the name of a file to be opened (e.g. "foo.tmp") with 
       type `mode', e.g. "rb" for reading or "wb" for writing.
 POST:  The file has been opened, a stream has been associated with it and 
        a (non-NULL) pointer to the stream has been returned. 
       OR
        The operation failed, the user has been notified of the error (by a
        call to swerr()) and NULL has been returned.
*/
FILE * open_file(const char * proc,  /* name of calling function        */
                 const char * fname, /* filename of file to open        */
                 const char * mode)  /* see def'n of `type' in fopen(3) */
{
           FILE * handle;

   if ((handle = fopen(fname, mode)) != NULL) {
      open_files++;
   } else {
      swerr(proc, 0, "cannot open file `%s'", fname); 
   }
   return(handle);
} /* open_file() */


/*
 ***********
 * close() *
 ***********
 PRE:  `handle' points to an open stream which is associated with a file 
       named by `fname'.
 POST:  TRUE has been returned and the stream was succesfully closed
       OR
        FALSE was returned, an error occurred and was reported (by swerr()). 
*/
bool close_file(const char * proc,    /* name of calling function */
                const char * fname,   /* filename of file to open */
                      FILE * handle)  /* stream to close */
{
   if (fclose(handle) != EOF) {
      open_files--;
      return(TRUE);
   } else {
      swerr(proc, 0, "cannot close file `%s'", fname);
      return(FALSE);
   }
} /* close_file() */

/* end of stream.c */

Hope Informative
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Server Data Types and Ranges. Sundaram Database Support 7 11-14-2008 05:11 AM
What is the difference between CHAR and VARCHAR data types? sundarraja Database Support 1 02-01-2008 10:36 PM
When working with shared data in threading how do you implement synchronization in a oxygen ASP and ASP.NET Programming 1 07-26-2007 04:07 AM
How many types of authentication modes are there and what are the Types.... Archer ASP and ASP.NET Programming 1 07-25-2007 03:48 AM
Java:Tutorial - Data Types pranky Java Programming 0 02-24-2007 12:47 AM


All times are GMT -7. The time now is 05:21 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0