Regarding DConf 2014
Hi, Would it be possible to have for Dconf 2014 something similar to what was made available for DConf 2013 (meaning links to the videos and slides)? Regards, Hugo
Decoding HTML escape sequences
Hi, I have some documents where some strings appears in HTML escape sequences in one of these forms: \x3C\x53\x43\x52\x49\x50\x54\x20\x4C\x41\x4E\x47\x55\x41\x47\x45\x3D\x22\x4A\x61\x76\x61\x53\x63\x72\x69\x70\x74\x22\x3e %3C%53%43%52%49%50%54%20%4C%41%4E%47%55%41%47%45%3D%22%4A%61%76%61%53%63%72%69%70%74%22%3e And I would like to recode them to readable form: SCRIPT LANGUAGE=Javascript I tried something like this, using regular expressions and the uri module: import std.stdio, std.file, std.encoding, std.string, std.regex, std.uri; static auto re = regex(`(%[a-fA-F0-9]{2})`); int main(in string[] args) { if (args.length 2) { writeln(Usage: unescape file1.htm file2.htm); return -1; } auto input = cast(Latin1String) read(args[1]); string buffer; transcode(input, buffer); string output; foreach(m; matchAll(buffer, re)) output ~= decode(m.hit); writeln(output); return 0; } Unfortunately it doesn't seem to work 100%. I would appreciate any suggestion. Regards, Hugo
list in spam blacklist? [OT]
Hi, IP 173.45.241.208 (slice-1.puremagic.com) seems to be blacklisted in zen.spamhaus.org Please check for a possible misuse of the server, or in case of a false positive report to spamhaus.org Regards, Hugo
linking against libmilter
Hi, Does anyone have an example of linking against libmilter? The milter applications I have seen are a little overweight for my taste, I just want to check email headers. I sure would like to take advante of the fast regexp of D rather than buildin my own solution in C (which I don't master). Regards, Hugo
Parsing and splitting textfile
Hello, Can you point me to an efficient way to parse a text file and split it by certain expression (for example, `\n\nFrom\ .+@.+$`), copying what has already been read to a separate file, and so on till the end of the file? I am trying to implement a mailbox to maildir format conversion application in D, but I would like to avoid loading each mbox completely into memory. Regards, Hugo
Re: Parsing and splitting textfile
On Mon, 24 Feb 2014 14:00:09 -0500, Steven Schveighoffer wrote: std.regex I should have explained myself better. I have already used regular expressions a couple of times. My doubt here is how parse the file progressively, not loading it completely into memory. If this can be done solely with std.regex, please clarify futher I was thinking in using byLine, but for that, I see first I must use something like: auto myfile = File(usermbox); Doesn't that load the whole file into memory? Regards, Hugo
Re: Parsing and splitting textfile
On Mon, 24 Feb 2014 19:08:16 + (UTC), Justin Whear wrote: Specifically std.regex.splitter[1] creates a lazy range over the input. You can couple this with lazy file reading (e.g. `File(mailbox).byChunk (1024).joiner`). Interesting, thanks.
Re: Parsing and splitting textfile
On Mon, 24 Feb 2014 19:08:16 + (UTC), Justin Whear wrote: Specifically std.regex.splitter[1] creates a lazy range over the input. You can couple this with lazy file reading (e.g. `File(mailbox).byChunk (1024).joiner`). Would something like this work? (I cannot test it right now) auto themailbox = args[1]; immutable uint chunksize = 1024 * 64; static auto re = regex(`\n\nFrom .+@.+$`); auto mailbox; auto mail; while (mailbox = File(themailbox).byChunk(chunksize).joiner) != EOF) { mail = splitter(mailbox, re); } If so, I have a couple of furter doubts: Using splitter actually removes the expression from the string, how could I reinsert it to the beginning of each resulting string in an efficient way (i.e. avoiding copying something which is already loaded in memory)? I am seeing the splitter fuction returns a struct, how could I progressively dump to disk each resulting string, removing it from the struct, so that so that it does not end up having the full mailbox loaded into memory, in this case as a struct? Regards, Hugo
Re: errors with filesystem operations
On Sat, 18 Jan 2014 11:33:16 +, Kagamin wrote: On Friday, 17 January 2014 at 12:52:09 UTC, Hugo Florentino wrote: On Fri, 17 Jan 2014 07:07:35 +, Kagamin wrote: Does it fail for that one directory only or for any directory? Interesting question. I would have to do more tests with odd names, but apparently both remove() and rename() have problems with directories with a blank name. In that case try a path with \\?\ prefix (unparsed path). Does not work. I modified the relevant block like this: if (exists(BlankDirToDelete)) { string SafeToBeDeleted = buildPath(`\\?\`, SearchPath, baseName(BlankDirToDelete)); writefln(`%s`, SafeToBeDeleted); try remove(SafeToBeDeleted); catch (FileException e) writeln(e.msg); try remove(`\\?\` ~ SafeToBeDeleted); catch (FileException e) writeln(e.msg); } The result? d:\ d:\ : Access denied. \\?\d:\ : Access denied. This happens even while executing the prompt as administrator, so it's not a problem of file privileges. Note that from the Windows explorer I am also having problems renaming or deleting the problematic directory, however with something else like Total Commander, I can both rename and delete with no issues. Regards, Hugo
Re: errors with filesystem operations
On Sat, 18 Jan 2014 11:51:48 +, Kagamin wrote: remove uses DeleteFile, but MSDN says To remove an empty directory, use the RemoveDirectory function. so remove probably won't work on directories. You are correct, so I made a few test using rmdir() with a blank for directory name and now I get: d:\ : Directory is not empty. However, the diretory is certainly empty. Go figure! Note that in Windows I cannot create a directoy with a blank name from D either, so in order to test this, I have to create it elsewhere.
Re: errors with filesystem operations
Update: the combination of both your suggestions worked: if (exists(BlankDirToDelete)) { try rmdir(`\\?\` ~ BlankDirToDelete); catch (FileException e) writeln(e.msg); } Thanks! Now I just have to find out why the block of the file extensions is failing.
Re: errors with filesystem operations
On Fri, 17 Jan 2014 07:07:35 +, Kagamin wrote: Does it fail for that one directory only or for any directory? Interesting question. I would have to do more tests with odd names, but apparently both remove() and rename() have problems with directories with a blank name. Curiously, moving files or directories from within that blank directory to another directory seems to working correctly, so it's not like the directory is not being detected. I haven't dived in the code for both functions, but I suspect that some validation may be causing the problem.
Re: errors with filesystem operations
On Fri, 17 Jan 2014 13:10:00 +, Kagamin wrote: I only noticed that rename uses MOVEFILE_REPLACE_EXISTING flag, which can't be used with directories. Are you sure remove fails too? If rename throws, remove is not called in your code. Well, as a matter of fact I did try to use remove directly and it failed with the same problem (directory in use by some process) Since subdirectories were being moved just fine, I thought once the content of the problematic directory was moved elsewhere I could rename the directory first and then remove it, but this approach failed too, at least from Windows.
Re: errors with filesystem operations
On Thu, 16 Jan 2014 15:41:06 +, Kagamin wrote: Probably because you use ansi api: if filename contains non-english character, there could be a problem. A filesystem support is primarily for storing files, attributes can be safely ignored. Hmm... that may be true for Linux filesystems, but not necessarily for filesystems used by Windows, where attributes do matter. Probably the majority of people here works from Linux and maybe most have the good fortune that the rest of the users in their organization either works in Linux too, or are computer literate persons. However where I work all users work in Windows (there is a specific application for the management of music rights which has not been ported to Linux AFAIK) and most of them have rather basic computer skills. So these users sometimes bring a USB memory from home, infected with some virus which the company antivirus does disinfect, but the effects (hidden folders or files moved to a directory with a blank name, among others) are not always reversed. So you see, I would prefer distributing an executable which fixes the memory for them rather than having them come into my office and make me reboot to Windows just to remove the hidden attribute from their folders or so on. Besides, if in my application I specifically set console codepage to UTF8 for the Windows version of the executable, and I am calling a D function (supposedly portable) to rename or delete a directory, and yet it does not get deleted, supposedly because it is being used (which I have tried to avoid in my code), I find this rather odd, and since I am new to D, I do not yet know the internals of the language well enough to realize where the problem is. Which is why I am asking for help here :)
errors with filesystem operations
Hi, I am trying to make a little application to revert the effect of some viruses on USB memories, and running it from Windows encountered some exceptions that IMHO should not have happened. Maybe I am missing something. Can you please check the commented blocks for errors? Also, is there a way from Linux to remove attributes readonly, system, hidden ? Regards, Hugo import std.stdio; import std.path; import std.regex; import std.file; import std.string; static string info = CureUSB 0.3 (2014-01-14); static string str_moved = Moved; static string str_deleted = Deleted; static string str_finished = Done; static string str_setattrib = Attributes set; static string str_safename = SafeToDelete; // Expression to match directories with blanks for name static auto re = regex(`^\s+$`); version(Windows) { import core.sys.windows.windows; extern(Windows) uint SetFileAttributesA(LPCSTR, DWORD); } int main() { string ExePath = thisExePath(); string SearchPath = rootName(ExePath); string BlankDirToDelete; version(Windows) SetConsoleOutputCP(65001); writefln(info); foreach(DirEntry d; dirEntries(SearchPath, SpanMode.shallow)) { auto m = matchFirst(baseName(d.name), re); if (m.captures.length 0) { if (isDir(d.name)) { foreach(DirEntry f; dirEntries(d.name, SpanMode.shallow)) { string origname = buildPath(SearchPath, baseName(f.name)); rename(f.name, origname); writefln(`%s %s`, str_moved, origname); } BlankDirToDelete = d.name; } break; } } // The following block does not work; supposedly the process does not have // access to the directory because it is being used by another process, // which does not seem to be the case. Uncomment to try /* if (exists(BlankDirToDelete)) try { string SafeToBeDeleted = buildPath(SearchPath, str_safename) rename(BlankDirToDelete, SafeToBeDeleted); remove(SafeToBeDeleted); } catch (FileException e) writeln(e.msg); */ foreach(DirEntry d; dirEntries(SearchPath, SpanMode.shallow)) { if (d.name != ExePath) { string bname = baseName(d.name); version(Windows) { if ( (bname != RECYCLER) (bname != $Recycle.bin) (bname != System Volume Information) (bname != Recovery) (bname != MSOCache) ) SetFileAttributesA(toStringz(d.name), FILE_ATTRIBUTE_NORMAL + FILE_ATTRIBUTE_ARCHIVE); } // The following block gives a FileException, claiming that the specified file // could not befound. Uncomment to try /* string exten = extension(d.name); if (exten.length 0) { // writefln(`%s %s`, d.name, extension(d.name)); //debug line if ( (exten == .exe) || (exten == .lnk) || (exten == .scr) || (exten == .cpl) || (exten == .hta) || (exten == .com) || (exten == .bat) || (exten == .vb) || (exten == .vbs) ) if (isDir(buildPath(SearchPath, stripExtension(baseName(d.name) { remove(d.name); writefln(`%s %s`, str_deleted, d.name); } } */ } } writefln(%s, str_setattrib); writefln(%s., str_finished); return 0; }
Re: errors with filesystem operations
On Wed, 15 Jan 2014 13:41:50 +0100, Jacob Carlborg wrote: ... On Linux, hidden files are all files where the filename starts with a dot. To make it visible you need to rename the file. As far as I know, Linux doesn't have any form of system attribute. I am aware of this. However, FAT32 and NTFS (through ntfs-3g) and even ExFAT (I do not remember now the name of the project) are supported by Linux, so there should be a way to alter file structure so as to change attributes even if the OS does not natively provide a function for this (why should a user working in Linux be forced to reboot in Windows not even to run an unported application but just to change file attributes in a filesystem the OS supports)? Has anyone attempted it from D?
importing modules from a function
Hi, A moment ago I was reading a reply to one of my posts, and noticed Artur Skawina did this: void main(string argv[]) { import std.string; ... I didn't know D allowed importing modules from a function. Now, my question is: what is the advantage of this over importing the regular way? Regards, Hugo
Re: inotify and recursion
On Fri, 27 Dec 2013 12:56:25 +, Gary Willoughby wrote: On Friday, 27 December 2013 at 03:39:58 UTC, Hugo Florentino wrote: BTW, it it a requirement to use malloc, and if so, when would I need to free the memory allocated by it? I use a static ubyte array. I've been using inotify quite a bit and found it to be very good but there are a few things to keep in mind though. First it can block further program execution when watching files. To avoid this use the select function in 'core.sys.posix.sys.select'. Thanks. Have you ever tried using epoll? I have read that for asynchronous things it works much better than select.
inotify and recursion
Hi, By any chance has anyone linked against libnotify so as to monitor certain changes in a directory and subdirectories, and act according to event? I am thinking in combining this with fossil [1] to automatically document changes in /etc My main concern is that (if I undestand it correctly), inotify requires a file descriptor for every object watched, and if a directory has many files this could probably have an impact on performance, or do you know if just by using the directory I want to monitor, everything within it will also be watched? Also, I have never linked before against a Linux library and inotify does not seem particularly easy because of the file descriptors, so if someone has linked against this library and has a sample code, I would very much appreciate if you could share it. Regards, Hugo [1] www.fossil-scm.org
Re: inotify and recursion
On Fri, 27 Dec 2013 03:23:00 +, David Eagen wrote: Here is a first attempt. I'm sure there are much better ways to do this but this should at least get you going in the right direction. ... Thanks! BTW, it it a requirement to use malloc, and if so, when would I need to free the memory allocated by it?
Re: Wrong output of quotes in Windows (encoding?)
On Thu, 19 Dec 2013 19:38:20 +, Simon wrote: Call: SetConsoleOutputCP(65001); Works for me on win7 64bit. Not sure how far back it's supported though. Interesting, thanks.
Re: how to detect OS architecture?
On Thu, 19 Dec 2013 10:53:12 -0800, Ali Çehreli wrote: On 12/18/2013 05:50 AM, Hugo Florentino wrote: BTW, how could I benchmark the performance of both solutions (lets say for a few thousand runs) to see if one is more efficient than the other? There is std.datetime.benchmark: http://dlang.org/phobos/std_datetime.html#.benchmark Ali Thanks. I don't know why I missed that.
Wrong output of quotes in Windows (encoding?)
Hi, A short while ago I had minor difficulties escaping quotes, and noticed (I don't remember where) a simple function by a D user which I have now tried to enhance. The problem is that output is incorrect in Windows (even with unicode-supporting fonts). I tried to use transcode but could not get it to work. Check the following code, and please advise me what to do in order to get the correct output: import std.stdio, std.string, std.encoding; @trusted string quote(in string str, in char chr = 'd') pure { switch(chr) { case 'b': return '`' ~ str ~ '`'; // backtick case 'd': return `` ~ str ~ ``; // double case 'f': return `«` ~ str ~ `»`; // french case 's': return `'` ~ str ~ `'`; // single case 't': return `“` ~ str ~ `”`; // typographic default: return `` ~ str ~ ``; // double } } void main() { char[] a = ['b', 'd', 'f', 's', 't']; auto input = just a test; foreach(char type; a) writeln(format(Quote type %s:\t%s, type, quote(input, type))); }
Re: how to detect OS architecture?
On Wed, 18 Dec 2013 13:20:45 -, Regan Heath wrote: On Wed, 18 Dec 2013 04:22:23 -, Hugo Florentino h...@acdam.cu wrote: On Tue, 17 Dec 2013 15:13:18 +0100, Gary Willoughby wrote: Make sure you handle if users have a 32bit OS installed on a 64bit PC. As a matter of fact that was the actual configuration in the system I wrote the app. I am now with a friend with the same configuration, and it also seems to be working. At work I use Windows 7 x86_64 and it also works. It works because the SYSTEM_INFO member wProcessorArchitecture is defined to be The processor architecture of the installed operating system .. note, *installed operating system*, not processor architecture. Well, isn't that what I needed to begin with? That's why I said OS architecture instead of CPU architecture. Unless you are refering to something else.
Re: how to detect OS architecture?
On Wed, 18 Dec 2013 13:16:35 -, Regan Heath wrote: IsWow64Process returns (in the output parameter) if the 32 bit application is running in WOW64. WOW64 *only* exists on a 64 bit OS. So, if true this tells you you're on a 64 bit OS. You don't need an additional call to GetNativeSystemInfo at all. Cool, thanks for clarifying. BTW, how could I benchmark the performance of both solutions (lets say for a few thousand runs) to see if one is more efficient than the other?
Re: Wrong output of quotes in Windows (encoding?)
On Wed, 18 Dec 2013 10:05:49 -0800, Ali Çehreli wrote: On 12/18/2013 05:32 AM, Hugo Florentino wrote: output is incorrect in Windows (even with unicode-supporting fonts). Is the code page also set to UTF-8? I think you must issue the command 'chcp 65001'. I have changed your program to print the code units individually in hex. I changed the test string to a single space character so that you can identify it easily on the output: import std.stdio, std.string, std.encoding; @trusted string quote(in string str, in char chr = 'd') pure { switch(chr) { case 'b': return '`' ~ str ~ '`'; // backtick case 'd': return `` ~ str ~ ``; // double case 'f': return `«` ~ str ~ `»`; // french case 's': return `'` ~ str ~ `'`; // single case 't': return `“` ~ str ~ `”`; // typographic default: return `` ~ str ~ ``; // double } } void main() { char[] a = ['b', 'd', 'f', 's', 't']; auto input = ; foreach(char type; a) writeln(format(Quote type %s:\t%(%02x %), type, cast(ubyte[])quote(input, type))); } Does the output of the program look correct according to UTF-8? Then your compiler has produced a correct program. :) Here is the output I get on SL6.1 compiled with dmd v2.065-devel-41ebb59: Quote type b: 60 20 60 Quote type d: 22 20 22 Quote type f: c2 ab 20 c2 bb Quote type s: 27 20 27 Quote type t: e2 80 9c 20 e2 80 9d I trust the correctness of this feature of D so much that I am too lazy to check whether those code units correspond to the intended Unicode characters. :) Ali Changing the codepage worked indeed. Thanks. Now, how could I do that programmatically, so that if my application runs on a system with a different codepage, the output looks correct? After all, not all users feel comfortable typing unknown commands.
Re: how to detect OS architecture?
On Tue, 17 Dec 2013 15:13:18 +0100, Gary Willoughby wrote: Make sure you handle if users have a 32bit OS installed on a 64bit PC. As a matter of fact that was the actual configuration in the system I wrote the app. I am now with a friend with the same configuration, and it also seems to be working. At work I use Windows 7 x86_64 and it also works.
Re: how to detect OS architecture?
On Tue, 17 Dec 2013 13:21:30 -, Regan Heath wrote: Is GetNativeSystemInfo your other solution? On the MSDN page for GetNativeSystemInfo it recommends using IsWow64Process to detect if you're running under WOW64, at which point you would then call GetNativeSystemInfo. I am not sure what GetNativeSystemInfo does if called from a 32 bit exe on a 32 bit OS.. It seems to work. After all it makes sense, the native system is actually 32 bits. I /know/ that the code in std.internal.windows.advapi32 which dynamically loads and calls IsWow64Process will work, because we use it here at work for this very purpose. It's also the simplest most direct way to answer this specific question and it's already present, tested and working in phobos .. so I would be inclined to use it, in preference over GetNativeSystemInfo. R If after using IsWOW64Process a GetNativeSystemInfo must still be issued like you mentioned earlier, I don't see the advantage over calling that function directly in the first place. Or am I missing something?
Re: regarding spawnProcess and parameters in Windows
On Mon, 16 Dec 2013 02:04:10 +0100, Danny Arends wrote: ... So I think this should work: spawnProcess([appexe,format(/INI=%s,appini)], [,],Config.suppressConsole); Hmm... that did not work either, it complained that the parameter was not correct. Actually, the syntax I was using should have worked (according to documentation, second overload of spawnProcess), and I also (unsuccessfully) tried with function execute. Anyway, since my launcher will only be run from Windows, finally I decided to call ShellExecuteA directly (I find it less cumbersome to use and actually more elegant). However, something is bothering me: when running the launcher, it opens a console temporarily before launching the other process (even when I am not using std.stdio) and I cannot get rid of this behavior. This is the code I am now using: import std.file: exists, getcwd; import std.path: buildPath, dirName; import std.string: format, toStringz; import core.sys.windows.windows; immutable static auto appname = myapp; extern(Windows) HANDLE ShellExecuteA(HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, int); int main(string[] args) { auto appath = getcwd(); auto appexe = buildPath(appath, appname ~ .exe); if (exists(appexe)) { auto param = format(`/ini=%s`, buildPath(appath, appname ~ .ini)); ShellExecuteA(null, , toStringz(appexe), toStringz(param), , SW_SHOWMAXIMIZED); scope(failure) return -1; } return 0; } Why does the console window appear and how can I prevent this? Regards, Hugo
Re: regarding spawnProcess and parameters in Windows
On Mon, 16 Dec 2013 18:59:52 +0900, Mike Parker wrote: On 12/16/2013 6:33 PM, Hugo Florentino wrote: ... Why does the console window appear and how can I prevent this? This is how Windows works. There are a couple of ways to eliminate the console. One is to use WinMain instead of main. That requires some boilerplate, though, as it bypasses the main function in DRuntime, so you have to initialize the runtime manually. It has the benefit of being portable across compilers. Another way, which is what I always do, is to add this to the command line (assuming DMD with OPTLINK): -L/SUBSYSTEM:WINDOWS:5.01 Every linker on Windows has some form of this flag. It may even be the same when using the VC toolchain. Also, you can drop the version number at the end. It depends on the minimum version of Windows you target and the target architecture (32-bit vs. 64-bit). Google it if you want to be sure of the details. That did the trick, thanks. Now, suppose I have two versions of the application (myapp32.exe and myapp64.exe), and I want my launcher to launch either, based on the architecture it detected from the OS being run on. What could I do in this case, which is actually what I need to do? (I just started with the simpler variant)
how to detect OS architecture?
Hi, I am writing a launcher to make a Windows application portable, but since this application supports both x86 and x86_64, I would like to detect the architecture of the OS my launcher is being run on, in order to launch the proper executable. How can I do this? Regards, Hugo
Re: regarding spawnProcess and parameters in Windows
On Mon, 16 Dec 2013 05:15:24 -0500, Hugo Florentino wrote: Now, suppose I have two versions of the application (myapp32.exe and myapp64.exe), and I want my launcher to launch either, based on the architecture it detected from the OS being run on. What could I do in this case, which is actually what I need to do? (I just started with the simpler variant) Disregard that (since Windows 64-bit can run 32-bit applications just fine), I made a new thread regarding the OS architecture detection.
Re: how to detect OS architecture?
On Mon, 16 Dec 2013 12:40:17 +0100, MrSmith wrote: version(Windows) { // Windows code goes here } else { // Other OS code goes here } More here: http://dlang.org/version.html I think he wants determine at runtime what architecture x86 or x64 processor supprots and launch appropriate executable. I think this is what he want http://dlang.org/phobos/core_cpuid.html#.isX86_64 Thanks, that's precisely what I needed :)
Re: how to detect OS architecture?
On Mon, 16 Dec 2013 12:59:52 +0100, John Colvin wrote: On Monday, 16 December 2013 at 11:56:07 UTC, Hugo Florentino wrote: On Mon, 16 Dec 2013 12:40:17 +0100, MrSmith wrote: I think this is what he want http://dlang.org/phobos/core_cpuid.html#.isX86_64 Thanks, that's precisely what I needed :) Are you sure? This will tell you about the processor, but not necessarily about what the OS supports. I don't know, but you may find that when using windows 32bit on an x64 machine, cpuid will tell you the cpu is 64bit, but the OS won't let you run any 64bit code. You are right. I realized that this function was not quite what I needed when running this code on a 32 bit system: import std.stdio, core.cpuid; int main() { immutable auto appname = myapp; auto appversion = !isX86_64() ? appname ~ 32 : appname ~ 64) ~ .exe; scope(failure) return -1; writeln(appversion); return 0; } I was expecting myapp32.exe but got myapp64.exe. Apparently what isX86_64() detects is the capability of the processor, not the arquitecture of the OS. So currently D has no specific function for detecting the OS architecture at runtime? I had not expected this. I will try using the other options though. Thanks
Re: how to detect OS architecture?
On Mon, 16 Dec 2013 20:23:00 +0100, Jacob Carlborg wrote: On 2013-12-16 17:46, Marco Leise wrote: Hehe, I guess the whole purpose of the launcher is to run in 32-bit and detect at runtime if the 64-bit main executable can be run or the 32-bit version must be used. The only advantage of that is that only a 32bit launcher needs to be distributed. Perhaps that's the whole idea. It is. :)
Re: how to detect OS architecture?
On Mon, 16 Dec 2013 17:04:18 -, Regan Heath wrote: ... Compile the launcher as 32bit, and use this global boolean isWow64: ... Thanks, it's nice to have another option. What do you guys think are the possible advantages/disadvantages of either solution?
regarding spawnProcess and parameters in Windows
Hello, I am trying to do a small launcher for a Windows application which (in order to be portable) requires a specific parameter, and I am getting this error when I try to run it in Windows 7 SP1: std.process.ProcessException@std\process.d(518): Failed to spawn new process (Access denied.) This is the code I am trying to use: import std.stdio, std.file, std.path, std.string, std.process; static auto appname = myapp; int main(string[] args) { auto appath = dirName(thisExePath()); auto appexe = buildPath(appath, appname ~ .exe); auto appini = buildPath(appath, appname ~ .ini); auto applaunchpars = format(`%s /ini=%s`, appexe, appini); if (exists(appexe)) { auto appPid = spawnProcess(applaunchpars, [ : ], Config.suppressConsole); scope(failure) return -1; } return 0; } The ugly [ : ] hack is because I haven't been able to invoke spawnProcess otherwise, at least withouth specifying stdin and stdout (which I don't really need right now). Actually, I was thinking in making my own wrapper for ShellExecuteA (I don't find spawnProcess particularly intuitive), but I would prefer the D way. Anyway, Where could the problem be? Regards, Hugo
regarding Latin1 to UTF8 encoding
Hi, I am having some problems trygin to pass regular expressions to a webpage encoded in Latin1. I have unsuccessfully tried to convert it to UTF8 before passing the regular expression. Initially I tried to do something like this: auto input = readText(myfile.htm); auto output = replace(input, re1, re2); But I got this error when trying to run the application: std.utf.UTFException@C:\DMD2\Windows\bin\..\..\src\phobos\std\utf.d(1113): Invalid UTF-8 sequence (at index 1) I then tried this, but the error remains auto input = readText(myfile.htm); string buffer; transcode(input, buffer); auto output = replace(buffer, re1, re2); Also, this did not work: auto input = cast(string) read(myfile.htm); string buffer; transcode(input, buffer); auto output = replace(buffer, re1, re2); core.exception.AssertError@std.encoding(1995): Assertion failure Please, any help would be appreciated. Regards, Hugo
Re: regarding Latin1 to UTF8 encoding
On Mon, 09 Dec 2013 03:44:19 +0100, Adam D. Ruppe wrote: On Monday, 9 December 2013 at 02:40:29 UTC, Hugo Florentino wrote: auto input = readText(myfile.htm); Don't use readText if it isn't utf-8; readtext assumes it is utf 8. I've never actually used std.encoding (I wrote my own encoding module for my dom.d, which I used for website scraping too) but I think this is what you want: Latin1String input = cast(Latin1String) std.file.read(myfile.htm); string buffer; transcode(input, buffer); auto output = replace(buffer, re1, re2); see if that works Actually, it did work, even keeping input type as auto. It seems the explicit typecast to Lating1String was the required element for it to work, which makes sense now that I think about it. Thanks a lot for the (amazingly quick) reply ;) Now, if I may add a closely related doubt: Suppose myfile.txt was given to me daily by careless people who usually save it as Latin1 but from time to time might save it as UTF8. Is there a way to detect the encoding prior to typecasting/loading the file? Regards, Hugo
Re: regarding Latin1 to UTF8 encoding
On Mon, 09 Dec 2013 04:19:51 +0100, Adam D. Ruppe wrote: On Monday, 9 December 2013 at 03:07:58 UTC, Hugo Florentino wrote: Is there a way to detect the encoding prior to typecasting/loading the file? UTF-8 can be detected fairly reliably, but not much luck for other encodings. A Windows-1258 and a Latin1 file, for example, are usually fairly indistinguishable from a binary perspective - they use the same numbers, just for different things. (It is possible to distinguish them if you use some context and grammar check kind of things, but that's not easy.) But utf-8 has a neat feature: any non-ascii stuff needs to validate, and it is unlikely that random data would correctly validate. std.utf.validate can do that (though it throws an exception if it fails, ugh!) So here's how I did it in my own characterencodings.d: https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/characterencodings.d#L138 string utf8string; import std.utf; try { validate!string(cast(string) rawdata); // validation passed, assume it is UTF-8 and use it utf8string = cast(string) rawdata; } catch(UTFException t) { // not utf-8, try latin1 transcode(cast(Latin1String) rawData, utf8string); } // now go ahead and use utf8 string, it should be set Clever solution, thanks. Coud this work using scope instead of try/catch? P.S. Nice unit, by the way.
Re: regading detection of stdin
On Fri, 06 Dec 2013 06:42:01 +0100, Jesse Phillips wrote: On Friday, 6 December 2013 at 02:41:28 UTC, Hugo Florentino wrote: I see... so the problem simply was that function readln was expecting user input. In that case, this is not what I intended. I want the application to receive the stdout of another command as stdin, and to detect if nothing is sent. How coud I accomplish this? Then you'll want std.process[1]. This provides functions for piping output[2]. As for detecting output, that can only be verified once the other process has finished; to handle that it should be reasonable to execute[3] and check the complete output. 1. http://dlang.org/phobos/std_process.html 2. http://dlang.org/phobos/std_process.html#.pipeProcess 3. http://dlang.org/phobos/std_process.html#.execute Hmm... I am not too clear even after reading the documentation. From what I could understand, these are all wrappers around spawnProcess and spawnShell, which are intended to launch a child process from the application, but that is not quite what I need. I simply want to support this kind of use: echo -n This is just a test | myapplication The command echo here serves only as an example, it could be any other command not necessarily open-source or programmed in D. What can I do? Regards, Hugo
Re: Digitalmars-d-learn Digest, Vol 95, Issue 30
On Sat, 07 Dec 2013 23:03:10 +0100 Chris Cain wrote: On Friday, 6 December 2013 at 00:24:22 UTC, Hugo Florentino wrote: Hi, I was trying to do something like this (using dmd.2.064.2 both from Windows and Linux), but if nothing is passed from stdin and no parameter is provided, the application freezes: ...snip... Where is the problem? Regards, Hugo I'm not sure I understand the problem. Your program works as I think you describe it should on my Linux box: --- zshazz@manjarox ~/projects/explore % cat /dev/null | rdmd inputStuff.d No argument passed as parameter or from stdin. zshazz@manjarox ~/projects/explore % echo -n | rdmd inputStuff.d No argument passed as parameter or from stdin. zshazz@manjarox ~/projects/explore % echo -n test | rdmd inputStuff.d Argument passed from stdin succesfully stored in variable s. zshazz@manjarox ~/projects/explore % rdmd inputStuff.d blah.txt Argument passed as parameter succesfully stored in variable s. --- If you just run it (via `rdmd inputStuff.d`) it pauses and waits for some sort of input, but that's expected. Using `ctrl-d` results in No argument passed as parameter or from stdin. as expected in that case. Could you describe what you're doing exactly that causes it to freeze? Actually it does not freeze, it is expecting user input as you said. I simply didn't realize this (that's the reason behind my choice of words). What I intend is for my application to simply display a message if no parameter was given and it was not piped after another command. You know, like the average messages you get when typing alone a command which expects some form of input. I don't know how detect input sent to it through a pipe without requiring user interaction, I have not been able to find a working example in the documentation, in the source code or using search engines.
Re: regading detection of stdin
On Sat, 07 Dec 2013 21:33:56 +0100, Adam D. Ruppe wrote: Sounds like what you need is to see if stdin is a tty. import core.sys.posix.unistd; // has isatty() void main() { import std.stdio; writeln(isatty(0)); // 0 is stdin, so this will show 1 if keyboard or 0 if pope } Interesting, thanks for pointing that (this function is not described at all in the source code) Now, would that work regardless of the terminal number ther user is in (ie tty1, tty2...)? Also, is there a way to do accomplish the same in Windows? I would prefer my application to support both platforms if possible (unless this posix unit is cross-platform, which seems unlikely).
regading detection of stdin
Hi, I was trying to do something like this (using dmd.2.064.2 both from Windows and Linux), but if nothing is passed from stdin and no parameter is provided, the application freezes: import std.stdio, std.file: readText; int main(string[] args) { string s; switch (args.length) { case 1: if ((s = stdin.readln()) is null) writeln(No argument passed as parameter or from stdin.); else writeln(Argument passed from stdin succesfully stored in variable s.); scope (failure) { writeln(Error reading from stdin.); return -1; } break; case 2: s = readText(args[1]); scope (failure) { writeln(Error reading from file passed as parameter.); return -2; } writeln(Argument passed as parameter succesfully stored in variable s.); break; default: writeln(Incorrect number of parameters. Maximum is one.); return -3; } return 0; } Where is the problem? Regards, Hugo
Re: regading detection of stdin
On Fri, 06 Dec 2013 01:26:28 +0100, Adam D. Ruppe wrote: On Friday, 6 December 2013 at 00:24:22 UTC, Hugo Florentino wrote: if nothing is passed from stdin and no parameter is provided, the application freezes: Does it freeze or just wait for you to press enter on the keyboard? I see... so the problem simply was that function readln was expecting user input. In that case, this is not what I intended. I want the application to receive the stdout of another command as stdin, and to detect if nothing is sent. How coud I accomplish this?