Thread overview
Idea for a core app class or struct
May 18, 2004
Ameer
May 18, 2004
KTC
May 21, 2004
J C Calvarese
May 21, 2004
Regan Heath
May 21, 2004
J C Calvarese
May 21, 2004
Regan Heath
May 21, 2004
Andy Friesen
May 21, 2004
J C Calvarese
May 21, 2004
Pablo Aguilar
May 22, 2004
J C Calvarese
May 18, 2004
Hey all.
I have been looking at the d specs  and am very pleased with what I see; it'll
probably  hit big pretty soon.
I've been wondering, if we could possibly have a generic app class or struct,
like in visual basic.  In vb, it normally contains the app's name, path,
major/minor/revission numbers, and a full version string.  This will  eliminate
the need for a "prog_version.d" file, and a separate variable, among other
things; if nothing else, you can tell app.path lot easier than args[0] if it's
even still used.
Thanks,



Ameer


May 18, 2004
This newsgroup is now deprecated. Try news://news.digitalmars.com/digitalmars.D


May 21, 2004
Ameer wrote:
> Hey all.
> I have been looking at the d specs  and am very pleased with what I see; it'll
> probably  hit big pretty soon.
> I've been wondering, if we could possibly have a generic app class or struct,
> like in visual basic.  In vb, it normally contains the app's name, path,
> major/minor/revission numbers, and a full version string.  This will  eliminate
> the need for a "prog_version.d" file, and a separate variable, among other
> things; if nothing else, you can tell app.path lot easier than args[0] if it's
> even still used.
> Thanks,
> 
> 
> 
> Ameer

(I'm cross-posting this to the new newsgroup: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D).

It sounds like a job for a library to me. Here's a function that could be used for app.path (I haven't tested it too thoroughly, but it seems to work):

/*
	apppath.d	
	Shows how to find the path of the current executable.
	(Windows-only)
*/

import std.c.windows.windows;
import std.c.stdio;
import std.string;

extern(C) int strlen(char* c);

extern(Windows)
{
	DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize);	
	HMODULE GetModuleHandleA(LPCSTR lpModuleName);
	
	alias GetModuleFileNameA GetModuleFileName;
	alias GetModuleHandleA GetModuleHandle;
}


char[] CharRetString(char c)
{
	/* return a string from a character */
	
	char[] s = "?";
	s[0] = c;
	return s;
}


char[] repeatString(int count, ubyte a)
{   /* returns a string containing 'count' occurences of the character
       corresponding to the ASCII character code 'a'. */

	char[] tmp;
	
	for (int i = 0; i < count; i++)
	  tmp ~= CharRetString(a);

	return tmp;
}


char[] AppExePath()
{
	/* returns the Path to the current .exe module (e.g. "c:\dmd\src\test\") */
	
    char[] strtmp = repeatString(1024, 32);
	int j;


    GetModuleFileName(GetModuleHandle(null), cast(char*) strtmp, 1024);
	j = rfind(strtmp[0..strlen(strtmp)], "\\");

	strtmp = strtmp[0..j] ~ "\\";
    return strtmp;
}


void main()
{
	printf("%.*s\n", AppExePath);
	
}

-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
May 21, 2004
Firstly I want to make it clear I am not trying to nitpick or criticise your code, I just want to understand D better and what it does in the background...

> char[] repeatString(int count, ubyte a)
> {   /* returns a string containing 'count' occurences of the character
>         corresponding to the ASCII character code 'a'. */
>
> 	char[] tmp;
> 	
> 	for (int i = 0; i < count; i++)
> 	  tmp ~= CharRetString(a);
>
> 	return tmp;
> }

Isn't the above in-efficient?
Can it be...

char[] repeatString(int count, char a) {
	char[] tmp;

	tmp.length = count;
	foreach(inout char c; tmp)
		c = 'a';

	return tmp;
}

?

Also why can't we go

char[] a = cast(char[])'a';

seems this could be explicitly cast'ed?
or am I missing something?

cos then you would not need this function...

> char[] CharRetString(char c)
> {
> /* return a string from a character */
>
> 	char[] s = "?";
> 	s[0] = c;
> 	return s;
> }

Regan.

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
May 21, 2004
Regan Heath wrote:
> Firstly I want to make it clear I am not trying to nitpick or criticise your code, I just want to understand D better and what it does in the background...

Sure. Nitpick away.

> 
>> char[] repeatString(int count, ubyte a)
>> {   /* returns a string containing 'count' occurences of the character
>>         corresponding to the ASCII character code 'a'. */
>>
>>     char[] tmp;
>>         for (int i = 0; i < count; i++)
>>       tmp ~= CharRetString(a);
>>
>>     return tmp;
>> }
> 
> 
> Isn't the above in-efficient?

Probably.

> Can it be...
> 
> char[] repeatString(int count, char a) {
>     char[] tmp;
> 
>     tmp.length = count;
>     foreach(inout char c; tmp)
>         c = 'a';
> 
>     return tmp;
> }
> 
> ?

Good idea. Setting the at the beginning would definitely be more efficient. Unless there is one already, I think we need an efficient repeatString-type function in std.string.

> 
> Also why can't we go
> 
> char[] a = cast(char[])'a';
> 
> seems this could be explicitly cast'ed?
> or am I missing something?

This is what the compiler says...
apppath.d(30): e2ir: cannot cast from char to char[]

I don't know if the direct cast will ever be allowed. I don't understand why it's not allowed (and I wish it were allowed). In the meantime, the function is alternative that compiles. And it allows me to move on.

> 
> cos then you would not need this function...
> 
>> char[] CharRetString(char c)
>> {
>> /* return a string from a character */
>>
>>     char[] s = "?";
>>     s[0] = c;
>>     return s;
>> }
> 
> 
> Regan.

Thanks for your comments.

-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
May 21, 2004
Regan Heath wrote:
> Firstly I want to make it clear I am not trying to nitpick or criticise your code, I just want to understand D better and what it does in the background...
> 
>> char[] repeatString(int count, ubyte a)
>> {   /* returns a string containing 'count' occurences of the character
>>         corresponding to the ASCII character code 'a'. */
>>
>>     char[] tmp;
>>         for (int i = 0; i < count; i++)
>>       tmp ~= CharRetString(a);
>>
>>     return tmp;
>> }
> 
> 
> Isn't the above in-efficient?

Sure is.  This is much faster:

char[] repeatString(int count, char a) {
   char[] c;
   c.length = count;
   c[] = a;
}

 -- andy
May 21, 2004
On Thu, 20 May 2004 23:57:51 -0500, J C Calvarese <jcc7@cox.net> wrote:
> Probably.
>
>> Can it be...
>>
>> char[] repeatString(int count, char a) {
>>     char[] tmp;
>>
>>     tmp.length = count;
>>     foreach(inout char c; tmp)
>>         c = 'a';
>>
>>     return tmp;
>> }
>>
>> ?
>
> Good idea. Setting the at the beginning would definitely be more efficient. Unless there is one already, I think we need an efficient repeatString-type function in std.string.

Andy's suggestion might be it. Alternately a constructor syntax..

char[] tmp(length);     //make it length big
char[] tmp(length,'a'); //a string filled with length a's

which does not work at present but could be added.

As you can achieve the above with Andy's suggestion maybe it's redundant.

>> Also why can't we go
>>
>> char[] a = cast(char[])'a';
>>
>> seems this could be explicitly cast'ed?
>> or am I missing something?
>
> This is what the compiler says...
> apppath.d(30): e2ir: cannot cast from char to char[]
>
> I don't know if the direct cast will ever be allowed. I don't understand why it's not allowed (and I wish it were allowed).

Strongly-typed language reasons?


-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
May 21, 2004
Andy Friesen wrote:

> Regan Heath wrote:
> 
>> Firstly I want to make it clear I am not trying to nitpick or criticise your code, I just want to understand D better and what it does in the background...
>>
>>> char[] repeatString(int count, ubyte a)
>>> {   /* returns a string containing 'count' occurences of the character
>>>         corresponding to the ASCII character code 'a'. */
>>>
>>>     char[] tmp;
>>>         for (int i = 0; i < count; i++)
>>>       tmp ~= CharRetString(a);
>>>
>>>     return tmp;
>>> }
>>
>>
>>
>> Isn't the above in-efficient?
> 
> 
> Sure is.  This is much faster:
> 
> char[] repeatString(int count, char a) {
>    char[] c;
>    c.length = count;
>    c[] = a;
> }
> 
>  -- andy

Oh, I forgot about that.

-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
May 21, 2004
> char[] AppExePath()
> {
> /* returns the Path to the current .exe module (e.g. "c:\dmd\src\test\")
*/
>
>      char[] strtmp = repeatString(1024, 32);
> int j;
>
>
>      GetModuleFileName(GetModuleHandle(null), cast(char*) strtmp, 1024);
> j = rfind(strtmp[0..strlen(strtmp)], "\\");
>
> strtmp = strtmp[0..j] ~ "\\";
>      return strtmp;
> }

You should try PathRemoveFileSpec from shlwapi.dll (shlwapi.h and
shlwapi.lib)


May 22, 2004
Pablo Aguilar wrote:
>>char[] AppExePath()
>>{
>>/* returns the Path to the current .exe module (e.g. "c:\dmd\src\test\")
> 
> */
> 
>>     char[] strtmp = repeatString(1024, 32);
>>int j;
>>
>>
>>     GetModuleFileName(GetModuleHandle(null), cast(char*) strtmp, 1024);
>>j = rfind(strtmp[0..strlen(strtmp)], "\\");
>>
>>strtmp = strtmp[0..j] ~ "\\";
>>     return strtmp;
>>}
> 
> 
> You should try PathRemoveFileSpec from shlwapi.dll (shlwapi.h and
> shlwapi.lib)

I'd rather not. I think I can find the path without using an OS function. I don't see the advantage in adding a .dll dependency (unless I had a grudge against Windows 95 users who haven't upgraded to IE 4.0):

Minimum operating systems
Windows 2000, Windows NT 4.0 with Internet Explorer 4.0, Windows 98,
Windows 95 with Internet Explorer 4.0

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/shlwapi/path/pathremovefilespec.asp


I've attached an improved version of the AppExePath function since I've received such a large response.


-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/