🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.c.moderated
5 messages
5 total messages Started by Justin C Lloyd Sun, 24 Mar 1996 17:41
ada-like naming scheme?
#2371
Author: Justin C Lloyd
Date: Sun, 24 Mar 1996 17:41
17 lines
973 bytes
Does anyone know how to do write a .c file so that you have to include the
name of c file in any function calls, similar to ada?

In ada, for instance:

   ada.text_io.get(x);

means there is a collection of procedures named ada (a package), and in that
one named text_io, and in the text_io collection, one called get.

Maybe a way to do this by putting everything in a struct? For instance...

   float_sort.quicksort(float_array);
   integer_sort.quicksort(int_array);
   integer_sort.selection_sort(int_array);

--JcL
Re: ada-like naming scheme?
#2385
Author: Robert F. Monroe
Date: Mon, 25 Mar 1996 12:21
61 lines
1999 bytes
In article: <4j41gd$nij@solutions.solon.com>  lloyd@upsilon.cs.fsu.edu 
(Justin C Lloyd) writes:
> Does anyone know how to do write a .c file so that you have to include the
> name of c file in any function calls, similar to ada?
> 
> In ada, for instance:
> 
>    ada.text_io.get(x);
> 
> means there is a collection of procedures named ada (a package), and in that
> one named text_io, and in the text_io collection, one called get.

That is similar to what a namespace does in C++. .

> Maybe a way to do this by putting everything in a struct? For instance...

The only way that I can think of doing it in C would be to use 
function pointers as members of the structure and have some sort 
of initialisation/allocation function associated with it. 
Something like:

#include <stdio.h>

extern int Get();
extern int Put(int);

typedef struct
{
	int (*get)();
	int (*put)(int);

} text_io;

text_io *new_text_io(void)
{
	text_io *tio = (text_io*)malloc(sizeof(text_io));

	if (tio == NULL)
		return NULL;

	tio->get = Get;
	tio->put = Put;

	return tio;
}

int main()
{
	text_io *tio = new_text_io();

	if (tio != NULL)
	{
		int i = tio->get();
		tio->put(i);
	}
}

That could be fairly ugly for a lot of purposes but it may be 
useful for something.  Anyway, it does work.

	Bob.
Re: ada-like naming scheme?
#2386
Author: Robert F. Monroe
Date: Mon, 25 Mar 1996 12:22
48 lines
1886 bytes
As soon as I posted my first idea, I had an even more 
twisted thought. If you don't mind completely undermining 
the type checking of parameters, try this one. If you are into 
Ada, this one will even let you create a "generic" io package!
As far as I know, any ANSI C compiler should handle it fine.

	Bob.

PS: Of course I would never do this myself, but I have a 
friend...

#include <stdio.h>

#define INTEGER_IO	(0)
#define TEXT_IO		(1)

extern int INTEGER_IO_Get(void *);
extern int INTEGER_IO_Put(void *);
extern int TEXT_IO_Get(void *);
extern int TEXT_IO_Put(void *);

typedef struct {int (*get)(),(*put)();} io_package;

#define init_io_package(io,t) \
	io.get=t##_Get; io.put=t##_Put

int main()
{
	int i;
	char Buff[80];
	io_package IntegerIO, TextIO;

	init_io_package(IntegerIO, INTEGER_IO);
	init_io_package(TextIO, TEXT_IO);

	IntegerIO.get(&i);
	IntegerIO.put(&i);

	TextIO.get(Buff);
	TextIO.put(Buff);

	return 0;
}

int INTEGER_IO_Get(void *i)	{return scanf("%d", (int*)i);}
int INTEGER_IO_Put(void *i)	{return printf("%d", *(int*)i);}
int TEXT_IO_Get(void *buff)	{return scanf("%s", (char*)buff);}
int TEXT_IO_Put(void *buff)	{return printf("%s", (char*)buff);}
Re: ada-like naming scheme?
#2390
Author: Richard Krehbiel
Date: Wed, 27 Mar 1996 00:55
46 lines
1885 bytes
lloyd@upsilon.cs.fsu.edu (Justin C Lloyd) wrote:

>Does anyone know how to do write a .c file so that you have to include the
>name of c file in any function calls, similar to ada?

There's no automatic way to do this.  You will have to do it by
adopting some convention.

>In ada, for instance:

>   ada.text_io.get(x);

>means there is a collection of procedures named ada (a package), and in that
>one named text_io, and in the text_io collection, one called get.

>Maybe a way to do this by putting everything in a struct? For instance...

>   float_sort.quicksort(float_array);
>   integer_sort.quicksort(int_array);
>   integer_sort.selection_sort(int_array);

You could do just that...

typedef int (*int_sort_fp)(int *), int_sort_f();
typedef int (*float_sort_fp)(float *), float_sort_f();

extern int_sort_f int_sort_quicksort;
extern float_sort_f float_sort_quicksort;

struct
{
    int_sort_fp quicksort;
} int_sort = { int_sort_quicksort };

struct
{
    float_sort_fp quicksort;
} float_sort = { float_sort_quicksort };

>From curiosity, why not just adopt a naming convention that prepends
the package/module names to the function name?  Why not simply call
them int_sort_quicksort() and float_sort_quicksort()?

--
Richard Krehbiel, Kastle Systems, Arlington VA USA
rich@kastle.com (work) or richk@mnsinc.com (personal)
Re: ada-like naming scheme?
#2435
Author: Jens M Andreasen
Date: Sat, 30 Mar 1996 04:01
19 lines
1044 bytes

On 26 Mar 1996, Richard Krehbiel wrote:

> >   ada.text_io.get(x);
-<snip>-
> >   integer_sort.quicksort(int_array);
> >   integer_sort.selection_sort(int_array);
> 

> >From curiosity, why not just adopt a naming convention that prepends
> the package/module names to the function name?  Why not simply call
> them int_sort_quicksort() and float_sort_quicksort()?
> 
With a slight change, the dot.approach could be useful if the app at an 
earlier stage had realized what method to use and changed the pointer 
accordingly. (integer.sort)

// Jens M Andreasen
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads