buffer.c 17.6 KB
Newer Older
1 2 3 4 5
/* buffer.c	19/8/1995
 * Buffers, version 0.3
 */

/* Copyright (c) 1995,1996 Sascha Demetrio
6
 * Copyright (c) 2009, 2010, 2015 Peter Pentchev
7 8 9 10 11 12 13
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
14
 *    If you modify any part of HEXER and redistribute it, you must add
15 16 17 18 19 20
 *    a notice to the `README' file and the modified source files containing
 *    information about the  changes you made.  I do not want to take
 *    credit or be blamed for your modifications.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
21
 *    If you modify any part of HEXER and redistribute it in binary form,
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
 *    you must supply a `README' file containing information about the
 *    changes you made.
 * 3. The name of the developer may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * HEXER WAS DEVELOPED BY SASCHA DEMETRIO.
 * THIS SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
 * NOT MAKE USE OF THIS WORK.
 *
 * DISCLAIMER:
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <memory.h>
#include <errno.h>

#include "buffer.h"
#include "defs.h"
60
#include "util.h"
61 62 63 64 65 66 67

struct BufferOptions b_default_options = {
  BUFFER_BLOCKSIZE,
  BUFFER_OPT,
  BUFFER_MAX_BLOCKS
};

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
long last_position = 0;
  /* `last_position' is the position of the first byte in line `last_number'.
   */
long last_number = 1;
  /* `last_number' is the number of the line most recently accessed using one
   * of the following functions.
   */
long last_length = -1;
  /* `last_length' is the length of the line `last_number'.  A value of -1
   * means that the length of that line hasn't been evaluated yet.
   */
Buffer *last_buffer = 0;
  /* `last_buffer' is a pointer to the buffer that the above counters are
   * talking about.
   * NOTE:  If `last_buffer' is modified, the variables `last_position',
    *  `last_number' and `last_length' are set to 0, 1, -1 (respectively).
   */

86 87
#if !HAVE_MEMMOVE
  static void
88
b_memmove(char *t, const char *s, const unsigned long count)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
{
  register long i;

  if (t > s)
    for (i = count - 1; i >= 0; --i) t[i] = s[i];
  else
    for (i = 0; i < count; ++i) t[i] = s[i];
}
/* b_memmove */
#else
#define b_memmove(a, b, c) memmove(a, b, c)
#endif

/* Whenever the buffer is modified, the macro `BUFFER_CHANGED' is called.
 * Purpose: 1. The buffer is marked modified;  2. Discard all information
 *   line numbers that could have become invalid by the modification.
 */
#define BUFFER_CHANGED(buffer) {                                              \
  if (buffer == last_buffer)                                                  \
    last_position = 0, last_number = 1, last_length = -1;                     \
  buffer->modified = 1; }

111 112
  unsigned long
count_lines(char *source, unsigned long count)
113 114
{
  char *i;
115
  unsigned long j = 0;
116 117 118 119 120 121 122 123 124 125 126

  for (i = source; i < source + count; i++) if (*i == '\n') j++;
  return j;
}
/* count_lines */


/* BufferBlock:
 */

  BufferBlock *
127
new_buffer_block(const unsigned long blocksize, char * const data)
128 129 130
{
  BufferBlock *new_block;

131
  new_block = (BufferBlock *)malloc_fatal(sizeof(BufferBlock));
132 133 134
  new_block->next_block = 0;

  if (data != 0) new_block->data = data;
135
  else new_block->data = (char *)malloc_fatal(blocksize);
136 137 138 139 140
  return new_block;
}
/* new_buffer_block */

  void
141
delete_buffer_block(BufferBlock * const block)
142 143 144 145 146 147 148 149 150 151 152
{
  free((char *)block->data);
  free((char *)block);
}
/* delete_buffer_block */


/* Buffer:
 */

  Buffer *
153
new_buffer(struct BufferOptions *arg_options)
154
{
155
  Buffer * new_buf;
156 157 158
  struct BufferOptions options;
  
  options = arg_options ? *arg_options : b_default_options;
159
  new_buf = (Buffer *)malloc_fatal(sizeof(Buffer));
160 161 162 163 164 165
  new_buf->first_block = 0;
  new_buf->size = 0;
  new_buf->blocksize = options.blocksize;
  new_buf->read_only = options.opt & B_READ_ONLY;
  new_buf->modified = 0;
  return new_buf;
166 167 168 169
}
/* new_buffer */

  int
170
delete_buffer(Buffer * const buffer)
171 172 173 174 175 176 177 178 179 180 181 182 183
{
  BufferBlock *i, *j;

  for (i = buffer->first_block; i; i = j) {
    j = i->next_block;
    delete_buffer_block(i);
  }
  free((char *)buffer);
  return 0;
}
/* delete_buffer */

  BufferBlock *
184
find_block(Buffer *buffer, unsigned long position)
185 186
{
  BufferBlock *i;
187
  unsigned long block_number;
188 189 190 191 192 193 194 195 196 197 198

  if (position >= buffer->size) return 0;
  for (i = buffer->first_block, block_number = 1;
       i != 0 && block_number * buffer->blocksize <= position;
       i = i->next_block, block_number++);
  assert(i);
  return i;
}
/* find_block */

  int
199
b_set_size(Buffer *buffer, unsigned long size)
200 201
{
  BufferBlock *i, *j = 0, *k;
202
  unsigned long block_number;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

  assert(!buffer->read_only);
  BUFFER_CHANGED(buffer);
  if (size < buffer->size) {
    if (!(i = find_block(buffer, size))) return -1;
    k = i;
    for (i = i->next_block; i; i = j) {
      j = i->next_block;
      delete_buffer_block(i);
    }
    k->next_block = 0;
  } else {
    if (buffer->first_block == 0) {
      buffer->first_block = new_buffer_block(buffer->blocksize, 0);
    }
    for (i = buffer->first_block, block_number = 1;
	 i->next_block != 0;
	 i = i->next_block, block_number++);
    for (;
         block_number * buffer->blocksize < size;
         i = i->next_block, block_number++) {
      if (!i) return -1;
      i->next_block = new_buffer_block(buffer->blocksize, 0);
    }
  }
  buffer->size = size;
  return 0;
}
/* b_set_size */

  long
234
b_read(Buffer *buffer, char *target, unsigned long position, unsigned long count)
235 236 237
{
  BufferBlock *block;
  unsigned long bs = buffer->blocksize;
238 239
  unsigned long ofs = position % bs;
  unsigned long r = bs - ofs;
240

241
  if (!(block = find_block(buffer, position))) return -1;
242
  if (count + position > buffer->size) count = buffer->size - position;
243 244
  if (count <= r) {
    b_memmove(target, block->data + ofs, count);
245 246
    return count;
  } else {
247 248 249 250 251
    b_memmove(target, block->data + ofs, r);
    long more = b_read(buffer, target + r, position + r, count - r);
    if (more < 0)
      return more;
    return r + more;
252 253 254 255
  }
}
/* b_read */

256 257
  unsigned long
b_write(Buffer *buffer, char *source, unsigned long position, unsigned long count)
258 259 260 261 262 263 264 265 266 267 268 269
{
  BufferBlock *block;
  unsigned long bs = buffer->blocksize;

  assert(!buffer->read_only);
  if (count) BUFFER_CHANGED(buffer);
  if (!(block = find_block(buffer, position))) return 0;
  if (count + position > buffer->size) count = buffer->size - position;
  if (count <= (bs - position % bs)) {
    b_memmove(block->data + position % bs, source, count);
    return count;
  } else {
270
    unsigned long r = bs - position % bs;
271 272 273 274 275 276 277
    b_memmove(block->data + position % bs, source, r);
    return r + b_write(buffer, source + r, position + r, count - r);
  }
}
/* b_write */

  long
278
b_write_append(Buffer *buffer, char *source, unsigned long position, unsigned long count)
279 280 281 282 283 284 285 286 287 288
{
  assert(!buffer->read_only);
  if (position + count > buffer->size) {
    if (b_set_size(buffer, position + count)) return -1;
    return b_write(buffer, source, position, count);
  } else return b_write(buffer, source, position, count);
}
/* b_write_append */

  long
289
b_append(Buffer *buffer, char *source, unsigned long count)
290 291 292 293 294
{
  return b_write_append(buffer, source, buffer->size, count);
}
/* b_append */

295 296
  unsigned long
b_count_lines(Buffer *buffer, unsigned long position, unsigned long count)
297 298 299 300 301 302 303 304 305
{
  BufferBlock *block;
  unsigned long bs = buffer->blocksize;

  if (!(block = find_block(buffer, position))) return 0;
  if (count + position > buffer->size) count = buffer->size - position;
  if (count <= bs - position % bs) 
    return count_lines(block->data + position % bs, count);
  else {
306
    unsigned long r = bs - position % bs;
307 308 309 310 311 312 313
    return count_lines(block->data + position % bs, r)
            + b_count_lines(buffer, position + r, count - r);
  }
}
/* b_count_lines */

  long
314
b_insert(Buffer *buffer, unsigned long position, unsigned long count)
315
{
316
  unsigned long i;
317 318 319 320 321 322 323 324 325 326

  assert(!buffer->read_only);
  if(b_set_size(buffer, buffer->size + count) < 0) return -1;
  if ((i = buffer->size - count - position))
    b_copy(buffer, buffer, position + count, position, i);
  return count;
}
/* b_insert */

  long
327
b_delete(Buffer *buffer, unsigned long position, unsigned long count)
328
{
329
  unsigned long size = buffer->size;
330 331

  assert(!buffer->read_only);
332 333 334
  if (b_copy(buffer, buffer, position, position + count,
         buffer->size - position - count) < 0)
    return -1;
335 336 337 338 339 340
  if (b_set_size(buffer, buffer->size - count) < 0) return -1;
  return size - buffer->size;
}
/* b_delete */

  long
341
b_copy(Buffer *target_buffer, Buffer *source_buffer, unsigned long target_position, unsigned long source_position, unsigned long count)
342 343
{
  BufferBlock *t_block;
344 345
  unsigned long t_offset = target_position % target_buffer->blocksize;
  unsigned long t_blocksize = target_buffer->blocksize;
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

  assert(!target_buffer->read_only);
  if (count) BUFFER_CHANGED(target_buffer);
  if ((target_buffer == source_buffer) &&
      (target_position > source_position)) {
    long r;
    r = b_copy_forward(target_buffer, target_position,
                       source_position, count);
    return r;
  }
  if (!(t_block = find_block(target_buffer, target_position))) return -1;
  if (count <= t_blocksize - t_offset) {
    long r = b_read(source_buffer, t_block->data + t_offset,
                    source_position, count);
    return r;
  } else {
    long r = b_read(source_buffer, t_block->data + t_offset,
                    source_position, t_blocksize - t_offset);
364 365 366
    if (r > 0 && (unsigned long)r >= t_blocksize - t_offset)
    {
      long more = b_copy(target_buffer, source_buffer,
367
                  target_position + r, source_position + r, count - r);
368 369 370 371 372
      if (more < 0)
	return more;
      else
	r += more;
    }
373 374 375 376 377 378
    return r;
  }
}
/* b_copy */

  long
379
b_copy_forward(Buffer *buffer, unsigned long target_position, unsigned long source_position, unsigned long count)
380 381
{
  BufferBlock *t_block, *s_block;
382 383 384 385
  unsigned long bs = buffer->blocksize;
  unsigned long t_offset = target_position % bs; /* target offset. */
  unsigned long s_offset = source_position % bs; /* source offset. */
  unsigned long tr_offset = (target_position + count - 1) % bs + 1;
386
    /* target reverse offset. */
387
  unsigned long sr_offset = (source_position + count) % bs;
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    /* source reverse offset. */

  assert(!buffer->read_only);
  assert(target_position > source_position);
  BUFFER_CHANGED(buffer);
  if (target_position > buffer->size) return -1;
  if (count + target_position > buffer->size)
    count = buffer->size - target_position;
  assert(t_block = find_block(buffer, target_position + count - 1));
  if (count <= bs - t_offset) {
    if (count <= bs - s_offset) {
      assert(s_block = find_block(buffer, source_position));
      b_memmove(t_block->data + t_offset, s_block->data + s_offset, count);
    } else {
      assert(s_offset > t_offset);
      assert(s_block = find_block(buffer, source_position + count));
      b_memmove(t_block->data + tr_offset - sr_offset, s_block->data,
                sr_offset);
      assert(s_block = find_block(buffer, source_position));
      b_memmove(t_block->data + t_offset, s_block->data + s_offset,
                count - sr_offset);
    }
    return count;
  } else {
412
    unsigned long r = tr_offset;
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    if (tr_offset >= sr_offset) {
      if (sr_offset) {
        assert(s_block = find_block(buffer, source_position + count));
        b_memmove(t_block->data + tr_offset - sr_offset,
                s_block->data, sr_offset);
      }
      if (sr_offset != tr_offset) {
        assert(s_block =
               find_block(buffer, source_position + count - tr_offset));
        b_memmove(t_block->data, s_block->data + bs - tr_offset + sr_offset,
                tr_offset - sr_offset);
      }
    } else {
      assert(s_block = find_block(buffer, source_position + count));
      b_memmove(t_block->data, s_block->data + sr_offset - tr_offset,
              tr_offset);
    }
    r += b_copy_forward(buffer, target_position, source_position, count - r);
    return r;
  }
}
/* b_copy_forward */

  void
437
b_clear(Buffer * const buffer)
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
{
  BufferBlock *i, *j;

  assert(!buffer->read_only);
  BUFFER_CHANGED(buffer);
  for (i = buffer->first_block; i; i = j) {
    j = i->next_block;
    delete_buffer_block(i);
  }
  buffer->first_block = 0;
  buffer->size = 0;
}
/* b_clear */

  long
453
b_read_buffer_from_file(Buffer * const buffer, const char * const filename)
454 455 456 457
{
  BufferBlock *i = 0;
  char *tmp;
  int file = open(filename, O_RDONLY);
458
  int err_f = 0;
459 460 461
    
  assert(!buffer->read_only);
  BUFFER_CHANGED(buffer);
462 463 464 465
  if (file <= 0) {
    close(file);
    return -1;
  }
466 467
  b_clear(buffer);
  do {
468 469
    ssize_t bytes_read;

470
    tmp = (char *)malloc_fatal(buffer->blocksize);
471 472 473 474 475 476 477 478 479 480 481 482
    bytes_read = read(file, tmp, buffer->blocksize);
    if (bytes_read > 0)	{
      if (buffer->first_block == 0) {
	buffer->first_block =
	  new_buffer_block(buffer->blocksize, tmp);
	i = buffer->first_block;
      } else {
	i->next_block = new_buffer_block(buffer->blocksize, tmp);
	i = i->next_block;
      }
      buffer->size += bytes_read;
    } else {
483
      err_f = bytes_read < 0;
484 485 486 487 488
      free((char *)tmp);
      break;
    }
  } while (1);
  close(file);
489 490 491 492
  if (err_f) {
    b_clear(buffer);
    return -1;
  }
493 494 495 496 497
  return buffer->size;
}
/* b_read_buffer_from_file */

  long
498
b_write_buffer_to_file(Buffer *buffer, char *filename)
499 500
{
  BufferBlock *i;
501 502 503
  unsigned long bytes_wrote = 0;
  unsigned long bs = buffer->blocksize;
  unsigned long blocks;
504 505
  int file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);

506 507
  if (file < 0)
    return -1;
508 509 510
  for (i = buffer->first_block, blocks = 1;
       i;
       i = i->next_block, blocks++) {
511
    ssize_t bytes;
512
    bytes = write_buf(file,
513 514 515 516 517 518 519 520 521 522 523 524 525 526
                  i->data,
                  (blocks * bs > buffer->size) ? buffer->size % bs : bs);
    if (bytes < 0) {
      close(file);
      return -1;
    }
    bytes_wrote += bytes;
  }
  close(file);
  return bytes_wrote;
}
/* b_write_buffer_to_file */

  long
527
b_copy_to_file(Buffer *buffer, const char *filename, unsigned long position, unsigned long count)
528
{
529
  char *tmp = malloc_fatal(buffer->blocksize);
530 531 532
  long bytes_read = 0, bytes_wrote = 0;
  int file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);

533 534
  if (file < 0) {
    free((char *)tmp);
535
    if (file >= 0) close(file);
536 537 538 539 540 541
    return -1;
  }
  do {
    bytes_read =
      b_read(buffer, tmp, position,
	     (buffer->blocksize < count) ? buffer->blocksize : count);
542
    if (bytes_read < 0 || write_buf(file, tmp, bytes_read) < 0) {
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
      free((char *)tmp);
      close(file);
      return -1;
    }
    count -= bytes_read;
    bytes_wrote += bytes_read;
    position += bytes_read;
    if (bytes_read == 0 || count <= 0) break;
  } while (1);
  free((char *)tmp);
  close(file);
  return bytes_wrote;
}
/* b_copy_to_file */

  long
559
b_paste_from_file(Buffer *buffer, const char *filename, unsigned long position)
560
{
561
  char *tmp = malloc_fatal(buffer->blocksize);
562 563 564 565
  long bytes_read, bytes_wrote = 0;
  int file = open(filename, O_RDONLY);
  
  assert(!buffer->read_only);
566 567
  if (file < 0) {
    free((char *)tmp);
568
    if (file >= 0) close(file);
569 570 571 572 573
    return -1;
  }
  BUFFER_CHANGED(buffer);
  do {
    bytes_read = read(file, tmp, buffer->blocksize);
574
    if (bytes_read < 0 || b_insert(buffer, position, bytes_read) < 0) {
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
      free((char *)tmp);
      close(file);
      return -1;
    }
    bytes_wrote += b_write(buffer, tmp, position, bytes_read);
    position += bytes_read;
  } while (bytes_read > 0);
  free((char *)tmp);
  close(file);
  return bytes_wrote;
}
/* b_paste_from_file */


/* The following set of functions is for dealing with text-buffers.
 */

592
  unsigned long
593
b_no_lines(Buffer *buffer)
594 595 596 597 598 599
{
  return b_count_lines(buffer, 0, buffer -> size);
}
/* b_no_lines */

  long
600
b_goto_line(Buffer *buffer, unsigned long number)
601 602
{
  BufferBlock *i;
603
  unsigned long newlines = 0, blocks = 0;
604 605

  if (number == 1) return 0;
606
  if (buffer == last_buffer && last_number >= 0 && number == (unsigned long)last_number) return last_position;
607
  for (i = buffer -> first_block; i; i = i -> next_block) {
608 609 610
    size_t ofs;
    for (ofs = 0; ofs < buffer -> blocksize; ofs++)
      if (i->data[ofs] == '\n')
611
        if (++newlines == number - 1) {
612 613
          unsigned long block_position = blocks * buffer -> blocksize;
          last_position = block_position + ofs + 1;
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
          last_number = number;
          last_buffer = buffer;
          last_length = -1;
          return last_position;
        }
    blocks++;
  }
  return -1;
}
/* b_goto_line */

/* end of buffer.c */


/* VIM configuration: (do not delete this line)
 *
630 631
 * vim:bk:nodg:efm=%f\:%l\:%m:hid:icon:
 * vim:sw=2:sm:textwidth=79:ul=1024:wrap:
632
 */