File: ptypes2tcl.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 78,780 kB
  • ctags: 27,490
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (93 lines) | stat: -rw-r--r-- 2,070 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <ctype.h>
#include "packet.h"

// The following copied from ~tclcl/tcl2c++.c
/*
 * Define TCL2C_INT if your compiler has problems with long strings.
 */
#if defined(WIN32) || defined(_WIN32) || defined(__alpha__) || defined(__hpux)
#define TCL2C_INT
#endif

char** p_info::name_;
unsigned int p_info::nPkt_ = 0;
PacketClassifier *p_info::pc_ = 0;
int p_info::addPacket(char *name)
{
	if(!nPkt_)
		initName();
	
	int newID = nPkt_-1;
	PT_NTYPE = nPkt_;
	initName();
	name_[newID] = name;
	return newID;
}

void
printLine(char *s) {
#ifdef TCL2C_INT
	for (unsigned int i = 0; i < strlen(s); i++) 
		if ((i > 0) && ((i % 20) == 0))
			printf("%u,\n", s[i]);
		else
			printf("%u,", s[i]);
	printf("%u,%u,\n", '\\', '\n');
#else
	printf("%s\\n\\\n", s);
#endif
}

char *
lcase(const char *s) {
	static char charbuf[512];
	char* to = charbuf;
	while ((*to++ = tolower(*s++)))
		/* NOTHING */;
	*to = '\0';
	return charbuf;
}

int main() {
	p_info pinfo;

#ifdef TCL2C_INT
	printf("static const char code[] = {\n");
#else
	printLine("static const char code[] = \"");
#endif
	printLine("global ptype pvals");
	printLine("set ptype(error) -1");
	printLine("set pvals(-1) error");
	char strbuf[512];
	for (unsigned int i = 0; i < PT_NTYPE; i++) {
		sprintf(strbuf, "set ptype(%s) %d", lcase(pinfo.name(packet_t(i))), i);
		printLine(strbuf);
		sprintf(strbuf, "set pvals(%d) %s", i, pinfo.name(packet_t(i)));
		printLine(strbuf);
	}
	printLine("proc ptype2val {str} {");
	printLine("global ptype");
	printLine("set str [string tolower $str]");
	printLine("if ![info exists ptype($str)] {");
	printLine("set str error");
	printLine("}");
	printLine("set ptype($str)");
	printLine("}");
	printLine("proc pval2type {val} {");
	printLine("global pvals");
	printLine("if ![info exists pvals($val)] {");
	printLine("set val -1");
	printLine("}");
	printLine("set pvals($val)");
	printLine("}");
#ifdef TCL2C_INT
	printf("0 };\n");
#else
	printf("\";\n");
#endif
	printf("#include \"config.h\"\n");
	printf("EmbeddedTcl et_ns_ptypes(code);\n");
	return 0;
}