OSDN Git Service

update libMiMic
[mimic/MiMicSDK.git] / lib / src / NyLPC_cRomPtrStream.c
1 /*********************************************************************************\r
2  * PROJECT: MiMic\r
3  * --------------------------------------------------------------------------------\r
4  *\r
5  * This file is part of MiMic\r
6  * Copyright (C)2011 Ryo Iizuka\r
7  *\r
8  * MiMic is free software: you can redistribute it and/or modify\r
9  * it under the terms of the GNU Lesser General Public License as published\r
10  * by the Free Software Foundation, either version 3 of the License, or\r
11  * (at your option) any later version.\r
12  *\r
13  * This program is distributed in the hope that it will be useful,\r
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
16  * GNU General Public License for more details.\r
17  *\r
18  * You should have received a copy of the GNU Lesser General Public License\r
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
20  *\r
21  * For further information please contact.\r
22  *  http://nyatla.jp/\r
23  *  <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
24  *\r
25  *********************************************************************************/\r
26 #include "NyLPC_cPtrStream_protected.h"\r
27 \r
28 /**\r
29  * ROMをラップするブロッキングストリーム型です。\r
30  * このクラスは、NyLPC_TcBlockingStreamにキャストすることができます。\r
31  特性\r
32  このクラスは、read onlyです。write動作は永久に失敗します。\r
33  また、タイムアウト値は意味を持たず、最終データを読みだした後はカウント0でタイムアウトと同じ挙動を示します。\r
34  */\r
35 typedef struct NyLPC_TcRomPtrStream NyLPC_TcRomPtrStream_t;\r
36 \r
37 /**********************************************************************\r
38  *\r
39  * NyLPC_TcRomBlockingStream class\r
40  *\r
41  **********************************************************************/\r
42 \r
43 struct NyLPC_TcRomPtrStream\r
44 {\r
45     NyLPC_TcPtrStream_t _super;\r
46     NyLPC_TChar* _rom;          //ROMアドレス\r
47     NyLPC_TInt32 _rom_size;     //ROMサイズ\r
48     NyLPC_TInt32 _ed;           //読出し済みサイズ\r
49     NyLPC_TInt16 _packet_size;  //一度当たりの読出しサイズ制限\r
50 };\r
51 \r
52 NyLPC_TBool NyLPC_TcRomPtrStream_initialize(NyLPC_TcRomPtrStream_t* i_inst,void* i_rom_addr,NyLPC_TInt32 i_length,NyLPC_TInt16 i_packetsize);\r
53 \r
54 #define NyLPC_TcRomPtrStream_finalize(i_inst)\r
55 \r
56 static NyLPC_TInt32 m_pread(NyLPC_TcPtrStream_t* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec);\r
57 static void m_close(NyLPC_TcPtrStream_t* i_inst);\r
58 static void m_preadSeek_func(NyLPC_TcPtrStream_t* i_inst,NyLPC_TUInt16 i_seek);\r
59 \r
60 //関数テーブル\r
61 static struct NyLPC_TcPtrStream_TInterface NyLPC_TcRomPtrStream_Interface=\r
62 {\r
63     m_pread,\r
64     NyLPC_cPtrStream_write_func,\r
65     m_preadSeek_func,\r
66     m_close\r
67 };\r
68 \r
69 \r
70 \r
71 NyLPC_TBool NyLPC_TcRomPtrStream_initialize(NyLPC_TcRomPtrStream_t* i_inst,void* i_rom_addr,NyLPC_TInt32 i_length,NyLPC_TInt16 i_packetsize)\r
72 {\r
73     NyLPC_ArgAssert(i_inst!=NULL);\r
74     NyLPC_ArgAssert(i_rom_addr!=NULL);\r
75     NyLPC_ArgAssert(i_length>0);\r
76     NyLPC_ArgAssert(i_packetsize>0);\r
77     i_inst->_super._interface=&NyLPC_TcRomPtrStream_Interface;\r
78     i_inst->_rom=(NyLPC_TChar*)i_rom_addr;\r
79     i_inst->_rom_size=i_length;\r
80     i_inst->_packet_size=i_packetsize;\r
81     return NyLPC_TBool_TRUE;\r
82 }\r
83 \r
84 static void m_preadSeek_func(NyLPC_TcPtrStream_t* i_inst,NyLPC_TUInt16 i_seek)\r
85 {\r
86     (void)i_inst;\r
87     NyLPC_TcRomPtrStream_t* inst=(NyLPC_TcRomPtrStream_t*)i_inst;\r
88     inst->_rom+=i_seek;\r
89 }\r
90 \r
91 \r
92 /*private*/\r
93 static NyLPC_TBool m_pread(NyLPC_TcPtrStream_t* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec)\r
94 {\r
95     NyLPC_TInt32 size,psize;\r
96     NyLPC_TcRomPtrStream_t* inst=(NyLPC_TcRomPtrStream_t*)i_inst;\r
97     //引数\r
98     NyLPC_ArgAssert(i_inst!=NULL);\r
99     NyLPC_ArgAssert(o_buf_ptr!=NULL);\r
100     //クローズしてない?\r
101     NyLPC_Assert(inst->_rom!=NULL);\r
102 \r
103     size=inst->_rom_size-inst->_ed;     //残り長さ計算\r
104     psize=(size>inst->_packet_size)?inst->_packet_size:size;    //パケットサイズ計算\r
105     *o_buf_ptr=(inst->_rom+inst->_ed);  //現在位置を返す\r
106     inst->_ed+=psize;//読出し位置更新\r
107     return psize;\r
108 }\r
109 \r
110 \r
111 static void m_close(NyLPC_TcPtrStream_t* i_inst)\r
112 {\r
113     NyLPC_TcRomPtrStream_t* inst=(NyLPC_TcRomPtrStream_t*)i_inst;\r
114     inst->_rom=NULL;\r
115 }\r
116 \r
117 \r
118 \r