00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <stdio.h>
00037 #include "gen_util.h"
00038
00039 void Emit_Header (FILE *hfile,
00040 const char *name,
00041 const char * const *interface_desc)
00042 {
00043 int i;
00044
00045 if (interface_desc) {
00046 for (i = 0; interface_desc[i] != NULL; ++i) {
00047 fprintf(hfile, "%s\n", interface_desc[i]);
00048 }
00049 }
00050
00051 fprintf(hfile, "\n#ifndef %s_INCLUDED\n", name);
00052 fprintf(hfile, "#define %s_INCLUDED\n", name);
00053
00054 fprintf (hfile, "#ifdef __cplusplus\n"
00055 "extern \"C\" {\n"
00056 "#endif\n\n");
00057
00058
00059
00060
00061 fprintf (hfile, "#ifndef defs_INCLUDED\n"
00062 "#define defs_INCLUDED\n"
00063 "typedef signed int INT;\n"
00064 "typedef signed int INT32;\n"
00065 "typedef signed long long INT64;\n"
00066 "typedef signed char mINT8;\n"
00067 "typedef signed short mINT16;\n"
00068 "typedef signed int mINT32;\n"
00069 "typedef signed long long mINT64;\n"
00070 "typedef unsigned int UINT;\n"
00071 "typedef unsigned int UINT32;\n"
00072 "typedef unsigned long long UINT64;\n"
00073 "typedef unsigned char mUINT8;\n"
00074 "typedef unsigned short mUINT16;\n"
00075 "typedef unsigned int mUINT32;\n"
00076 "typedef unsigned long long mUINT64;\n"
00077 "typedef int BOOL;\n"
00078 "typedef unsigned char mBOOL;\n"
00079 "#ifndef TRUE\n"
00080 "#define TRUE ((BOOL) 1)\n"
00081 "#endif\n"
00082 "#ifndef FALSE\n"
00083 "#define FALSE ((BOOL) 0)\n"
00084 "#endif\n"
00085 "#if (defined(_LANGUAGE_C) || defined(__GNUC__)) && !defined(inline)\n"
00086 "#define inline static __inline\n"
00087 "#endif\n"
00088 "#endif\n\n");
00089 }
00090
00091 void Emit_Footer (FILE *hfile)
00092 {
00093 fprintf (hfile, "\n#ifdef __cplusplus\n"
00094 "}\n"
00095 "#endif\n"
00096 "#endif\n");
00097 }
00098
00099 typedef enum {
00100 DK_MACRO
00101 } DEFINITION_KIND;
00102
00103 typedef struct definition {
00104 DEFINITION_KIND kind;
00105 const char *name;
00106 const char *s;
00107 struct definition *next;
00108 } DEFINITION;
00109
00110 static DEFINITION *defs;
00111 static DEFINITION *lastdef;
00112
00113 void Define_Macro (const char *name, const char *def)
00114 {
00115 DEFINITION *newdef = new DEFINITION;
00116 newdef->kind = DK_MACRO;
00117 newdef->name = name;
00118 newdef->s = def;
00119 newdef->next = NULL;
00120 if (defs == NULL) {
00121 defs = newdef;
00122 } else {
00123 lastdef->next = newdef;
00124 }
00125 lastdef = newdef;
00126 }
00127
00128 void Emit_Definitions (FILE *hfile, const char *prefix)
00129 {
00130 DEFINITION *def;
00131
00132 if (defs != NULL) fprintf(hfile, "\n");
00133
00134 for (def = defs; def != NULL; def = def->next) {
00135 int c;
00136 int pos;
00137 const char *s = def->s;
00138 pos = fprintf(hfile, "#define %s%s ", prefix, def->name);
00139 while (pos++ < 40) fputc(' ', hfile);
00140 fprintf(hfile, "(\"");
00141 while (c = *s++) {
00142 if (c == '\\') {
00143 fprintf(hfile, "\\\\");
00144 } else if (c < ' ') {
00145 switch (c) {
00146 case '\n':
00147 fprintf(hfile, "\\n");
00148 break;
00149 case '\t':
00150 fprintf(hfile, "\\t");
00151 break;
00152 default:
00153 fprintf(hfile, "\\%03o", c);
00154 break;
00155 }
00156 } else {
00157 fputc(c, hfile);
00158 }
00159 }
00160 fprintf(hfile, "\")\n");
00161 }
00162 }