-/**
- * Insert a source fragment description into the table at <off>.
- */
-static int insert_source(struct Source *source,size_t off) {
- int index = find_source( off );
- int i;
- // Ensure at least 5 "free" Sources in <source.array>
- // and allocate space for 20 new otherwise.
- if ( sources.count + 5 > sources.limit ) {
- size_t size = sources.limit + 20;
- struct Source *new = realloc(
- sources.array, size * sizeof( struct Source ) );
- if ( new == 0 ) {
- return -1;
- }
- sources.array = new;
- sources.limit = size;
- }
-#if DEBUG
- fprintf( stderr, "index=%d\n", index );
-#endif
- if ( index < sources.count ) {
- ssize_t b = ( sources.count - index ) * sizeof(struct Source);
-#if DEBUG
- fprintf( stderr, "b=%ld\n", b );
-#endif
- if ( sources.array[ index ].start < off ) {
- // Split the <index> record at <off>
- // and adjust index
- memcpy( SOURCEARRAYP( index+2 ), SOURCEARRAYP( index ), b );
- sources.count += 2;
- b = off - sources.array[ index ].start;
- sources.array[ index + 2 ].from += b; // adjust tail fragment
- sources.array[ index++ ].to = b; // adjust head fragment
-#if DEBUG
- print_source( &sources.array[ index-1 ] );
- print_source( &sources.array[ index ] );
- print_source( &sources.array[ index+1 ] );
- fprintf( stderr, "---\n");
-#endif
- } else {
- // Insert the new source at <index>
- memcpy( SOURCEARRAYP( index+1 ), SOURCEARRAYP( index ), b );
- sources.count += 1;
- }
- } else {
- // Append the new source to <sources> (at <index>)
- sources.count += 1;
- }
- sources.array[ index ].filename = source->filename;
- sources.array[ index ].fd = source->fd;
- sources.array[ index ].from = source->from;
- sources.array[ index ].to = source->to;
- for ( i = index; i < sources.count; i++ ) {
- sources.array[ i ].start = off;
- off += sources.array[ i ].to - sources.array[ i ].from;
-#if DEBUG
- print_source( &sources.array[ i ] );
-#endif
- }
- sources.size = off;
-#if DEBUG
- fprintf( stderr, "count=%d size=%ld\n", sources.count, sources.size );
-#endif
- return index;
-}
-
-// Read <size> bytes from <off> in file