Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / object / random_gen.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 // $XConsortium: random_gen.C /main/6 1996/08/21 15:56:48 drk $
24
25 #include <sys/time.h>
26 #include <sys/times.h>
27 #include "object/random_gen.h"
28
29
30 random_gen::~random_gen()
31 {
32 }
33
34 random_gen::random_gen()
35 {
36 #ifdef __uxp__
37    int seed;
38    struct tms tp;
39    if ((seed = (int)times(&tp)) < 0)
40       seed = 19;
41 #else
42    struct timeval tp;
43    struct timezone tzp;
44
45    int seed = ( gettimeofday(&tp, &tzp) == 0 ) ? int(tp.tv_sec) : 19;
46 #endif
47
48 #ifdef CONTROLLED_SEED
49 if ( getenv("SEED") )
50    seed = atoi(getenv("SEED"));
51
52 cerr << "seed=" << seed << "\n";
53 #endif
54
55    rand_gen.seed(seed);
56 }
57    
58 static char char_set[] =
59    { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
60     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
61     'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
62     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
63     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
64     ' ', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '.', '_', 
65     '+', '|', '=', '\\', '~'
66    };
67      
68 void random_gen::random_string(ostream& out, int len)
69 {
70    out << len << "\t";
71    for ( int i=0; i<len; i++ )
72      out << char_set[rand_gen.rand() % sizeof(char_set)]; 
73
74    out << "\n";
75 }
76
77 static char* buf = 0;
78 static int buf_len = 0;
79
80 const char* random_gen::get_random_string(int low, int high)
81 {
82    int x = pick_a_value(low, high);
83  
84    if ( buf_len < x ) {
85       buf_len = x;
86       delete buf;
87       buf = new char[buf_len+1];
88    }
89    int i;
90    for ( i=0; i<buf_len; i++ )
91      buf[i] = char_set[rand_gen.rand() % sizeof(char_set)]; 
92
93    buf[i] = 0;
94    
95    return buf;
96 }
97
98 int random_gen::pick_a_value(int low, int high)
99 {
100    return ( low < high ) ? rand_gen.rand() % (high - low) + low : low;
101 }
102
103 void random_gen::_random_string(ostream& out, int low, int high)
104 {
105    int l = pick_a_value(low, high);
106    random_string(out, l);
107 }
108
109 void random_gen::random_string(ostream& out, int low, int high, Boolean x)
110 {
111    if ( x == true )
112       out << "6\n";
113    _random_string(out, low, high);
114 }
115
116 void 
117 random_gen::random_string_to_be_compressed(ostream& out, int l, int h, const oid_t& id)
118 {
119    out << "11\n";
120    out << id << "\n";
121    _random_string(out, l, h);
122 }
123
124 void random_gen::random_oid(ostream& out, int class_code, int instance_code)
125 {
126    out << "7\n";
127    out << class_code << "." << instance_code << "\n";
128 }
129
130 void random_gen::random_integer(ostream& out, int value)
131 {
132    out << "4\n";
133    out << value << "\n";
134 }