From b641d4a5083cffc56d2c7b6ac051a2113860820d Mon Sep 17 00:00:00 2001 From: Jeremy Rand Date: Wed, 14 Apr 2021 23:12:04 -0400 Subject: [PATCH] Add a test markdown file. Parse the arguments in main to get the input and output file. Read the input file and pass it to the parser. The test markdown file is successfully parsed! --- md2teach.xcodeproj/project.pbxproj | 2 + .../xcshareddata/xcschemes/md2teach.xcscheme | 8 +++ md2teach/main.c | 67 +++++++++++++++++-- md2teach/test.md | 39 +++++++++++ 4 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 md2teach/test.md diff --git a/md2teach.xcodeproj/project.pbxproj b/md2teach.xcodeproj/project.pbxproj index 143d738..9ed9df2 100644 --- a/md2teach.xcodeproj/project.pbxproj +++ b/md2teach.xcodeproj/project.pbxproj @@ -51,6 +51,7 @@ 9D65330C2626246700105D50 /* md4c.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = md4c.c; sourceTree = ""; }; 9DDFC7B026269D23006D6E71 /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; 9DDFC7B126269D3F006D6E71 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + 9DDFC7B42627E081006D6E71 /* test.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = test.md; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -92,6 +93,7 @@ 9D6532EE2626240800105D50 /* Makefile */, 9D6532F02626240800105D50 /* make */, 9D6532FB2626240800105D50 /* Supporting Files */, + 9DDFC7B42627E081006D6E71 /* test.md */, ); path = md2teach; sourceTree = ""; diff --git a/md2teach.xcodeproj/xcshareddata/xcschemes/md2teach.xcscheme b/md2teach.xcodeproj/xcshareddata/xcschemes/md2teach.xcscheme index e8273c0..7259fc8 100644 --- a/md2teach.xcodeproj/xcshareddata/xcschemes/md2teach.xcscheme +++ b/md2teach.xcodeproj/xcshareddata/xcschemes/md2teach.xcscheme @@ -58,6 +58,14 @@ argument = "$TARGET_BUILD_DIR/md2teach" isEnabled = "YES"> + + + + #include +#include #include #include "md4c.h" @@ -58,8 +60,6 @@ MD_PARSER parser = { NULL // syntax }; -char *testMarkdown = "Hello, world!\n\nMore text\n"; - // Implementation @@ -108,10 +108,69 @@ static void debugLogHook(const char * message, void * userdata) int main(int argc, char * argv[]) { - int result; + static int result; + + static char * commandName; + + static char * inputFileName; + static FILE * inputFile; + static long inputFileLen; + static char * inputBuffer; + + static char * outputFileName; + printf("Stack start: %p\n", &result); - result = md_parse(testMarkdown, strlen(testMarkdown), &parser, NULL); + if (argc != 3) { + fprintf(stderr, "USAGE: %s inputfile outputfile\n", argv[0]); + exit(1); + } + + commandName = argv[0]; + inputFileName = argv[1]; + outputFileName = argv[2]; + + inputFile = fopen(inputFileName, "r"); + if (inputFile == NULL) { + fprintf(stderr, "%s: Unable to open file %s, %s\n", commandName, inputFileName, strerror(errno)); + exit(1); + } + + if (fseek(inputFile, 0l, SEEK_END) != 0) { + fprintf(stderr, "%s: Unable to seek to the end of file %s, %s\n", commandName, inputFileName, strerror(errno)); + fclose(inputFile); + exit(1); + } + + inputFileLen = ftell(inputFile); + if (inputFileLen < 0) { + fprintf(stderr, "%s: Unable to get size of file %s, %s\n", commandName, inputFileName, strerror(errno)); + fclose(inputFile); + exit(1); + } + + inputBuffer = malloc(inputFileLen); + if (inputBuffer == NULL) { + fprintf(stderr, "%s: Unable to allocate %ld bytes for input buffer\n", commandName, inputFileLen); + fclose(inputFile); + exit(1); + } + + if (fseek(inputFile, 0l, SEEK_SET) != 0) { + fprintf(stderr, "%s: Unable to seek to the beginning of file %s, %s\n", commandName, inputFileName, strerror(errno)); + free(inputBuffer); + fclose(inputFile); + exit(1); + } + + if (fread(inputBuffer, 1, inputFileLen, inputFile) != inputFileLen) { + fprintf(stderr, "%s: Unable to read all of file %s, %s\n", commandName, inputFileName, strerror(errno)); + free(inputBuffer); + fclose(inputFile); + exit(1); + } + + result = md_parse(inputBuffer, inputFileLen, &parser, NULL); printf("Parser result: %d\n", result); return 0; diff --git a/md2teach/test.md b/md2teach/test.md new file mode 100644 index 0000000..4c9fb1c --- /dev/null +++ b/md2teach/test.md @@ -0,0 +1,39 @@ +# h1 +## h2 +### h3 +#### h4 +##### h5 +###### h6 + +h1 +== + +h2 +-- + +-------------------- + + indented code + +``` +fenced code +``` + + + +> quote + +* list item +1. list item + +[ref]: /url + +paragraph +© Ӓ ꯍ +`code` +*emph* **strong** ***strong emph*** +_emph_ __strong__ ___strong emph___ +[ref] [ref][] [link](/url) +![ref] ![ref][] ![img](/url) + +\\ \* \. \` \