Advertisement
utroz

oriented_c

Jan 21st, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.14 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2011 - Raphael S.Carvalho (a.k.a Utroz)
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify it
  5.  * under the terms of the GNU General Public License as published by the Free
  6.  * Software Foundation; either version 2 of the License, or (at your option)
  7.  * any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful, but WITHOUT
  10.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12.  * more details.  You should have received a copy of the GNU General Public
  13.  * License along with this program; if not, write to the Free Software
  14.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  15.  */
  16.  
  17. //oriented_c.h
  18. #ifndef _ORIENTED_C
  19. #define _ORIENTED_C
  20.  
  21. #ifdef PACKAGE
  22.     #include<stdio.h>
  23.     #include<stdarg.h>
  24.     #include<string.h>
  25.     #include<stdlib.h>
  26.    
  27.     #define send_string(from, to) \
  28.      strcpy(to, from)
  29.    
  30.     /* Data Types */
  31.     enum {
  32.     STR = 0 ,
  33.     INT ,
  34.     FLOAT
  35.     } data_type;
  36. #endif    
  37.    
  38. #define ERROR(error)    \
  39.     {   perror(error);  \
  40.     exit(EXIT_FAILURE); }    
  41.  
  42. typedef int integer;
  43. typedef char string;
  44. typedef struct class class_t;
  45.  
  46. /*
  47.  * Custom Functions
  48.  * print: Custom print.
  49.  */  
  50. #define PRINT_(...) fprintf(stderr, __VA_ARGS__)
  51. #define print(buffer, ...) PRINT_ (buffer, __VA_ARGS__)
  52.  
  53. /* Data Input */
  54. int
  55. input (int, void *, unsigned int); 
  56.  
  57. /* Data Output */
  58. void
  59. printdt (int, ...);
  60.  
  61. #endif
  62. // ------------ //
  63.  
  64. //class.h
  65. #define PACKAGE
  66. #include "oriented_c.h"
  67.  
  68. // :: Classes :: //
  69. struct class {
  70.     // Data
  71.     string  name[16];
  72.     integer     age;
  73.    
  74.     // Methods
  75.     integer     (*get_age) (class_t*);
  76.     void    (*set_age) (class_t*, integer);
  77. };
  78.  
  79. /* Function implements */
  80. integer get(class_t *obj) {
  81.     return obj->age;       
  82. }
  83.  
  84. void set(class_t *obj, integer value) {
  85.     obj->age = value;
  86. }
  87.  
  88. /* Constructor Method */
  89. void init (class_t *obj, integer value, string* name)
  90. {
  91.     /* Initializing data
  92.      * from the object */
  93.     send_string(name, obj->name);
  94.     obj->age = value;
  95.    
  96.     /* Assignment function address */  
  97.     obj->get_age = get;
  98.     obj->set_age = set;
  99. }
  100. // ------------ //
  101.  
  102. // test.c
  103. #include "class.h"
  104.  
  105. /*  ::RESULT::
  106.     bash-4.1$ ./test
  107.     rafa
  108.     22
  109.     Test[0]: Name: rafa | Age: 22
  110.     Test[1]: Name: Raphael | Age: 23
  111.  */
  112.  
  113. main()
  114. {  
  115.     class_t test[2];  
  116.     string name[16];
  117.     integer age;
  118.  
  119.     printdt(STR, "Input your name:\n");
  120.     input(STR, name, 15);
  121.     printdt(STR, "Input your age: ");
  122.     input(INT, &age, 0);
  123.  
  124.     // Calling constructor method */
  125.     init(&test[0], age, name); // Create an object to test[0].
  126.     init(&test[1], age, name); // Create an object to test[1].
  127.    
  128.     test[1].set_age(&test[1], 23);
  129.     send_string("Raphael", test[1].name);      
  130.  
  131.     print("Test[%d]: Name: %s | Age: %d\n", 0, test[0].name, test[0].age);
  132.     print("Test[%d]: Name: %s | Age: %d\n", 1, test[1].name, test[1].age);
  133.  
  134.     exit(0);
  135. }
  136. // ------------ //
  137.  
  138. // oriented_c.c
  139. #define PACKAGE
  140. #include "oriented_c.h"
  141.  
  142. int
  143. input (int data, void *addr, size_t size)              
  144. {
  145.     signed char c;
  146.     int i;
  147.    
  148.     if(data == STR) {
  149.     char *buffer = (char *) addr;
  150.    
  151.     for(i = 0; i < (size-1) && (c = getchar()) != EOF
  152.     && c != '\n'; i++)
  153.         buffer[i] = c;     
  154.  
  155.     buffer[i] = '\0';  
  156.    
  157.     } else if (data == INT) {
  158.     int *addr_int = (int *) addr;
  159.     i = scanf("%d", addr_int); 
  160.    
  161.     } else if (data == FLOAT) {
  162.     float *addr_float = (float *) addr;
  163.     i = scanf("%f", addr_float);     
  164.    
  165.     } else
  166.     ERROR("Invalid arguments");
  167.    
  168.     return i;
  169. }
  170.  
  171. void
  172. printdt (int data, ...)
  173. {
  174.     va_list args;
  175.     va_start (args, data);
  176.     char buffer[128];
  177.  
  178.     switch (data) {
  179.     case STR:
  180.         vsprintf (buffer, "%s", args);
  181.         break;     
  182.     case INT:
  183.         vsprintf (buffer, "%d", args);
  184.         break;
  185.        
  186.     case FLOAT:
  187.         vsprintf (buffer, "%f", args);
  188.         break;     
  189.     default:
  190.         ERROR("print: Invalid arguments");  break;
  191.     }
  192.     fprintf (stdout, "%s", buffer);
  193.     va_end ( args );
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement