www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - DerelictVorbis and string pointer

reply ANtlord <antlord92 gmail.com> writes:
Hello D community!

I'm developing an application that must work on audio especially 
playback Ogg files. So I took library DerelictVorbis [0] testing 
basic functions like `ov_fopen`. The tests were successful so I 
decided to implement core components of the application using D 
and Derelict libraries.

Today I encountered a problem, the `ov_fopen` returns -1 instead 
of 0. It means that something goes wrong and the file that is 
pointed by a string is not opened. I figured out it so there is 
the error is not occurred when a file path is pointed by a string 
variable from CLI input arguments but it is occurred when the 
path is pointed by a string variable filled dynamically (for 
example file path is read from another file).

Here code goes

public import derelict.vorbis;
public import derelict.vorbis.file;

void main(string[] args) {
	DerelictVorbis.load();
	DerelictVorbisFile.load();
	OggVorbis_File _ovFile;

	immutable filepath = args[1];
	

	import std.file;
	import std.string;
	string filepath2 = "./name.txt".readText.strip;
	assert(filepath2 == filepath);
	int res = ov_fopen(filepath2.ptr, &_ovFile); // res == -1
	// int res = ov_fopen(filepath.ptr, &_ovFile); // res == 0
	assert(res == 0, "ov_fopen returns %d".format(res));
}

Actually I get it worked replacing `string filepath2` by `char[] 
filepath2` but filepath is string still and it works correctly.

So what detail The devil is in? Is there an issue in 
DerelictVorbis or in compiler. Don't I know something about 
implementation of strings or pointers in D?

Thanks in advance!

DMD 2.080
DerelictVorbis 2.0.0-beta.2

[0] https://github.com/DerelictOrg/DerelictVorbis
Jun 23 2018
next sibling parent reply ANtlord <antlord92 gmail.com> writes:
On Sunday, 24 June 2018 at 01:26:48 UTC, ANtlord wrote:
 Actually I get it worked replacing `string filepath2` by 
 `char[] filepath2` but filepath is string still and it works 
 correctly.
It doesn't work
Jun 23 2018
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 24 June 2018 at 01:43:41 UTC, ANtlord wrote:
 On Sunday, 24 June 2018 at 01:26:48 UTC, ANtlord wrote:
 Actually I get it worked replacing `string filepath2` by 
 `char[] filepath2` but filepath is string still and it works 
 correctly.
It doesn't work
Vorbis is a C library, so you need to null terminate your string using toStringz. The reason why the one from the command line works is because it is already null terminate.
Jun 23 2018
parent ANtlord <antlord92 gmail.com> writes:
On Sunday, 24 June 2018 at 01:54:52 UTC, Nicholas Wilson wrote:
 On Sunday, 24 June 2018 at 01:43:41 UTC, ANtlord wrote:
 On Sunday, 24 June 2018 at 01:26:48 UTC, ANtlord wrote:
 Actually I get it worked replacing `string filepath2` by 
 `char[] filepath2` but filepath is string still and it works 
 correctly.
It doesn't work
Vorbis is a C library, so you need to null terminate your string using toStringz. The reason why the one from the command line works is because it is already null terminate.
It works! Thank you!
Jun 23 2018
prev sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 24/06/2018 1:26 PM, ANtlord wrote:
 Hello D community!
 
 I'm developing an application that must work on audio especially 
 playback Ogg files. So I took library DerelictVorbis [0] testing basic 
 functions like `ov_fopen`. The tests were successful so I decided to 
 implement core components of the application using D and Derelict 
 libraries.
 
 Today I encountered a problem, the `ov_fopen` returns -1 instead of 0. 
 It means that something goes wrong and the file that is pointed by a 
 string is not opened. I figured out it so there is the error is not 
 occurred when a file path is pointed by a string variable from CLI input 
 arguments but it is occurred when the path is pointed by a string 
 variable filled dynamically (for example file path is read from another 
 file).
 
 Here code goes
 
 public import derelict.vorbis;
 public import derelict.vorbis.file;
 
 void main(string[] args) {
      DerelictVorbis.load();
      DerelictVorbisFile.load();
      OggVorbis_File _ovFile;
 
      immutable filepath = args[1];
 
 
      import std.file;
      import std.string;
      string filepath2 = "./name.txt".readText.strip;
      assert(filepath2 == filepath);
      int res = ov_fopen(filepath2.ptr, &_ovFile); // res == -1
      // int res = ov_fopen(filepath.ptr, &_ovFile); // res == 0
      assert(res == 0, "ov_fopen returns %d".format(res));
 }
 
 Actually I get it worked replacing `string filepath2` by `char[] 
 filepath2` but filepath is string still and it works correctly.
 
 So what detail The devil is in? Is there an issue in DerelictVorbis or 
 in compiler. Don't I know something about implementation of strings or 
 pointers in D?
 
 Thanks in advance!
 
 DMD 2.080
 DerelictVorbis 2.0.0-beta.2
 
 [0] https://github.com/DerelictOrg/DerelictVorbis
So where exactly is the null byte for the C string?
Jun 23 2018