[commit: packages/Win32] depend-os-windows, fix-appveyor-curl-ussue, master, win32-2.5.4.1-ghc-8.2: Add commandline arguments splitting. (#76) (2567e43)

git at git.haskell.org git at git.haskell.org
Mon Apr 17 21:28:36 UTC 2017


Repository : ssh://git@git.haskell.org/Win32

On branches: depend-os-windows,fix-appveyor-curl-ussue,master,win32-2.5.4.1-ghc-8.2
Link       : http://git.haskell.org/packages/Win32.git/commitdiff/2567e4395be97ee2bdb11c622cfbf2c549931c13

>---------------------------------------------------------------

commit 2567e4395be97ee2bdb11c622cfbf2c549931c13
Author: Tamar Christina <Mistuke at users.noreply.github.com>
Date:   Sat Feb 18 18:07:14 2017 +0000

    Add commandline arguments splitting. (#76)


>---------------------------------------------------------------

2567e4395be97ee2bdb11c622cfbf2c549931c13
 System/Win32/Console.hsc | 27 +++++++++++++++++++++++++--
 changelog.md             |  4 ++++
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/System/Win32/Console.hsc b/System/Win32/Console.hsc
index a40887f..42b162f 100644
--- a/System/Win32/Console.hsc
+++ b/System/Win32/Console.hsc
@@ -25,13 +25,22 @@ module System.Win32.Console (
         setConsoleOutputCP,
         -- * Ctrl events
         CtrlEvent, cTRL_C_EVENT, cTRL_BREAK_EVENT,
-        generateConsoleCtrlEvent
+        generateConsoleCtrlEvent,
+        -- * Command line
+        commandLineToArgv
   ) where
 
 ##include "windows_cconv.h"
 
 import System.Win32.Types
 
+import Foreign.C.Types (CInt(..))
+import Foreign.C.String (withCWString, CWString)
+import Foreign.Ptr (Ptr)
+import Foreign.Storable (peek)
+import Foreign.Marshal.Array (peekArray)
+import Foreign.Marshal.Alloc (alloca)
+
 foreign import WINDOWS_CCONV unsafe "windows.h GetConsoleCP"
         getConsoleCP :: IO UINT
 
@@ -59,4 +68,18 @@ generateConsoleCtrlEvent e p
 foreign import WINDOWS_CCONV safe "windows.h GenerateConsoleCtrlEvent"
     c_GenerateConsoleCtrlEvent :: CtrlEvent -> DWORD -> IO BOOL
 
--- ToDo: lots more
+foreign import WINDOWS_CCONV unsafe "Shellapi.h CommandLineToArgvW"
+     c_CommandLineToArgvW :: CWString -> Ptr CInt -> IO (Ptr CWString)
+
+-- | This function can be used to parse commandline arguments and return
+--   the split up arguments as elements in a list.
+commandLineToArgv :: String -> IO [String]
+commandLineToArgv []  = return []
+commandLineToArgv arg =
+  do withCWString arg $ \c_arg -> do
+       alloca $ \c_size -> do
+         res <- c_CommandLineToArgvW c_arg c_size
+         size <- peek c_size
+         args <- peekArray (fromIntegral size) res
+         _ <- localFree res
+         mapM peekTString args
\ No newline at end of file
diff --git a/changelog.md b/changelog.md
index e7e4297..d3069b6 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,5 +1,9 @@
 # Changelog for [`Win32` package](http://hackage.haskell.org/package/Win32)
 
+## Unreleased GIT version
+
+* Add `commandLineToArgv`
+
 ## 2.5.1.0 *Feb 2017*
 
 * Add `withHandleToHANDLE` (originally found in the `ansi-terminal` library)



More information about the ghc-commits mailing list