Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
micropython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
micropython
Commits
3990b171
Commit
3990b171
authored
Jul 28, 2016
by
Paul Sokolovsky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
py/objstringio: Implement MP_STREAM_SEEK ioctl and add seek() method.
parent
f039ac5b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
3 deletions
+24
-3
py/objstringio.c
py/objstringio.c
+24
-3
No files found.
py/objstringio.c
View file @
3990b171
...
...
@@ -75,13 +75,18 @@ STATIC mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size,
(
void
)
errcode
;
mp_obj_stringio_t
*
o
=
MP_OBJ_TO_PTR
(
o_in
);
check_stringio_is_open
(
o
);
mp_uint_t
remaining
=
o
->
vstr
->
alloc
-
o
->
pos
;
if
(
size
>
remaining
)
{
mp_int_t
remaining
=
o
->
vstr
->
alloc
-
o
->
pos
;
mp_uint_t
org_len
=
o
->
vstr
->
len
;
if
((
mp_int_t
)
size
>
remaining
)
{
// Take all what's already allocated...
o
->
vstr
->
len
=
o
->
vstr
->
alloc
;
// ... and add more
vstr_add_len
(
o
->
vstr
,
size
-
remaining
);
}
// If there was a seek past EOF, clear the hole
if
(
o
->
pos
>
org_len
)
{
memset
(
o
->
vstr
->
buf
+
org_len
,
0
,
o
->
pos
-
org_len
);
}
memcpy
(
o
->
vstr
->
buf
+
o
->
pos
,
buf
,
size
);
o
->
pos
+=
size
;
if
(
o
->
pos
>
o
->
vstr
->
len
)
{
...
...
@@ -92,8 +97,23 @@ STATIC mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size,
STATIC
mp_uint_t
stringio_ioctl
(
mp_obj_t
o_in
,
mp_uint_t
request
,
uintptr_t
arg
,
int
*
errcode
)
{
(
void
)
errcode
;
//
mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
mp_obj_stringio_t
*
o
=
MP_OBJ_TO_PTR
(
o_in
);
switch
(
request
)
{
case
MP_STREAM_SEEK
:
{
struct
mp_stream_seek_t
*
s
=
(
struct
mp_stream_seek_t
*
)
arg
;
mp_uint_t
ref
=
0
;
switch
(
s
->
whence
)
{
case
1
:
// SEEK_CUR
ref
=
o
->
pos
;
break
;
case
2
:
// SEEK_END
ref
=
o
->
vstr
->
len
;
break
;
}
o
->
pos
=
ref
+
s
->
offset
;
s
->
offset
=
o
->
pos
;
return
0
;
}
case
MP_STREAM_FLUSH
:
return
0
;
default:
...
...
@@ -160,6 +180,7 @@ STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = {
{
MP_ROM_QSTR
(
MP_QSTR_readall
),
MP_ROM_PTR
(
&
mp_stream_readall_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_readline
),
MP_ROM_PTR
(
&
mp_stream_unbuffered_readline_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_write
),
MP_ROM_PTR
(
&
mp_stream_write_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_seek
),
MP_ROM_PTR
(
&
mp_stream_seek_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_flush
),
MP_ROM_PTR
(
&
mp_stream_flush_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_close
),
MP_ROM_PTR
(
&
stringio_close_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_getvalue
),
MP_ROM_PTR
(
&
stringio_getvalue_obj
)
},
...
...
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