Fix Object ref not being written: this prevented GenericStreamTextBasedSet to be...
[asdcplib.git] / src / AS_DCP.cpp
1 /*
2 Copyright (c) 2004-2014, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. 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 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    AS_DCP.cpp
28     \version $Id$       
29     \brief   AS-DCP library, misc classes and subroutines
30 */
31
32 #include "AS_DCP_internal.h"
33 #include <assert.h>
34
35 const char*
36 ASDCP::Version()
37 {
38   return PACKAGE_VERSION;
39 }
40
41
42
43 //------------------------------------------------------------------------------------------
44 //
45
46 // Encodes a rational number as a string having a single delimiter character between
47 // numerator and denominator.  Retuns the buffer pointer to allow convenient in-line use.
48 const char*
49 ASDCP::EncodeRational(const Rational& rational, char* str_buf, ui32_t buf_len, char delimiter)
50 {
51   assert(str_buf);
52   snprintf(str_buf, buf_len, "%u%c%u", rational.Numerator, delimiter, rational.Denominator);
53   return str_buf;
54 }
55
56 // Decodes a rational number havng a single non-digit delimiter character between
57 // the numerator and denominator.  Returns false if the string does not contain
58 // the expected syntax.
59 bool
60 ASDCP::DecodeRational(const char* str_rational, Rational& rational)
61 {
62   assert(str_rational);
63   rational.Numerator = strtol(str_rational, 0, 10);
64
65   const char* p = str_rational;
66   while ( *p && isdigit(*p) )
67     {
68       ++p;
69     }
70
71   if ( p[0] == 0 || p[1] == 0 )
72     {
73       return false;
74     }
75
76   ++p;
77   rational.Denominator = strtol(p, 0, 10);
78   return  true;
79 }
80
81
82 //------------------------------------------------------------------------------------------
83 //
84 // frame buffer base class implementation
85
86 ASDCP::FrameBuffer::FrameBuffer() :
87   m_Data(0), m_Capacity(0), m_OwnMem(false), m_Size(0),
88   m_FrameNumber(0), m_SourceLength(0), m_PlaintextOffset(0)
89 {
90 }
91
92 ASDCP::FrameBuffer::~FrameBuffer()
93 {
94   if ( m_OwnMem && m_Data != 0 )
95     free(m_Data);
96 }
97
98 // Instructs the object to use an externally allocated buffer. The external
99 // buffer will not be cleaned up by the frame buffer when it is destroyed.
100 // Call with (0,0) to revert to internally allocated buffer.
101 // Returns error if the buf_addr argument is NULL and either buf_size is
102 // non-zero or internally allocated memory is in use.
103 ASDCP::Result_t
104 ASDCP::FrameBuffer::SetData(byte_t* buf_addr, ui32_t buf_size)
105 {
106   // if buf_addr is null and we have an external memory reference,
107   // drop the reference and place the object in the initialized-
108   // but-no-buffer-allocated state
109   if ( buf_addr == 0 )
110     {
111       if ( buf_size > 0 || m_OwnMem )
112         return RESULT_PTR;
113
114       m_OwnMem = false;
115       m_Capacity = m_Size = 0;
116       m_Data = 0;
117       return RESULT_OK;
118     }
119
120   if ( m_OwnMem && m_Data != 0 )
121     free(m_Data);
122
123   m_OwnMem = false;
124   m_Capacity = buf_size;
125   m_Data = buf_addr;
126   m_Size = 0;
127
128   return RESULT_OK;
129 }
130
131 // Sets the size of the internally allocate buffer. Returns RESULT_CAPEXTMEM
132 // if the object is using an externally allocated buffer via SetData();
133 // Resets content size to zero.
134 ASDCP::Result_t
135 ASDCP::FrameBuffer::Capacity(ui32_t cap_size)
136 {
137   if ( ! m_OwnMem && m_Data != 0 )
138     return RESULT_CAPEXTMEM; // cannot resize external memory
139
140   if ( m_Capacity < cap_size )
141     {
142       if ( m_Data != 0 )
143         {
144           assert(m_OwnMem);
145           free(m_Data);
146         }
147
148       m_Data = (byte_t*)malloc(cap_size);
149
150       if ( m_Data == 0 )
151         return RESULT_ALLOC;
152
153       m_Capacity = cap_size;
154       m_OwnMem = true;
155       m_Size = 0;
156     }
157
158   return RESULT_OK;
159 }
160
161
162 //
163 // end AS_DCP.cpp
164 //