www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - .h to .d advice needed

reply "Emp" <empty nomail.com> writes:
I'm trying to translate NIDAQmx.h to a .d which D can use.
But I'm not really sure whether this is usefull or not. The lib is only msvc 
compatible, is this a problem?
I really know like null about linking (thus liking D :).

If it is usefull to translate it to D, I'm afraid of this header thing.
It is more than 6000 lines tall :( and, next to what is on the website, I 
have no clue :)

simple things like:

#define FOO 1
to:
const int FOO = 1;

Can't be done by a simple replace and manually adding the '=' is undoable 
for 6000 lines.
Is there some program which can do these things automated?
Thx for any advice/help. 
Aug 06 2006
next sibling parent "Lionello Lunesu" <lio lunesu.remove.com> writes:
Yes, it's called "htod" and available on the digitalmars.com webpage.

L.

"Emp" <empty nomail.com> wrote in message 
news:eb6e25$i35$1 digitaldaemon.com...
 I'm trying to translate NIDAQmx.h to a .d which D can use.
 But I'm not really sure whether this is usefull or not. The lib is only 
 msvc compatible, is this a problem?
 I really know like null about linking (thus liking D :).

 If it is usefull to translate it to D, I'm afraid of this header thing.
 It is more than 6000 lines tall :( and, next to what is on the website, I 
 have no clue :)

 simple things like:

 #define FOO 1
 to:
 const int FOO = 1;

 Can't be done by a simple replace and manually adding the '=' is undoable 
 for 6000 lines.
 Is there some program which can do these things automated?
 Thx for any advice/help.
 

Aug 07 2006
prev sibling parent reply nobody <nobody mailinator.com> writes:
Emp wrote:
...

 Is there some program which can do these things automated?
 Thx for any advice/help. 

htod seems to be the tool you are looking for. http://www.digitalmars.com/d/htod.html While D is binary compatible with C code, it cannot compile C code nor C header files. In order for D to link with C code, the C declarations residing in C header files need to be converted to a D module. htod is a migration tool to aid in convering C header files. For people reading this thread who are not familiar with RegExps changing #define FOO 1 to const int FOO = 1; is easy if you take the time to understand how RegExp can generalize many patterns. Regular and replacement expressions should be available in any basic code editor. They are also included in Phobos in std.regex. The docs contain a link to this page which documents D's variation of regexp: http://www.digitalmars.com/ctg/regular.html Decimal constants [0-9]+ Octal Constants 0[0-9]+ Hexadecimal Constants 0[xX][0-9A-F] Variable Names start alpha or under and allow digits, '-' and '_' [a-z_][0-9a-z_-]+ They are seperated by one or more spaces or tabs [ \t]+ Putting it all together #DEFINE[ \t]+([a-z_][0-9a-z_-]+)[ \t]+(([0-9]+)|(0[0-9]+)|(0[x][0-9A-F])) Now you can search (ignoring case) and replace all those patterns with const int $1 = $2; So #DEFINE FOO 1 #DEFINE FOO-bar 010 #DEFINE FOO_baz 0x10 becomes const int FOO = 1; const int foo-bar = 010; const int __fOo_bAz = 0x10;
Aug 07 2006
next sibling parent nobody <nobody mailinator.com> writes:
nobody wrote:

Horrible typo. Instead of

   #DEFINE FOO_baz 0x10

 becomes

   const int __fOo_bAz = 0x10;

I meant #DEFINE __FOO_baz 0x10 becomes const int __FOO_baz = 0x10; Sorry!
Aug 07 2006
prev sibling parent reply "Emp" <empty nomail.com> writes:
Thx! I did not know about these kind of search commands before :)
I still think it would be a hellish job for me to do everything through 
search commands (I did a few :) love them regexp)
H2d would be a nice help but I see it is a linux only app.(winxp here)


"nobody" <nobody mailinator.com> wrote in message 
news:eb6ton$v9q$1 digitaldaemon.com...
 Emp wrote:
 ...

 Is there some program which can do these things automated?
 Thx for any advice/help.

htod seems to be the tool you are looking for. http://www.digitalmars.com/d/htod.html While D is binary compatible with C code, it cannot compile C code nor C header files. In order for D to link with C code, the C declarations residing in C header files need to be converted to a D module. htod is a migration tool to aid in convering C header files. For people reading this thread who are not familiar with RegExps changing #define FOO 1 to const int FOO = 1; is easy if you take the time to understand how RegExp can generalize many patterns. Regular and replacement expressions should be available in any basic code editor. They are also included in Phobos in std.regex. The docs contain a link to this page which documents D's variation of regexp: http://www.digitalmars.com/ctg/regular.html Decimal constants [0-9]+ Octal Constants 0[0-9]+ Hexadecimal Constants 0[xX][0-9A-F] Variable Names start alpha or under and allow digits, '-' and '_' [a-z_][0-9a-z_-]+ They are seperated by one or more spaces or tabs [ \t]+ Putting it all together #DEFINE[ \t]+([a-z_][0-9a-z_-]+)[ \t]+(([0-9]+)|(0[0-9]+)|(0[x][0-9A-F])) Now you can search (ignoring case) and replace all those patterns with const int $1 = $2; So #DEFINE FOO 1 #DEFINE FOO-bar 010 #DEFINE FOO_baz 0x10 becomes const int FOO = 1; const int foo-bar = 010; const int __fOo_bAz = 0x10;

Aug 07 2006
next sibling parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Emp wrote:
 H2d would be a nice help but I see it is a linux only app.(winxp here)

Erm, actually look again. Its a Windows only app right now. Listed under the "Bugs" header is "no Linux version yet." -- Chris Nicholson-Sauls
Aug 07 2006
prev sibling parent reply clayasaurus <clayasaurus gmail.com> writes:
Emp wrote:
 Thx! I did not know about these kind of search commands before :)
 I still think it would be a hellish job for me to do everything through 
 search commands (I did a few :) love them regexp)
 H2d would be a nice help but I see it is a linux only app.(winxp here)
 

The dsource h2d project is linux only, the digitalmars h2d is windows only.
Aug 07 2006
parent reply "Emp" <empty nomail.com> writes:
Thx, I thought there was only one, the dsource one that is.

I've succesfully run the windows version on my headerfile, but I'm a bit 
suspicious about the output.
(cmd: 'htod NIDAQmx.h' and it should be an ANSI C header)
All the error codes have been commented out:
//C     #define DAQmxErrorCOCannotKeepUpInHWTimedSinglePoint 
(-209805)
And '-209805' is nowhere else to be found (in a global search).

Another question(not about the header file, but some example code :)
How do I translate this to D?
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) 
goto Error; else

It is used like this:
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));

Thx.
Aug 07 2006
parent reply Derek Parnell <derek nomail.afraid.org> writes:
On Tue, 8 Aug 2006 02:53:18 +0200, Emp wrote:

 Thx, I thought there was only one, the dsource one that is.
 
 I've succesfully run the windows version on my headerfile, but I'm a bit 
 suspicious about the output.
 (cmd: 'htod NIDAQmx.h' and it should be an ANSI C header)
 All the error codes have been commented out:
 //C     #define DAQmxErrorCOCannotKeepUpInHWTimedSinglePoint 
 (-209805)
 And '-209805' is nowhere else to be found (in a global search).
 
 Another question(not about the header file, but some example code :)
 How do I translate this to D?
 #define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) 
 goto Error; else
 
 It is used like this:
 DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
 
 Thx.

Yuck! Why do people make it hard for themselves ;-) void DAQmxErrChk( int(?) functionCall ) { if (DAQmxFailed (error = functionCall)) throw new DAQException("whatever"); } Then replace the 'Error:' label with ... scope(failure) { // Handle any exception thrown during this scope. if (error == ...) ... } -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 8/08/2006 10:56:34 AM
Aug 07 2006
parent reply "Emp" <empty nomail.com> writes:
Thx!
Now if only I would understand why there are  lines like these in the 
original header file:

//*** Values for DAQmx_Read_WaitMode ***
//*** Value set WaitMode ***
#define DAQmx_Val_WaitForInterrupt 12523 // Wait For Interrupt
#define DAQmx_Val_Poll 12524 // Poll
#define DAQmx_Val_Yield 12525 // Yield
#define DAQmx_Val_Sleep 12547 // Sleep
//*** Values for DAQmx_Write_WaitMode ***
//*** Value set WaitMode2 ***
#define DAQmx_Val_Poll 12524 // Poll
#define DAQmx_Val_Yield 12525 // Yield
#define DAQmx_Val_Sleep 12547 // Sleep

See the double defining. Any idea of why is this done?
I just removed all the double consts, all 100 of them :/
:)


 Yuck! Why do people make it hard for themselves ;-)

 void DAQmxErrChk( int(?) functionCall )
 {
   if (DAQmxFailed (error = functionCall))
      throw new DAQException("whatever");
 }

 Then replace the 'Error:' label with ...

  scope(failure)
  {
    // Handle any exception thrown during this scope.
    if (error == ...) ...
  }


 -- 
 Derek
 (skype: derek.j.parnell)
 Melbourne, Australia
 "Down with mediocrity!"
 8/08/2006 10:56:34 AM 

Aug 07 2006
parent reply Derek Parnell <derek nomail.afraid.org> writes:
On Tue, 8 Aug 2006 03:52:14 +0200, Emp wrote:

 Thx!
 Now if only I would understand why there are  lines like these in the 
 original header file:
 
 //*** Values for DAQmx_Read_WaitMode ***
 //*** Value set WaitMode ***
 #define DAQmx_Val_WaitForInterrupt 12523 // Wait For Interrupt
 #define DAQmx_Val_Poll 12524 // Poll
 #define DAQmx_Val_Yield 12525 // Yield
 #define DAQmx_Val_Sleep 12547 // Sleep
 //*** Values for DAQmx_Write_WaitMode ***
 //*** Value set WaitMode2 ***
 #define DAQmx_Val_Poll 12524 // Poll
 #define DAQmx_Val_Yield 12525 // Yield
 #define DAQmx_Val_Sleep 12547 // Sleep
 
 See the double defining. Any idea of why is this done?

Poor quality control. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 8/08/2006 11:55:18 AM
Aug 07 2006
parent reply "Emp" <empty nomail.com> writes:
 Poor quality control.

HtoD commented all the error handling code out, so your nice code doesn't work :( (main.d(46): undefined identifier DAQmxFailed) Why did htod do this? //from header file /****************************************************************************** *** NI-DAQmx Error Codes ***************************************************** ************************************************************** ***************///C #define DAQmxSuccess (0)//C #define DAQmxFailed(error) ((error)<0)// Error and Warning Codes//C #define DAQmxErrorCOCannotKeepUpInHWTimedSinglePoint(-209805)//C #define DAQmxErrorWaitForNextSampClkDetected3OrMoreSampClks(-209803)
Aug 07 2006
parent reply "Emp" <empty nomail.com> writes:
let me retype that:

 //from header file
/**********************************
*** NI-DAQmx Error Codes
 *********************************************************/
//C     #define DAQmxSuccess                                  (0)
//C     #define DAQmxFailed(error)                           ((error)<0)
// Error and Warning Codes
//C     #define DAQmxErrorCOCannotKeepUpInHWTimedSinglePoint 
(-209805)
//C     #define DAQmxErrorWaitForNextSampClkDetected3OrMoreSampClks 
(-209803)
Aug 07 2006
parent reply "Emp" <empty nomail.com> writes:
I changed it to:

const DAQmxSuccess=0;

bool DAQmxFailed(int error){
 if (error<0) return true;
 return false;
}

And changed your code with:
throw new StringException()

So now I get this error:
C:\Program Files\...\NIDAQmx.lib
 Error 43: Not a Valid Library File
:D
I'm beginning to get this feeling that maybe everything was for nothing :(


 let me retype that:

 //from header file
 /**********************************
 *** NI-DAQmx Error Codes
 *********************************************************/
 //C     #define DAQmxSuccess                                  (0)
 //C     #define DAQmxFailed(error)                           ((error)<0)
 // Error and Warning Codes
 //C     #define DAQmxErrorCOCannotKeepUpInHWTimedSinglePoint (-209805)
 //C     #define DAQmxErrorWaitForNextSampClkDetected3OrMoreSampClks
 (-209803)

Aug 08 2006
parent reply Don Clugston <dac nospam.com.au> writes:
NEmp wrote:
 I changed it to:
 
 const DAQmxSuccess=0;
 
 bool DAQmxFailed(int error){
  if (error<0) return true;
  return false;
 }
 
 And changed your code with:
 throw new StringException()
 
 So now I get this error:
 C:\Program Files\...\NIDAQmx.lib
  Error 43: Not a Valid Library File
 :D
 I'm beginning to get this feeling that maybe everything was for nothing :(

Not at all. I've been using NI-DAQmx with D for months. I can confirm that it works perfectly after translation. I'd post you my translation, except that would probably violate NI's copyright restrictions. You need to run the 'coffimplib' program (download from Digital Mars) on NIDAQmx.lib, to turn it from a Microsoft-format import lib into Digital Mars/Watcom/etc format. BTW, the reason all those #defines are duplicated is that they really should have been enums. Good to see another NI user.
Aug 08 2006
parent reply "Emp" <empty nomail.com> writes:
Oh Yeah! my first readout from my virtual card :D.
Thx, everybody.

 Not at all. I've been using NI-DAQmx with D for months. I can confirm that 
 it works perfectly after translation. I'd post you my translation, except 
 that would probably violate NI's copyright restrictions.

Could you maybe tell me how you handled the error handling code which htod commented out?
 You need to run the 'coffimplib' program (download from Digital Mars) on 
 NIDAQmx.lib, to turn it from a Microsoft-format import lib into Digital 
 Mars/Watcom/etc format.

 BTW, the reason all those #defines are duplicated is that they really 
 should have been enums.

You mean that the original (labview) headers were enums?
 Good to see another NI user.

(I will be using the physical card for the readout of a purkinje eye tracker, pretty nifty stuff :)
Aug 08 2006
parent Don Clugston <dac nospam.com.au> writes:
Emp wrote:
 Oh Yeah! my first readout from my virtual card :D.
 Thx, everybody.
 
 Not at all. I've been using NI-DAQmx with D for months. I can confirm that 
 it works perfectly after translation. I'd post you my translation, except 
 that would probably violate NI's copyright restrictions.

Could you maybe tell me how you handled the error handling code which htod commented out?

I did my translation before htod existed. It's pretty simple stuff though... bool DAQmxFailed(int error) { return error<0; } and all the errors should be constants. const DAQmxSuccess = 0; const DAQmxErrorCOCannotKeepUpInHWTimedSinglePoint = -209805; .. repeat 2000 times or so. <g>
 
 You need to run the 'coffimplib' program (download from Digital Mars) on 
 NIDAQmx.lib, to turn it from a Microsoft-format import lib into Digital 
 Mars/Watcom/etc format.

 BTW, the reason all those #defines are duplicated is that they really 
 should have been enums.

You mean that the original (labview) headers were enums?

No, but they should have been. The original headers seem to be written in very ancient C (about 1982 era :-) ).
 
 Good to see another NI user.

(I will be using the physical card for the readout of a purkinje eye tracker, pretty nifty stuff :)

Cool! I think D is fantastic for this stuff.
Aug 10 2006