Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2011 - Raphael S.Carvalho (a.k.a Utroz)
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details. You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
- */
- //oriented_c.h
- #ifndef _ORIENTED_C
- #define _ORIENTED_C
- #ifdef PACKAGE
- #include<stdio.h>
- #include<stdarg.h>
- #include<string.h>
- #include<stdlib.h>
- #define send_string(from, to) \
- strcpy(to, from)
- /* Data Types */
- enum {
- STR = 0 ,
- INT ,
- FLOAT
- } data_type;
- #endif
- #define ERROR(error) \
- { perror(error); \
- exit(EXIT_FAILURE); }
- typedef int integer;
- typedef char string;
- typedef struct class class_t;
- /*
- * Custom Functions
- * print: Custom print.
- */
- #define PRINT_(...) fprintf(stderr, __VA_ARGS__)
- #define print(buffer, ...) PRINT_ (buffer, __VA_ARGS__)
- /* Data Input */
- int
- input (int, void *, unsigned int);
- /* Data Output */
- void
- printdt (int, ...);
- #endif
- // ------------ //
- //class.h
- #define PACKAGE
- #include "oriented_c.h"
- // :: Classes :: //
- struct class {
- // Data
- string name[16];
- integer age;
- // Methods
- integer (*get_age) (class_t*);
- void (*set_age) (class_t*, integer);
- };
- /* Function implements */
- integer get(class_t *obj) {
- return obj->age;
- }
- void set(class_t *obj, integer value) {
- obj->age = value;
- }
- /* Constructor Method */
- void init (class_t *obj, integer value, string* name)
- {
- /* Initializing data
- * from the object */
- send_string(name, obj->name);
- obj->age = value;
- /* Assignment function address */
- obj->get_age = get;
- obj->set_age = set;
- }
- // ------------ //
- // test.c
- #include "class.h"
- /* ::RESULT::
- bash-4.1$ ./test
- rafa
- 22
- Test[0]: Name: rafa | Age: 22
- Test[1]: Name: Raphael | Age: 23
- */
- main()
- {
- class_t test[2];
- string name[16];
- integer age;
- printdt(STR, "Input your name:\n");
- input(STR, name, 15);
- printdt(STR, "Input your age: ");
- input(INT, &age, 0);
- // Calling constructor method */
- init(&test[0], age, name); // Create an object to test[0].
- init(&test[1], age, name); // Create an object to test[1].
- test[1].set_age(&test[1], 23);
- send_string("Raphael", test[1].name);
- print("Test[%d]: Name: %s | Age: %d\n", 0, test[0].name, test[0].age);
- print("Test[%d]: Name: %s | Age: %d\n", 1, test[1].name, test[1].age);
- exit(0);
- }
- // ------------ //
- // oriented_c.c
- #define PACKAGE
- #include "oriented_c.h"
- int
- input (int data, void *addr, size_t size)
- {
- signed char c;
- int i;
- if(data == STR) {
- char *buffer = (char *) addr;
- for(i = 0; i < (size-1) && (c = getchar()) != EOF
- && c != '\n'; i++)
- buffer[i] = c;
- buffer[i] = '\0';
- } else if (data == INT) {
- int *addr_int = (int *) addr;
- i = scanf("%d", addr_int);
- } else if (data == FLOAT) {
- float *addr_float = (float *) addr;
- i = scanf("%f", addr_float);
- } else
- ERROR("Invalid arguments");
- return i;
- }
- void
- printdt (int data, ...)
- {
- va_list args;
- va_start (args, data);
- char buffer[128];
- switch (data) {
- case STR:
- vsprintf (buffer, "%s", args);
- break;
- case INT:
- vsprintf (buffer, "%d", args);
- break;
- case FLOAT:
- vsprintf (buffer, "%f", args);
- break;
- default:
- ERROR("print: Invalid arguments"); break;
- }
- fprintf (stdout, "%s", buffer);
- va_end ( args );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement