1 | /****************************************************************************** |
---|
2 | * $Id: misc.h 1621 2007-03-31 20:30:18Z joshe $ |
---|
3 | * |
---|
4 | * Copyright (c) 2007 Joshua Elsasser |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #ifndef TR_DAEMON_MISC_H |
---|
26 | #define TR_DAEMON_MISC_H |
---|
27 | |
---|
28 | #include <limits.h> |
---|
29 | |
---|
30 | #include "bsdqueue.h" |
---|
31 | |
---|
32 | #define CONF_FILE_LOCK "lock" |
---|
33 | #define CONF_FILE_SOCKET "socket" |
---|
34 | #define CONF_FILE_STATE "state" |
---|
35 | |
---|
36 | enum confpathtype |
---|
37 | { |
---|
38 | CONF_PATH_TYPE_DAEMON, |
---|
39 | CONF_PATH_TYPE_GTK, |
---|
40 | }; |
---|
41 | |
---|
42 | struct event_base; |
---|
43 | struct bufferevent; |
---|
44 | |
---|
45 | /* this doesn't seem to be in the libevent header */ |
---|
46 | int bufferevent_base_set( struct event_base *, struct bufferevent * ); |
---|
47 | |
---|
48 | #ifdef __GNUC__ |
---|
49 | # define UNUSED __attribute__((unused)) |
---|
50 | #else |
---|
51 | # define UNUSED |
---|
52 | #endif |
---|
53 | |
---|
54 | #ifdef __GNUC__ |
---|
55 | # define PRINTF( fmt, args ) __attribute__((format (printf, fmt, args))) |
---|
56 | #else |
---|
57 | # define PRINTF( fmt, args ) |
---|
58 | #endif |
---|
59 | |
---|
60 | #define ARRAYLEN( ary ) ( sizeof( ary ) / sizeof( (ary)[0] ) ) |
---|
61 | |
---|
62 | #ifndef MIN |
---|
63 | #define MIN( aa, bb ) ( (aa) < (bb) ? (aa) : (bb) ) |
---|
64 | #endif |
---|
65 | #ifndef MAX |
---|
66 | #define MAX( aa, bb ) ( (aa) > (bb) ? (aa) : (bb) ) |
---|
67 | #endif |
---|
68 | |
---|
69 | #undef NULL |
---|
70 | #define NULL ( ( void * )0 ) |
---|
71 | |
---|
72 | #define TORRENT_ID_VALID( id ) ( 0 < (id) && INT_MAX > (id) ) |
---|
73 | |
---|
74 | #define SAFEFREE( ptr ) \ |
---|
75 | do \ |
---|
76 | { \ |
---|
77 | int saved = errno; \ |
---|
78 | free( ptr ); \ |
---|
79 | errno = saved; \ |
---|
80 | } \ |
---|
81 | while( 0 ) |
---|
82 | #define SAFEFREESTRLIST( ptr ) \ |
---|
83 | do \ |
---|
84 | { \ |
---|
85 | int saved = errno; \ |
---|
86 | FREESTRLIST( ptr ); \ |
---|
87 | errno = saved; \ |
---|
88 | } \ |
---|
89 | while( 0 ) |
---|
90 | #define SAFEBENCFREE( val ) \ |
---|
91 | do \ |
---|
92 | { \ |
---|
93 | int saved = errno; \ |
---|
94 | tr_bencFree( val ); \ |
---|
95 | errno = saved; \ |
---|
96 | } \ |
---|
97 | while( 0 ) |
---|
98 | |
---|
99 | #define INTCMP_FUNC( name, type, id ) \ |
---|
100 | int \ |
---|
101 | name( struct type * _icf_first, struct type * _icf_second ) \ |
---|
102 | { \ |
---|
103 | if( _icf_first->id < _icf_second->id ) \ |
---|
104 | { \ |
---|
105 | return -1; \ |
---|
106 | } \ |
---|
107 | else if( _icf_first->id > _icf_second->id ) \ |
---|
108 | { \ |
---|
109 | return 1; \ |
---|
110 | } \ |
---|
111 | else \ |
---|
112 | { \ |
---|
113 | return 0; \ |
---|
114 | } \ |
---|
115 | } |
---|
116 | |
---|
117 | struct stritem |
---|
118 | { |
---|
119 | char * str; |
---|
120 | SLIST_ENTRY( stritem ) next; |
---|
121 | }; |
---|
122 | |
---|
123 | SLIST_HEAD( strlist, stritem ); |
---|
124 | |
---|
125 | #define FREESTRLIST( _fl_head ) \ |
---|
126 | while( !SLIST_EMPTY( _fl_head ) ) \ |
---|
127 | { \ |
---|
128 | struct stritem * _fl_dead = SLIST_FIRST( _fl_head ); \ |
---|
129 | SLIST_REMOVE_HEAD( _fl_head, next ); \ |
---|
130 | free( _fl_dead->str ); \ |
---|
131 | free( _fl_dead ); \ |
---|
132 | } |
---|
133 | |
---|
134 | void setmyname ( const char * ); |
---|
135 | const char * getmyname ( void ); |
---|
136 | void confpath ( char *, size_t, const char *, enum confpathtype ); |
---|
137 | void absolutify( char *, size_t, const char * ); |
---|
138 | |
---|
139 | #endif /* TR_DAEMON_MISC_H */ |
---|