2 Copyright (C) 1995-2011, The AROS Development Team. All rights reserved.
6 #include <aros/debug.h>
7 #include <exec/memory.h>
9 #include <proto/exec.h>
15 static BOOL
bufferExpand(Buffer
*out
, LONG size
, APTR SysBase
)
17 ULONG newLength
= out
->len
+ size
;
19 if (newLength
> out
->mem
)
21 ULONG newSize
= BUF_SIZE
;
24 while (newSize
< newLength
)
27 if ((tmp
= AllocMem(newSize
+ 1, MEMF_ANY
)) == NULL
)
31 CopyMem(out
->buf
, tmp
, out
->len
);
34 FreeMem(out
->buf
, out
->mem
);
42 LONG
bufferInsert(STRPTR str
, ULONG size
, Buffer
*out
, APTR SysBase
)
45 if (!bufferExpand(out
, size
, SysBase
))
46 return ERROR_NO_FREE_STORE
;
48 memmove(out
->buf
+ size
, out
->buf
, out
->len
);
49 CopyMem(str
, out
->buf
, size
);
50 newLength
= out
->len
+ size
;
52 out
->buf
[newLength
] = '\0';
57 LONG
bufferAppend(STRPTR str
, ULONG size
, Buffer
*out
, APTR SysBase
)
60 if (!bufferExpand(out
, size
, SysBase
))
61 return ERROR_NO_FREE_STORE
;
63 CopyMem(str
, out
->buf
+ out
->len
, size
);
64 newLength
= out
->len
+ size
;
66 out
->buf
[newLength
] = '\0';
71 LONG
bufferCopy(Buffer
*in
, Buffer
*out
, ULONG size
, APTR SysBase
)
73 STRPTR s
= in
->buf
+ in
->cur
;
74 LONG ret
= bufferAppend(s
, size
, out
, SysBase
);
79 void bufferFree(Buffer
*b
, APTR SysBase
)
84 FreeMem(b
->buf
, b
->mem
+ 1);
92 LONG
bufferReadItem(STRPTR buf
, ULONG size
, Buffer
*in
, APTR DOSBase
)
94 struct CSource tin
= { in
->buf
, in
->len
, in
->cur
};
95 LONG ret
= ReadItem(buf
, size
, &tin
);
97 in
->cur
= tin
.CS_CurChr
;
100 * Workaround for intentional ReadItem() bug:
101 * If it hits end of buffer, it will push back one character.
102 * This bug replicates OS3.1 behavior, on which many existing
103 * programs rely. They all have own workarounds. We too.
105 if (in
->cur
== in
->len
- 1)
107 D(bug("[bufferReadItem] Getting last character\n"));
114 void bufferReset(Buffer
*b
)