Vijenthiravasan M
2008-09-22 08:54:00 UTC
module EmbeddingSystem (
Table
-- some display utilities --
, printTable -- Table -> IO ()
, writeTable -- Table -> String
)
where
import qualified List
import Maybe
import Char -- import Char library
import IO -- import Input Output library
import Prelude -- import Prelude library
-------------------------
-- data type declaration
-------------------------
type Name = String
--type Table = String
type Field = String
type Value = String
type Schema = [String]
type Record = [Value]
type Records = [Record]
data Table = Table
String --table name
[String] --field names
[[String]] --records
-------------------------------------------------------------------
-- Sample Database --
-- here to test the input slection for the menu valide or invalid--
-------------------------------------------------------------------
top10 = Table
"LargestCountries"
[ "Rank","Country", "Area"]
[ [ "1", "Russia", "17,075,400"]
, [ "2", "Canada", "9,976,140"]
, [ "3", "United States", "9,629,091"]
, [ "4", "China", "9,596,960"]
, [ "5", "Brazil", "8,511,965"]
, [ "6", "Australia", "7,686,850"]
, [ "7", "India", "3,287,590"]
, [ "8", "Argentina", "2,776,890"]
, [ "9", "Kazakhstan", "2,717,306"]
, [ "10", "Sudan", "2,505,810"]
]
-------------------------------------------------------------------
-- start Function --
-- here to start the Index and to show the main menu --
-------------------------------------------------------------------
start:: IO()
start=
do
menu
input <- getLine
if ((input) == "5")
then
return ()
else do
checkMenu (input)
start
--ww :: String -> Table
--ww a(Table n s v) =
--Table a s v
-------------------------------------------------------------------
-- main menu Function --
-- main menu design to show the main menu --
-------------------------------------------------------------------
menu :: IO()
menu =
do
putStr "\n\n"
putStrLn "\t*********************************************"
putStrLn "\t**** Embedding System ****"
putStrLn "\t============================================="
putStrLn "\t** Main Menu: ***"
putStrLn "\t** 1. Showing a table ***"
putStrLn "\t** 2. Selecting a row or column ***"
putStrLn "\t** 3. Selecting a record ***"
putStrLn "\t** 4. Other queries using DB Operators ***"
putStrLn "\t** 5. Exit ***"
putStrLn "\t*********************************************"
putStr "\tPlease Select Your Choice: "
return ()
-------------------------------------------------------------------
-- check menu Function --
-- here to test the input slection for the menu valide or invalid--
-------------------------------------------------------------------
checkMenu :: String -> IO()
checkMenu x =
do
if (x=="1")
then
input
else do
putStr "\n\n\tSorry, Invalid Number Entered!\n"
return ()
--------------------------------------------------------------
-- input --
--Allowed the user to enter input file to generate the index--
--------------------------------------------------------------
input :: IO ()
input
= do
--hSetBuffering stdout NoBuffering
putStr "\n\n\tPlease enter your words: "
a <- getLine
output (a)
--printTable top10
--------------------------------------------------------------
-- Output --
-- To save output file after generate the index --
-------------------------------------------------------------
output :: Table -> IO()
output a =
do
printTable a
-------------------------------------------------------------------
-- Pretty Print --
-- here to test the input slection for the menu valide or invalid--
-------------------------------------------------------------------
maxWidths t@(Table n s v) =
maxWidths' ((fieldNameWidths t) : (columnWidths t)) where
maxWidths' :: [[Int]] -> [Int]
maxWidths' [] = []
maxWidths' [una] = una -- if just one list, then it's max
maxWidths' (una:mas) = zipWith max una (maxWidths' mas)
columnWidths (Table n s v) = map (map length) v
fieldNameWidths (Table n s v) = map length s
writeTable t@(Table n s v) =
let ms = maxWidths t
ss = sum ms + 1 + length ms
br = nChars '-' ss ++ newline
in newline
++ n ++ ":" ++ newline
++ br
++ formedLine ms s ++ newline