add_run.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* This file is part of the software similarity tester SIM.
  2. Written by Dick Grune, Vrije Universiteit, Amsterdam.
  3. $Id: add_run.c,v 2.5 2001/11/08 12:30:28 dick Exp $
  4. */
  5. #include <malloc.h>
  6. #include "sim.h"
  7. #include "runs.h"
  8. #include "percentages.h"
  9. #include "options.h"
  10. #include "error.h"
  11. #include "add_run.h"
  12. static void set_chunk(
  13. struct chunk *,
  14. struct text *,
  15. unsigned int,
  16. unsigned int
  17. );
  18. static void set_pos(
  19. struct position *,
  20. int,
  21. struct text *,
  22. unsigned int
  23. );
  24. void
  25. add_run(struct text *txt0, unsigned int i0,
  26. struct text *txt1, unsigned int i1,
  27. unsigned int size
  28. ) {
  29. /* Adds the run of given size to our collection.
  30. */
  31. register struct run *r = (struct run *)malloc(sizeof (struct run));
  32. if (!r) fatal("out of memory");
  33. set_chunk(&r->rn_cn0, txt0, i0 - txt0->tx_start, size);
  34. set_chunk(&r->rn_cn1, txt1, i1 - txt1->tx_start, size);
  35. r->rn_size = size;
  36. if (option_set('p') ? add_to_percentages(r) : add_to_runs(r)) {
  37. /* OK */
  38. }
  39. else fatal("out of memory");
  40. }
  41. static void
  42. set_chunk(struct chunk *cnk, struct text *txt,
  43. unsigned int start, unsigned int size
  44. ) {
  45. /* Fill the chunk *cnk with info about the piece of text
  46. in txt starting at start extending over size tokens.
  47. */
  48. cnk->ch_text = txt;
  49. set_pos(&cnk->ch_first, 0, txt, start);
  50. set_pos(&cnk->ch_last, 1, txt, start + size - 1);
  51. }
  52. static void
  53. set_pos(struct position *pos, int type, struct text *txt, unsigned int start) {
  54. /* Fill a single struct position */
  55. pos->ps_next = txt->tx_pos;
  56. txt->tx_pos = pos;
  57. pos->ps_type = type;
  58. pos->ps_tk_cnt = start;
  59. pos->ps_nl_cnt = -1; /* uninitialized */
  60. }