Thread overview
How do I advance an array ?
Nov 18, 2006
Tal
Nov 18, 2006
Lionello Lunesu
Nov 18, 2006
Chris Miller
Nov 18, 2006
%u
November 18, 2006
Hello, I'm new to D, my origin is C++.

I want to search for a value in an ini file. I search for the right section, e.g. "[D]", and then I want to find the variable, like "variable=", and then change it's value.

This is how I try to do it:

char[] bfr;
bfr = cast(char[]) read (ini_path);
int i = find (bfr,"[D]");
i = find (bfr+i, "Compilator File=");

Since bfr is "char[]" and i is "int" it's erroneous. However none of my trials to bypass this type-problem were successful.

How do I do it ?

Thanx in advance,
Tal
November 18, 2006
bfr[i..$] will give you the subset from i till (but not including) bft's length ($ is short for length).


November 18, 2006
Tal wrote:
> Hello, I'm new to D, my origin is C++.
> 
> I want to search for a value in an ini file. I search for the right section,
> e.g. "[D]", and then I want to find the variable, like "variable=", and then
> change it's value.
> 
> This is how I try to do it:
> 
> char[] bfr;
> bfr = cast(char[]) read (ini_path);
> int i = find (bfr,"[D]");
> i = find (bfr+i, "Compilator File=");
> 
> Since bfr is "char[]" and i is "int" it's erroneous. However none of my trials
> to bypass this type-problem were successful.
> 
> How do I do it ?
> 
> Thanx in advance,
> Tal

"Pointer magic" does not apply to D arrays, as they are not just flat pointers, but a structure of a pointer and length.  Assuming your INI requires entities be on a line alone, you could break the string into lines first.  Or, you could use a slice.  Example:

# char[] bfr ;
# bfr = cast(char[]) read(ini_path);
# int i = find(bfr, "[D]");
# i = find(bfr[i .. $], "Compilator File=");

That said, it might actually be easier to go with lines, or just parse out the entire file, manipulate, and dump back to disc.

-- Chris Nicholson-Sauls
November 18, 2006
On Sat, 18 Nov 2006 15:33:42 -0500, Tal <talgamma@gmail.com> wrote:

> char[] bfr;
> bfr = cast(char[]) read (ini_path);
> int i = find (bfr,"[D]");
> i = find (bfr+i, "Compilator File=");
>

Looks like it will also match with:
   [Foo]
   bar=this [D] is not a section.

but to get a substring:
   bfr[i .. bfr.length]

Note: there is an .ini file module at http://www.dprogramming.com/ini.php
November 18, 2006
Thanx a lot.