forked from rsta2/circle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p9util.cpp
122 lines (89 loc) · 1.95 KB
/
p9util.cpp
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
#include "p9util.h"
#include <circle/logger.h>
#include <circle/string.h>
#include <circle/stdarg.h>
#include <circle/debug.h>
#include <assert.h>
static const char From[] = "wlan";
int print (const char *fmt, ...)
{
va_list var;
va_start (var, fmt);
CString Msg;
Msg.FormatV (fmt, var);
va_end (var);
char Buffer[1000];
strncpy (Buffer, (const char *) Msg, sizeof Buffer);
Buffer[sizeof Buffer-1] = '\0';
char *pLine;
char *pLineEnd;
for (pLine = Buffer; (pLineEnd = strchr (pLine, '\n')) != 0; pLine = pLineEnd+1)
{
*pLineEnd = '\0';
CLogger::Get ()->Write (From, LogDebug, "%s", pLine);
}
if (*pLine != '\0')
{
CLogger::Get ()->Write (From, LogDebug, "%s", pLine);
}
return Msg.GetLength ();
}
int sprint (char *str, const char *fmt, ...)
{
va_list var;
va_start (var, fmt);
CString Msg;
Msg.FormatV (fmt, var);
va_end (var);
strcpy (str, (const char *) Msg);
return Msg.GetLength ();
}
int snprint (char *str, size_t size, const char *fmt, ...)
{
va_list var;
va_start (var, fmt);
CString Msg;
Msg.FormatV (fmt, var);
va_end (var);
strncpy (str, (const char *) Msg, size);
str[size-1] = '\0';
return Msg.GetLength () < size-1 ? Msg.GetLength () : size-1;
}
char *seprint (char *str, char *end, const char *fmt, ...)
{
va_list var;
va_start (var, fmt);
CString Msg;
Msg.FormatV (fmt, var);
va_end (var);
assert (str < end);
size_t len = end-str-1;
strncpy (str, (const char *) Msg, len);
*(end-1) = '\0';
return str + (Msg.GetLength () < len ? Msg.GetLength () : len);
}
long readstr (ulong offset, void *buf, size_t len, const void *p)
{
const char *p1 = (const char *) p;
size_t plen = strlen (p1);
if (offset >= plen)
{
return 0;
}
p1 += offset;
char *p2 = (char *) buf;
long result = 0;
while (*p1 != '\0' && len > 0)
{
*p2++ = *p1++;
result++;
len--;
}
return result;
}
void hexdump (const void *p, size_t len, const char *from)
{
#ifndef NDEBUG
debug_hexdump (p, len, from);
#endif
}