File: diffutils.cpp

package info (click to toggle)
xxdiff 1%3A5.1%2Bgit20250320%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,448 kB
  • sloc: cpp: 18,519; python: 6,143; ansic: 1,546; sh: 1,532; perl: 308; lex: 284; yacc: 279; lisp: 250; tcl: 213; makefile: 91
file content (282 lines) | stat: -rw-r--r-- 6,989 bytes parent folder | download | duplicates (4)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* -*- c-file-style: "xxdiff" -*- */
/******************************************************************************\
 * $RCSfile$
 *
 * Copyright (C) 1999-2003  Martin Blais <blais@furius.ca>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 ******************************************************************************/

/*==============================================================================
 * EXTERNAL DECLARATIONS
 *============================================================================*/

#include <getopt.h>
#include <diffutils.h>

#include <iostream>

#include <stdio.h>
#include <setjmp.h>

// I haven't tested on any platform that doesn't have vsnprintf yet.
// I just enable this for now.
// Please log a bug if this causes problems for your compilation.
#define HAVE_VPRINTF

#ifdef HAVE_VPRINTF

#if __STDC__
#include <stdarg.h>
#define VA_START(args, lastarg) va_start(args, lastarg)
#else /* !__STDC__ */
#include <varargs.h>
#define VA_START(args, lastarg) va_start(args)
#endif /* !__STDC__ */

#else /* !HAVE_VPRINTF */

#ifdef HAVE_DOPRNT
#define va_alist args
#define va_dcl int args;
#else /* !HAVE_DOPRNT */
#define va_alist a1, a2, a3, a4, a5, a6, a7, a8
#define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
#endif /* !HAVE_DOPRNT */

#endif /* !HAVE_VPRINTF */

extern "C" {

extern int diffutils_diff_main( int, char** );
extern int diffutils_diff3_main( int, char** );
extern FILE* outfile;

}

/*==============================================================================
 * LOCAL DECLARATIONS
 *============================================================================*/

namespace {

QTextStream* streamStdout = 0;
QTextStream* streamStderr = 0;

// For non-local exit.
jmp_buf exit_env;

char* buffer = 0;
int buflen = 0;
const int BUFINC = 1024;

}

/*==============================================================================
 * PUBLIC FUNCTIONS
 *============================================================================*/

//#define DBGSS

extern "C" {

/*------------------------------------------------------------------------------
 */
int buffer_vsnprintf( const char *format, va_list arg )
{
   int nb;
   while ( 1 ) {
      nb = vsnprintf( buffer, buflen, format, arg );
      if ( nb >= 0 && nb < buflen ) {
         break;
      }
      buflen = buflen + BUFINC;
      buffer = (char*) realloc( buffer, sizeof(char) * buflen );
   }
   return nb;
}

/*------------------------------------------------------------------------------
 */
void xxdiff_diff_fprintf( FILE* fp, char *message, ... )
{
   va_list args;
   VA_START( args, message );
   buffer_vsnprintf( message, args );
   va_end (args);
   
   if ( fp == outfile ) {
      XX_ASSERT( streamStdout );
      *streamStdout << buffer;
   }
   else {
      XX_ASSERT( streamStderr );
      *streamStderr << buffer;
   }
}

/*------------------------------------------------------------------------------
 */
void xxdiff_diff_printf( char* message, ... )
{
   va_list args;
   VA_START( args, message );
   buffer_vsnprintf( message, args );
   va_end (args);
   
   XX_ASSERT( streamStdout );
   *streamStdout << buffer;
}

/*------------------------------------------------------------------------------
 */
size_t xxdiff_diff_fwrite(
   const void* ptr,
   size_t      size,
   size_t      nmemb,
   FILE*       stream
)
{
   if ( stream == outfile ) {
      XX_ASSERT( streamStdout );
      streamStdout->writeRawBytes( 
         static_cast<const char*>(ptr), size * nmemb
      );
   }
   else {
      XX_ASSERT( streamStderr );
      streamStderr->writeRawBytes(
         static_cast<const char*>(ptr), size * nmemb
      );
   }
   return size * nmemb;
}

/*------------------------------------------------------------------------------
 */
ssize_t xxdiff_diff_write( 
   int         fd,
   const void* buf,
   size_t      count
)
{
   if ( fd == 1 ) {
      XX_ASSERT( streamStdout );
      streamStdout->writeRawBytes( static_cast<const char*>(buf), count );
   }
   else {
      XX_ASSERT( streamStderr );
      streamStderr->writeRawBytes( static_cast<const char*>(buf), count );
   }
   return count;
}

/*------------------------------------------------------------------------------
 */
void xxdiff_diff_exit( int x )
{
   longjmp( exit_env, 1000 + x );
   // bye bye, internal diff!
}

} // extern "C"

XX_NAMESPACE_BEGIN

//------------------------------------------------------------------------------
//
XxDiffutils::XxDiffutils()
{}

//------------------------------------------------------------------------------
//
XxDiffutils::~XxDiffutils()
{
   if ( _istream ) {
      delete _istream;
   }
}

//------------------------------------------------------------------------------
//
void XxDiffutils::diff( QStringList& out_args )
{
   diff_fun( out_args, diffutils_diff_main );
}

//------------------------------------------------------------------------------
//
void XxDiffutils::diff3( QStringList& out_args )
{
   diff_fun( out_args, diffutils_diff3_main );
}

//------------------------------------------------------------------------------
//
void XxDiffutils::diff_fun( 
   QStringList& out_args, 
   int(*main_fun)(int, char**) 
)
{
   QByteArray qs_cout;
   QTextStream oss_cout( qs_cout, IO_WriteOnly );
   QByteArray qs_cerr;
   QTextStream oss_cerr( qs_cerr, IO_WriteOnly );

   streamStdout = & oss_cout;
   streamStderr = & oss_cerr;

   int argc = 0;
   const char** argv =
      (const char**) malloc( sizeof(char*) * (out_args.count() + 1) );
   argv[argc++] = "fakeExeName";
   for ( QStringList::Iterator it = out_args.begin();
         it != out_args.end();
         ++it ) {
      argv[argc++] = strdup( (*it).toLocal8Bit().constData() );
   }
   argv[argc] = 0;

   int i = setjmp( exit_env );
   if ( i == 0 ) {
      optind = 0;
      main_fun( argc, argv );
   }

   for ( const char** dargv = argv; *dargv; ++dargv ) {
      free( const_cast<char*>( *dargv ) );
   }
   free( argv );

   oss_cout << flush;
   oss_cerr << flush;
   _outputB = qs_cout /*+ qs_cerr*/;
   _istream = new QTextStream( _outputB, IO_ReadOnly );
}

//------------------------------------------------------------------------------
//
QString XxDiffutils::readLine()
{
   if ( !_istream ) {
      return QString::null;
   }

   return _istream->readLine();
}

XX_NAMESPACE_END