Reading/writing to files, cross-platform

BlitzMax Forums/BlitzMax Beginners Area/Reading/writing to files, cross-platform

jjsonick(Posted 2007) [#1]
I've read several reports that Vista doesn't allow programs to write to anywhere but My Documents (and please correct me if I'm wrong).

Given this, what would be best practices for a program that 1) will be creating/writing to/reading from multiple files (probably text files) during runtime and 2) you want to be cross-platform?

Will it require having the program check the OS and choose between a Vista-specific routine (that would only write to My Documents) and a general routine (for all other OS's)?

Thanks,
JJ


Sub_Zero(Posted 2007) [#2]
1&2. I guess detecting the os first would be the best practice, but it depends if the only difference in file creating/writing/reading is the location of the file. If it is, then you only need to change the location, since the file functions allready are cross-platform.

To detect OS, you can simply do:

?Win32
Print "This is win32"
?linux
Print "This is linux"
?MacOS
Print "This is mac os"
?


I'm not sure if you can do a;

?Vista
print "This is vista"
?


yet, but if you need to detect vista at this time, i guess a search at http://msdn.microsoft.com will give you an answer how to detect if the os is vista or not.


Sub_Zero(Posted 2007) [#3]
Update: I'm currently downloading vista, will let you know if

?Vista
print "This is vista"
?


works.


FlameDuck(Posted 2007) [#4]
Given this, what would be best practices for a program that 1) will be creating/writing to/reading from multiple files (probably text files) during runtime and 2) you want to be cross-platform?
Actually Vista helps you out there. Now you can just write to every users "home" directory.


jjsonick(Posted 2007) [#5]
Thanks, Sub_Zero and FlameDuck.

So it looks like I can do something like

Global appDataPath$ = getenv_("HOMEDRIVE") + getenv_("HOMEPATH") + "\Application Data\appname\"

to set the path for files the game will use in Windows (even if the user has the Documents and Settings directory on a drive other than C:). From what I've read, this should work in both XP and Vista, which would cover the case if BlitzMax doesn't do a ?Vista test.

I assume just writing streams out to the currentdir is fine in both Mac and Linux?


Sub_Zero(Posted 2007) [#6]
?vista
?

Does not work.

However, in Vista x86 (32-bit), the os detection in blitzmax is equal at win32. So I can confirm your method will work as expected.


jjsonick(Posted 2007) [#7]
Great, much thanks.


Brucey(Posted 2007) [#8]
For cross-platformness, this thread might be of interest.
It mentions a cross-platform module that gives access to things like the Home and Document folders for the user, using APIs rather than environment variables...

:-)


jjsonick(Posted 2007) [#9]
Thanks, Brucey! I'll definitely check your volumes module out.


Sub_Zero(Posted 2007) [#10]
The volumes module is great =)


WendellM(Posted 2007) [#11]
FlameDuck wrote:
Actually Vista helps you out there. Now you can just write to every users "home" directory.


Do you know of a better way than:
getenv_("USERPROFILE")+"\My Documents\"
to do this? XP seems not to allow renaming "My Documents", but I recall doing it on one Win 95/98 system.

I found that Brucey's module works great in XP, but there was an issue with 98, so I'm investigating a solution that goes back that far. The above code works as long as "My Documents" hasn't been renamed, since getenv_("USERPROFILE") returns an empty string under 98.

EDIT: I just noticed JazzieB's comment in another thread: "I can't simply add /My Documents/ on the end as [...] it's not actually called that in a lot of countries."

So, any suggestions? Is there no environment variable in 98, XP, and Vista that points to a localized version of My Documents?


JazzieB(Posted 2007) [#12]
I now have Windows Vista and have been doing some testing, although I'm far from complete, but here are my observations so far...

It looks like a program can read and write files to its own folder, i.e. the folder it was installed to. At least I've tested my games from a standard/restricted account and they seem to be able to store high scores and config files in the program's own folder without any issues.

I intend to do some more thorough testing tomorrow (or within the next couple of days), to confirm if there's any differences between admin and standard accounts, both with UAC enabled and disabled. The only difference I have found so far are with the installation itself (i.e. from an installer), as these look as though they need to be run from an admin account, or ran as admin from a standard account. The presence of UAC also makes a difference, i.e. admin account with no UAC means the installer runs as it did under XP. Any other account has other steps.

Besides all this, it would still be useful to retrieve the location of any special folder, so that things can be done the 'proper' way in future.


WendellM(Posted 2007) [#13]
Good to know, thanks. I look forward to your testing results since I plan to use an installer like InnoSetup for larger games/projects, but I'd also like little ones that don't require an installer to continue to work in Vista as they did in XP. I hope that programs which haven't been installed per se can still write/read the My Documents folder...?

After posting the earlier message above, I kept digging and found:
http://technet2.microsoft.com/WindowsVista/en/library/3f1be40e-70c6-462c-9e8f-591d14d875cd1033.mspx

And this:
http://blitzbasic.com/Community/posts.php?topic=45722
had the other half (getting it working in BlitzMax)

So, this seems to do what Vista seems to need (get at "My Documents" regardless of where it is or what it's called), along with other good stuff:


It works in both in XP and in 98 SE, so it should(?) in Vista.