1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     ut_ResDictionary.cpp
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #include "precompiled.h"
19 
20 #include <nw/ut/ut_ResDictionary.h>
21 
22 namespace nw {
23 namespace ut {
24 
25 
26 s32
GetIndex(const char * key) const27 ResDicLinear::GetIndex(const char* key) const
28 {
29     if ( (! this->IsValid()) || (! key) ) { return -1; }
30 
31     const ResDicLinearData::ResDicNodeData* x = ref().data;
32 
33     for ( int i = 0; i < s32(ref().numData); ++i )
34     {
35         if ( x->toName.offset != 0 && std::strcmp(key, x->toName.to_ptr()) == 0 )
36         {
37             return static_cast<s32>( i );
38         }
39         ++x;
40     }
41     return -1;
42 }
43 
44 
45 ResDicPatriciaData::ResDicNodeData*
Get(const ResName rhs) const46 ResDicPatricia::Get(const ResName rhs) const
47 {
48     size_t len = rhs.GetLength();
49     const char* s = rhs.GetName();
50     const ResDataType& r = ref();
51 
52     const ResDicPatriciaData::ResDicNodeData* p;
53     const ResDicPatriciaData::ResDicNodeData* x;
54 
55     p = &r.data[0];
56     x = &r.data[p->idxLeft];
57 
58     while(p->ref > x->ref)
59     {
60         p = x;
61         // ビットが1なら右、0なら左をたどる
62 
63         u32 wd = u32(x->ref) >> 3;
64         u32 pos = u32(x->ref) & 7;
65 
66         if (wd < len && ((s[wd] >> pos) & 1))
67         {
68             x = &r.data[x->idxRight];
69         }
70         else
71         {
72             x = &r.data[x->idxLeft];
73         }
74     }
75 
76     if (rhs == ResName(((u8*)&r + x->ofsString - sizeof(u32))))
77     {
78         return const_cast<ResDicPatriciaData::ResDicNodeData*>(x);
79     }
80     return NULL;
81 }
82 
83 ResDicPatriciaData::ResDicNodeData*
Get(const char * s,size_t len) const84 ResDicPatricia::Get(const char* s, size_t len) const
85 {
86     const ResDicPatriciaData::ResDicNodeData* p;
87     const ResDicPatriciaData::ResDicNodeData* x;
88     const ResDataType& r = this->ref();
89 
90     p = &r.data[0];
91     x = &r.data[p->idxLeft];
92 
93     while(p->ref > x->ref)
94     {
95         p = x;
96         // ビットが1なら右、0なら左をたどる
97 
98         u32 wd = u32(x->ref) >> 3;
99         u32 pos = u32(x->ref) & 7;
100 
101         if (wd < len && ((s[wd] >> pos) & 1))
102         {
103             x = &r.data[x->idxRight];
104         }
105         else
106         {
107             x = &r.data[x->idxLeft];
108         }
109     }
110 
111     if (x->ofsString != 0 && !std::strcmp(s, x->ofsString.to_ptr<char>()))
112     {
113         return const_cast<ResDicPatriciaData::ResDicNodeData*>(x);
114     }
115     return NULL;
116 }
117 
118 
119 
120 /* ------------------------------------------------------------------------
121     ResDic
122    ------------------------------------------------------------------------ */
123 
124 } /* namespace ut */
125 } /* namespace nw */
126 
127