oi
[asdcplib.git] / src / Dict.cpp
1 /*
2 Copyright (c) 2006-2009, 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    Dict.cpp
28   \version $Id$
29   \brief   MXF dictionary
30 */
31
32
33 #include "KM_mutex.h"
34 #include "KLV.h"
35 #include "MDD.cpp"
36
37 //------------------------------------------------------------------------------------------
38
39 //static ASDCP::Dictionary s_SMPTEDict;
40 //static ASDCP::Dictionary s_InteropDict;
41
42 const ASDCP::Dictionary&
43 ASDCP::DefaultSMPTEDict() {
44   // return s_SMPTEDict;
45   return DefaultCompositeDict();
46 }
47
48 const ASDCP::Dictionary&
49 ASDCP::DefaultInteropDict() {
50   // return s_InteropDict;
51   return DefaultCompositeDict();
52 }
53
54 //
55 //
56 static ASDCP::Dictionary s_CompositeDict;
57 static Kumu::Mutex s_Lock;
58 static bool s_md_init = false;
59
60 //
61 const ASDCP::Dictionary&
62 ASDCP::DefaultCompositeDict()
63 {
64   if ( ! s_md_init )
65     {
66       Kumu::AutoMutex AL(s_Lock);
67
68       if ( ! s_md_init )
69         {
70           for ( ui32_t x = 0; x < ASDCP::MDD_Table_size; x++ )
71             s_CompositeDict.AddEntry(s_MDD_Table[x], x);
72           //        s_md_lookup.insert(std::map<UL, ui32_t>::value_type(UL(s_MDD_Table[x].ul), x));
73
74           s_md_init = true;
75         }
76     }
77
78   return s_CompositeDict;
79 }
80
81 //------------------------------------------------------------------------------------------
82 //
83
84 ASDCP::Dictionary::Dictionary() {}
85 ASDCP::Dictionary::~Dictionary() {}
86
87
88 //
89 bool
90 ASDCP::Dictionary::AddEntry(const MDDEntry& Entry, ui32_t index)
91 {
92   m_MDD_Table[index] = Entry;
93   m_md_lookup.insert(std::map<UL, ui32_t>::value_type(UL(Entry.ul), index));
94   return true;
95 }
96
97 //
98 const ASDCP::MDDEntry&
99 ASDCP::Dictionary::Type(MDD_t type_id) const
100 {
101   return m_MDD_Table[type_id];
102 }
103
104 //
105 const ASDCP::MDDEntry*
106 ASDCP::Dictionary::FindUL(const byte_t* ul_buf) const
107 {
108   std::map<UL, ui32_t>::const_iterator i = m_md_lookup.find(UL(ul_buf));
109   
110   if ( i == m_md_lookup.end() )
111     {
112       byte_t tmp_ul[SMPTE_UL_LENGTH];
113       memcpy(tmp_ul, ul_buf, SMPTE_UL_LENGTH);
114       tmp_ul[SMPTE_UL_LENGTH-1] = 0;
115
116       i = m_md_lookup.find(UL(tmp_ul));
117
118       if ( i == m_md_lookup.end() )
119         return 0;
120     }
121
122   return &m_MDD_Table[(*i).second];
123 }
124
125 //
126 void
127 ASDCP::Dictionary::Dump(FILE* stream) const
128 {
129   if ( stream == 0 )
130     stream = stderr;
131
132   MDD_t di = (MDD_t)0;
133   char     str_buf[64];
134
135   while ( di < MDD_Max )
136     {
137       MDDEntry TmpType = m_MDD_Table[di];
138       UL TmpUL(TmpType.ul);
139       fprintf(stream, "%s: %s\n", TmpUL.EncodeString(str_buf, 64), TmpType.name);
140       di = (MDD_t)(di + 1);
141     }
142 }
143
144 //
145 // end Dict.cpp
146 //