www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - converting c++ code to D

reply clayasaurus <clayasaurus gmail.com> writes:
Hello. Sorry for the long thread but I've recently been trying to 
convert C++ programs to the D and have stumbled across some c++ code 
that i'm not quite sure how to translate to D.

----------------------------------------------------------------------
ex 1:
c++:
bits[i >> 5] &= ~(1 << (i & 31));
D: ? i don't know if D has the same binary operations as c++ :-/
----------------------------------------------------------------------

----------------------------------------------------------------------
ex 2: new
c++:
uint *bits;
bits = new uint[size];
D:
uint* bits;
bits.length = size;
i'm not sure if this is correct. or else can i use
bits = new uint[size]; just like the c++ code?
----------------------------------------------------------------------

----------------------------------------------------------------------
ex3: memset
c++:
uint *bits;
memset(bits, 0, sizeof(uint) * size);
D:
import internal.memset; // seems strange to import internal
uint* bits;
memset(bits, 0, uint.sizeof * size); // assuming D memset works like c++ 
equiv
----------------------------------------------------------------------

----------------------------------------------------------------------
ex4: ifstream
c++:
ifstream fin("filename");
// Read in the name of the level that will be loaded
fin >> strLevel >> strLevel;
	
// Now we need to read in the gamma level for our lightmaps
fin >> strTemp  >> g_Gamma;
fin.close();

D:
import std.stream;
File file = new File("filename", FileMode.In);
file.read(strLevel); file.read(strLevel);
file.read(strTemp);
file.read(g_Gamma);
file.close();
would these produce the same results? or do i not understand the >> 
operators well :-/
----------------------------------------------------------------------

----------------------------------------------------------------------
ex5: atexit

void Quit();

C: atexit(Quit);
D: atexit(Quit); // D compiler gets mad when i try this
main.d(8): voids have no value
main.d(8): function atexit (void(C *)()) does not match argument types 
(void)
main.d(8): cannot implicitly convert expression quit() of type void to 
void(C *)()

/////////////////////////
import std.c.stdlib;

void quit() {
}

int main() {

    atexit(quit);

    return 0;
}
////////////////////////

----------------------------------------------------------------------

Thanks anyone for reading this post and/or taking the time to reply! :)
Oct 19 2004
next sibling parent reply "Pablo Aguilar" <paguilarg hotmail.com> writes:
I think you should declare quit as "extern (C)":

extern (C) void quit()
{
}

 ----------------------------------------------------------------------
 ex5: atexit

 void Quit();

 C: atexit(Quit);
 D: atexit(Quit); // D compiler gets mad when i try this
 main.d(8): voids have no value
 main.d(8): function atexit (void(C *)()) does not match argument types 
 (void)
 main.d(8): cannot implicitly convert expression quit() of type void to 
 void(C *)()

 /////////////////////////
 import std.c.stdlib;

 void quit() {
 }

 int main() {

    atexit(quit);

    return 0;
 }
 ////////////////////////

 ----------------------------------------------------------------------

 Thanks anyone for reading this post and/or taking the time to reply! :) 
Oct 19 2004
parent clayasaurus <clayasaurus gmail.com> writes:
Pablo Aguilar wrote:
 I think you should declare quit as "extern (C)":
 
 extern (C) void quit()
 {
 }
 
I tried this code... ////////////////////////////// import std.c.stdlib; extern(C) void quit() { } int main() { atexit(quit); return 0; } ///////////////////////////// and get the same compiler errors :-/
Oct 19 2004
prev sibling next sibling parent John Reimer <brk_6502 NOSP_AM.yahoo.com> writes:
clayasaurus wrote:

 ----------------------------------------------------------------------
 ex 2: new
 c++:
 uint *bits;
 bits = new uint[size];
 D:
 uint* bits;
 bits.length = size;
 i'm not sure if this is correct. or else can i use
 bits = new uint[size]; just like the c++ code?
 ----------------------------------------------------------------------
uint *bits; bits = new uint[size]; should work fine for d. But... uint* bits; bits.length = size; I don't think that works. "bits" is a pointer so doesn't have a length associated with it. It looks like you were thinking of dynamic arrays in this case: uint[] bits; bits.length = size; That would work because dynamic arrays are made up of the length value and the pointer to the array. Later, John
Oct 19 2004
prev sibling next sibling parent "Carlos Santander B." <carlos8294 msn.com> writes:
"clayasaurus" <clayasaurus gmail.com> escribió en el mensaje 
news:cl4eti$1au2$1 digitaldaemon.com...
| Hello. Sorry for the long thread but I've recently been trying to
| convert C++ programs to the D and have stumbled across some c++ code
| that i'm not quite sure how to translate to D.
|
| ----------------------------------------------------------------------
| ex 1:
| c++:
| bits[i >> 5] &= ~(1 << (i & 31));
| D: ? i don't know if D has the same binary operations as c++ :-/
| ----------------------------------------------------------------------
|

I think it should work the same in D.

| ----------------------------------------------------------------------
| ex 2: new
| c++:
| uint *bits;
| bits = new uint[size];
| D:
| uint* bits;
| bits.length = size;
| i'm not sure if this is correct. or else can i use
| bits = new uint[size]; just like the c++ code?
| ----------------------------------------------------------------------
|

Ditto here.

| ----------------------------------------------------------------------
| ex5: atexit
|
| void Quit();
|
| C: atexit(Quit);
| D: atexit(Quit); // D compiler gets mad when i try this
| main.d(8): voids have no value
| main.d(8): function atexit (void(C *)()) does not match argument types
| (void)
| main.d(8): cannot implicitly convert expression quit() of type void to
| void(C *)()

atexit( &Quit );

|
| /////////////////////////
| import std.c.stdlib;
|
| void quit() {
| }
|
| int main() {
|
|    atexit(quit);
|
|    return 0;
| }
| ////////////////////////
|
| ----------------------------------------------------------------------
|
| Thanks anyone for reading this post and/or taking the time to reply! :)

-----------------------
Carlos Santander Bernal 
Oct 19 2004
prev sibling next sibling parent "Walter" <newshound digitalmars.com> writes:
"clayasaurus" <clayasaurus gmail.com> wrote in message
news:cl4eti$1au2$1 digitaldaemon.com...
 ----------------------------------------------------------------------
 ex 1:
 c++:
 bits[i >> 5] &= ~(1 << (i & 31));
 D: ? i don't know if D has the same binary operations as c++ :-/
 ----------------------------------------------------------------------
Should be the same.
 ----------------------------------------------------------------------
 ex 2: new
 c++:
 uint *bits;
 bits = new uint[size];
 D:
 uint* bits;
 bits.length = size;
 i'm not sure if this is correct. or else can i use
 bits = new uint[size]; just like the c++ code?
 ----------------------------------------------------------------------
Either way will work in D.
 ----------------------------------------------------------------------
 ex3: memset
 c++:
 uint *bits;
 memset(bits, 0, sizeof(uint) * size);
 D:
 import internal.memset; // seems strange to import internal
 uint* bits;
 memset(bits, 0, uint.sizeof * size); // assuming D memset works like c++
 equiv
 ----------------------------------------------------------------------
You can do that (D's memset is C's), or: bits[0..size] = 0;
 ----------------------------------------------------------------------
 ex5: atexit

 void Quit();

 C: atexit(Quit);
 D: atexit(Quit); // D compiler gets mad when i try this
 main.d(8): voids have no value
 main.d(8): function atexit (void(C *)()) does not match argument types
 (void)
 main.d(8): cannot implicitly convert expression quit() of type void to
 void(C *)()
 ----------------------------------------------------------------------
Declare Quit as: extern (C) void Quit(); and pass it to atexit as: atexit(&Quit);
Oct 19 2004
prev sibling parent reply Ben Hinkle <bhinkle4 juno.com> writes:
 ----------------------------------------------------------------------
 ex4: ifstream
 c++:
 ifstream fin("filename");
 // Read in the name of the level that will be loaded
 fin >> strLevel >> strLevel;
 
 // Now we need to read in the gamma level for our lightmaps
 fin >> strTemp  >> g_Gamma;
 fin.close();
 
 D:
 import std.stream;
 File file = new File("filename", FileMode.In);
 file.read(strLevel); file.read(strLevel);
 file.read(strTemp);
 file.read(g_Gamma);
 file.close();
 would these produce the same results? or do i not understand the >>
 operators well :-/
 ----------------------------------------------------------------------
read() is for binary data. Use scanf for parsing text data: import std.stream; File file = new File("filename", FileMode.In); file.scanf("%.*s %.*s",&strLevel,&strLevel); //strLevel twice? file.scanf("%d %d",&strTemp,&g_Gamma); file.close(); Also check out Sean Kelly's un-formatter and stream stuff to see if using his scanf is simpler. -Ben
Oct 20 2004
parent Sean Kelly <sean f4.ca> writes:
In article <cl5l92$2jpd$1 digitaldaemon.com>, Ben Hinkle says...
read() is for binary data. Use scanf for parsing text data:

import std.stream;
File file = new File("filename", FileMode.In);
file.scanf("%.*s %.*s",&strLevel,&strLevel); //strLevel twice?
file.scanf("%d %d",&strTemp,&g_Gamma);
file.close();

Also check out Sean Kelly's un-formatter and stream stuff to see if using
his scanf is simpler.
My stream rewrite isn't as feature-complete as the Phobos version (I wrote it as an example and haven't fleshed it out completely), but it does address this issue. All the get/put functions do formatted i/o and I've gotten rid of most of the unformatted i/o calls. You can get it here: http://home.f4.ca/sean/d/stream.d This library requires my un-formatter code, which is available here: http://home.f4.ca/sean/d/stdio.zip This zipfile contains a revised version of utf.d, unformat.d (a support function called unFormat that is functionally the reverse of doFormat), and stdio.d, which contains readf wrappers for unFormat. You can integrate this stuff into Phobos or use it as-is. The only notable difference between doFormat and unFormat is that unFormat will not throw a FormatError when the parameters don't match up. This is easy to fix, but I haven't done so as it would create a visible dependency on std.format. Sean Sean
Oct 20 2004