File: cpr.cpp

package info (click to toggle)
ucommon 5.2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,156 kB
  • sloc: cpp: 41,584; sh: 11,440; ansic: 511; makefile: 210
file content (185 lines) | stat: -rw-r--r-- 4,247 bytes parent folder | download
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
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.

#include <config.h>
#include <ucommon/export.h>
#include <ucommon/cpr.h>

#if !defined(NEW_STDCPP) && !defined(OLD_STDCPP)
#undef  HAVE_STDEXCEPT
#endif

#ifdef  HAVE_STDEXCEPT
#include <stdexcept>
#endif

#include <ucommon/export.h>
#include <ucommon/string.h>
#include <ucommon/socket.h>
#include <errno.h>
#include <stdarg.h>

#include <ctype.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <limits.h>

using namespace UCOMMON_NAMESPACE;

#ifdef  _MSWINDOWS_
int cpr_setenv(const char *sym, const char *val, int flag)
{
    char buf[128];

    if(!flag) {
        if(GetEnvironmentVariable(sym, buf, sizeof(buf)) > 0)
            return 0;
        if(GetLastError() != ERROR_ENVVAR_NOT_FOUND)
            return -1;
    }
    if(SetEnvironmentVariable(sym, val) == 0)
        return -1;
    return 0;
}

#endif

void cpr_runtime_error(const char *str)
{
    assert(str != NULL);

#ifdef  HAVE_STDEXCEPT
    throw std::runtime_error(str);
#endif
    abort();
}

// just become we need to get binary types in a specific binary endian order.

extern "C" uint16_t lsb_getshort(uint8_t *b)
{
    assert(b != NULL);
    return (b[1] * 256) + b[0];
}

extern "C" uint32_t lsb_getlong(uint8_t *b)
{
    assert(b != NULL);
    return (b[3] * 16777216l) + (b[2] * 65536l) + (b[1] * 256) + b[0];
}

extern "C" uint16_t msb_getshort(uint8_t *b)
{
    assert(b != NULL);
    return (b[0] * 256) + b[1];
}

extern "C" uint32_t msb_getlong(uint8_t *b)
{
    assert(b != NULL);
    return (b[0] * 16777216l) + (b[1] * 65536l) + (b[2] * 256) + b[3];
}

extern "C" void lsb_setshort(uint8_t *b, uint16_t v)
{
    assert(b != NULL);
    b[0] = v & 0x0ff;
    b[1] = (v / 256) & 0xff;
}

extern "C" void msb_setshort(uint8_t *b, uint16_t v)
{
    assert(b != NULL);
    b[1] = v & 0x0ff;
    b[0] = (v / 256) & 0xff;
}

// oh, and we have to be able to set them in order too...

extern "C" void lsb_setlong(uint8_t *b, uint32_t v)
{
    assert(b != NULL);
    b[0] = (uint8_t)(v & 0x0ff);
    b[1] = (uint8_t)((v / 256) & 0xff);
    b[2] = (uint8_t)((v / 65536l) & 0xff);
    b[3] = (uint8_t)((v / 16777216l) & 0xff);
}

extern "C" void msb_setlong(uint8_t *b, uint32_t v)
{
    assert(b != NULL);
    b[3] = (uint8_t)(v & 0x0ff);
    b[2] = (uint8_t)((v / 256) & 0xff);
    b[1] = (uint8_t)((v / 65536l) & 0xff);
    b[0] = (uint8_t)((v / 16777216l) & 0xff);
}

extern "C" void cpr_memswap(void *s1, void *s2, size_t size)
{
    assert(s1 != NULL);
    assert(s2 != NULL);
    assert(size > 0);

    char *buf = new char[size];
    memcpy(buf, s1, size);
    memcpy(s1, s2, size);
    memcpy(s2, buf, size);
    delete[] buf;
}

// if malloc ever fails, we probably should consider that a critical error and
// kill the leaky dingy, which this does for us here..

extern "C" void *cpr_memalloc(size_t size)
{
    void *mem;

    if(!size)
        ++size;

    mem = malloc(size);
    crit(mem != NULL, "memory alloc failed");
    return mem;
}

extern "C" void *cpr_memassign(size_t size, caddr_t addr, size_t max)
{
    assert(addr);
    crit((size <= max), "memory assign failed");
    return addr;
}

#ifdef  __GNUC__

// here we have one of those magic things in gcc, and what to do when
// we have an unimplemented virtual function if we build ucommon without
// a stdc++ runtime library.

extern "C" void __cxa_pure_virtual(void)
{
    abort();
}

#endif

// vim: set ts=4 sw=4:
// Local Variables:
// c-basic-offset: 4
// tab-width: 4
// End: