Re: Replace (ie: substitute) a type in varadic args

2016-08-02 Thread Sean Campbell via Digitalmars-d-learn

On Tuesday, 2 August 2016 at 07:24:28 UTC, Saurabh Das wrote:
How can I substitute the type of an argument received via a 
varadic template?


For example say I want to generalise this scenario:

auto myConverterFunction1(bool arg1, bool arg2, ubyte arg3, int 
arg4)

{
return targetFunction(cast(ubyte)arg1, cast(ubyte)arg2, 
arg3, arg4);

}

So I'll have something like:

auto myConverterFunction2(Args...)(Args args)
{
// Don't know how to do this part...
}

Basically I need to substitute bool for ubyte to interface with 
the Java JNI.


Thanks,
Saurabh


Just of the top of my head, using ugly string mixins, this:
auto myConverterFunc(Args...)(Args args)
{
string genCode()
{
string code = "targetFunction(";
foreach (i, Arg; Args)
{
static if (is(Arg == bool))
	code ~= format("cast(ubyte)args[%s]%s", i, i == 
Args.length ? "" : ",");

else
	code ~= format("args[%s]%s", i, i == Args.length ? 
"" : ",");

}
code ~= ");";
return code;
}
mixin(genCode());
}

void targetFunction(ubyte i, ubyte j, uint k, int l)
{
writefln("i : %s, j : %s, k : %s, l : %s",i,j,k,l);
}

void main()
{
myConverterFunc(true,false,10,20);
}


Re: winsamp.d dont compiler.

2016-07-31 Thread Sean Campbell via Digitalmars-d-learn

On Sunday, 31 July 2016 at 23:36:54 UTC, Cassio Butrico wrote:

in C:\d\dmd2\samples\d  winsamp.d dont compiler.

dmd winsamp gdi32.lib winsamp.def
winsamp.d(53): Error: function 
core.sys.windows.winuser.LoadIconA (void*, const(char)*) is not 
callable using argument types (typeof(null), wchar*)
winsamp.d(54): Error: function 
core.sys.windows.winuser.LoadCursorA (void*, const(char)*) is 
not callable using argument types (typeof(null), wchar*)
winsamp.d(57): Error: cannot implicitly convert expression 
(toStringz(className)) of type immutable(char)* to const(wchar)*
winsamp.d(59): Error: function 
core.sys.windows.winuser.RegisterClassA (const(WNDCLASSA)*) is 
not callable using argument types (WNDCLASSW*)


--- errorlevel 1


There was changes made to core.sys.windows so it defaults to 
unicode.

Try dmd winsamp gdi32.lib winsamp.def -version=ANSI
or change all of the xxxA functions to xxxW and use toUTF16z (in 
stf.utf) instead of toStringz.


Re: What is the best way to stop App after exception?

2016-02-15 Thread Sean Campbell via Digitalmars-d-learn

On Monday, 15 February 2016 at 11:38:05 UTC, Suliman wrote:
I have got class Config with method parseconfig. I need 
terminate App if parsing of config was failed. The problem that 
I do not know how to do it better.


void parseconfig()
{
 try
 {
  //something go wrong
 }

catch(Exception e)
 {
  writeln(e.msg);
  // throw any exception here
 }
}


But my main.d also have try catch block and parseconfig() calls 
inside it. The problem that wen exception rise I do not know 
how to terminate app. Exception simply print on screen and app 
is continue.


void main()
 {
 try
  {
   parseconfig(); // exception was already handled inside:  
void parseconfig()

  }
 }

What is the best practice to stop app?


If it's an problem parsing config, it would be best practice to 
throw an exception or error, but for future reference, if you 
want to terminate execution use Runtime.terminate from 
core.runtime, followed by C's exit from core.stdc.stdlib. 
Runtime. terminate will do cleanup and allow the program to 
shutdown properly


Re: std.zip for Binary example

2016-01-17 Thread Sean Campbell via Digitalmars-d-learn

On Sunday, 17 January 2016 at 10:34:19 UTC, locco wrote:

Hi :)
I found this example:
==
import std.file: write;
import std.string: representation;
void main()
{
   char[] data = "Test data.\n".dup;
   // Create an ArchiveMember for the test file.
   ArchiveMember am = new ArchiveMember();
   am.name = "test.txt";
   am.expandedData(data.representation);
   // Create an archive and add the member.
   ZipArchive zip = new ZipArchive();
   zip.addMember(am);
   // Build the archive
   void[] compressed_data = zip.build();
   // Write to a file
   write("test.zip", compressed_data);
}
==
But i cound't find example code for binary file.
How can i make ArciveMember for binary file?



std.zip dosen't discriminate against text and binary files files.
p.s. remember to import std.zip



What is the corect behavour for lazy in foreach variadic template

2015-09-25 Thread Sean Campbell via Digitalmars-d-learn

Take the following code for example

module test;
import std.stdio;

void test(T...)(lazy T args)
{
foreach(arg;args) //bar is invoked here
{
writeln("about to process arg");
		writefln("processing arg %s",arg); //but it should be invoked 
here, right?

}
}

int bar()
{
writeln("bar invoked");
return 1;
}

void main()
{
test(bar());
}

shouldn't bar be evaluated when writefln is called, not at the 
start of the loop.


Re: Exec function D from C++

2015-06-21 Thread Sean Campbell via Digitalmars-d-learn

On Sunday, 21 June 2015 at 13:12:03 UTC, MGW wrote:

Linux 32, dmd 2.067.0

I want to connect and execute function D from main() C++.

 d1.d 
import core.stdc.stdio;

extern (C++) void d1() {
printf(printf - exec d1());
}

 main_c1.cpp 
#include stdio.h

void d1(void);

int main(void) {
d1();
return 0;
}

 compile 
dmd -c d1.d
gcc main_c1.cpp d1.o libphobos2.a -lpthread -o main_c1
./main_c1  ---  This option works correctly!


If I try to cause the functions connected to GC in the D 
function, there is an error.

 d1.d 
import import std.stdio;

extern (C++) void d1() {
writeln(writeln - exec d1());
}

 compile 
dmd -c d1.d
gcc main_c1.cpp d1.o libphobos2.a -lpthread -o main_c1
./main_c1  ---  Error:
'./main_c1' terminated by signal SIGSEGV (Address boundary 
error)


Examples from dmd2/html/d/dll-linux.html don't work if instead 
of printf()
to use writeln(). I think that a problem in initialization of 
Phobos.


actually its that druntime hasn't been initialized just add:
import core.runtime;
import std.stdio;

extern(C++) void d1() {
 writeln(writeln - exec d1());
}

extern(C++) void d_initialize() {
 Runtime.initialize();
}
extern(C++) void d_terminate()
{
 Runtime.terminate();
}

and the C++
#include stdio.h

void d1(void);
void d_initialize(void);
void d_terminate(void);

int main(void) {
d_initialize();
d1();
d_terminate();
return 0;
}



using pipeprocess on non-applications

2014-07-26 Thread Sean Campbell via Digitalmars-d-learn
is there any way to detect if a file is a binary executable that 
is cross platform or a way to detect whether pipeprocss failed to 
execute a file if it wasn't executable.


zlibs gzip dosent work with http

2014-07-24 Thread Sean Campbell via Digitalmars-d-learn
I'm trying to add gzip compression to a HTTP server i wrote in D. 
here is the code that dose the gzip encoding.


if ((Info.modGzip)  
(indexOf(client.getRequestHeaderFieldValue(Accept-Encoding),gzip) 
!= -1)){

writeln(gzip);
auto gzip = new Compress(HeaderFormat.gzip);
client.addToResponseHeader(Content-Encoding: gzip);
client.sendHeader(200 ok);
while (0  (filestream.readBlock(readbuffer))){
client.client.send(gzip.compress(readbuffer));
}
client.client.send(gzip.flush());
delete gzip;
delete filestream;
}

but when i test it Firefox, internet explorer and chrome says 
that the encoding or compression is bad. why? the data is 
compressed with gzip.


the rest of the code is available at 
http://sourceforge.net/p/netspark/netsparkb1/ci/master/tree/


Git
git clone git://git.code.sf.net/p/netspark/netsparkb1 
netspark-netsparkb1


Re: get os thread handles

2014-07-20 Thread Sean Campbell via Digitalmars-d-learn

On Sunday, 20 July 2014 at 09:53:52 UTC, Jonathan M Davis wrote:

On Sunday, 20 July 2014 at 09:34:46 UTC, Sean Campbell wrote:

How do i get an os thread handle from a thread object.
or are d thread not wrapped os threads.


They do wrap OS threads, but they encapsulate them in a 
cross-platform manner, and looking over Thread, it doesn't look 
like anything along the lines of an OS thread handle is exposed 
in the API.


What do you need the OS thread handle for?


sonce the standard so i can get pause/resume support for d threads


is there any way for an object to make it self no longer usable

2014-07-19 Thread Sean Campbell via Digitalmars-d-learn
is there any way for an object to make it self no longer usable? 
eg


class someclass {
string somevalue;
bool someflag;
int somelength;
this (value,length,flag) {
somevalue = value;
someflag = flag;
somelength = length;
}
void modify_value(string new_value){
somevalue = new_value;
}
void finalize_data(){
//do something with data
//make this invalid
// eg delete this or this = null
}
}

I don't want to use a private flag to tell weather the object is 
valid or not


Re: is there any way for an object to make it self no longer usable

2014-07-19 Thread Sean Campbell via Digitalmars-d-learn

On Saturday, 19 July 2014 at 13:46:08 UTC, Kagamin wrote:
You can encapsulate it in a wrapper, which will control access 
to the object and reject if necessary.


how ?
surely not by writing another class with wrapper methods for the 
existing one.


md5 hashing acting strangly?

2014-07-16 Thread Sean Campbell via Digitalmars-d-learn

i have the following code
char[] Etag(string file){
auto info = DirEntry(file);
ubyte[16] hash = md5Of(to!string(info.timeLastAccessed.day)~
   to!string(info.timeLastAccessed.month)~
   to!string(~info.timeLastAccessed.year)~
   file);
char[] hashstring = toHexString(hash);
writeln(hashstring);
return hashstring;
}
the proper hash string prints out in the console, but when i try 
to use the return value of Etag it becomes an obscure string?


phobos iterate through json subkeys?

2014-07-15 Thread Sean Campbell via Digitalmars-d-learn
How Can I Iterate through JSON Subkeys using std.json? Or Do I 
Have To Use An External Library For that


Raw Binary into the console

2014-07-12 Thread Sean Campbell via Digitalmars-d-learn

How Can I Print Raw Binary Into The Console?
I Have Variable Of Type ubyte which equals 0b011 How Can I Get 
The Variable To Print Out As 011


Re: Concatenates int

2014-07-10 Thread Sean Campbell via Digitalmars-d-learn

perhaps I'd better state what I'm doing.
i have an array of 4 bytes and a want to convert them to a 32 bit
int
and convert the 32 bit int back into a 4 bytes again.


Re: Concatenates int

2014-07-10 Thread Sean Campbell via Digitalmars-d-learn

On Thursday, 10 July 2014 at 13:51:22 UTC, Rikki Cattermole wrote:

On 11/07/2014 1:18 a.m., Sean Campbell wrote:

perhaps I'd better state what I'm doing.
i have an array of 4 bytes and a want to convert them to a 32 
bit

int
and convert the 32 bit int back into a 4 bytes again.


Small hack I use in Dakka:

union RawConvTypes(T) {
T value;
ubyte[T.sizeof] bytes;

ubyte[T.sizeof] opCast() {
return bytes;
}
}

auto iRCT = RawConvTypes!int(5);
assert(iRCT.bytes == [5, 0, 0, 0]);

Can be quite useful for evil conversions.


this may sound stupid (new to system programming) but how do you 
convert to int form ubyte[]


Re: Concatenates int

2014-07-10 Thread Sean Campbell via Digitalmars-d-learn

On Thursday, 10 July 2014 at 15:51:11 UTC, Olivier Pisano wrote:

Hello,

I may have not understood what you actually want to do, but
aren't std.bitmanip.peek or std.bitmanip.read what you are
looking for ?

http://dlang.org/phobos/std_bitmanip.html#.peek


std.bitmanip.peek and std.bitmanip.read will do nicely should of 
looked more closely
if I need to Concatenate ints I'l just use a recursive pow based 
on length


int ConcatInt(int[] anint){
int total = 0;
for(int i=0;ianint.length;i++){
total += anint[i]*10^^(anint.length-i-1);
}
return total;
}


What am I doing Wrong (OpenGL SDL)

2014-07-04 Thread Sean Campbell via Digitalmars-d-learn
I cannot figure out what is wrong with this code and why i keep 
getting object.error access violation. the code is simple 
tutorial code for SDL and OpenGL what am i doing wrong (the 
access violation seems to be with glGenBuffers)

The Code

import std.stdio;
import derelict.opengl3.gl3;
import derelict.sdl2.sdl;

float vertices[] = [
0.0f,  0.5f, // Vertex 1 (X, Y)
0.5f, -0.5f, // Vertex 2 (X, Y)
-0.5f, -0.5f  // Vertex 3 (X, Y)
];

int main(string args[]){
DerelictSDL2.load();
DerelictGL3.load();
SDL_Init(SDL_INIT_EVERYTHING);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 
SDL_GL_CONTEXT_PROFILE_CORE);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,2 );
	SDL_Window* window = SDL_CreateWindow(OpenGL, 100, 100, 800, 
600, SDL_WINDOW_OPENGL);

SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_Event windowEvent;
GLuint vbo;
glGenBuffers(1, vbo); // Generate 1 buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, (vertices[0]).sizeof * 
vertices.length, vertices.ptr, GL_STATIC_DRAW);

while (true)
{
if (SDL_PollEvent(windowEvent))
{
if (windowEvent.type == SDL_QUIT){
return 0;
			}else if (windowEvent.type == SDL_KEYUP  
windowEvent.key.keysym.sym == SDLK_ESCAPE){

return 0;
}
SDL_GL_SwapWindow(window);
}
}
return 0;
}


Re: What am I doing Wrong (OpenGL SDL)

2014-07-04 Thread Sean Campbell via Digitalmars-d-learn

On Friday, 4 July 2014 at 08:02:59 UTC, Misu wrote:
Can you try to add DerelictGL3.reload(); after 
SDL_GL_CreateContext ?


yes this solved the problem. however why? is it a problem with 
the SDL binding?


Exception style

2014-06-25 Thread Sean Campbell via Digitalmars-d-learn

i know you can use both
throw new Exception(msg);
and

module test;

class testException {
 this (string msg) {
  super(some error info : ~msg);
 }
}

throw new testException(msg);

but which one is better conforms to the d style?


Re: Exception style

2014-06-25 Thread Sean Campbell via Digitalmars-d-learn

On Wednesday, 25 June 2014 at 09:54:16 UTC, bearophile wrote:

Sean Campbell:


but which one is better conforms to the d style?


Both are valid. Use the second when you want a more precise 
exception in your code, this is common in larger programs.


Keep in mind that you can allocate an exception in a point and 
throw it in another. This reduces the runtime allocations and 
should allow more @nogc code.


Bye,
bearophile


Thank you very much for the quick reply, and a method to save 
memory.