[Libreoffice-commits] .: 2 commits - desktop/source desktop/unx scripting/source

Michael Meeks michael at kemper.freedesktop.org
Fri May 6 06:57:37 PDT 2011


 desktop/source/pagein/file_image.h     |   81 ----------
 desktop/source/pagein/file_image_unx.c |  153 -------------------
 desktop/source/pagein/makefile.mk      |   27 ---
 desktop/source/pagein/pagein-main.c    |   12 -
 desktop/source/pagein/pagein.c         |  162 --------------------
 desktop/unx/source/file_image.h        |   81 ++++++++++
 desktop/unx/source/file_image_unx.c    |  153 +++++++++++++++++++
 desktop/unx/source/makefile.mk         |    2 
 desktop/unx/source/pagein.c            |  162 ++++++++++++++++++++
 scripting/source/pyprov/makefile.mk    |    3 
 scripting/source/pyprov/msgbox.py      |  260 +++++++++++++++++++++++++++++++++
 11 files changed, 659 insertions(+), 437 deletions(-)

New commits:
commit 515bcff5b5fbf8dd70165d3e04833e281db70f4b
Author: Laurent Godard <listes.godard at laposte.net>
Date:   Fri May 6 14:06:55 2011 +0100

    add useful msgbox helper awt wrapper

diff --git a/scripting/source/pyprov/makefile.mk b/scripting/source/pyprov/makefile.mk
index a96c3f3..a304d97 100755
--- a/scripting/source/pyprov/makefile.mk
+++ b/scripting/source/pyprov/makefile.mk
@@ -39,7 +39,8 @@ TARGET=pyprov
 # simply do not fiddle with that. cws sb123, sb129
 ALL : ALLTAR \
     $(DLLDEST)$/officehelper.py	\
-        $(DLLDEST)$/mailmerge.py
+    $(DLLDEST)$/msgbox.py \
+    $(DLLDEST)$/mailmerge.py
 
 $(DLLDEST)$/%.py: %.py
     cp $? $@
diff --git a/scripting/source/pyprov/msgbox.py b/scripting/source/pyprov/msgbox.py
new file mode 100644
index 0000000..c9ee80b
--- /dev/null
+++ b/scripting/source/pyprov/msgbox.py
@@ -0,0 +1,260 @@
+# -*- tab-width: 4; indent-tabs-mode: nil -*-
+#
+# Version: MPL 1.1 / GPLv3+ / LGPLv3+
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License or as specified alternatively below. You may obtain a copy of
+# the License at http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Initial Developer of the Original Code is
+#       Laurent Godard <listes.godard at laposte.net>
+# Portions created by the Initial Developer are Copyright (C) 2010 the
+# Initial Developer. All Rights Reserved.
+#
+# Major Contributor(s):
+#       Timo <timo at iera.de>
+#
+# For minor contributions see the git repository.
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either the GNU General Public License Version 3 or later (the "GPLv3+"), or
+# the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
+# in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
+# instead of those above.
+
+# prepare pythoin environnement - Add the path of this class
+from os import path
+from sys import modules
+from sys import path as syspath
+
+# pyUNO program itself
+import uno, unohelper
+
+# UNO GUI toolkit
+from com.sun.star.awt.WindowClass import TOP, SIMPLE
+from com.sun.star.awt.PushButtonType import STANDARD as standard
+from com.sun.star.awt.PushButtonType import OK as ok
+from com.sun.star.awt.PushButtonType import CANCEL as cancel
+from com.sun.star.awt.PushButtonType import HELP as help
+from com.sun.star.awt.TextAlign import CENTER as center
+from com.sun.star.awt.TextAlign import LEFT as left
+from com.sun.star.awt.TextAlign import RIGHT as right
+
+# used UNO listeners
+from com.sun.star.awt import XActionListener
+
+class MsgBox(unohelper.Base):
+    """Inspect UNO object, link to sdk and recusrsive calls"""
+
+    def __init__(self, aContext):
+        """acontext : a Valid UNO context
+        """
+
+        self.VERSION = '0.1'
+        self.ctx = aContext
+        self.smgr = aContext.ServiceManager
+        # UI Dialog object
+        self.dialog=None
+        # List of openned Listeners
+        self.lst_listeners={}
+        #UI parameters
+        self.ButtonSize = 50
+        self.boxSize = 200
+        self.lineHeight = 10
+        self.fromBroxSize = False
+        self.numberOfLines = -1
+
+        self.Buttons = []
+        self.Response = ''
+
+        return
+
+    #####################################################
+    #                 GUI definition                    #
+    #####################################################
+    def _createBox(self):
+        """Create the Box"""
+
+        # computes parameters of the message dialog
+        if self.numberOfLines == -1:
+            #calculate
+            numberOfLines = len(self.message.split(chr(10)))
+        else:
+            numberOfLines = self.numberOfLines
+
+        numberOfButtons = len(self.Buttons)
+        self.ButtonSpace = self.ButtonSize/2
+        if self.fromBroxSize:
+            # button size is calculated from boxsize
+            size = (2 * self.boxSize) / (3 * numberOfButtons + 1)
+            self.ButtonSize = size
+            self.ButtonSpace = self.ButtonSize/2
+        else:
+            # boxsize i calculated form buttonsize
+            self.boxSize = numberOfButtons * (self.ButtonSize +
+                                            self.ButtonSpace) + self.ButtonSpace
+
+        # create the dialog model and set the properties
+        dialog_model = self.smgr.createInstanceWithContext(
+                                    'com.sun.star.awt.UnoControlDialogModel',
+                                    self.ctx)
+        dialog_model.PositionX = 50
+        dialog_model.Step = 1
+        dialog_model.TabIndex = 7
+        dialog_model.Width = self.boxSize#numberOfButtons * (self.ButtonSize +
+                             #               self.ButtonSpace) + 25
+        dialog_model.Height = 10 + self.lineHeight * numberOfLines + 10 + 12  + 10
+        dialog_model.PositionY = 63
+        dialog_model.Sizeable = True
+        dialog_model.Closeable = False
+
+        dialog = self.smgr.createInstanceWithContext(
+                'com.sun.star.awt.UnoControlDialog', self.ctx)
+
+        # label Label0
+        label = dialog_model.createInstance(
+                'com.sun.star.awt.UnoControlFixedTextModel')
+        label.PositionX =  10
+        label.TabIndex = 9
+        label.Width = dialog_model.Width - label.PositionX
+        label.Height = self.lineHeight* numberOfLines
+        label.PositionY = 10
+        label.Align = left
+        label.MultiLine = True
+        label.Label = self.message
+        dialog_model.insertByName('Label0', label)
+
+        nb = 0
+        for buttonName in self.Buttons:
+            nb +=1
+            button = dialog_model.createInstance(
+                                    'com.sun.star.awt.UnoControlButtonModel')
+            button.PositionX = nb * self.ButtonSpace + (nb-1)* self.ButtonSize
+            button.TabIndex = 8
+            button.Height = 12
+            button.Width = self.ButtonSize
+            button.PositionY = 10 + label.Height + 10
+            button.PushButtonType = standard
+            if nb == 1:
+                button.DefaultButton = True
+            else:
+                button.DefaultButton = False
+            button.Label = buttonName
+            dialog_model.insertByName('Btn' + str(nb), button )
+
+        if not dialog.getModel():
+            dialog.setModel(dialog_model)
+
+        # UNO toolkit definition
+        toolkit = self.smgr.createInstanceWithContext('com.sun.star.awt.Toolkit', self.ctx)
+        a_rect = uno.createUnoStruct( 'com.sun.star.awt.Rectangle' )
+        a_rect.X = 50
+        dialog.setTitle ( self.title )
+        a_rect.Width = 270
+        a_rect.Height = 261
+        a_rect.Y = 63
+        win_descriptor = uno.createUnoStruct('com.sun.star.awt.WindowDescriptor')
+        win_descriptor.Type = TOP
+        win_descriptor.ParentIndex = -1
+        win_descriptor.Bounds = a_rect
+        peer = toolkit.createWindow( win_descriptor )
+        dialog.createPeer( toolkit, peer )
+
+        return dialog
+
+    def _addListeners(self):
+        """Add listeners to dialog"""
+        nb = 0
+        for buttonName in self.Buttons:
+            nb +=1
+            a_control = self.dialog.getControl('Btn'+str(nb))
+            the_listener = ButtonListener(self)
+            a_control.addActionListener(the_listener)
+            self.lst_listeners['Btn'+str(nb)] = the_listener
+        return
+
+    def _removeListeners(self):
+        """ remove listeners on exiting"""
+        nb = 0
+        for buttonName in self.Buttons:
+            nb +=1
+            a_control = self.dialog.getControl('Btn'+str(nb))
+            a_control.removeActionListener(self.lst_listeners['Btn'+str(nb)])
+        return
+
+    def show(self, message, decoration, title):
+        self.message = message
+        self.decoration = decoration
+        self.title = title
+        # Create GUI
+        self.dialog = self._createBox()
+        self._addListeners()
+        #execute the dialog --> blocking call
+        self.dialog.execute()
+        #end --> release listeners and dispose dialog
+        self._removeListeners()
+        self.dialog.dispose()
+        return self.Response
+
+    def addButton(self, caption):
+        self.Buttons.append(caption)
+        return
+
+    def renderFromBoxSize(self, size = 150):
+        self.boxSize = size
+        self.fromBroxSize = True
+        return
+
+    def renderFromButtonSize(self, size = 50):
+        self.ButtonSize = size
+        self.fromBroxSize = False
+        return
+
+class ButtonListener(unohelper.Base, XActionListener):
+    """Stops the MessageBox, sets the button label as returned value"""
+    def __init__(self, caller):
+        self.caller = caller
+
+    def disposing(self, eventObject):
+        pass
+
+    def actionPerformed(self, actionEvent):
+        button = actionEvent.Source
+        self.caller.Response = button.Model.Label
+        self.caller.dialog.endExecute()
+        return
+
+### TEST
+if __name__ == '__main__':
+    # get the uno component context from the PyUNO runtime
+    localContext = uno.getComponentContext()
+
+    # create the UnoUrlResolver
+    resolver = localContext.ServiceManager.createInstanceWithContext(
+                    "com.sun.star.bridge.UnoUrlResolver", localContext )
+
+    # connect to the running office
+    # OOo has to be launched in listen mode as
+    # ./soffice "-accept=socket,host=localhost,port=2002;urp;"
+    ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
+    myBox = MsgBox(ctx)
+    myBox.addButton("Yes")
+    myBox.addButton("No")
+    myBox.addButton("May be")
+    myBox.renderFromBoxSize(150)
+    myBox.numberOflines = 2
+
+    print myBox.show("A very long message A very long message A very long message A very long message A very long message A very long message A very long message A very long message A very long message A very long message " + chr(10)+chr(10)+"Do you agree ?",0,"Dialog title")
+
+    myBox = MsgBox(ctx)
+    myBox.addButton("oK")
+    myBox.renderFromButtonSize()
+    myBox.numberOflines = 2
+
+    print myBox.show("A small message",0,"Dialog title")
commit be4a42b2e3fef784f08fc37008539790cf97fdcc
Author: Michael Meeks <michael.meeks at novell.com>
Date:   Wed May 4 21:35:31 2011 +0100

    kill standalone pagein, it is now an integral part of oosplash.bin

diff --git a/desktop/source/pagein/file_image.h b/desktop/source/pagein/file_image.h
deleted file mode 100755
index 4234140..0000000
--- a/desktop/source/pagein/file_image.h
+++ /dev/null
@@ -1,81 +0,0 @@
-/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
-/*************************************************************************
- *
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- * 
- * Copyright 2000, 2010 Oracle and/or its affiliates.
- *
- * OpenOffice.org - a multi-platform office productivity suite
- *
- * This file is part of OpenOffice.org.
- *
- * OpenOffice.org is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License version 3
- * only, as published by the Free Software Foundation.
- *
- * OpenOffice.org is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Lesser General Public License version 3 for more details
- * (a copy is included in the LICENSE file that accompanied this code).
- *
- * You should have received a copy of the GNU Lesser General Public License
- * version 3 along with OpenOffice.org.  If not, see
- * <http://www.openoffice.org/license.html>
- * for a copy of the LGPLv3 License.
- *
- ************************************************************************/
-
-#ifndef INCLUDED_FILE_IMAGE_H
-#define INCLUDED_FILE_IMAGE_H
-
-#ifndef INCLUDED_STDDEF_H
-#include <stddef.h>
-#define INCLUDED_STDDEF_H
-#endif
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/** file_image.
- */
-struct file_image_st
-{
-    void * m_base;
-    size_t m_size;
-};
-
-typedef struct file_image_st file_image;
-
-#define FILE_IMAGE_INITIALIZER { 0, 0 }
-
-
-/** file_image_open.
- */
-int file_image_open (
-    file_image * image,
-    const char * filename);
-
-
-/** file_image_pagein.
- */
-int file_image_pagein (
-    file_image * image);
-
-
-/** file_image_close.
- */
-int file_image_close (
-    file_image * image);
-
-
-/** Epilog.
- */
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* INCLUDED_ODEP_IMAGE_H */
-
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/source/pagein/file_image_unx.c b/desktop/source/pagein/file_image_unx.c
deleted file mode 100755
index 41d60e8..0000000
--- a/desktop/source/pagein/file_image_unx.c
+++ /dev/null
@@ -1,153 +0,0 @@
-/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
-/*************************************************************************
- *
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- * 
- * Copyright 2000, 2010 Oracle and/or its affiliates.
- *
- * OpenOffice.org - a multi-platform office productivity suite
- *
- * This file is part of OpenOffice.org.
- *
- * OpenOffice.org is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License version 3
- * only, as published by the Free Software Foundation.
- *
- * OpenOffice.org is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Lesser General Public License version 3 for more details
- * (a copy is included in the LICENSE file that accompanied this code).
- *
- * You should have received a copy of the GNU Lesser General Public License
- * version 3 along with OpenOffice.org.  If not, see
- * <http://www.openoffice.org/license.html>
- * for a copy of the LGPLv3 License.
- *
- ************************************************************************/
-
-#include "file_image.h"
-
-#include <unistd.h>
-
-#include <errno.h>
-#include <fcntl.h>
-
-#if defined(LINUX)
-#  ifndef __USE_BSD
-#    define __USE_BSD /* madvise, MADV_WILLNEED */
-#  endif
-#endif /* Linux */
-
-#include <sys/mman.h>
-#include <sys/stat.h>
-
-#include <string.h>
-
-/*
- * file_image_open
- */
-int file_image_open (file_image * image, const char * filename)
-{
-    int         result = 0;
-    int         fd;
-    struct stat st;
-    void *      p;
-
-    if (image == 0)
-        return (EINVAL);
-
-    image->m_base = MAP_FAILED, image->m_size = 0;
-
-    if ((fd = open (filename, O_RDONLY)) == -1)
-        return (errno);
-
-    if (fstat (fd, &st) == -1)
-    {
-        result = errno;
-        goto cleanup_and_leave;
-    }
-
-    p = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-    if (p == MAP_FAILED)
-    {
-        result = errno;
-        goto cleanup_and_leave;
-    }
-
-    image->m_base = p, image->m_size = st.st_size;
-
-cleanup_and_leave:
-    close (fd);
-    return (result);
-}
-
-/*
- * file_image_pagein.
- */
-int file_image_pagein (file_image * image)
-{
-    file_image    w;
-    long          s;
-    size_t        k;
-    volatile char c = 0;
-
-    if (image == 0)
-        return (EINVAL);
-
-    if ((w.m_base = image->m_base) == 0)
-        return (EINVAL);
-    if ((w.m_size = image->m_size) == 0)
-        return (0);
-
-    if (madvise (w.m_base, w.m_size, MADV_WILLNEED) == -1) 
-    {
-#ifndef MACOSX
-        return (errno);
-#else
-        /* madvise MADV_WILLNEED need not succeed here */
-        /* but that is fine */
-#endif
-    }
-
-
-#ifndef MACOSX	
-    if ((s = sysconf (_SC_PAGESIZE)) == -1)
-        s = 0x1000;
-#else
-    s = getpagesize();
-#endif
-
-    k = (size_t)(s);
-    while (w.m_size > k)
-    {
-        c ^= ((char*)(w.m_base))[0];
-        w.m_base  = (char*)(w.m_base) + k;
-        w.m_size -= k;
-    }
-    if (w.m_size > 0)
-    {
-        c ^= ((char*)(w.m_base))[0];
-        w.m_base  = (char*)(w.m_base) + w.m_size;
-        w.m_size -= w.m_size;
-    }
-
-    return (0);
-}
-
-/*
- * file_image_close
- */
-int file_image_close (file_image * image)
-{
-    if (image == 0)
-        return (EINVAL);
-
-    if (munmap (image->m_base, image->m_size) == -1)
-        return (errno);
-
-    image->m_base = 0, image->m_size = 0;
-    return (0);
-}
-
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/source/pagein/makefile.mk b/desktop/source/pagein/makefile.mk
index 2b02a4d..1125e45 100755
--- a/desktop/source/pagein/makefile.mk
+++ b/desktop/source/pagein/makefile.mk
@@ -29,11 +29,6 @@ PRJ=..$/..
 
 PRJNAME=desktop
 TARGET=pagein
-TARGETTYPE=CUI
-LIBTARGET=NO
-
-NO_DEFAULT_STL=TRUE
-LIBSALCPPRT=$(0)
 
 # --- Settings -----------------------------------------------------
 
@@ -41,27 +36,6 @@ LIBSALCPPRT=$(0)
 
 .INCLUDE .IGNORE : icuversion.mk
 
-.IF "$(OS)"=="MACOSX"
-
-dummy:
-    @echo "Pagein disabled for mac"
-
-.ELSE
-
-# --- Files --------------------------------------------------------
-
-OBJFILES= \
-    $(OBJ)$/pagein.obj \
-    $(OBJ)$/pagein-main.obj \
-    $(OBJ)$/file_image_unx.obj
-
-APP1TARGET=$(TARGET)
-APP1OBJS=$(OBJFILES)
-APP1CODETYPE=C
-
-# depends on libc only.
-STDLIB=
-
 # --- Targets ------------------------------------------------------
 
 ALL: \
@@ -193,4 +167,3 @@ $(MISC)$/$(TARGET)-common : makefile.mk
     @-echo $(DLLPRE)sfx$(DFTDLLPOST)             >> $@
     @-echo $(DLLPRE)sofficeapp$(DLLPOST)         >> $@
 
-.ENDIF
\ No newline at end of file
diff --git a/desktop/source/pagein/pagein-main.c b/desktop/source/pagein/pagein-main.c
deleted file mode 100644
index f8fe82a..0000000
--- a/desktop/source/pagein/pagein-main.c
+++ /dev/null
@@ -1,12 +0,0 @@
-#include <stdio.h>
-/*
- * De-coupled to allow pagein to be re-used in the unx
- * splash / quick-starter
- */
-extern int pagein_execute (int argc, char **argv);
-
-int main (int argc, char **argv)
-{
-    return pagein_execute (argc, argv);
-}
-
diff --git a/desktop/source/pagein/pagein.c b/desktop/source/pagein/pagein.c
deleted file mode 100755
index b625c57..0000000
--- a/desktop/source/pagein/pagein.c
+++ /dev/null
@@ -1,162 +0,0 @@
-/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
-/*************************************************************************
- *
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- * 
- * Copyright 2000, 2010 Oracle and/or its affiliates.
- *
- * OpenOffice.org - a multi-platform office productivity suite
- *
- * This file is part of OpenOffice.org.
- *
- * OpenOffice.org is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License version 3
- * only, as published by the Free Software Foundation.
- *
- * OpenOffice.org is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Lesser General Public License version 3 for more details
- * (a copy is included in the LICENSE file that accompanied this code).
- *
- * You should have received a copy of the GNU Lesser General Public License
- * version 3 along with OpenOffice.org.  If not, see
- * <http://www.openoffice.org/license.html>
- * for a copy of the LGPLv3 License.
- *
- ************************************************************************/
-
-#include "file_image.h"
-
-#include <unistd.h>
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-
-/* do_pagein */
-static int do_pagein (const char * filename, size_t * size)
-{
-    int result;
-    file_image image = FILE_IMAGE_INITIALIZER;
-
-    if ((result = file_image_open (&image, filename)) != 0)
-        return (result);
-
-    if ((result = file_image_pagein (&image)) != 0)
-    {
-        fprintf (stderr, "file_image_pagein %s: %s\n", filename, strerror(result));
-        goto cleanup_and_leave;
-    }
-
-    if (size)
-    {
-        *size = image.m_size;
-    }
-
-cleanup_and_leave:
-    file_image_close (&image);
-    return (result);
-}
-
-extern int pagein_execute (int argc, char **argv);
-
-/* main */
-int pagein_execute (int argc, char **argv)
-{
-    int    i, v = 0;
-    size_t nfiles = 0, nbytes = 0;
-
-    if (argc < 2)
-    {
-        fprintf (
-            stderr,
-            "%s: Usage: pagein [-v[v]] [-L<path>] [@]<filename> ...\n",
-            argv[0]);
-        return (1);
-    }
-
-    for (i = 1; i < argc; i++)
-    {
-        FILE   * fp = 0;
-        size_t   k  = 0;
-
-        if (argv[i][0] == '-')
-        {
-            /* option */
-            int j = 1;
-            switch (argv[i][j])
-            {
-                case 'v':
-                    /* verbosity level */
-                    for (v += 1, j += 1; argv[i][j]; j++)
-                        v += (argv[i][j] == 'v');
-                    break;
-                case 'L':
-                    /* search path */
-                    if (chdir (&(argv[i][2])) == -1)
-                        fprintf (stderr, "chdir %s: %s\n", &(argv[i][2]), strerror(errno));
-                    break;
-                default:
-                    /* ignored */
-                    break;
-            }
-
-            /* next argv */
-            continue;
-        }
-
-        if ((argv[i][0] == '@') && ((fp = fopen (argv[i], "r")) == 0))
-        {
-            char fullpath[4096];
-            char *path;
-            strncpy (fullpath, argv[i] + 1, 3000);
-            if (!(path = strrchr (fullpath, '/')))
-                path = fullpath;
-            else
-                path++;
-
-            if ((fp = fopen (&(argv[i][1]), "r")) == 0)
-            {
-                fprintf (stderr, "fopen %s: %s\n", &(argv[i][1]), strerror(errno));
-                continue;
-            }
-            while (fgets (path, 1024, fp) != 0)
-            {
-                path[strlen(path) - 1] = '\0', k = 0;
-
-                /* paths relative to the location of the pagein file */
-                if (do_pagein (fullpath, &k) == 0)
-                {
-                    /* accumulate total size */
-                    nbytes += k;
-                }
-
-                if (v >= 2)
-                    fprintf (stderr, "pagein(\"%s\") = %d bytes\n", path, (int) k);
-                nfiles += 1;
-            }
-            fclose (fp);
-        }
-        else
-        {
-            if (fp != 0)
-                fclose (fp);
-
-            if (do_pagein (argv[i], &k) == 0)
-            {
-                /* accumulate total size */
-                nbytes += k;
-            }
-
-            if (v >= 2)
-                fprintf (stderr, "pagein(\"%s\") = %d bytes\n", argv[i], (int) k);
-            nfiles += 1;
-        }
-    }
-
-    if (v >= 1)
-        fprintf (stderr, "Total: %d files (%d bytes)\n", (int) nfiles, (int) nbytes);
-    return (0);
-}
-
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/unx/source/file_image.h b/desktop/unx/source/file_image.h
new file mode 100755
index 0000000..4d08171
--- /dev/null
+++ b/desktop/unx/source/file_image.h
@@ -0,0 +1,81 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * This file is part of OpenOffice.org.
+ *
+ * OpenOffice.org is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 3
+ * only, as published by the Free Software Foundation.
+ *
+ * OpenOffice.org is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License version 3 for more details
+ * (a copy is included in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * version 3 along with OpenOffice.org.  If not, see
+ * <http://www.openoffice.org/license.html>
+ * for a copy of the LGPLv3 License.
+ *
+ ************************************************************************/
+
+#ifndef INCLUDED_FILE_IMAGE_H
+#define INCLUDED_FILE_IMAGE_H
+
+#ifndef INCLUDED_STDDEF_H
+#include <stddef.h>
+#define INCLUDED_STDDEF_H
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** file_image.
+ */
+struct file_image_st
+{
+    void * m_base;
+    size_t m_size;
+};
+
+typedef struct file_image_st file_image;
+
+#define FILE_IMAGE_INITIALIZER { 0, 0 }
+
+
+/** file_image_open.
+ */
+int file_image_open (
+    file_image * image,
+    const char * filename);
+
+
+/** file_image_pagein.
+ */
+int file_image_pagein (
+    file_image * image);
+
+
+/** file_image_close.
+ */
+int file_image_close (
+    file_image * image);
+
+
+/** Epilog.
+ */
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* INCLUDED_ODEP_IMAGE_H */
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/unx/source/file_image_unx.c b/desktop/unx/source/file_image_unx.c
new file mode 100755
index 0000000..fa1af92
--- /dev/null
+++ b/desktop/unx/source/file_image_unx.c
@@ -0,0 +1,153 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * This file is part of OpenOffice.org.
+ *
+ * OpenOffice.org is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 3
+ * only, as published by the Free Software Foundation.
+ *
+ * OpenOffice.org is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License version 3 for more details
+ * (a copy is included in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * version 3 along with OpenOffice.org.  If not, see
+ * <http://www.openoffice.org/license.html>
+ * for a copy of the LGPLv3 License.
+ *
+ ************************************************************************/
+
+#include "file_image.h"
+
+#include <unistd.h>
+
+#include <errno.h>
+#include <fcntl.h>
+
+#if defined(LINUX)
+#  ifndef __USE_BSD
+#    define __USE_BSD /* madvise, MADV_WILLNEED */
+#  endif
+#endif /* Linux */
+
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <string.h>
+
+/*
+ * file_image_open
+ */
+int file_image_open (file_image * image, const char * filename)
+{
+    int         result = 0;
+    int         fd;
+    struct stat st;
+    void *      p;
+
+    if (image == 0)
+        return (EINVAL);
+
+    image->m_base = MAP_FAILED, image->m_size = 0;
+
+    if ((fd = open (filename, O_RDONLY)) == -1)
+        return (errno);
+
+    if (fstat (fd, &st) == -1)
+    {
+        result = errno;
+        goto cleanup_and_leave;
+    }
+
+    p = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+    if (p == MAP_FAILED)
+    {
+        result = errno;
+        goto cleanup_and_leave;
+    }
+
+    image->m_base = p, image->m_size = st.st_size;
+
+cleanup_and_leave:
+    close (fd);
+    return (result);
+}
+
+/*
+ * file_image_pagein.
+ */
+int file_image_pagein (file_image * image)
+{
+    file_image    w;
+    long          s;
+    size_t        k;
+    volatile char c = 0;
+
+    if (image == 0)
+        return (EINVAL);
+
+    if ((w.m_base = image->m_base) == 0)
+        return (EINVAL);
+    if ((w.m_size = image->m_size) == 0)
+        return (0);
+
+    if (madvise (w.m_base, w.m_size, MADV_WILLNEED) == -1)
+    {
+#ifndef MACOSX
+        return (errno);
+#else
+        /* madvise MADV_WILLNEED need not succeed here */
+        /* but that is fine */
+#endif
+    }
+
+
+#ifndef MACOSX
+    if ((s = sysconf (_SC_PAGESIZE)) == -1)
+        s = 0x1000;
+#else
+    s = getpagesize();
+#endif
+
+    k = (size_t)(s);
+    while (w.m_size > k)
+    {
+        c ^= ((char*)(w.m_base))[0];
+        w.m_base  = (char*)(w.m_base) + k;
+        w.m_size -= k;
+    }
+    if (w.m_size > 0)
+    {
+        c ^= ((char*)(w.m_base))[0];
+        w.m_base  = (char*)(w.m_base) + w.m_size;
+        w.m_size -= w.m_size;
+    }
+
+    return (0);
+}
+
+/*
+ * file_image_close
+ */
+int file_image_close (file_image * image)
+{
+    if (image == 0)
+        return (EINVAL);
+
+    if (munmap (image->m_base, image->m_size) == -1)
+        return (errno);
+
+    image->m_base = 0, image->m_size = 0;
+    return (0);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/unx/source/makefile.mk b/desktop/unx/source/makefile.mk
index 5c5c8f3..ad68ca9 100755
--- a/desktop/unx/source/makefile.mk
+++ b/desktop/unx/source/makefile.mk
@@ -45,7 +45,7 @@ OBJFILES= \
     $(OBJ)$/args.obj
 
 .IF "$(OS)"!="MACOSX"
-PAGEIN_OBJS= \
+OBJFILES += \
     $(OBJ)$/pagein.obj \
     $(OBJ)$/file_image_unx.obj
 .ENDIF
diff --git a/desktop/unx/source/pagein.c b/desktop/unx/source/pagein.c
new file mode 100755
index 0000000..947b4b7
--- /dev/null
+++ b/desktop/unx/source/pagein.c
@@ -0,0 +1,162 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * This file is part of OpenOffice.org.
+ *
+ * OpenOffice.org is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 3
+ * only, as published by the Free Software Foundation.
+ *
+ * OpenOffice.org is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License version 3 for more details
+ * (a copy is included in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * version 3 along with OpenOffice.org.  If not, see
+ * <http://www.openoffice.org/license.html>
+ * for a copy of the LGPLv3 License.
+ *
+ ************************************************************************/
+
+#include "file_image.h"
+
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+/* do_pagein */
+static int do_pagein (const char * filename, size_t * size)
+{
+    int result;
+    file_image image = FILE_IMAGE_INITIALIZER;
+
+    if ((result = file_image_open (&image, filename)) != 0)
+        return (result);
+
+    if ((result = file_image_pagein (&image)) != 0)
+    {
+        fprintf (stderr, "file_image_pagein %s: %s\n", filename, strerror(result));
+        goto cleanup_and_leave;
+    }
+
+    if (size)
+    {
+        *size = image.m_size;
+    }
+
+cleanup_and_leave:
+    file_image_close (&image);
+    return (result);
+}
+
+extern int pagein_execute (int argc, char **argv);
+
+/* main */
+int pagein_execute (int argc, char **argv)
+{
+    int    i, v = 0;
+    size_t nfiles = 0, nbytes = 0;
+
+    if (argc < 2)
+    {
+        fprintf (
+            stderr,
+            "%s: Usage: pagein [-v[v]] [-L<path>] [@]<filename> ...\n",
+            argv[0]);
+        return (1);
+    }
+
+    for (i = 1; i < argc; i++)
+    {
+        FILE   * fp = 0;
+        size_t   k  = 0;
+
+        if (argv[i][0] == '-')
+        {
+            /* option */
+            int j = 1;
+            switch (argv[i][j])
+            {
+                case 'v':
+                    /* verbosity level */
+                    for (v += 1, j += 1; argv[i][j]; j++)
+                        v += (argv[i][j] == 'v');
+                    break;
+                case 'L':
+                    /* search path */
+                    if (chdir (&(argv[i][2])) == -1)
+                        fprintf (stderr, "chdir %s: %s\n", &(argv[i][2]), strerror(errno));
+                    break;
+                default:
+                    /* ignored */
+                    break;
+            }
+
+            /* next argv */
+            continue;
+        }
+
+        if ((argv[i][0] == '@') && ((fp = fopen (argv[i], "r")) == 0))
+        {
+            char fullpath[4096];
+            char *path;
+            strncpy (fullpath, argv[i] + 1, 3000);
+            if (!(path = strrchr (fullpath, '/')))
+                path = fullpath;
+            else
+                path++;
+
+            if ((fp = fopen (&(argv[i][1]), "r")) == 0)
+            {
+                fprintf (stderr, "fopen %s: %s\n", &(argv[i][1]), strerror(errno));
+                continue;
+            }
+            while (fgets (path, 1024, fp) != 0)
+            {
+                path[strlen(path) - 1] = '\0', k = 0;
+
+                /* paths relative to the location of the pagein file */
+                if (do_pagein (fullpath, &k) == 0)
+                {
+                    /* accumulate total size */
+                    nbytes += k;
+                }
+
+                if (v >= 2)
+                    fprintf (stderr, "pagein(\"%s\") = %d bytes\n", path, (int) k);
+                nfiles += 1;
+            }
+            fclose (fp);
+        }
+        else
+        {
+            if (fp != 0)
+                fclose (fp);
+
+            if (do_pagein (argv[i], &k) == 0)
+            {
+                /* accumulate total size */
+                nbytes += k;
+            }
+
+            if (v >= 2)
+                fprintf (stderr, "pagein(\"%s\") = %d bytes\n", argv[i], (int) k);
+            nfiles += 1;
+        }
+    }
+
+    if (v >= 1)
+        fprintf (stderr, "Total: %d files (%d bytes)\n", (int) nfiles, (int) nbytes);
+    return (0);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list