1 /*****************************************************************************
2  *
3  * Copyright (c) 2008-2010, CoreCodec, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of CoreCodec, Inc. nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CoreCodec, Inc. ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL CoreCodec, Inc. BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  ****************************************************************************/
29 
30 #ifndef __NODETOOLS_H
31 #define __NODETOOLS_H
32 
33 //some helper functions
34 
tcsdup(const tchar_t * s)35 static INLINE tchar_t* tcsdup(const tchar_t* s)
36 {
37 	size_t n = tcslen(s)+1;
38 	tchar_t* p = (tchar_t*)malloc(n*sizeof(tchar_t));
39 	if (p) tcscpy_s(p,n,s);
40 	return p;
41 }
42 
EqInt(const int * a,const int * b)43 static INLINE bool_t EqInt(const int* a, const int* b) { return *a == *b; }
EqTick(const tick_t * a,const tick_t * b)44 static INLINE bool_t EqTick(const tick_t* a, const tick_t* b) { return *a == *b; }
EqBool(const bool_t * a,const bool_t * b)45 static INLINE bool_t EqBool(const bool_t* a, const bool_t* b) { return *a == *b; }
EqPtr(void ** a,void ** b)46 static INLINE bool_t EqPtr(void** a, void** b) { return *a == *b; }
EqFrac(const cc_fraction * a,const cc_fraction * b)47 static INLINE bool_t EqFrac(const cc_fraction* a, const cc_fraction* b)
48 {
49 	if (a->Den == b->Den && a->Num == b->Num)
50 		return 1;
51 	if (!a->Den) return b->Den==0;
52 	if (!b->Den) return 0;
53 	return (int64_t)b->Den * a->Num == (int64_t)a->Den * b->Num;
54 }
55 
56 // variable names: Result, Data, Size
57 
58 #define GETVALUE(Value,Type)								\
59 		{													\
60 			assert(Size == sizeof(Type));					\
61 			*(Type*)Data = Value;							\
62 			Result = ERR_NONE;								\
63 		}
64 
65 #define GETVALUECOND(Value,Type,Cond)						\
66 		{													\
67 			assert(Size == sizeof(Type));					\
68 			if (Cond)										\
69 			{												\
70 				*(Type*)Data = Value;						\
71 				Result = ERR_NONE;							\
72 			}												\
73 		}
74 
75 #define SETVALUE(Value,Type,Update)							\
76 		{													\
77 			assert(Size == sizeof(Type));					\
78 			Value = *(Type*)Data;							\
79 			Result = Update;								\
80 		}
81 
82 #define SETVALUENULL(Value,Type,Update,Null)				\
83 		{													\
84 			if (Data)										\
85 			{												\
86 				assert(Size == sizeof(Type));				\
87 				Value = *(Type*)Data;						\
88 			}												\
89 			else											\
90 				Null;										\
91 			Result = Update;								\
92 		}
93 
94 #define SETVALUECOND(Value,Type,Update,Cond)				\
95 		{													\
96 			assert(Size == sizeof(Type));					\
97 			if (Cond)										\
98 			{												\
99 				Value = *(Type*)Data;						\
100 				Result = Update;							\
101 			}												\
102 		}
103 
104 #define SETVALUECMP(Value,Type,Update,EqFunc)				\
105 		{													\
106 			assert(Size == sizeof(Type));					\
107 			Result = ERR_NONE;								\
108 			if (!EqFunc(&Value,(Type*)Data))				\
109 			{												\
110 				Value = *(Type*)Data;						\
111 				Result = Update;							\
112 			}												\
113 		}
114 
115 #define SETPACKETFORMAT(Value,Type,Update)					\
116 		{													\
117 			assert(Size == sizeof(Type) || !Data);			\
118 			Result = PacketFormatCopy(&Value,(Type*)Data);	\
119 			if (Result == ERR_NONE)							\
120 				Result = Update;							\
121 		}
122 
123 #define SETPACKETFORMATCMP(Value,Type,Update)				\
124 		{													\
125 			assert(Size == sizeof(Type) || !Data);			\
126 			Result = ERR_NONE;								\
127 			if (!EqPacketFormat(&Value,(Type*)Data))		\
128 			{												\
129 				Result = PacketFormatCopy(&Value,(Type*)Data);\
130 				if (Result == ERR_NONE)						\
131 					Result = Update;						\
132 			}												\
133 		}
134 
ScaleTick(int64_t v,int64_t Num,int64_t Den)135 static INLINE tick_t ScaleTick(int64_t v, int64_t Num, int64_t Den)
136 {
137     return (tick_t)Scale64(v,Num,Den);
138 }
139 
ScalePos(int64_t v,int64_t Num,int64_t Den)140 static INLINE filepos_t ScalePos(int64_t v, int64_t Num, int64_t Den)
141 {
142     return (filepos_t)Scale64(v,Num,Den);
143 }
144 
145 NODE_DLL int ScaleRound(int_fast32_t v,int_fast32_t Num,int_fast32_t Den);
146 
147 #endif
148