#36 Cleanup: explicit casts, native booleans (C++), minor fix

open
nobody
None
5
2005-07-16
2005-07-16
No

Html Tidy compiles as C++ with only a few minor
changes - mostly using explicit casts for type
conversion. Why bother?
- Explicit casts make code easier to read
- C++ provides compiler checks (lint
- step towards object version
- required for .NET native mode.

These changes pass the regression tests using Visual
Studio .NET 2.0 beta 2, when compiled as C (with
USE_NATIVE_BOOL undefined) and as C++ (with
USE_NATIVE_BOOL defined).

1. Explicit casts for type conversion:

C++ is especially fussy about type conversions. In
general, it's a good idea to be perfectly explicit about
integer <=> boolean, char * <=> void *, and so on.
Explicit casts can only help the compiler, and they
make the code easier to read.

1a. Minor fixes:

In tidylib.c, the functions tidySetOptionCallback() and
tidySetReportFilter() are defined as Bool, but in the
production source they return yes or -EINVAL (which
evaluates to -22). I don't think it's safe to assume -22
will evaluate to "no" under all platforms, although there
is no difference in my regression tests.

2. Use native boolean where available:

The current Bool enum is an inspired workaround for C
compiler portability, but it adds extra overhead when
your compiler (or language, in the case of C++) has a
built-in boolean type. To minimize side effects, I've
added a new preprocessor definition,
USE_NATIVE_BOOL. When this is defined, the code
uses native booleans:

#ifdef USE_NATIVE_BOOL
typedef bool Bool;
#define no false
#define yes true
#else /* otherwise, it defaults to the original enum: */
typedef enum
{
no,
yes
} Bool;
#endif

To simplify the integer / flag / Bool conversion (usually
at the return of a subroutine), I added a new macro,
ToBool(x).

#ifdef USE_NATIVE_BOOL
#define ToBool(x) ((x) != 0)
#else /* otherwise, it defaults to the original enum: */
#define ToBool(x) (Bool)(x)
#endif

CHANGE LIST

Changed files: platform.h, tidy.c, clean.c, config.c,
fileio.c, lexer.c, parser.c, pprint.c, streamio.c, tags.c,
tidylib.c
Diff files are attached.

1. Explicit casts for type conversion:

clean.c 2220 prop->name = (tmbstr)MemAlloc( 8 +
tmbstrlen(enc) + 1 );
config.c 610 tmbstr p = (tmbstr)MemAlloc( len );
lexer.c 1479 TidyDoctypeModes dtmode =
(TidyDoctypeModes)cfg(doc, TidyDoctypeMode);
streamio.c 118 in->charbuf = (tchar *)MemAlloc(sizeof
(tchar) * in->bufsize);
streamio.c 498 in->charbuf = (tchar *)MemRealloc(in-
>charbuf, sizeof(tchar) * ++(in->bufsize));
tidylib.c 875 tidyBufAttach( &inbuf, (byte*)content,
tmbstrlen(content)+1 );
tidy.c 67 char *c1buf = (char *)malloc(l1+1);
tidy.c 68 char *c2buf = (char *)malloc(l2+1);
tidy.c 86 char *c1buf = (char *)malloc(l1+1);
tidy.c 87 char *c2buf = (char *)malloc(l2+1);
tidy.c 88 char *c3buf = (char *)malloc(l3+1);
tidy.c 278 name = (tmbstr)malloc(len+1);
tidy.c 314 escpName = (tmbstr)malloc(len+1);
tidy.c 757 val = (tmbstr)malloc(len+1);
tidy.c 770 tmbstr val = (tmbstr)malloc(1+strlen(d->vals));

tidylib.c 184 /* return -EINVAL; Function returns Bool,
not int*/
tidylib.c 185 return no;
tidylib.c 616 /* return -EINVAL; Function returns Bool,
not int*/
tidylib.c 617 return no;

tidy.c 343 /* C++ C2675: unary '++' : 'CmdOptCategory'
does not define this operator or a conversion to a type
acceptable to the predefined operator */
tidy.c 344 #ifdef __cplusplus
tidy.c 345 for( cat=CmdOptCatFIRST; cat!
=CmdOptCatLAST; cat = (CmdOptCategory)(((int)cat)
+1))
tidy.c 346 #else
tidy.c 347 for( cat=CmdOptCatFIRST; cat!
=CmdOptCatLAST; ++cat)
tidy.c 348 #endif

2. Use native boolean where available:

config.c 1011 SetOptionBool( doc, entry->id, ToBool
(flag) );
config.c 1011 SetOptionBool( doc, entry->id, ToBool
(flag) );
fileio.c 46 isEOF = ToBool(feof( fin->fp ));
lexer.c 195 return ToBool(map & white);
lexer.c 201 return ToBool(map & newline);
lexer.c 219 return ToBool(map & letter);
lexer.c 225 return ToBool(map & namechar);
lexer.c 572 return ToBool(map & lowercase);
lexer.c 579 return ToBool(map & uppercase);
parser.c 78 return ToBool(node->tag->model &
CM_NEW);
pprint.c 1001 Bool scriptlets = ToBool(cfg(doc,
TidyWrapScriptlets));
tags.c 507 deleteIt = ToBool( curr->model &
CM_EMPTY );
tags.c 511 deleteIt = ToBool( curr->model &
CM_INLINE );

Discussion

  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    platform.h changes

     
  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    clean.c changes

     
  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    config.c changes

     
  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    fileio.c changes

     
  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    lexer.c changes

     
  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    parser.c changes

     
  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    pprint.c changes

     
  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    streamio.c changes

     
  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    tags.c changes

     
  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    tidylib.c changes

     
  • Eric Hartwell

    Eric Hartwell - 2005-07-16

    tidy.c changes

     
  • Björn Höhrmann

    Logged In: YES
    user_id=188003

    Looks good to me (though I did not check the patches for
    tidylib.c/tidy.c, they are not yet added to the bug).

     
  • Arnaud Desitter

    Arnaud Desitter - 2005-08-17

    Logged In: YES
    user_id=566665

    Mostly applied so far.

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks