@@ -30,44 +30,90 @@ static int loglevel(lua_State *L) {
3030 return 0 ;
3131}
3232
33+ static lua_Integer _readfile (lua_State * L , void * buff , lua_Integer size , int idx ) {
34+ lua_getmetatable (L , idx );
35+ lua_getfield (L , -1 , "read" );
36+ lua_pushvalue (L , idx );
37+ lua_pushinteger (L , size );
38+ lua_call (L , 2 , 2 );
39+ if (lua_isnil (L , -2 )) {
40+ lua_pop (L , 3 );
41+ return 0 ;
42+ }
43+
44+ size_t eleng = 0 ;
45+ const char * read = luaL_checklstring (L , -2 , & eleng );
46+ char * cbuff = (char * )buff ;
47+ while (eleng -- ) * cbuff ++ = * read ++ ;
48+ lua_pop (L , 3 );
49+ return (lua_Integer )(cbuff - (char * )buff );
50+ }
51+
52+ static lua_Integer _seekfile (lua_State * L , lua_Integer offset , int origin , int idx ) {
53+ lua_getmetatable (L , idx );
54+ lua_getfield (L , -1 , "seek" );
55+ lua_pushvalue (L , idx );
56+ switch (origin ) {
57+ case SEEK_CUR :
58+ lua_pushliteral (L , "cur" );
59+ break ;
60+
61+ case SEEK_END :
62+ lua_pushliteral (L , "end" );
63+ break ;
64+
65+ case SEEK_SET :
66+ lua_pushliteral (L , "set" );
67+ break ;
68+ }
69+ lua_pushinteger (L , offset );
70+ lua_call (L , 3 , 2 );
71+ if (lua_isnil (L , -2 )) {
72+ lua_pop (L , 3 );
73+ return 1 ;
74+ }
75+
76+ lua_pop (L , 3 );
77+ return 0 ;
78+ }
79+
3380static int wavguess (lua_State * L ) {
34- FILE * fw = * (FILE * * )luaL_checkudata (L , 1 , LUA_FILEHANDLE );
3581 int temp , nfsz , nsamp ;
3682 short nfmt , nchs ;
3783
38- if (fread ( & temp , 4 , 1 , fw ) != 1 || temp != 0x46464952 /*RIFF*/ )
84+ if (_readfile ( L , & temp , 4 , 1 ) != 4 || temp != 0x46464952 /*RIFF*/ )
3985 return luaL_error (L , "Not a wave file" );
40- if (fseek ( fw , 4 , SEEK_CUR ) != 0 || fread ( & temp , 4 , 1 , fw ) != 1 || temp != 0x45564157 /*WAVE*/ )
86+ if (_seekfile ( L , 4 , SEEK_CUR , 1 ) != 0 || _readfile ( L , & temp , 4 , 1 ) != 4 || temp != 0x45564157 /*WAVE*/ )
4187 return luaL_error (L , "WAVE id was not found" );
42- if (fread ( & temp , 4 , 1 , fw ) != 1 || temp != 0x20746D66 /*fmt */ )
88+ if (_readfile ( L , & temp , 4 , 1 ) != 4 || temp != 0x20746D66 /*fmt */ )
4389 return luaL_error (L , "No format sub-chunk found" );
44- if (fread ( & nfsz , 4 , 1 , fw ) != 1 || nfsz <= 0 )
90+ if (_readfile ( L , & nfsz , 4 , 1 ) != 4 || nfsz <= 0 )
4591 return luaL_error (L , "Invalid format sub-chunk size" );
46- if (fread ( & nfmt , 2 , 1 , fw ) != 1 || (nfmt != 0x0001 && nfmt != 0x0003 ))
92+ if (_readfile ( L , & nfmt , 2 , 1 ) != 2 || (nfmt != 0x0001 && nfmt != 0x0003 ))
4793 return luaL_error (L , "Unsupported data format" );
48- if (fread ( & nchs , 2 , 1 , fw ) != 1 || nchs > 1 )
94+ if (_readfile ( L , & nchs , 2 , 1 ) != 2 || nchs > 1 )
4995 return luaL_error (L , "Not a mono sound file" );
50- if (fread ( & nsamp , 4 , 1 , fw ) != 1 || nsamp == 0 )
96+ if (_readfile ( L , & nsamp , 4 , 1 ) != 4 || nsamp == 0 )
5197 return luaL_error (L , "Failed to determine sample rate" );
52- if (fseek ( fw , 8 , SEEK_CUR ) != 0 )
98+ if (_seekfile ( L , 8 , SEEK_CUR , 1 ) != 0 )
5399 return luaL_error (L , "Unexpected EOF" );
54100
55101 // Читаем расширенные данные для чанка формата, если такие имеются
56102 if (nfsz > 16 ) {
57103 temp = 0 ;
58104
59- if (fread ( & temp , 2 , 1 , fw ) != 1 || fseek ( fw , temp , SEEK_CUR ) != 0 )
105+ if (_readfile ( L , & temp , 2 , 1 ) != 2 || _seekfile ( L , temp , SEEK_CUR , 1 ) != 0 )
60106 return luaL_error (L , "Something wrong with the wave extension block" );
61107 }
62108
63109 while (1 ) {
64- if (fread ( & temp , 4 , 1 , fw ) != 1 || fread ( & nfsz , 4 , 1 , fw ) != 1 ) // Реюзаем nfsz, т.к. дальше он не нужен
110+ if (_readfile ( L , & temp , 4 , 1 ) != 4 || _readfile ( L , & nfsz , 4 , 1 ) != 4 ) // Реюзаем nfsz, т.к. дальше он не нужен
65111 return luaL_error (L , "Failed to read chunk info" );
66112
67113 if (temp == 0x61746164 ) // При появлении data чанка завершаем цикл
68114 break ;
69115
70- fseek ( fw , nfsz , SEEK_CUR );
116+ _seekfile ( L , nfsz , SEEK_CUR , 1 );
71117 }
72118
73119 lua_pushinteger (L , nsamp );
0 commit comments