-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Preview of contribution / More .ini APIs (nearly completes set of PrivateProfile API) #250
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
*/ | ||
package com.sun.jna.platform.win32; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.util.ArrayList; | ||
|
@@ -307,6 +309,66 @@ public static final void writePrivateProfileString(final String appName, final S | |
throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); | ||
} | ||
|
||
/** | ||
* Retrieves all the keys and values for the specified section of an initialization file. | ||
* | ||
* <p> | ||
* Each string has the following format: {@code key=string}. | ||
* </p> | ||
* <p> | ||
* This operation is atomic; no updates to the specified initialization file are allowed while this method is executed. | ||
* </p> | ||
* | ||
* @param appName | ||
* The name of the section in the initialization file. | ||
* @param fileName | ||
* The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the | ||
* Windows directory. | ||
* @return The key name and value pairs associated with the named section. | ||
*/ | ||
public static final List<String> getPrivateProfileSection(final String appName, final String fileName) { | ||
final char buffer[] = new char[32768]; // Maximum section size according to MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724348(v=vs.85).aspx) | ||
if (Kernel32.INSTANCE.GetPrivateProfileSection(appName, buffer, new DWORD(buffer.length), fileName).intValue() == 0) | ||
throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); | ||
return asList(Native.toStrings(buffer)); | ||
} | ||
|
||
/** | ||
* Retrieves the names of all sections in an initialization file. | ||
* <p> | ||
* This operation is atomic; no updates to the initialization file are allowed while this method is executed. | ||
* </p> | ||
* | ||
* @param fileName | ||
* The name of the initialization file. If this parameter is {@code NULL}, the function searches the Win.ini file. If this parameter does not | ||
* contain a full path to the file, the system searches for the file in the Windows directory. | ||
* @return the section names associated with the named file. | ||
*/ | ||
public static final List<String> getPrivateProfileSectionNames(final String fileName) { | ||
final char buffer[] = new char[65536]; // Maximum INI file size according to MSDN (http://support.microsoft.com/kb/78346) | ||
if (Kernel32.INSTANCE.GetPrivateProfileSectionNames(buffer, new DWORD(buffer.length), fileName).intValue() == 0) | ||
throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); | ||
return asList(Native.toStrings(buffer)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As said above, your concerns are not correct. It is documented in MSDN that GetPrivateProfileSectionNames will never return more than 32767 bytes + NUL, so my code does not imply any other limitation than the operating system itself already has. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EDIT: Actually it is not documented in MSDN but in the Microsoft Knowledge Base (http://support.microsoft.com/kb/78346/en). While that article says that even some Microsoft tools cannot handle files larger than 32K, I just expanded the buffer to 64K in my latest COMMIT to reflect the official size limit documented in that article. So JNA now can handle larger files than Windows itself! ;-) Also I added code comments so other authors can see where the numbers are taken from. :-) |
||
} | ||
|
||
/** | ||
* @param appName | ||
* The name of the section in which data is written. This section name is typically the name of the calling application. | ||
* @param strings | ||
* The new key names and associated values that are to be written to the named section. Each entry must be of the form {@code key=value}. | ||
* @param fileName | ||
* The name of the initialization file. If this parameter does not contain a full path for the file, the function searches the Windows directory | ||
* for the file. If the file does not exist and lpFileName does not contain a full path, the function creates the file in the Windows directory. | ||
*/ | ||
public static final void writePrivateProfileSection(final String appName, final List<String> strings, final String fileName) { | ||
final StringBuilder buffer = new StringBuilder(); | ||
for (final String string : strings) | ||
buffer.append(string).append('\0'); | ||
buffer.append('\0'); | ||
if (!(Kernel32.INSTANCE.WritePrivateProfileSection(appName, buffer.toString(), fileName))) | ||
throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); | ||
} | ||
|
||
/** | ||
* Convenience method to get the processor information. Takes care of auto-growing the array. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should rewrite this by making two calls, otherwise this has a hidden limitation of an upper 32K bound. Call it once to get the size, allocate the string and call it again.
MSDN says: the return value specifies the number of characters copied to the buffer, not including the terminating null character. If the buffer is not large enough to contain all the key name and value pairs associated with the named section, the return value is equal to nSize minus two.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no 'hidden' limitation. Actually the limitation is documented in MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724348(v=vs.85).aspx): ...nSize [in]
The size of the buffer pointed to by the lpReturnedString parameter, in characters. The maximum profile section size is 32,767 characters...
Hence, the above function is perfectly well and nothing is "hidden". :-)