Skip to content
This repository has been archived by the owner on Nov 19, 2019. It is now read-only.

Commit

Permalink
* Guard against passing name of unwriteable file to SaveToFile; thank…
Browse files Browse the repository at this point in the history
…s and.damore

* Replace grid_sheet.lua in parent directory with common.lua in demo directory

* Return number of characters printed by hpdf.Page_TextRect()
  • Loading branch information
kurt.w.jung committed Mar 8, 2012
1 parent 7fd01f1 commit 0dd8ca5
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 31 deletions.
9 changes: 6 additions & 3 deletions Makefile
Expand Up @@ -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

Expand Down Expand Up @@ -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)"

Expand All @@ -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)
Expand All @@ -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"' $<
4 changes: 2 additions & 2 deletions demo/arc_demo.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions grid_sheet.lua → demo/common.lua
Expand Up @@ -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
3 changes: 2 additions & 1 deletion demo/encryption_demo.lua
Expand Up @@ -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
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion demo/ext_gstate_demo.lua
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions demo/font_demo.lua
Expand Up @@ -16,6 +16,7 @@ It is provided "as is" without express or implied warranty.
--]]--
require "hpdf"
require "common"
local font_list = {
"Courier",
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions 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")
Expand Down
6 changes: 2 additions & 4 deletions 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")
Expand Down
3 changes: 2 additions & 1 deletion demo/line_demo.lua
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions demo/link_demo.lua
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion 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
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions demo/outline_demo.lua
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion demo/permission_demo.lua
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion demo/slide_show_demo.lua
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions demo/text_demo.lua
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions 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
Expand Down
4 changes: 2 additions & 2 deletions doc/text/demos.txt
Expand Up @@ -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)"
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
9 changes: 8 additions & 1 deletion hpdf.c
Expand Up @@ -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;
}
Expand Down

0 comments on commit 0dd8ca5

Please sign in to comment.