source: sources/src/toolsMisc.cc @ 1500:872b0b69e0d5

Revision 1500:872b0b69e0d5, 6.4 KB checked in by niam, 12 months ago (diff)

{issue #103} added implementation of sl- and dl- lists

Line 
1/***************************************************************************
2 *            toolsMisc.cc
3 *
4 *  Wed Aug 24 2005
5 *  Copyright  2005  Dmytro Milinevskyy
6 *  milinevskyy@gmail.com
7 ****************************************************************************/
8
9/*
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU Lesser General Public License version 2.1 as published by
12 *  the Free Software Foundation;
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU Library General Public License for more details.
18 *
19 *  You should have received a copy of the GNU Lesser General Public License
20 *  along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24/**
25 * vim indentation settings
26 * set tabstop=4
27 * set shiftwidth=4
28 */
29
30#include <libdodo/directives.h>
31
32#include <sys/stat.h>
33#include <errno.h>
34#include <string.h>
35#include <stdio.h>
36#include <time.h>
37#include <stdlib.h>
38
39#include <libdodo/toolsMisc.h>
40#include <libdodo/types.h>
41#include <libdodo/toolsString.h>
42#include <libdodo/toolsMiscEx.h>
43
44using namespace dodo::tools;
45
46//-------------------------------------------------------------------
47
48void
49misc::randomBlob(void          *data,
50             unsigned long size,
51             short         strength)
52{
53    FILE *file;
54
55    if (strength == RANDOM_STRENGTH_WEAK) {
56        srand(time(NULL));
57
58        unsigned long i = 0;
59        for (;i<size;i+=sizeof(int)) {
60            ((int *)data)[i] = rand();
61        }
62        unsigned long reminder = size%sizeof(int);
63        if (reminder) {
64            size = rand();
65            memcpy((char *)data+(i-sizeof(int)), &size, reminder);
66        }
67    } else if (strength == RANDOM_STRENGTH_NORMAL) {
68        file = fopen("/dev/urandom", "r");
69        if (file == NULL)
70            dodo_throw exception::basic(exception::MODULE_TOOLSMISC, MISCEX_RANDOM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
71    } else if (strength == RANDOM_STRENGTH_STRONG) {
72            file = fopen("/dev/random", "r");
73            if (file == NULL)
74                dodo_throw exception::basic(exception::MODULE_TOOLSMISC, MISCEX_RANDOM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
75    } else {
76        dodo_throw exception::basic(exception::MODULE_TOOLSMISC, MISCEX_RANDOM, exception::ERRNO_LIBDODO, MISCEX_WRONGSTRENGTH, TOOLSMISCEX_WRONGSTRENGTH_STR, __LINE__, __FILE__);
77    }
78
79    while (true) {
80        if (fread(data, size, 1, file) == 0) {
81            if (feof(file) != 0 || errno == EAGAIN)
82                break;
83
84            if (errno == EINTR)
85                continue;
86
87            if (ferror(file) != 0)
88                dodo_throw exception::basic(exception::MODULE_TOOLSMISC, MISCEX_RANDOM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
89        }
90
91        break;
92    }
93
94    if (fclose(file) != 0)
95        dodo_throw exception::basic(exception::MODULE_TOOLSMISC, MISCEX_RANDOM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
96}
97
98//-------------------------------------------------------------------
99
100dodo::string
101misc::randomString(unsigned long size,
102                   short         strength)
103{
104    dodo::string s('\0', size);
105    char *data = (char *)s.data();
106
107    randomBlob(data, size, strength);
108
109    for (unsigned long i = 0; i < size; ++i)
110        if (data[i] == '\0')
111            data[i] = '*';
112
113    return s;
114}
115
116//-------------------------------------------------------------------
117
118bool
119misc::isInArray(const dodoStringArray &arr,
120                const dodo::string      &needle,
121                bool                  icase)
122{
123    bool (*cmpFunc)(const dodo::string &,
124                    const dodo::string &);
125
126    if (icase)
127        cmpFunc = string::iequal;
128    else
129        cmpFunc = string::equal;
130
131    dodoStringArray::const_iterator i(arr.begin()), j(arr.end());
132    for (; i != j; ++i)
133        if (cmpFunc(*i, needle))
134            return true;
135
136    return false;
137}
138
139//-------------------------------------------------------------------
140
141bool
142misc::isInList(const dodoStringList &arr,
143               const dodo::string     &needle,
144               bool                 icase)
145{
146    bool (*cmpFunc)(const dodo::string &,
147                    const dodo::string &);
148
149    if (icase)
150        cmpFunc = string::iequal;
151    else
152        cmpFunc = string::equal;
153
154    dodoStringList::iterator i(arr.begin()), j(arr.end());
155    for (; i != j; ++i)
156        if (cmpFunc(*i, needle))
157            return true;
158
159    return false;
160}
161
162//-------------------------------------------------------------------
163
164dodo::dodoStringArray
165misc::split(const dodo::string &fields,
166            const dodo::string &separator,
167            int              limit)
168{
169    unsigned long i(0), j(0), sep_size(separator.size());
170    int k(1);
171    dodoStringArray arr;
172
173    while (true) {
174        if (limit != -1) {
175            if (k > limit) {
176                arr.back() += dodo::string(fields.data() + j - sep_size);
177
178                break;
179            }
180            ++k;
181        }
182
183        i = fields.find(separator, i);
184        if (i == dodo::string::POSITION_END) {
185            arr.push_back(dodo::string(fields.data() + j, fields.size() - j));
186
187            break;
188        } else
189            arr.push_back(dodo::string(fields.data() + j, i - j));
190
191        i += sep_size;
192        j = i;
193    }
194
195    return arr;
196}
197
198//-------------------------------------------------------------------
199
200dodo::string
201misc::join(const dodoStringArray &fields,
202           const dodo::string      &separator,
203           int                   limit)
204{
205    if (fields.size() == 0)
206        dodo_throw exception::basic(exception::MODULE_TOOLSMISC, MISCEX_JOIN, exception::ERRNO_LIBDODO, MISCEX_EMPTYARRAY, TOOLSMISCEX_EMPTYARRAY_STR, __LINE__, __FILE__);
207
208    int k(0);
209
210    dodo::string temp;
211    dodoStringArray::const_iterator i(fields.begin()), j(fields.end());
212    if (i != j) {
213        --j;
214        for (; i != j; ++i) {
215            if (limit != -1) {
216                if (k > limit)
217                    return temp;
218
219                ++k;
220            }
221            temp += dodo::string(*i);
222            temp += dodo::string(separator);
223        }
224        temp += dodo::string(*i);
225    }
226
227    return temp;
228}
229
230//-------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.