~alcinnz/rhapsode

43479697d9f8d1f7b28d65aa0cb2d77cd590ce9b — Adrian Cochrane 4 years ago 505b29c
Output converted webpage.
2 files changed, 7 insertions(+), 5 deletions(-)

M src/CExports.hs
M src/main.c
M src/CExports.hs => src/CExports.hs +4 -3
@@ 29,6 29,7 @@ import Data.ByteString (useAsCString)
-- For c_extractLinks
import Links (Link(..), extractLinks)
import qualified Data.Text.Foreign as Txt
import Data.Text (strip)

-- Types I can export to C
import Foreign.Ptr


@@ 120,11 121,11 @@ foreign export ccall c_extractLinks :: StablePtr Page -> IO (CArray CString)
c_extractLinks c_page = do
    page <- deRefStablePtr c_page
    ret <- forM (extractLinks $ html page) $ \link -> do
        c_label <- text2cstring $ label link
        c_title <- text2cstring $ title link
        c_label <- text2cstring $ strip $ label link
        c_title <- text2cstring $ strip $ title link
        c_href <- newCString $ uriToString id (href link) ""
        return [c_label, c_title, c_href]
    nil <- newCString ""
    nil <- newCString " "
    newArray0 nil $ concat ret

text2cstring txt = Txt.withCStringLen txt $ \s -> (peekCStringLen s >>= newCString)

M src/main.c => src/main.c +3 -2
@@ 1,6 1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "HsFFI.h"

struct session;


@@ 26,7 26,8 @@ int main(int argc, char **argv) {
    char *ssml = c_renderDoc(session, page);
    char **links = c_extractLinks(page);

    printf("%i Hello, world!\n", argc);
    printf("%s\n", ssml);
    for (int i = 0; strcmp(links[i], " ") != 0; i++) fprintf(stderr, "%s\n", links[i]);

    c_freePage(page);
    c_freePage(referer);