Re: [std.file] dirEntries

2011-12-12 Thread Vladimir Panteleev
On Monday, 12 December 2011 at 14:43:38 UTC, Tobias Pankrath 
wrote:

I can't see, what I'am doing wrong. Can anyone help?


You can't do this only using a glob. The glob syntax used by 
dirEntries is described here:


http://dlang.org/phobos/std_path.html#globMatch

You can do this with std.algorithm.filter: auto files = filter!
   q{a.name.startsWith("temp_") && !a.name.canFind('.')}
   (dirEntries("myDir", SpanMode.breadth));

foreach (de; files)
   writeln(de.name);



Restrict access to "critical" functions

2011-12-12 Thread Christian Köstlin

Hi,

I want to restrict the access of a piece of d2-code to just some
functions I declare allowed. E.g. I would like to forbid all access
to io and prevent the program to format my hd. Or even better I would
like to tell D2 which functions of the std-libraries are allowed, all 
other functions should not be callable.


Goal would be to have a possibility to compile and let run code from 
random people (some of them perhaps evil minded), watch over the 
processes and kill them, if they take too long or use up too much memory.


Thanks in advance

Christian Köstlin


Re: How is string from "..." different from string from a file ?

2011-12-12 Thread ParticlePeter
Thank you very much, you made my day, that was it :-)

Cheers, ParticlePeter !

> OpenGL probably wants a zero-terminated string. It works if you add the 
> code as a literal because string literals are zero-terminated.
> 
> string fragString = readText( "Shader.vert" ) ~ '\0';
> 
> Alternatively, you can embed the file in you executable:
> 
> immutable string fragString = import( "Shader.vert" ); // read at 
> compile time



Re: [std.file] dirEntries

2011-12-12 Thread Adam
I'm not sure if it's a different RegEx pattern than other languages,
but you may wish to try:

temp_[^\.]*

[] typically indicates a character class or set of characters.
^ is used to indicate unallowed / exception characters
. will typically need to be escaped, depending on context.



Re: How is string from "..." different from string from a file ?

2011-12-12 Thread Steven Schveighoffer

On Mon, 12 Dec 2011 12:59:11 -0500, Timon Gehr  wrote:


On 12/12/2011 06:37 PM, bearophile wrote:

Timon Gehr:


string fragString = readText( "Shader.vert" ) ~ '\0';


I think using toStringz is more self-documenting.

Bye,
bearophile


There is nothing more self-documenting than actually appending the zero.  
Claiming toStringz is better in that regard is like saying a+1 is less  
self-documenting than doAddo(a) ;)
There might be other benefits of using toStringz though, (for example,  
it won't add the zero if it is already there, but that does not apply  
here).


x ~ y *always* makes a copy of x, whereas toStringz(x) will (should?) use  
append (which could potentially save another heap allocation).  However,  
I'm not sure what kind of state the result of readText is in.


-Steve


Re: How is string from "..." different from string from a file ?

2011-12-12 Thread Timon Gehr

On 12/12/2011 06:37 PM, bearophile wrote:

Timon Gehr:


string fragString = readText( "Shader.vert" ) ~ '\0';


I think using toStringz is more self-documenting.

Bye,
bearophile


There is nothing more self-documenting than actually appending the zero. 
Claiming toStringz is better in that regard is like saying a+1 is less 
self-documenting than doAddo(a) ;)
There might be other benefits of using toStringz though, (for example, 
it won't add the zero if it is already there, but that does not apply here).


Re: How is string from "..." different from string from a file ?

2011-12-12 Thread bearophile
Timon Gehr:

> string fragString = readText( "Shader.vert" ) ~ '\0';

I think using toStringz is more self-documenting.

Bye,
bearophile


Re: How is string from "..." different from string from a file ?

2011-12-12 Thread Timon Gehr

On 12/12/2011 03:35 PM, ParticlePeter wrote:

Hi,

I have a hard time reading in a string from a file. I don't get any compile 
time or run time errors, but my application does not work reliably when I read 
a string from a file. But when I define the same string within my code, 
everything runs perfect, allways.
The string I want to use is an OpenGL Shader, but the problem is not to be 
related to OpenGL as far as a I see.
Are there some format strings which I need to get rid of, and how ?

I tried:
import std.file ;
string fragString = readText( "Shader.vert" ) ;

import std.file , std.utf ;
string fragString = toUTF8( readText( "Shader.vert" ) ) ;

import std.stdio ;
string text = "" ;
auto file = File( "Shader.vert" ) ;
foreach( line ; file.byLine() )  string ~= strip( to!( string )( line ) ) ;

What else could I try ?

Cheers, ParticlePeter !



OpenGL probably wants a zero-terminated string. It works if you add the 
code as a literal because string literals are zero-terminated.


string fragString = readText( "Shader.vert" ) ~ '\0';

Alternatively, you can embed the file in you executable:

immutable string fragString = import( "Shader.vert" ); // read at 
compile time


Re: Reading in files as int/float array or one String/char[] ( OpenGL Data )

2011-12-12 Thread Simon

On 12/12/2011 09:31, ParticlePeter wrote:

Hi,

I read several posts about reading in files, but I can't find the "best" way 
for my purpose. I am using std.file in the examples bellow.

1.) I'm trying to read an OpenGL Vertex and Fragment Shader. The wired thing on 
my approach is that it works ... sometimes, but sometimes the Shaders cannot be 
compiled.
So how should I read a file as one complete char[] in a safe way ? This is my 
way:
   auto fileFrag = cast( GLchar[] )readText( "Shader/Shader_01.frag" ) ;
   const GLchar * fragSource = cast( GLchar * )fileFrag ;


string literals are zero terminated, but strings returned by library 
functions may not be.


Try appending a zero to the read string:

string fragString = readText( "Shader.vert" ) ~ "\0";

--
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk


[std.file] dirEntries

2011-12-12 Thread Tobias Pankrath
Hello, 

I'm struggling with std.file.dirEntries. I want iterate over every file 
below a directory, which name starts with "test_" and does not
contain a point (".").

I've tried this, but it does not work:

--
foreach(DirEntry de; dirEntries("myDir", "temp_[!.]*", SpanMode.breadth))
{
writeln(de.name);
}
--

It also prints names of files, which names do contain points.

I can't see, what I'am doing wrong. Can anyone help?

Tobias


How is string from "..." different from string from a file ?

2011-12-12 Thread ParticlePeter
Hi,

I have a hard time reading in a string from a file. I don't get any compile 
time or run time errors, but my application does not work reliably when I read 
a string from a file. But when I define the same string within my code, 
everything runs perfect, allways.
The string I want to use is an OpenGL Shader, but the problem is not to be 
related to OpenGL as far as a I see. 
Are there some format strings which I need to get rid of, and how ?

I tried:
import std.file ;
string fragString = readText( "Shader.vert" ) ;

import std.file , std.utf ;
string fragString = toUTF8( readText( "Shader.vert" ) ) ;

import std.stdio ;
string text = "" ;
auto file = File( "Shader.vert" ) ;
foreach( line ; file.byLine() )  string ~= strip( to!( string )( line ) ) ;

What else could I try ?

Cheers, ParticlePeter !



Re: Reading in files as int/float array or one String/char[] ( OpenGL Data )

2011-12-12 Thread Kagamin
2.) I am reading in Vertex Data from a file. It holds only 
floats, e.g.:

-38.3887 97.7612 -10.5231 -38.3572 98.8543 -10.5064 ...
There is no metadata in the file giving information about the 
count of values.
3.) When reading a file line by line it would be nice to have 
the count of Lines of the file ( numLines )


If each line takes at least 25 bytes, you can divide file size by 
25 - this will be an estimation of number of lines.


Re: Reading in files as int/float array or one String/char[] ( OpenGL Data )

2011-12-12 Thread bearophile
ParticlePeter:

>   auto fileFrag = cast( GLchar[] )readText( "Shader/Shader_01.frag" ) ;
>   const GLchar * fragSource = cast( GLchar * )fileFrag ;

Avoid raw casts every time it is possible. The second cast is not necessary, 
using the ".ptr".


> I use a dynamic array, and append each line to my array, which I think is not 
> the most clever or efficient approach:

Try std.array.appender.


> 3.) When reading a file line by line it would be nice to have the count of 
> Lines of the file ( numLines ) and the line number in a variable, so I could 
> random access a fixed size array with this line number.

Fixed-sized arrays are often not good if you need to put lot of data inside 
them, because the stack has limited space (unless you allocate the fixed sized 
array on the heap).


> Can I get all floats ( words as float ) from a file at once without using 
> file.byLine into a Dynamic array ( or fixed array, I know the word count ) ? 

In theory std.conv.parse is useful for this:

import std.stdio, std.conv;
void main() {
string s = "[[1,2],[3,4]]";
auto p = parse!(double[][])(s);
writeln(p);
}

In practice the signature of one parse overload is:
Target parse(Target, Source)(ref Source s, dchar lbracket = '[', dchar rbracket 
= ']', dchar comma = ','); 

lbracket is a char instead of being a string, so I don't know how to parse a 
row that lacks commas and brackets. This seems an enhancement request.


> How can I get the Line count from a file,

You probably need to just iterate the file lines, and count them.


> and how can I get the line numbers without initializing a counter in front of 
> the foreach loop and increase it manually inside the loop ?

With walkLength:


import std.stdio, std.range;
void main() {
immutable size_t nLines = walkLength(File("data.txt").byLine());
writeln(nLines);
}

Bye,
bearophile


Re: Reading in files as int/float array or one String/char[] ( OpenGL

2011-12-12 Thread ParticlePeter
Andrej Mitrovic Wrote:

> I have a funky feeling you're reading NeHe? Try this:

THX, that's very cool, will definitly look deeper into these samples. I know 
NeHe, but am following The OpenGL Book, and using DerelictSDL. This shows me 
how you do it, but does not answer my questions, my Shader Issue in particular.
I need to add that when I use the Shaders from within my code, defined as a 
String, the Shader Program compiles and links always, but when I read the same 
Shaders from a file with my method they compile and link randomly. How do you 
read in Shaders ?

Cheers, ParticlePeter !



Re: Reading in files as int/float array or one String/char[] ( OpenGL Data )

2011-12-12 Thread Andrej Mitrovic
I have a funky feeling you're reading NeHe? Try this:

https://github.com/AndrejMitrovic/DNeonHelium/blob/master/Samples/win32/lesson25.d#L87


Reading in files as int/float array or one String/char[] ( OpenGL Data )

2011-12-12 Thread ParticlePeter
Hi,

I read several posts about reading in files, but I can't find the "best" way 
for my purpose. I am using std.file in the examples bellow.

1.) I'm trying to read an OpenGL Vertex and Fragment Shader. The wired thing on 
my approach is that it works ... sometimes, but sometimes the Shaders cannot be 
compiled. 
So how should I read a file as one complete char[] in a safe way ? This is my 
way:
  auto fileFrag = cast( GLchar[] )readText( "Shader/Shader_01.frag" ) ;
  const GLchar * fragSource = cast( GLchar * )fileFrag ;

2.) I am reading in Vertex Data from a file. It holds only floats, e.g.:
-38.3887 97.7612 -10.5231 
-38.3572 98.8543 -10.5064 
...
There is no metadata in the file giving information about the count of values. 
I use a dynamic array, and append each line to my array, which I think is not 
the most clever or efficient approach:
  vertices = new GLfloat[0] ;
  auto file = File( "Data/Vertices_01.txt" ) ;
  foreach( line ; file.byLine() )
vertices ~= to!( GLfloat[] )( line.split ) ;
  file.close() ;

Can I get all floats ( words as float ) from a file at once without using 
file.byLine into a Dynamic array ( or fixed array, I know the word count ) ? If 
not, what would be the most efficient approach for these kind of files ( they 
might get huge ) ?

3.) When reading a file line by line it would be nice to have the count of 
Lines of the file ( numLines ) and the line number in a variable, so I could 
random access a fixed size array with this line number. I tried this code, 
which works for arrays, but unfortunately not for lines ( for me ).
  GLfloat arrVertex[ numLines ][ 3 ] ;
  foreach( i , line ; file.byLine() )
arrVertex[ i ] = to!( GLfloat[3] )( line.split ) ;

How can I get the Line count from a file, and how can I get the line numbers 
without initializing a counter in front of the foreach loop and increase it 
manually inside the loop ?

Thank you for any advice,

Cheers, ParticlePeter !