Index: ossp-pkg/sio/al.c RCS File: /v/ossp/cvs/ossp-pkg/sio/Attic/al.c,v rcsdiff -q -kk '-r1.3' '-r1.4' -u '/v/ossp/cvs/ossp-pkg/sio/Attic/al.c,v' 2>/dev/null --- al.c 2002/10/14 12:32:16 1.3 +++ al.c 2002/10/14 13:34:25 1.4 @@ -2,6 +2,8 @@ #include #include +/****************************************************************************/ + #define LIST(elem) \ struct { elem *head, *tail; } #define NODE(elem) \ @@ -123,6 +125,8 @@ #define FOREACHR(q,l,n) for (n = TAIL(q,l); n; n = PREV(n,l)) #define FOREACHD(q,l,n,r) for (n = TAIL(q,l); n && (r = PREV(n,l), 1); n = r) +/****************************************************************************/ + #include "al.h" /* unique library identifier */ @@ -184,7 +188,7 @@ /* number of bytes of a span that are stored in a chunk */ #define AL_CHUNK_SPAN(alc, off, n) \ - ((n) == AL_BYTES_ALL || (n) > AL_CHUNK_LEN(alc) - (off) ? \ + ((n) > AL_CHUNK_LEN(alc) - (off) ? \ AL_CHUNK_LEN(alc) - (off) : (n)) /* number of bytes a chunk can be grown to the end */ @@ -357,41 +361,50 @@ * if off is negative, treat it as relative offset from the end * a reference to the chunk is stored in *alcp * the relative offset into the chunk is stored in *skipp + * return AL_OK and *alcp == NULL if positioned exactly to end of list * return AL_ERR_EOF when no such chunk can be found */ static al_rc_t al_seek(al_t *al, size_t off, al_chunk_t **alcp, size_t *skipp) { al_chunk_t *cur; - size_t pos; + size_t pos, end; size_t chunksize; if (off >= 0) { pos = 0; FOREACH(al,chunks,cur) { chunksize = AL_CHUNK_LEN(cur); - if (pos <= off && off < pos+chunksize) { + end = pos+chunksize; + if (pos <= off && off < end) { *alcp = cur; *skipp = off - pos; return AL_OK; } - if (pos+chunksize >= off) - return AL_ERR_EOF; - pos += chunksize; + if (end > off) + break; + pos = end; + } + /* seek to EOF position is ok */ + if (pos == off) { + *alcp = NULL; + *skipp = 0; + return AL_OK; } } else { off += al->bytes; pos = al->bytes; FOREACHR(al,chunks,cur) { chunksize = AL_CHUNK_LEN(cur); + end = pos; pos -= chunksize; - if (pos <= off && off < pos+chunksize) { + if (pos <= off && off < end) { *alcp = cur; *skipp = off - pos; return AL_OK; } - if (pos+chunksize < off) - return AL_ERR_EOF; + if (pos < off) + break; } } @@ -416,7 +429,7 @@ } #endif -/******************************************************************/ +/****************************************************************************/ /* * allocate an empty assembly list @@ -589,19 +602,15 @@ al_chunk_t *cur, *next; size_t pos, skip, len, step; - /* simplify API */ - if (n == AL_BYTES_ALL) n = al->bytes - off; - - pos = off; - /* * seek to beginning, return EOF when seek position does not exist * EOD must be a valid seek position so that we can append data */ - rc = al_seek(al, pos, &cur, &skip); + rc = al_seek(al, off, &cur, &skip); if (rc != AL_OK) return rc; /* as long as there is data to move */ + pos = off; while (n > 0 && cur != NULL) { next = NEXT(cur, chunks); len = AL_CHUNK_LEN(cur); @@ -661,9 +670,6 @@ pos += step; cur = next; skip = 0; - - if (cur == NULL) - break; } /* @@ -704,7 +710,6 @@ nal->bytes = 0; } - return AL_OK; } @@ -743,8 +748,6 @@ { al_rc_t rc; - if (n == AL_BYTES_ALL) n = al->bytes - off; - txp->cur = NULL; rc = al_seek(al, off, &txp->cur, &txp->skip); @@ -889,3 +892,22 @@ return AL_CHUNK_PTR(alc, off); } +/* + * convert error code into human readable form + */ +const char *al_error(al_rc_t rc) +{ + const char *mess; + + switch (rc) { + case AL_OK: mess = "Everything Ok"; break; + case AL_ERR_ARG: mess = "Invalid Argument"; break; + case AL_ERR_MEM: mess = "Not Enough Memory"; break; + case AL_ERR_EOF: mess = "End Of Data"; break; + case AL_ERR_INT: mess = "Internal Error"; break; + default: mess = "Invalid Result Code"; break; + } + + return mess; +} + Index: ossp-pkg/sio/al.h RCS File: /v/ossp/cvs/ossp-pkg/sio/Attic/al.h,v rcsdiff -q -kk '-r1.2' '-r1.3' -u '/v/ossp/cvs/ossp-pkg/sio/Attic/al.h,v' 2>/dev/null --- al.h 2002/10/14 12:32:16 1.2 +++ al.h 2002/10/14 13:34:25 1.3 @@ -20,8 +20,6 @@ AL_BACKWARD } al_td_t; -#define AL_BYTES_ALL (-1) - struct al_tx_st; typedef struct al_tx_st al_tx_t; @@ -44,3 +42,4 @@ size_t al_chunk_span(al_chunk_t *alc, size_t off, size_t n); char *al_chunk_ptr(al_chunk_t *alc, size_t off); +const char *al_error(al_rc_t rc); Index: ossp-pkg/sio/al_test.c RCS File: /v/ossp/cvs/ossp-pkg/sio/Attic/al_test.c,v rcsdiff -q -kk '-r1.2' '-r1.3' -u '/v/ossp/cvs/ossp-pkg/sio/Attic/al_test.c,v' 2>/dev/null --- al_test.c 2002/10/14 12:32:16 1.2 +++ al_test.c 2002/10/14 13:34:25 1.3 @@ -7,17 +7,16 @@ #define S(s) s, strlen(s) -al_rc_t printchunk(al_chunk_t *alc, void *u) +const char *fill(char *p, int n) { - char buf[40]; - char *p = al_chunk_ptr(alc,0); - size_t n = al_chunk_len(alc); - int k; + static char buf[4 * 80 + 1]; + int k; k=0; while (n > 0 && k < sizeof(buf)-5) { if (isprint(*p)) { - buf[k++] = *p; + buf[k] = *p; + k += 1; } else { sprintf(buf+k,"<%2.2x>",*p); k += 4; @@ -27,10 +26,26 @@ } buf[k] = '\0'; - printf("C: %08lx + %-6d = %s\n", + return (const char *)buf; +} + +al_rc_t printchunk(al_chunk_t *alc, void *u) +{ + size_t len, pre, post; + + len = al_chunk_len(alc); + pre = 20; if (pre > len) pre = len; + post = 20; if (post > len-pre) post = len-pre; + + printf("C: %08lx + %-6d = ", (long)al_chunk_ptr(alc, 0), - al_chunk_len(alc), - buf); + al_chunk_len(alc)); + fputs(fill(al_chunk_ptr(alc,0),pre), stdout); + if (post > 0) { + fputs(" .. ", stdout); + fputs(fill(al_chunk_ptr(alc,len-post),post), stdout); + } + fputs("\n", stdout); return AL_OK; } @@ -43,17 +58,22 @@ void print(const char *tag, al_t *al) { - char buf[10000]; + char *buf; size_t n, len; - printf("%s\n",tag); n = al_bytes(al); - if (n > sizeof(buf)) n = sizeof(buf); + buf = (char *)malloc(n); + if (buf == NULL) abort(); + al_flatten(al, 0, n, buf, &len); + + printf("%s = %d of %d\n",tag,len,n); fwrite(">>", 2, 1, stdout); fwrite(buf, len, 1, stdout); fwrite("<<", 2, 1, stdout); printf("\n\n"); + + free(buf); } void checklen(al_t *al) @@ -78,6 +98,7 @@ int main() { + al_rc_t rc; al_t *al, *al2, *al3; char baf[] = "Mittendrin\n"; int i; @@ -96,23 +117,21 @@ al_append_bytes(al3, S("HUHU WORLD\n")); - DUMP("DATA",al); DUMP("BUFFER",al2); DUMP("REPLACEMENT", al3); #if 1 - al_splice(al, 102, 200, al3, al2); -#else - al_splice(al, 102, 200, NULL, NULL); + rc = al_splice(al, al_bytes(al)-500, 500, al3, al2); #endif + printf("splice result: %d (%s)\n\n",rc,al_error(rc)); checklen(al); checklen(al2); checklen(al3); DUMP("SPLICED",al); - print("RESULT", al); + print("SPLICED", al); DUMP("BUFFER",al2); print("BUFFER", al2);