source: roaraudio/libroarpulse/util.c @ 3515:9570578e59c2

Last change on this file since 3515:9570578e59c2 was 3515:9570578e59c2, checked in by phi, 14 years ago

implemented pa_hexstr() and pa_parsehex()

File size: 4.0 KB
Line 
1//util.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *  The code (may) include prototypes and comments (and maybe
6 *  other code fragements) from libpulse*. They are mostly copyrighted by:
7 *  Lennart Poettering <poettering@users.sourceforge.net> and
8 *  Pierre Ossman <drzeus@drzeus.cx>
9 *
10 *  This file is part of libroarpulse a part of RoarAudio,
11 *  a cross-platform sound system for both, home and professional use.
12 *  See README for details.
13 *
14 *  This file is free software; you can redistribute it and/or modify
15 *  it under the terms of the GNU General Public License version 3
16 *  as published by the Free Software Foundation.
17 *
18 *  RoarAudio is distributed in the hope that it will be useful,
19 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 *  GNU General Public License for more details.
22 *
23 *  You should have received a copy of the GNU General Public License
24 *  along with this software; see the file COPYING.  If not, write to
25 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 *  NOTE for everyone want's to change something and send patches:
28 *  read README and HACKING! There a addition information on
29 *  the license of this document you need to read before you send
30 *  any patches.
31 *
32 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
33 *  or libpulse*:
34 *  The libs libroaresd, libroararts and libroarpulse link this libroar
35 *  and are therefore GPL. Because of this it may be illigal to use
36 *  them with any software that uses libesd, libartsc or libpulse*.
37 */
38
39#include <libroarpulse/libroarpulse.h>
40
41/** Return the binary file name of the current process. This is not
42 * supported on all architectures, in which case NULL is returned. */
43char *pa_get_binary_name(char *s, size_t l) {
44#ifdef __linux__
45 int ret;
46 char path[PATH_MAX];
47
48 if ( (ret = readlink("/proc/self/exe", path, PATH_MAX-1)) != -1 ) {
49  path[ret] = 0;
50  return strncpy(s, pa_path_get_filename(path), l);
51 }
52#endif
53
54 return NULL;
55}
56
57/** Return a pointer to the filename inside a path (which is the last
58 * component). */
59// some versions declare this as const char * f(...)
60// and newer(?) versions as char * f(...)...
61ROAR_HAVE_TYPE_PA_PATH_GET_FILENAME pa_path_get_filename(const char *p) {
62 char * r;
63
64 if ( (r = strrchr(p, '/')) ) {
65  return (const char *) r+1;
66 } else {
67  return p;
68 }
69
70}
71
72/** Return the current username in the specified string buffer. */
73char *pa_get_user_name(char *s, size_t l);
74
75/** Return the current hostname in the specified buffer. */
76char *pa_get_host_name(char *s, size_t l);
77
78/** Return the fully qualified domain name in s */
79char *pa_get_fqdn(char *s, size_t l);
80
81/** Return the home directory of the current user */
82char *pa_get_home_dir(char *s, size_t l) {
83 char * home = getenv("HOME");
84
85 if ( home == NULL )
86  return NULL;
87
88 strncpy(s, home, l);
89
90 s[l-1] = 0;
91
92 return s;
93}
94
95/** Wait t milliseconds */
96int pa_msleep(unsigned long t) {
97#ifdef ROAR_TARGET_WIN32
98 Sleep(t);
99#else
100 usleep(1000*t);
101#endif
102 return 0;
103}
104
105/* Format the specified data as a hexademical string */
106char *pa_hexstr(const uint8_t* d, size_t dlength, char *s, size_t slength) {
107 const char table[] = "0123456789abcdef";
108 char * p = s;
109 size_t i;
110
111 for (i = 0; i < dlength && slength > 2; i++, slength -= 2, d++) {
112  *(p++) = table[*d >>    4];
113  *(p++) = table[*d &  0x0F];
114 }
115
116 *p = 0;
117
118 return s;
119}
120
121/* Parse a hexadecimal string as created by pa_hexstr() to a BLOB */
122size_t pa_parsehex(const char *p, uint8_t *d, size_t dlength) {
123 size_t have = 0;
124 register int low_nibble = 0;
125 register char c;
126
127 if ( dlength == 0 )
128  return 0;
129
130 for (; (c = *p); p++) {
131  if ( c >= '0' && c <= '9' ) {
132   c -= '0';
133  } else if ( c >= 'a' && c <= 'f' ) {
134   c -= 'a' - 10;
135  } else if ( c >= 'A' && c <= 'F' ) {
136   c -= 'A' - 10;
137  } else {
138   return (size_t)-1;
139  }
140
141  if ( low_nibble ) {
142   *d |= c & 0x0F;
143   low_nibble = 0;
144
145   d++;
146   have++;
147   dlength--;
148
149   if ( dlength == 0 )
150    return have;
151  } else {
152   *d = (c & 0x0F) << 4;
153   low_nibble = 1;
154  }
155 }
156
157 return have;
158}
159
160
161//ll
Note: See TracBrowser for help on using the repository browser.