www.digitalmars.com         C & C++   DMDScript  

D - expat.h --> expat.d - help

reply Brad Anderson <brad sankaty.com> writes:
I'm trying to wrap expat.h in D and I'm having an issue with the following
snippet:

/+
     // original expat.h

     #ifndef XMLPARSEAPI
     #if defined(_MSC_EXTENSIONS) && !defined(__BEOS__) && !defined(__CYGWIN__)
     #define XMLPARSEAPI(type) __declspec(dllimport) type __cdecl
     #else
     #define XMLPARSEAPI(type) type
     #endif
     #endif  /* not defined XMLPARSEAPI */
+/


/+
     // my attempt: expat.d
+/

     version(XMLPARSEAPI) {} else { // version(!XMLPARSEAPI)
         version(_MSC_EXTENSIONS) {
             version(__BEOS__) {} else { // version(!__BEOS__)
                 version(__CYGWIN__) {} else { // version(!__CYGWIN__)

                     // ERROR: semicolon expected following function declaration
                     void XMLPARSEAPI(type) __declspec(dllimport) type __cdecl; 

                 }
             }
         } else {    // version(!_MSC_EXTENSIONS)

             // ERROR: semicolon expected following function declaration
             void XMLPARSEAPI(type) type;
         }
     }

It doesn't help that I'm a novice at C as well as D, but any assistance is 
appreciated.

Cheers,
Brad

P.S.  Benji, I'm still looking forward to your DOM parser.  This has just
turned 
into an exercise of converting a header file.
Aug 31 2003
parent reply "Walter" <walter digitalmars.com> writes:
Since those macro names have no meaning in D anyway, I think what would work
best is to replace the shebang
with simply:

export
{
 extern (C)
 {
    ... declarations ...
 }
}

The export gives the __declspec(dllimport) semantics, and the extern (C)
does the __cdecl.

"Brad Anderson" <brad sankaty.com> wrote in message
news:biu6rn$2l53$1 digitaldaemon.com...
 I'm trying to wrap expat.h in D and I'm having an issue with the following
snippet:
 /+
      // original expat.h

      #ifndef XMLPARSEAPI
      #if defined(_MSC_EXTENSIONS) && !defined(__BEOS__) &&
!defined(__CYGWIN__)
      #define XMLPARSEAPI(type) __declspec(dllimport) type __cdecl
      #else
      #define XMLPARSEAPI(type) type
      #endif
      #endif  /* not defined XMLPARSEAPI */
 +/


 /+
      // my attempt: expat.d
 +/

      version(XMLPARSEAPI) {} else { // version(!XMLPARSEAPI)
          version(_MSC_EXTENSIONS) {
              version(__BEOS__) {} else { // version(!__BEOS__)
                  version(__CYGWIN__) {} else { // version(!__CYGWIN__)

                      // ERROR: semicolon expected following function
declaration
                      void XMLPARSEAPI(type) __declspec(dllimport) type
__cdecl;
                  }
              }
          } else {    // version(!_MSC_EXTENSIONS)

              // ERROR: semicolon expected following function declaration
              void XMLPARSEAPI(type) type;
          }
      }

 It doesn't help that I'm a novice at C as well as D, but any assistance is
 appreciated.

 Cheers,
 Brad

 P.S.  Benji, I'm still looking forward to your DOM parser.  This has just
turned
 into an exercise of converting a header file.
Sep 01 2003
parent reply Brad Anderson <brad sankaty.com> writes:
Walter wrote:
 Since those macro names have no meaning in D anyway, I think what would work
 best is to replace the shebang
 with simply:
 
 export
 {
  extern (C)
  {
     ... declarations ...
  }
 }
 
 The export gives the __declspec(dllimport) semantics, and the extern (C)
 does the __cdecl.
 
Okay, I tried this... export { extern (C) { #ifndef XMLPARSEAPI #if defined(_MSC_EXTENSIONS) && !defined(__BEOS__) && !defined(__CYGWIN__) #define XMLPARSEAPI(type) __declspec(dllimport) type __cdecl #else #define XMLPARSEAPI(type) type #endif #endif /* not defined XMLPARSEAPI */ } } results from compiler (DMD 0.69, WINXP): d:\dmd\bin\..\src\phobos\c\expat.d(72): #line integer ["filespec"]\n expected d:\dmd\bin\..\src\phobos\c\expat.d(73): #line integer ["filespec"]\n expected d:\dmd\bin\..\src\phobos\c\expat.d(73): semicolon expected following function declaration d:\dmd\bin\..\src\phobos\c\expat.d(73): Declaration expected, not '&&' Line 72 is: #ifndef XMLPARSEAPI Do I just need to get rid of all the macros? If so, how do I handle the two options? --- And throughout the rest of the header, I see declarations using the XMLPARSEAPI(type) from above. Thoughts on those declarations? ex: /* This is called for an element declaration. See above for description of the model argument. It's the caller's responsibility to free model when finished with it. */ typedef void (*XML_ElementDeclHandler) (void *userData, const XML_Char *name, XML_Content *model); XMLPARSEAPI(void) XML_SetElementDeclHandler(XML_Parser parser, XML_ElementDeclHandler eldecl); Thanks, Brad
Sep 02 2003
next sibling parent reply Brad Anderson <brad sankaty.com> writes:
one of Pavel's old posts (9/7/02):

Walter wrote:

 In general, it is not. However, most macros fall into predictable patterns
 like:

     #define ABC    3

 and can be handled appropriately. Ones that don't would just be ignored.
 This means that the tool won't be perfect, but could handle 95% of the
 conversion chore.
Problems start when it comes to things like this: #define FOO 666 #define BAR FOO // macro? constant? #define BAZ MACRO(BAR/FOO) // now what? I'd say that, unfortunately, WinAPI headers have quite a lot of such things. And one still have to distinguish between conditional compilation flags and macro... -------------- So is this header just too involved to wrap in D, and a rewrite would be more appropriate? BA
Sep 02 2003
parent "Walter" <walter digitalmars.com> writes:
"Brad Anderson" <brad sankaty.com> wrote in message
news:bj3no8$1b31$1 digitaldaemon.com...
 So is this header just too involved to wrap in D, and a rewrite would be
more
 appropriate?
Not at all. I'd start with compiling it with -e -l and expand all the macros.
Sep 03 2003
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Brad Anderson" <brad sankaty.com> wrote in message
news:bj3n1p$1a51$1 digitaldaemon.com...
 Walter wrote:
 Since those macro names have no meaning in D anyway, I think what would
work
 best is to replace the shebang
 with simply:

 export
 {
  extern (C)
  {
     ... declarations ...
  }
 }

 The export gives the __declspec(dllimport) semantics, and the extern (C)
 does the __cdecl.
Okay, I tried this... export { extern (C) { #ifndef XMLPARSEAPI #if defined(_MSC_EXTENSIONS) && !defined(__BEOS__) &&
!defined(__CYGWIN__)
      #define XMLPARSEAPI(type) __declspec(dllimport) type __cdecl
      #else
      #define XMLPARSEAPI(type) type
      #endif
      #endif  /* not defined XMLPARSEAPI */
    }
 }


 results from compiler (DMD 0.69, WINXP):
 d:\dmd\bin\..\src\phobos\c\expat.d(72): #line integer ["filespec"]\n
expected
 d:\dmd\bin\..\src\phobos\c\expat.d(73): #line integer ["filespec"]\n
expected
 d:\dmd\bin\..\src\phobos\c\expat.d(73): semicolon expected following
function
 declaration
 d:\dmd\bin\..\src\phobos\c\expat.d(73): Declaration expected, not '&&'

 Line 72 is: #ifndef XMLPARSEAPI

 Do I just need to get rid of all the macros?  If so, how do I handle the
two
 options?
D doesn't have a macro preprocessor, nor does it have __declspec(dllimport).
 And throughout the rest of the header, I see declarations using the
 XMLPARSEAPI(type) from above.  Thoughts on those declarations?

 ex:
 /* This is called for an element declaration. See above for
     description of the model argument. It's the caller's responsibility
     to free model when finished with it.
 */
 typedef void (*XML_ElementDeclHandler) (void *userData,
                                          const XML_Char *name,
                                          XML_Content *model);

 XMLPARSEAPI(void)
 XML_SetElementDeclHandler(XML_Parser parser,
   XML_ElementDeclHandler eldecl);
alias void function(void *, XML_Char *, XML_Content *) XML_ElementDeclHandler; export extern (C) void XML_SetElementDeclHandler(XML_Parser parse, XML_ElementDeclHandler eldecl);
Sep 03 2003
parent reply Brad Anderson <brad sankaty.com> writes:
Okay, does anyone have any thoughts about converting this mess:


typedef struct {
   void *(*malloc_fcn)(size_t size);
   void *(*realloc_fcn)(void *ptr, size_t size);
   void (*free_fcn)(void *ptr);
} XML_Memory_Handling_Suite;



Thanks,
Brad

P.S.  Walter, thanks for the page htomodule.html.  Along with Mike Wynn's
Deimos 
page, I'm getting places without having much experience with header files in
the 
past.  Now about all that free time I want to have...
Sep 18 2003
parent "Walter" <walter digitalmars.com> writes:
"Brad Anderson" <brad sankaty.com> wrote in message
news:bkdlai$1k3g$1 digitaldaemon.com...
 Okay, does anyone have any thoughts about converting this mess:


 typedef struct {
    void *(*malloc_fcn)(size_t size);
    void *(*realloc_fcn)(void *ptr, size_t size);
    void (*free_fcn)(void *ptr);
 } XML_Memory_Handling_Suite;
struct XML_Memory_Handling_Suite { void *(*malloc_fcn)(size_t size); void *(*realloc_fcn)(void *ptr, size_t size); void (*free_fcn)(void *ptr); }
Oct 25 2003