summaryrefslogtreecommitdiff
path: root/TAO/tests/Demux_Test/CodeGen/gen.cpp
blob: ecc4fd5ce5afac0cd08b914fb162c0d6781efc94 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "tao.h"

static long GenDistinctString(ACE_Unbounded_Queue<ACE_CString> &, int);
static long GenRand(int, int);
static long IsPresent(ACE_Unbounded_Queue<ACE_CString> &, int, ACE_CString &);
static ACE_CString	CreateString(long);
static char	GetVarChar(int);

// object table generator
long GenObjectTable(ACE_Unbounded_Queue<ACE_CString> &arr, int limit)
{
  int i,j,k;	// indices
  long	rnd;	// random number
  int opt;
  fstream arrfp;
  ACE_CString *str;


  // Generate "limit" distinct strings
  for (i=0; i < limit; i++)
    {
      if (GenDistinctString(arr, i) == -1) // side effect: inserts a new string
	 return -1;                        // at locn i in array "arr" 
    }
	
  // write the strings into a file whose format will be understood by GPERF
  arrfp.open("gperf_objects.dat",ios::out);
  
  arrfp << "struct object_db {" << endl;
  arrfp << "\tchar *name; // name of method" << endl;
  arrfp << "\tCORBA_Object_ptr obj; //fn pointer to obj impl" << endl;
  arrfp << "};" << endl;
  arrfp << "%%" << endl;

  for(i=0; i < limit; i++)
    {
      if (arr.get(str, i) == -1)
	{
	  // error
	  return -1;
	}
      arrfp << str->rep() << ", " << "0" << endl;
      // we do not know the addresses of the objects. We shall insert them
      // later. 
    }
  arrfp.close();
  return 0;
}

// method table generator
long GenMethodTable(ACE_Unbounded_Queue<ACE_CString> &arr, int limit)
{
  int i,j,k;	// indices
  long	rnd;	// random number
  int opt;
  fstream arrfp;
  ACE_CString *str;


  // Generate "limit" distinct strings
  for (i=0; i < limit; i++){
    if (GenDistinctString(arr, i) == -1) // side effect: inserts a new string at locn i
	return -1;			 // in array "arr" 
  }
	
  // write the strings into a file whose format will be understood by GPERF
  arrfp.open("gperf_methods.dat",ios::out);
  
  arrfp << "struct method_db {" << endl;
  arrfp << "\tchar *name; // name of method" << endl;
  arrfp << "\tTAO_Skeleton skel_ptr; //fn pointer to skeleton" << endl;
  arrfp << "};" << endl;
  arrfp << "%%" << endl;

  for(i=0; i < limit; i++)
    {
      if (arr.get(str, i) == -1)
	{
	  // error
	  return -1;
	}
      arrfp << (str->rep()) << ", " << "_skel_tao_demux::" <<  str->rep() <<
	"_skel" << endl;
    }
  arrfp.close();
  return 0;
}


// generate a distinct string and insert it at location "limit"
long GenDistinctString(ACE_Unbounded_Queue<ACE_CString> &arr, int limit)
{
  int i, j;	// indices
  long rnd;
  ACE_CString s;
  long status;

  rnd = GenRand(3,32); // get a random number between 3 and 32 which will be
  // used as a string length of the distinct string to be
  // generated. We use 3-32 because certain function
  // names such as _N, _C cause name conflicts.
  // Continue generating strings until a distinct one not generated before
  // is formed
  do {
    s = CreateString(rnd);
    status = IsPresent (arr, limit, s);
    if (status == -1)
      return -1;
  } while (status > 0);

  // save it at this location
  if (arr.enqueue_tail(s) == -1)
    {
      return -1;
    }  
  return 0;
}

// generate a random number in the given limits
long GenRand(int low, int up)
{
  return (lrand48() % (up-low+1)) + low; // rnd will be the size of the 
}

// check if the given string is present in the array
long IsPresent(ACE_Unbounded_Queue<ACE_CString> &arr, int limit, ACE_CString &s)
{
  ACE_CString *str;

  if (!strcasecmp(s.rep(), "_is_a"))
    return 1;

  for (int i=0; i < limit; i++){
    if (arr.get(str, i) == -1)
      {
	return -1;
      }
    if (!strcasecmp(str->rep(), s.rep()))
      return 1;  // they are same
  }
  return 0; 	// not present
}

// create a string of length = len
ACE_CString	CreateString(long len)
{
  int i;
  ACE_CString s;
  char ch;

  for (i=0; i < len; i++){
    ch = GetVarChar(i);
    s += ACE_CString(&ch);
  }
  return s;
}

// Generate a valid character for a C++ variable
char GetVarChar(int i)
{
  // for i == 0, we cannot return a digit as the first character
  char ch;
  while(1) {
    ch = GenRand(48, 122);
    if (isdigit(ch) || isupper(ch) || islower(ch) || (ch == '_')){
      if ((i == 0) && (isdigit(ch) || (ch == '_')))
	continue;
      break;	//otherwise
    }
  }
  return ch;
}