File: flags.c

package info (click to toggle)
numdiff 5.6.0-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,460 kB
  • sloc: ansic: 9,578; sh: 3,383; makefile: 275
file content (251 lines) | stat: -rw-r--r-- 5,974 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
    Numdiff - compare putatively similar files, 
    ignoring small numeric differences
    Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012  Ivano Primi  <ivprimi@libero.it>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include"numdiff.h"
#include<xalloc.h>

/* Constants and default values */
#define CHUNK_ALLOC  20480

static flg_array internal_table;

/*
  `table' must be the address of a `flg_array' structure.
*/
static int init_flg_array (flg_array* table)
{
  if ((table))
    {
      table->ptr = NULL;
      table->size = table->len = 0;
      return 0;
    }
  else
    return -1;
}

int init_flags (void)
{
  return init_flg_array (&internal_table);
}

/*
  `table' must be the address of a `flg_array' structure.
*/
static int print_flg_array (FILE* fp, const flg_array* table)
{
  size_t n;
  unsigned short i;
  unsigned char byte, elem;

  if (!table || !table->ptr)
    return (fputs (_("<The array is empty>\n"), fp) == EOF);
  else
    {
      int outerror = 0;

      for (n = 0; !outerror && n < table->len; n += 4U)
	{
	  byte = table->ptr[n/4U];
	  for (i = 0; i < 4U; i++)
	    {
	      if ( (elem = (byte & 0x03 << 2*i) >> 2 * i) )
		{
		  if ( fprintf (fp, "%u", elem) < 0 )
		    {
		      outerror = 1;
		      break;
		    }
		}
	      else
		break;
	    }
	}
      if (!outerror)
	outerror = (fputc ('\n', fp) == EOF);
      return outerror;
    }
}

int print_flags (FILE* fp)
{
  return print_flg_array (fp, &internal_table);
}

/*
  array must be the address of a `flg_array' structure.
*/
static void addnewelem (flg_array* array, unsigned char elem)
{
  if (!array)
    return;
  if ( !array->ptr )
    {
      array->ptr = xcalloc (CHUNK_ALLOC, sizeof (unsigned char));
      array->size = CHUNK_ALLOC;
      array->ptr[0] = elem;
      array->len = 1;
#ifdef __MEMDEBUG__
      fprintf (stderr, "size = %6zu, len = %6zu, string:",
	       array->size, array->len);
      print_flg_array (stderr, array);
#endif
    }
  else
    {
      if (array->len == 4 * array->size - 1)
	{
	  unsigned char* p;

	  array->ptr = xrealloc (array->ptr, array->size + CHUNK_ALLOC);
	  array->size += CHUNK_ALLOC;
	  array->ptr[array->len / 4U] |= elem << 2 * (array->len % 4U);
	  array->len++; /* Now array->len == 4 * array->"old"size */
	  for (p = array->ptr + array->len / 4; p < array->ptr + array->size; *p = 0, p++);
#ifdef __MEMDEBUG__
	  fprintf (stderr, "size = %6zu, len = %6zu, string:",
		   array->size, array->len);
	  print_flg_array (stderr, array);
#endif
	}
      else
	{
	  array->ptr[array->len / 4U] |= elem << 2 * (array->len % 4U);
	  array->len++;
#ifdef __MEMDEBUG__
	  fprintf (stderr, "size = %6zu, len = %6zu, string:",
		   array->size, array->len);
	  print_flg_array (stderr, array);
#endif
	}
    }
}

flg_array copy_of_intflagtab (void)
{
  return internal_table;
}

/*
  array must be the address of a `flg_array' structure.
*/
static void destroy_flg_array (flg_array* array)
{
  if ((array) && (array->ptr))
    {
      free ((void*)array->ptr);
      array->ptr = NULL;
      array->size = array->len = 0;
    }
} 

void erase_flags (void)
{
  destroy_flg_array (&internal_table);
}

static void notedown_sdiff_common_lines (lin, lin);
static void notedown_sdiff_hunk (struct change *);

/* Next line number to be printed in the two input files.  */
static lin next0, next1;

/* 
 * Note down the edit-script SCRIPT in the INTERNAL_TABLE.
 */

void
notedown_sdiff_script (struct change *script)
{
  next0 = next1 = - files[0].prefix_lines;
  print_script (script, notedown_sdiff_hunk);

  notedown_sdiff_common_lines (files[0].valid_lines, files[1].valid_lines);
#ifdef _DEBUG_FLAGSTABLE_
  print_flg_array (stderr, &internal_table);
#endif
}

/* Print lines common to both files in side-by-side format.  */
static void
notedown_sdiff_common_lines (lin limit0, lin limit1)
{
  lin i0 = next0, i1 = next1;

  if (i0 != limit0 || i1 != limit1)
    {
      for (; i0 != limit0 && i1 != limit1; i0++, i1++)
	addnewelem (&internal_table, 3);

      for (; i1 != limit1; i1++)
	addnewelem (&internal_table, 2);

      for (; i0 != limit0; i0++)
	addnewelem (&internal_table, 1);
    }

  next0 = limit0;
  next1 = limit1;
}

/* Note down a hunk of an sdiff diff.
   This is a contiguous portion of a complete edit script,
   describing changes in consecutive lines.  */

static void
notedown_sdiff_hunk (struct change *hunk)
{
  lin first0, last0, first1, last1;
  register lin i, j;

  /* Determine range of line numbers involved in each file.  */
  enum changes changes =
    analyze_hunk (hunk, &first0, &last0, &first1, &last1);
  if (!changes)
    return;

  /* Note down lines up to this change.  */
  notedown_sdiff_common_lines (first0, first1);

  /* Note down ``xxx  |  xxx '' lines */
  if (changes == CHANGED)
    {
      for (i = first0, j = first1;  i <= last0 && j <= last1;  i++, j++)
	addnewelem (&internal_table, 3);
      changes = (i <= last0 ? OLD : 0) + (j <= last1 ? NEW : 0);
      next0 = first0 = i;
      next1 = first1 = j;
    }

  /* Note down ``     >  xxx '' lines */
  if (changes & NEW)
    {
      for (j = first1; j <= last1; ++j)
	addnewelem (&internal_table, 2);
      next1 = j;
    }

  /* Note down ``xxx  <     '' lines */
  if (changes & OLD)
    {
      for (i = first0; i <= last0; ++i)
	addnewelem (&internal_table, 1);
      next0 = i;
    }
}