Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::lib::os: add get_home_dir() and get_config_dir() #1010

Closed
wants to merge 2 commits into from
Closed

std::lib::os: add get_home_dir() and get_config_dir() #1010

wants to merge 2 commits into from

Conversation

pierrec
Copy link
Contributor

@pierrec pierrec commented Sep 22, 2023

No description provided.

**/
fn String! get_config_dir(Allocator* using = mem::heap())
{
$if env::WIN32:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use "Path" for this

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I meant to use Path to append paths together. I think your code is also incorrect, forgetting to add '/' in some cases. Path ensures valid paths and can concatenate into new paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have already fixed the missing / for Linux&co.
I dont think Path is required as the concatenation occurs when it is known that this is a non Windows code path.

/**
* Returns the current user's home directory.
**/
fn String! get_home_dir()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the ownership and allocation origin of String here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I was wondering the same about get_var(). I think an allocator may be required for those functions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_var() is grabbing it from the os which then might change it, but there is no need to deallocate it.

Copy link
Contributor Author

@pierrec pierrec Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_var() is grabbing it from the os which then might change it, but there is no need to deallocate it.

It currently does for non-WIndows os'es but with WIN32 you need to allocate it.
Will add the allocator for WIN32.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best is probably to make the non-Win32 also allocate.

str.init(s.len + DIR.len, using);
str.append(s);
str.append(DIR);
return str.as_str();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is returning string as a String, but if you try to release this String then that will fail, because the string is part of a DString. as_str() is getting the view. Maybe we should rename all of these .str_view() to clarify it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense as there is already .array_view(). Maybe this should be done before this PR?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the update.

buffer.reserve(n - N);
get_env_win32(name, buffer);
}
return buffer.str_view()[:n];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still buggy, it will not be possible to release this string. You want to allocate the buffer with a simple tmalloc, create a string from it and copy it with using.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then:

return buffer.str_view()[:n].copy(using);

str.init(s.len + DIR.len, using);
str.append(s);
str.append(DIR);
return str.str_view();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns the view of a dstring, so cannot be released.

@@ -83,7 +83,15 @@ extern fn Win32_BOOL getOverlappedResult(
Win32_LPDWORD lpNumberOfBytesTransferred,
Win32_BOOL bWait
) @extern("GetOverlappedResult");

extern fn Win32_DWORD getEnvironmentVariable(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename: win32_GetEnvironmentVariableA

extern fn Win32_BOOL setEnvironmentVariable(
Win32_LPCSTR lpName,
Win32_LPCSTR lpBuffer
) @extern("SetEnvironmentVariableA");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to win32_SetEnvironmentVariableA

Win32_LPCSTR lpName,
Win32_LPSTR lpBuffer,
Win32_DWORD nSize
) @extern("GetEnvironmentVariableA");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use the A functions, since they are not UTF8 safe. Use the W variants and do the string conversion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try with GetEnvironmentVariable but then it couldnt be found.

@lerno
Copy link
Collaborator

lerno commented Sep 25, 2023

Let me see if I can make a version of this.

@pierrec
Copy link
Contributor Author

pierrec commented Sep 25, 2023

Let me see if I can make a version of this.

Go for it :).

@lerno
Copy link
Collaborator

lerno commented Sep 25, 2023

set / get / clear env should probably be impoved by returning an optional on error.

@pierrec
Copy link
Contributor Author

pierrec commented Sep 25, 2023

Thanks for this and sorry for the long back and forth.

@pierrec pierrec closed this Sep 25, 2023
@lerno
Copy link
Collaborator

lerno commented Sep 25, 2023

No not at all.

@lerno
Copy link
Collaborator

lerno commented Sep 25, 2023

I think it resulted in good improvements for the stdlib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants