From 0dd8ca5c7622f3708a2fbe18ac418cc08073bac9 Mon Sep 17 00:00:00 2001 From: "kurt.w.jung" Date: Thu, 8 Mar 2012 18:28:49 +0000 Subject: [PATCH] * Guard against passing name of unwriteable file to SaveToFile; thanks and.damore * Replace grid_sheet.lua in parent directory with common.lua in demo directory * Return number of characters printed by hpdf.Page_TextRect() --- Makefile | 9 ++++++--- demo/arc_demo.lua | 4 ++-- grid_sheet.lua => demo/common.lua | 8 ++++++++ demo/encryption_demo.lua | 3 ++- demo/ext_gstate_demo.lua | 3 ++- demo/font_demo.lua | 4 ++-- demo/grid_demo.lua | 4 ++-- demo/hello_demo.lua | 6 ++---- demo/line_demo.lua | 3 ++- demo/link_demo.lua | 4 ++-- demo/logo_demo.lua | 3 ++- demo/outline_demo.lua | 4 ++-- demo/permission_demo.lua | 3 ++- demo/slide_show_demo.lua | 3 ++- demo/text_demo.lua | 4 ++-- doc/text/change-log.txt | 8 ++++++-- doc/text/demos.txt | 4 ++-- doc/text/license.txt | 2 +- hpdf.c | 9 ++++++++- 19 files changed, 57 insertions(+), 31 deletions(-) rename grid_sheet.lua => demo/common.lua (92%) diff --git a/Makefile b/Makefile index 6495e1b..50c6479 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ HPDFINC= # --- End of user settings, no need to change anything below this line. --- TARGET=hpdf.so -PACKAGE=luahpdf-1.1 +PACKAGE=luahpdf-1.2 TAR=$(PACKAGE).tar.gz ZIP=$(PACKAGE).zip @@ -43,6 +43,9 @@ HTML = \ $(TARGET) : hpdf.c cc -ansi -DHPDF_SHARED -pedantic -Wall -O2 $(CFLAGS) $(LDFLAGS) -o $@ $(LUAINC) $(HPDFINC) -shared $(LUALIB) $(HPDFLIB) $< +dump : + cc -E -dM -ansi -DHPDF_SHARED -pedantic -Wall -O2 $(CFLAGS) $(LUAINC) $(HPDFINC) -shared hpdf.c > $@ + test : $(TARGET) @lua -e "package.path=\"\" package.cpath=\"./?.so;./?.dll\" require \"hpdf\" print(hpdf.VERSION_TEXT)" @@ -69,7 +72,7 @@ package : clean doc cp doc/text/*.txt $(PACKAGE)/doc/text cp doc/html/*.css doc/html/*.png doc/html/*.html $(PACKAGE)/doc/html cp demo/* $(PACKAGE)/demo - cp README Makefile hpdf.c grid_sheet.lua $(PACKAGE) + cp README Makefile hpdf.c $(PACKAGE) tar czvf $(TAR) $(PACKAGE) zip -r $(ZIP) $(PACKAGE) rm -fr $(PACKAGE) @@ -84,4 +87,4 @@ $(HTML): doc/html/%.html: doc/text/%.txt $(PDF): %.pdf: %.lua $(TARGET) @luac -l -p $< | grep SETGLOBAL; true - lua -e 'package.path="./?.lua" package.cpath="./?.so"' $< + lua -e 'package.path="demo/?.lua;./?.lua" package.cpath="./?.so"' $< diff --git a/demo/arc_demo.lua b/demo/arc_demo.lua index d3a1108..bf819ee 100644 --- a/demo/arc_demo.lua +++ b/demo/arc_demo.lua @@ -16,7 +16,7 @@ It is provided "as is" without express or implied warranty. --]]-- require "hpdf" -require "grid_sheet" +require "common" local pdf = hpdf.New() if pdf then @@ -79,7 +79,7 @@ if pdf then hpdf.Page_Fill(page) -- save the document to a file - hpdf.SaveToFile(pdf, string.gsub(arg[0], "%.%w*$", "") .. ".pdf") + save_to_file(pdf, arg[0]) -- clean up hpdf.Free(pdf) diff --git a/grid_sheet.lua b/demo/common.lua similarity index 92% rename from grid_sheet.lua rename to demo/common.lua index 67aeee0..80cf1f3 100644 --- a/grid_sheet.lua +++ b/demo/common.lua @@ -106,3 +106,11 @@ function hpdf.print_grid(pdf, page) hpdf.Page_SetGrayStroke(page, 0) end -- hpdf.print_grid + +function save_to_file(pdf, script_name) + script_name = string.gsub(script_name, "%.%w*$", "") .. ".pdf" + if 0 ~= hpdf.SaveToFile(pdf, script_name) then + io.stderr:write('Error writing to ', script_name, '\n') + end +end + diff --git a/demo/encryption_demo.lua b/demo/encryption_demo.lua index 931f563..c524afa 100644 --- a/demo/encryption_demo.lua +++ b/demo/encryption_demo.lua @@ -16,6 +16,7 @@ It is provided "as is" without express or implied warranty. --]]-- require "hpdf" +require "common" local pdf = hpdf.New() if pdf then @@ -32,7 +33,7 @@ if pdf then hpdf.Page_ShowText(page, text) hpdf.Page_EndText (page) hpdf.SetPassword(pdf, "owner", "user") - hpdf.SaveToFile(pdf, string.gsub(arg[0], "%.%w*$", "") .. ".pdf") + save_to_file(pdf, arg[0]) hpdf.Free(pdf) else io.write("Error creating PDF object\n") diff --git a/demo/ext_gstate_demo.lua b/demo/ext_gstate_demo.lua index 6276c0d..298027e 100644 --- a/demo/ext_gstate_demo.lua +++ b/demo/ext_gstate_demo.lua @@ -16,6 +16,7 @@ It is provided "as is" without express or implied warranty. --]]-- require "hpdf" +require "common" local function draw_circles(page, description, x, y) hpdf.Page_SetLineWidth(page, 1.0) @@ -97,7 +98,7 @@ if pdf then end -- J, Rec -- save the document to a file - hpdf.SaveToFile(pdf, string.gsub(arg[0], "%.%w*$", "") .. ".pdf") + save_to_file(pdf, arg[0]) -- clean up hpdf.Free(pdf) diff --git a/demo/font_demo.lua b/demo/font_demo.lua index ed52ca9..6599448 100644 --- a/demo/font_demo.lua +++ b/demo/font_demo.lua @@ -16,6 +16,7 @@ It is provided "as is" without express or implied warranty. --]]-- require "hpdf" +require "common" local font_list = { "Courier", @@ -34,7 +35,6 @@ local font_list = { "ZapfDingbats"} local page_title = "Font demonstration" -local fname = string.gsub(arg[0], "%.%w*$", "") .. ".pdf" local pdf = hpdf.New() if pdf then @@ -90,7 +90,7 @@ if pdf then end -- j, font_str hpdf.Page_EndText(page) - hpdf.SaveToFile(pdf, fname) + save_to_file(pdf, arg[0]) hpdf.Free(pdf) else diff --git a/demo/grid_demo.lua b/demo/grid_demo.lua index 26ffa39..288e993 100644 --- a/demo/grid_demo.lua +++ b/demo/grid_demo.lua @@ -1,11 +1,11 @@ require "hpdf" -require "grid_sheet" +require "common" local pdf = hpdf.New() if pdf then local page = hpdf.AddPage(pdf) hpdf.print_grid(pdf, page) - hpdf.SaveToFile(pdf, string.gsub(arg[0], "%.%w*$", "") .. ".pdf") + save_to_file(pdf, arg[0]) hpdf.Free(pdf) else io.write("Error creating PDF object\n") diff --git a/demo/hello_demo.lua b/demo/hello_demo.lua index c896b80..70a3a68 100644 --- a/demo/hello_demo.lua +++ b/demo/hello_demo.lua @@ -1,20 +1,18 @@ require "hpdf" +require "common" local pdf = hpdf.New() if pdf then local page = hpdf.AddPage(pdf) - -- hpdf.Page_SetWidth(page, 72) - -- hpdf.Page_SetHeight(page, 72) local height = hpdf.Page_GetHeight(page) local width = hpdf.Page_GetWidth(page) - -- io.write("width ", width, ", height ", height, "\n") local font = hpdf.GetFont(pdf, "Times-Roman") hpdf.Page_SetFontAndSize(page, font, 24) hpdf.Page_BeginText(page) hpdf.Page_TextOut(page, 60, height - 60, "Hello from Haru version " .. hpdf.VERSION_TEXT) hpdf.Page_EndText(page) - hpdf.SaveToFile(pdf, string.gsub(arg[0], "%.%w*$", "") .. ".pdf") + save_to_file(pdf, arg[0]) hpdf.Free(pdf) else io.write("Error creating PDF object\n") diff --git a/demo/line_demo.lua b/demo/line_demo.lua index 71f901d..4b3aa0f 100644 --- a/demo/line_demo.lua +++ b/demo/line_demo.lua @@ -16,6 +16,7 @@ It is provided "as is" without express or implied warranty. --]]-- require "hpdf" +require "common" local function draw_line(page, x, y, label) hpdf.Page_BeginText(page) @@ -301,7 +302,7 @@ if pdf then hpdf.Page_Stroke(page) -- save the document to a file - hpdf.SaveToFile(pdf, string.gsub(arg[0], "%.%w*$", "") .. ".pdf") + save_to_file(pdf, arg[0]) -- clean up hpdf.Free(pdf) diff --git a/demo/link_demo.lua b/demo/link_demo.lua index 2fb5050..e84fc99 100644 --- a/demo/link_demo.lua +++ b/demo/link_demo.lua @@ -16,6 +16,7 @@ It is provided "as is" without express or implied warranty. --]]-- require "hpdf" +require "common" local function print_page(pdf, font, page_num) local page = hpdf.AddPage(pdf) @@ -30,7 +31,6 @@ local function print_page(pdf, font, page_num) end -- print_page local uri = "http://sourceforge.net/projects/libharu" -local fname = string.gsub(arg[0], "%.%w*$", "") .. ".pdf" local pdf = hpdf.New() if pdf then @@ -180,7 +180,7 @@ if pdf then hpdf.Page_EndText(index_page) -- save the document to a file - hpdf.SaveToFile(pdf, fname) + save_to_file(pdf, arg[0]) -- clean up hpdf.Free(pdf) diff --git a/demo/logo_demo.lua b/demo/logo_demo.lua index 1e85050..46e8e39 100644 --- a/demo/logo_demo.lua +++ b/demo/logo_demo.lua @@ -1,4 +1,5 @@ require "hpdf" +require "common" local function logodraw(pdf, page, width, left, bottom) local scale = 1 - math.sqrt(2) / 2 @@ -50,7 +51,7 @@ if pdf then local width = hpdf.Page_GetWidth(page) local edge = width / 2 logodraw(pdf, page, edge, (width - edge) / 2, (height - edge) / 2) - hpdf.SaveToFile(pdf, string.gsub(arg[0], "%.%w*$", "") .. ".pdf") + save_to_file(pdf, arg[0]) hpdf.Free(pdf) else io.write("Error creating PDF object\n") diff --git a/demo/outline_demo.lua b/demo/outline_demo.lua index d448835..f0b7239 100644 --- a/demo/outline_demo.lua +++ b/demo/outline_demo.lua @@ -16,6 +16,7 @@ It is provided "as is" without express or implied warranty. --]]-- require "hpdf" +require "common" local function print_page(page, page_num) hpdf.Page_SetWidth(page, 800) @@ -32,7 +33,6 @@ local function create_dst(page, outline) hpdf.Outline_SetDestination(outline, dst) end -- create_dst -local fname = string.gsub(arg[0], "%.%w*$", "") .. ".pdf" local pdf = hpdf.New() if pdf then @@ -68,7 +68,7 @@ if pdf then end -- j -- save the document to a file - hpdf.SaveToFile(pdf, fname) + save_to_file(pdf, arg[0]) -- clean up hpdf.Free(pdf) diff --git a/demo/permission_demo.lua b/demo/permission_demo.lua index af3ffdc..2f22f53 100644 --- a/demo/permission_demo.lua +++ b/demo/permission_demo.lua @@ -16,6 +16,7 @@ It is provided "as is" without express or implied warranty. --]]-- require "hpdf" +require "common" local text = "User cannot print and copy this document." local owner_passwd = "owner" @@ -45,7 +46,7 @@ if pdf then hpdf.SetEncryptionMode(pdf, "HPDF_ENCRYPT_R3", 16) -- save the document to a file - hpdf.SaveToFile(pdf, string.gsub(arg[0], "%.%w*$", "") .. ".pdf") + save_to_file(pdf, arg[0]) -- clean up hpdf.Free(pdf) diff --git a/demo/slide_show_demo.lua b/demo/slide_show_demo.lua index b8e9a69..d566e72 100644 --- a/demo/slide_show_demo.lua +++ b/demo/slide_show_demo.lua @@ -16,6 +16,7 @@ It is provided "as is" without express or implied warranty. --]]-- require "hpdf" +require "common" local function print_page(page, style, font, page_prev, page_next) @@ -111,7 +112,7 @@ if pdf then hpdf.SetPageMode(pdf, "HPDF_PAGE_MODE_FULL_SCREEN") -- save the document to a file - hpdf.SaveToFile(pdf, string.gsub(arg[0], "%.%w*$", "") .. ".pdf") + save_to_file(pdf, arg[0]) -- clean up hpdf.Free(pdf) diff --git a/demo/text_demo.lua b/demo/text_demo.lua index f60b0b3..b6692f0 100644 --- a/demo/text_demo.lua +++ b/demo/text_demo.lua @@ -16,7 +16,7 @@ It is provided "as is" without express or implied warranty. --]]-- require "hpdf" -require "grid_sheet" +require "common" local function show_stripe_pattern(page, x, y) @@ -264,7 +264,7 @@ if pdf then hpdf.Page_EndText(page) -- save the document to a file - hpdf.SaveToFile(pdf, string.gsub(arg[0], "%.%w*$", "") .. ".pdf") + save_to_file(pdf, arg[0]) -- clean up hpdf.Free(pdf) diff --git a/doc/text/change-log.txt b/doc/text/change-log.txt index 9d574ef..7170033 100644 --- a/doc/text/change-log.txt +++ b/doc/text/change-log.txt @@ -1,11 +1,15 @@ Change log ========== -This is a list of changes to LuaHPDF. The current version is 1.1. +This is a list of changes to LuaHPDF. The current version is 1.2. -Version 1.2: unreleased +Version 1.2: 2012-03-08 ----------------------- +* Guard against passing name of unwriteable file to SaveToFile; thanks and.damore + +* Replace grid_sheet.lua in parent directory with common.lua in demo directory + * Return number of characters printed by hpdf.Page_TextRect() Version 1.1: 2012-02-23 diff --git a/doc/text/demos.txt b/doc/text/demos.txt index 5223019..5a4fe32 100644 --- a/doc/text/demos.txt +++ b/doc/text/demos.txt @@ -13,9 +13,9 @@ On Unix-type systems, execute the following command to generate the PDF files: This will build the LuaHPDF binding if necessary, and then invoke Lua to generate each demonstration file. The `package.path` and `package.cpath` variables are modified on the command line so that demonstration scripts load -hpdf.so and grid_sheet.lua as modules. +hpdf.so and common.lua as modules. -The grid_sheet.lua script is a helper module that you may wish to install in a +The common.lua script is a helper module that you may wish to install in a standard Lua module directory. Run lua -e "print(package.path)" diff --git a/doc/text/license.txt b/doc/text/license.txt index a967662..66591e2 100644 --- a/doc/text/license.txt +++ b/doc/text/license.txt @@ -1,4 +1,4 @@ -LuaHPDF is copyright 2007,2010 Kurt Jung +LuaHPDF is copyright 2007,2012 Kurt Jung Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/hpdf.c b/hpdf.c index 64fdc94..aeba2c4 100644 --- a/hpdf.c +++ b/hpdf.c @@ -784,9 +784,16 @@ static int LclSaveToFile(lua_State *L) /* HPDF_STATUS <- hpdf.SaveToFile(pdf, file_name) */ { + FILE * fl; + HPDF_STATUS result; HPDF_Doc pdf = LclHandleGet(L, CnHndDoc, 1); const char * file_name = luaL_checkstring(L, 2); - HPDF_STATUS result = HPDF_SaveToFile(pdf, file_name); + fl = fopen(file_name, "w"); + result = (fl == (FILE *) 0) ? 0x1016 : HPDF_OK; + if (HPDF_OK == result) { + fclose(fl); + result = HPDF_SaveToFile(pdf, file_name); + } lua_pushinteger(L, result); return 1; }