Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
2553795b
Commit
2553795b
authored
Dec 31, 2014
by
funshine
Browse files
add file.read() api, read(n) will read n byte.
parent
eb27c4fb
Changes
1
Hide whitespace changes
Inline
Side-by-side
app/modules/file.c
View file @
2553795b
...
@@ -152,9 +152,15 @@ static int ICACHE_FLASH_ATTR file_check( lua_State* L )
...
@@ -152,9 +152,15 @@ static int ICACHE_FLASH_ATTR file_check( lua_State* L )
#endif
#endif
//
Lua: readline
()
//
g_read
()
static
int
ICACHE_FLASH_ATTR
file_read
line
(
lua_State
*
L
)
static
int
ICACHE_FLASH_ATTR
file_
g_
read
(
lua_State
*
L
,
int
n
,
int16_t
end_char
)
{
{
if
(
n
<
0
||
n
>
LUAL_BUFFERSIZE
)
n
=
LUAL_BUFFERSIZE
;
if
(
end_char
<
0
||
end_char
>
255
)
end_char
=
EOF
;
signed
char
ec
=
(
signed
char
)
end_char
;
luaL_Buffer
b
;
luaL_Buffer
b
;
if
((
FS_OPEN_OK
-
1
)
==
file_fd
)
if
((
FS_OPEN_OK
-
1
)
==
file_fd
)
return
luaL_error
(
L
,
"open a file first"
);
return
luaL_error
(
L
,
"open a file first"
);
...
@@ -170,7 +176,7 @@ static int ICACHE_FLASH_ATTR file_readline( lua_State* L )
...
@@ -170,7 +176,7 @@ static int ICACHE_FLASH_ATTR file_readline( lua_State* L )
break
;
break
;
}
}
p
[
i
++
]
=
c
;
p
[
i
++
]
=
c
;
}
while
((
c
!=
EOF
)
&&
(
c
!=
'\n'
)
&&
(
i
<
LUAL_BUFFERSIZE
)
);
}
while
((
c
!=
EOF
)
&&
(
c
!=
ec
)
&&
(
i
<
n
)
);
#if 0
#if 0
if(i>0 && p[i-1] == '\n')
if(i>0 && p[i-1] == '\n')
...
@@ -187,6 +193,40 @@ static int ICACHE_FLASH_ATTR file_readline( lua_State* L )
...
@@ -187,6 +193,40 @@ static int ICACHE_FLASH_ATTR file_readline( lua_State* L )
return
1
;
/* read at least an `eol' */
return
1
;
/* read at least an `eol' */
}
}
// Lua: read()
// file.read() will read all byte in file
// file.read(10) will read 10 byte from file, or EOF is reached.
// file.read('q') will read until 'q' or EOF is reached.
static
int
ICACHE_FLASH_ATTR
file_read
(
lua_State
*
L
)
{
unsigned
need_len
=
LUAL_BUFFERSIZE
;
int16_t
end_char
=
EOF
;
size_t
el
;
if
(
lua_type
(
L
,
1
)
==
LUA_TNUMBER
)
{
need_len
=
(
unsigned
)
luaL_checkinteger
(
L
,
1
);
if
(
need_len
>
LUAL_BUFFERSIZE
){
need_len
=
LUAL_BUFFERSIZE
;
}
}
else
if
(
lua_isstring
(
L
,
1
))
{
const
char
*
end
=
luaL_checklstring
(
L
,
1
,
&
el
);
if
(
el
!=
1
){
return
luaL_error
(
L
,
"wrong arg range"
);
}
end_char
=
(
int16_t
)
end
[
0
];
}
return
file_g_read
(
L
,
need_len
,
end_char
);
}
// Lua: readline()
static
int
ICACHE_FLASH_ATTR
file_readline
(
lua_State
*
L
)
{
return
file_g_read
(
L
,
LUAL_BUFFERSIZE
,
'\n'
);
}
// Lua: write("string")
// Lua: write("string")
static
int
ICACHE_FLASH_ATTR
file_write
(
lua_State
*
L
)
static
int
ICACHE_FLASH_ATTR
file_write
(
lua_State
*
L
)
{
{
...
@@ -233,6 +273,7 @@ const LUA_REG_TYPE file_map[] =
...
@@ -233,6 +273,7 @@ const LUA_REG_TYPE file_map[] =
{
LSTRKEY
(
"close"
),
LFUNCVAL
(
file_close
)
},
{
LSTRKEY
(
"close"
),
LFUNCVAL
(
file_close
)
},
{
LSTRKEY
(
"write"
),
LFUNCVAL
(
file_write
)
},
{
LSTRKEY
(
"write"
),
LFUNCVAL
(
file_write
)
},
{
LSTRKEY
(
"writeline"
),
LFUNCVAL
(
file_writeline
)
},
{
LSTRKEY
(
"writeline"
),
LFUNCVAL
(
file_writeline
)
},
{
LSTRKEY
(
"read"
),
LFUNCVAL
(
file_read
)
},
{
LSTRKEY
(
"readline"
),
LFUNCVAL
(
file_readline
)
},
{
LSTRKEY
(
"readline"
),
LFUNCVAL
(
file_readline
)
},
#if defined(BUILD_WOFS)
#if defined(BUILD_WOFS)
{
LSTRKEY
(
"format"
),
LFUNCVAL
(
file_format
)
},
{
LSTRKEY
(
"format"
),
LFUNCVAL
(
file_format
)
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment