[trunk] rework actual code to handle DOS/UNIX end of lines in a transparent manner.
This will help cross compilation cases
This commit is contained in:
parent
7aee50de97
commit
9155e98911
@ -29,13 +29,13 @@
|
||||
*
|
||||
* Created on: 25 juil. 2011
|
||||
* Author: mickael
|
||||
* BASELINE MUST BE GENERATED BY UNIX PLATFORM REGARDING TO THE CRLF PROBLEM
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "opj_getopt.h"
|
||||
|
||||
@ -117,9 +117,11 @@ int main(int argc, char **argv)
|
||||
{
|
||||
test_cmp_parameters inParam;
|
||||
FILE *fbase=NULL, *ftest=NULL;
|
||||
int chbase, chtest;
|
||||
int same = 1;
|
||||
unsigned long l=1, pos;
|
||||
char lbase[256];
|
||||
char strbase[256];
|
||||
char ltest[256];
|
||||
char strtest[256];
|
||||
|
||||
if( parse_cmdline_cmp(argc, argv, &inParam) == 1 )
|
||||
{
|
||||
@ -150,86 +152,24 @@ int main(int argc, char **argv)
|
||||
}
|
||||
printf("Ok.\n");
|
||||
|
||||
pos=(unsigned long)ftell(fbase);
|
||||
|
||||
while(!feof(fbase))
|
||||
while (fgets(lbase, sizeof(lbase), fbase) && fgets(ltest,sizeof(ltest),ftest))
|
||||
{
|
||||
chbase = fgetc(fbase);
|
||||
if(ferror(fbase))
|
||||
int nbase = sscanf(lbase, "%255[^\r\n]", strbase);
|
||||
int ntest = sscanf(ltest, "%255[^\r\n]", strtest);
|
||||
assert( nbase != 255 && ntest != 255 );
|
||||
if( nbase != 1 || ntest != 1 )
|
||||
{
|
||||
printf("Error reading base file.\n");
|
||||
fprintf(stderr, "could not parse line from files\n" );
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
chtest = fgetc(ftest);
|
||||
if(ferror(ftest))
|
||||
if( strcmp( strbase, strtest ) != 0 )
|
||||
{
|
||||
printf("Error reading test file.\n");
|
||||
fprintf(stderr,"<%s> vs. <%s>\n", strbase, strtest);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* CRLF problem (Baseline must be always generated by unix platform)*/
|
||||
if (chbase == '\n' && chtest == '\r')
|
||||
if (fgetc(ftest) == '\n')
|
||||
chtest = '\n';
|
||||
|
||||
if(chbase != chtest)
|
||||
{
|
||||
int nbytes = 2048;
|
||||
int CRLF_shift=1;
|
||||
char *strbase, *strtest, *strbase_d, *strtest_d;
|
||||
|
||||
printf("Files differ at line %lu:\n", l);
|
||||
fseek(fbase,(long)pos,SEEK_SET);
|
||||
|
||||
/* Take into account CRLF characters when we write \n into
|
||||
// dump file when we used WIN platform*/
|
||||
#ifdef _WIN32
|
||||
CRLF_shift = 2;
|
||||
fseek(ftest,pos + l - 1,SEEK_SET);
|
||||
#else
|
||||
fseek(ftest,(long)pos,SEEK_SET);
|
||||
#endif
|
||||
|
||||
strbase = (char *) malloc((size_t)nbytes + 1);
|
||||
strtest = (char *) malloc((size_t)nbytes + 1);
|
||||
|
||||
if (fgets(strbase, nbytes, fbase) == NULL)
|
||||
fprintf(stderr,"\nWARNING: fgets return a NULL value");
|
||||
else
|
||||
{
|
||||
if (fgets(strtest, nbytes, ftest) == NULL)
|
||||
fprintf(stderr,"\nWARNING: fgets return a NULL value");
|
||||
else
|
||||
{
|
||||
strbase_d = (char *) malloc(strlen(strbase)+1);
|
||||
strtest_d = (char *) malloc(strlen(strtest)+1);
|
||||
strncpy(strbase_d, strbase, strlen(strbase)-1);
|
||||
strncpy(strtest_d, strtest, strlen(strtest)-(size_t)CRLF_shift);
|
||||
strbase_d[strlen(strbase)-1] = '\0';
|
||||
strtest_d[strlen(strtest)-(size_t)CRLF_shift] = '\0';
|
||||
printf("<%s> vs. <%s>\n", strbase_d, strtest_d);
|
||||
free(strbase_d);
|
||||
free(strtest_d);
|
||||
}
|
||||
}
|
||||
|
||||
free(strbase);
|
||||
free(strtest);
|
||||
|
||||
same = 0;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (chbase == '\n')
|
||||
{
|
||||
l++;
|
||||
pos = (unsigned long)ftell(fbase);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
same = 0;
|
||||
printf("\n***** TEST SUCCEED: Files are the same. *****\n");
|
||||
cleanup:
|
||||
/*Close File*/
|
||||
|
Loading…
Reference in New Issue
Block a user