[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36

John Goerzen jgoerzen at complete.org
Fri Apr 23 14:55:32 UTC 2010


The following commit has been merged in the master branch:
commit a082a53a0bba9629d8e52af8f4a91d5af508df15
Author: John Goerzen <jgoerzen at complete.org>
Date:   Sat Apr 2 00:44:37 2005 +0100

    New CSV module
    
    Keywords:
    
    
    (jgoerzen at complete.org--projects/missingh--head--0.7--patch-209)

diff --git a/ChangeLog b/ChangeLog
index c790661..0e7f57d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,26 @@
 # arch-tag: automatic-ChangeLog--jgoerzen at complete.org--projects/missingh--head--0.7
 #
 
+2005-04-01 17:44:37 GMT	John Goerzen <jgoerzen at complete.org>	patch-209
+
+    Summary:
+      New CSV module
+    Revision:
+      missingh--head--0.7--patch-209
+
+
+    new files:
+     MissingH/Str/.arch-ids/=id MissingH/Str/CSV.hs
+     testsrc/Str/.arch-ids/=id testsrc/Str/CSVtest.hs
+
+    modified files:
+     ChangeLog MissingH.cabal testsrc/Tests.hs
+
+    new directories:
+     MissingH/Str MissingH/Str/.arch-ids testsrc/Str
+     testsrc/Str/.arch-ids
+
+
 2005-03-15 16:28:17 GMT	John Goerzen <jgoerzen at complete.org>	patch-208
 
     Summary:
diff --git a/MissingH.cabal b/MissingH.cabal
index ea13a7a..0581a93 100644
--- a/MissingH.cabal
+++ b/MissingH.cabal
@@ -15,6 +15,7 @@ Exposed-Modules: MissingH.IO, MissingH.IO.Binary, MissingH.List,
     MissingH.Hsemail.Rfc2822,
   MissingH.Regex.Pesco,
   MissingH.Str,
+    MissingH.Str.CSV,
   MissingH.Cmd,
   MissingH.FiniteMap, MissingH.Path, MissingH.Path.NameManip,
   MissingH.Time,
diff --git a/MissingH/Str/CSV.hs b/MissingH/Str/CSV.hs
new file mode 100644
index 0000000..357e376
--- /dev/null
+++ b/MissingH/Str/CSV.hs
@@ -0,0 +1,90 @@
+{- arch-tag: CSV and TSV utilities
+Copyright (C) 2005 John Goerzen <jgoerzen at complete.org>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+-}
+
+{- |
+   Module     : MissingH.Str.CSV
+   Copyright  : Copyright (C) 2005 John Goerzen
+   License    : GNU GPL, version 2 or above
+
+   Maintainer : John Goerzen,
+   Maintainer : jgoerzen at complete.org
+   Stability  : provisional
+   Portability: portable
+
+Haskell Parsec parsers for comma-separated value (CSV) files.
+
+Written by John Goerzen, jgoerzen\@complete.org
+-}
+
+module MissingH.Str.CSV(csvFile) where
+import Text.ParserCombinators.Parsec
+
+eol = (try $ string "\n\r") <|> (try $ string "\r\n") <|> string "\n" <|>
+      string "\r" <?> "End of line"
+cell = quotedcell <|> many (noneOf ",\n\r")
+quotedchar = noneOf "\"" 
+             <|> (try $ do string "\"\""
+                           return '"'
+                 )
+quotedcell :: CharParser st String
+quotedcell = do char '"'
+                content <- many quotedchar
+                char '"'
+                return content
+line = sepBy cell (char ',')
+
+{- | Parse a Comma-Separated Value (CSV) file.  The return value is a list of
+lines; each line is a list of cells; and each cell is a String.
+
+Please note that CSV files may have a different number of cells on each line.
+Also, it is impossible to distinguish a CSV line that has a call with no data
+from a CSV line that has no cells.
+
+Here are some examples:
+
+>Input (literal strings)          Parses As (Haskell String syntax)
+>-------------------------------- ---------------------------------
+>1,2,3                            [["1", "2", "3"]]
+>
+>l1                               [["l1"], ["l2"]]
+>l2
+>
+> (empty line)                    [[""]]
+>
+>NQ,"Quoted"                      [["NQ", "Quoted"]]
+>
+>NQ,"Embedded""Quote"             [["NQ", "Embedded\"Quote"]]
+
+To parse a String, you might use:
+
+>import Text.ParserCombinators.Parsec
+>import MissingH.Str.CSV
+>....
+>parse csvFile "" mystring
+
+To parse a file, you might instead use:
+
+>do result <- parseFromFile csvFile "/path/to/file"
+
+Please note that the result of parsing will be of type
+(Either ParseError [[String]]).  A Left result indicates an error.
+For more details, see the Parsec information.
+-}
+
+csvFile :: CharParser st [[String]]
+csvFile = endBy line eol
diff --git a/testsrc/Str/CSVtest.hs b/testsrc/Str/CSVtest.hs
new file mode 100644
index 0000000..2c060de
--- /dev/null
+++ b/testsrc/Str/CSVtest.hs
@@ -0,0 +1,43 @@
+{- arch-tag: CSV tests main file
+Copyright (C) 2005 John Goerzen <jgoerzen at complete.org>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+-}
+
+module Str.CSVtest(tests) where
+import HUnit
+import MissingH.Str.CSV
+import Text.ParserCombinators.Parsec
+
+test_csv =
+    let f inp exp = TestLabel inp $ TestCase $ 
+                    exp @=? case parse csvFile "" inp of
+                                  Right x -> Right x
+                                  Left y -> Left (show y)
+        in [
+        f "" (Right []),
+        f "\n" (Right [[""]]),
+        f "1,2,3\n" (Right [["1", "2", "3"]]),
+        f "This is a,Test,Really\n" (Right [["This is a", "Test", "Really"]]),
+        f "l1\nl2\n" (Right [["l1"], ["l2"]]),
+        f "NQ,\"Quoted\"\n" (Right [["NQ", "Quoted"]]),
+        f "1Q,\"\"\"\"\n" (Right [["1Q", "\""]]),
+        f ",\"\"\n" (Right [["", ""]]),
+        f "\"Embedded\"\"Quote\"\n" (Right [["Embedded\"Quote"]])
+        ]
+
+tests = TestList [TestLabel "csv" (TestList test_csv)]
+
+
diff --git a/testsrc/Tests.hs b/testsrc/Tests.hs
index f8f50a8..2b96aea 100644
--- a/testsrc/Tests.hs
+++ b/testsrc/Tests.hs
@@ -37,12 +37,14 @@ import qualified GZiptest
 import qualified HVIOtest
 import qualified HVFStest
 import qualified Timetest
+import qualified Str.CSVtest
 
 test1 = TestCase ("x" @=? "x")
 
 tests = TestList [TestLabel "test1" test1,
                  TestLabel "List" Listtest.tests,
                  TestLabel "Str" Strtest.tests,
+                 TestLabel "CSV" Str.CSVtest.tests,
                  TestLabel "Time" Timetest.tests,
                  TestLabel "FiniteMap" FiniteMaptest.tests,
                  TestLabel "AnyDBM" AnyDBMtest.tests,

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list