On Tue, Jul 24, 2012 at 4:45 PM, Roland Mainz <roland.ma...@nrubsig.org> wrote:
> On Tue, Jul 24, 2012 at 11:51 AM, Simon Toedt <simon.to...@gmail.com> wrote:
>> I'm looking for code examples which show how I can allocate C strings
>> using sfio string streams. Has anyone an example how I can get the
>> sfio equivalent of strdup() for "aaaa", "bbbbbb" and "cccccccc" and a
>> malloc(256), get pointers back to the strings and allocated memory in
>> the sfio stream and finally deallocate the whole memory after all work
>> has been done?
>
> Example:
> -- snip --
> #include <sfio.h>
> #include <stk.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> static
> const char *sfstrdup(Sfio_t *str, const char *s)
> {
>         ssize_t pos=stktell(str);
>         sfputr(str, s, 0);
>         return stkptr(str, pos);
> }
>
> int main(int ac, char *av[])
> {
>         Sfio_t *str= sfstropen();
>         const char *s;
>
>         printf("tell=%ld\n", (long)stktell(str));
>
>         s=sfstrdup(str, "hello");
>         printf("|%s|, tell=%ld\n", s, (long)stktell(str));
>
>         s=sfstrdup(str, "world");
>         printf("|%s|, tell=%ld\n", s, (long)stktell(str));
>
>         sfclose(str);
>
>         return EXIT_SUCCESS;
> }
> -- snip --

Is the following replacement for |malloc()| correct (it's past
midnight here... so I better ask before doing something horrible
stupid...), e.g. is the error handling correct ? Is the resulting
memory just allocated or allocated+filled with '\0' ?
-- snip --
void *sfstrmalloc(Sfio_t *str, size_t size)
{
        ssize_t pos=stktell(str);
        sfseek(str, (Sfoff_t)(size+1), SEEK_CUR);
        return stkptr(str, pos);
}
-- snip --

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) roland.ma...@nrubsig.org
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)
_______________________________________________
ast-developers mailing list
ast-developers@research.att.com
https://mailman.research.att.com/mailman/listinfo/ast-developers

Reply via email to