Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to initialize locale directory localedir #796

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ dnl libgphoto2-2.3.1.2.trunk (new gp_camera_get_storageinfo() funct
dnl A:R:C=0:0:6 libgphoto2-2.5.0 (fixed CameraList API, ... )
dnl A:R:C=1:0:7 libgphoto2-2.5.24 (added GP_EVENT_FILE_CHANGED ... )
dnl A:R:C=2:0:8 libgphoto2-2.5.28 (added gp_filesystem_set_info_dirty ... )
AC_SUBST([LIBGPHOTO2_AGE], [2])
dnl A:R:C=3:0:9 libgphoto2-2.5.XXX (added gp_init_localedir ... )
AC_SUBST([LIBGPHOTO2_AGE], [3])
AC_SUBST([LIBGPHOTO2_REVISION], [0])
AC_SUBST([LIBGPHOTO2_CURRENT], [8])
AC_SUBST([LIBGPHOTO2_CURRENT], [9])
AC_SUBST([LIBGPHOTO2_CURRENT_MIN],
[`expr $LIBGPHOTO2_CURRENT - $LIBGPHOTO2_AGE`])
AC_SUBST([LIBGPHOTO2_VERSION_INFO],
Expand Down
2 changes: 2 additions & 0 deletions gphoto2/gphoto2-abilities-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ int gp_abilities_list_get_abilities (CameraAbilitiesList *list, int index,

const char *gp_message_codeset (const char *);

int gp_init_localedir (const char *localedir);


/**
* Name of the environment variable which may contain the path where
Expand Down
53 changes: 52 additions & 1 deletion libgphoto2/gphoto2-abilities-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "config.h"
#include <gphoto2/gphoto2-abilities-list.h>

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -71,6 +72,56 @@ gp_message_codeset (const char *codeset)
return bind_textdomain_codeset (GETTEXT_PACKAGE_LIBGPHOTO2, codeset);
}

/**
* \brief Initialize the localedir directory for the libgphoto2 gettext domain
*
* Override the localedir directory libgphoto2 uses for its message
* translations.
*
* You only need to call this if you have a non-standard installation
* where the locale files are at a location which differs from the
* compiled in default location.
*
* If you do need to call this function, call it before calling any
* non-initialization function.
*
* Internally, this will make sure bindtextdomain() is called for the
* relevant gettext text domain(s).
*
* \param localedir Root directory of libgphoto2's localization files.
* If NULL, use the compiled in default value, which
* will be something like "/usr/share/locale".
* \return gphoto2 error code.
*/
int
gp_init_localedir (const char *localedir)
{
static int locale_initialized = 0;
if (locale_initialized) {
gp_log(GP_LOG_DEBUG, "gp_init_localedir",
"ignoring late call (localedir value %s)",
localedir?localedir:"NULL");
return GP_OK;
}
const int gpp_result = gp_port_init_localedir (localedir);
if (gpp_result != GP_OK) {
return gpp_result;
}
const char *actual_localedir = (localedir?localedir:LOCALEDIR);
const char *const gettext_domain = GETTEXT_PACKAGE_LIBGPHOTO2;
if (bindtextdomain (gettext_domain, actual_localedir) == NULL) {
if (errno == ENOMEM)
return GP_ERROR_NO_MEMORY;
return GP_ERROR;
}
gp_log(GP_LOG_DEBUG, "gp_init_localedir",
"localedir has been set to %s%s",
actual_localedir,
localedir?"":" (compile-time default)");
locale_initialized = 1;
return GP_OK;
}

/**
* \brief Allocate the memory for a new abilities list.
*
Expand All @@ -92,7 +143,7 @@ gp_abilities_list_new (CameraAbilitiesList **list)
* an other way without introducing a global initialization
* function...
*/
bindtextdomain (GETTEXT_PACKAGE_LIBGPHOTO2, LOCALEDIR);
gp_init_localedir (NULL);

C_MEM (*list = calloc (1, sizeof (CameraAbilitiesList)));

Expand Down
1 change: 1 addition & 0 deletions libgphoto2/libgphoto2.sym
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ gp_filesystem_set_funcs
gp_file_unref
gp_gamma_correct_single
gp_gamma_fill_table
gp_init_localedir
gp_library_version
gp_list_append
gp_list_count
Expand Down
5 changes: 3 additions & 2 deletions libgphoto2_port/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ dnl A:R:C=7:0:7 libgphoto2_port-0.7.0 libgphoto2-2.3
dnl A:R:C=7:1:7 libgphoto2_port-0.7.1 libgphoto2-2.3.x
dnl A:R:C=8:0:8 libgphoto2_port-0.8.0 libgphoto2-2.4.x
dnl A:R:C=9:1:9 libgphoto2_port-0.10.0 libgphoto2-2.5.x
AC_SUBST([LIBGPHOTO2_PORT_AGE], [0])
dnl A:R:C=1:0:13 libgphoto2-2.5.XXX (added gp_port_init_localedir ... )
AC_SUBST([LIBGPHOTO2_PORT_AGE], [1])
AC_SUBST([LIBGPHOTO2_PORT_REVISION], [0])
AC_SUBST([LIBGPHOTO2_PORT_CURRENT], [12])
AC_SUBST([LIBGPHOTO2_PORT_CURRENT], [13])
AC_SUBST([LIBGPHOTO2_PORT_CURRENT_MIN],
[`expr $LIBGPHOTO2_PORT_CURRENT - $LIBGPHOTO2_PORT_AGE`])
AC_SUBST([LIBGPHOTO2_PORT_VERSION_INFO],
Expand Down
3 changes: 3 additions & 0 deletions libgphoto2_port/gphoto2/gphoto2-port-info-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ int gp_port_info_list_get_info (GPPortInfoList *list, int n, GPPortInfo *info);

const char *gp_port_message_codeset (const char*);

int gp_port_init_localedir (const char *localedir);


/**
* Name of the environment variable which may contain the path where
* to look for the IO libs. If this environment variable is not defined,
Expand Down
68 changes: 64 additions & 4 deletions libgphoto2_port/libgphoto2_port/gphoto2-port-info-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <gphoto2/gphoto2-port-info-list.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Expand Down Expand Up @@ -62,14 +63,65 @@ struct _GPPortInfoList {
#define CR(x) {int r=(x);if (r<0) return (r);}


/**
* \brief Initialize the localedir directory for the libgphoto2_port gettext domain
*
* Override the localedir directory libgphoto2_port uses for its message
* translations.
*
* This function is called by the gp_init_localedir() function, so if
* you are calling that already, there is no need to call
* gp_port_init_localedir() yourself.
*
* You only need to call this if you have a non-standard installation
* where the locale files are at a location which differs from the
* compiled in default location.
*
* If you need to call this function, call it before calling any
* non-initialization function.
*
* Internally, this will make sure bindtextdomain() is called for the
* relevant gettext text domain(s).
*
* \param localedir Root directory of libgphoto2_port's localization files.
* If NULL, use the compiled in default value, which
* will be something like "/usr/share/locale".
* \return gphoto2 error code.
*/
int
gp_port_init_localedir (const char *localedir)
{
static int locale_initialized = 0;
if (locale_initialized) {
gp_log(GP_LOG_DEBUG, "gp_port_init_localedir",
"ignoring late call (localedir value %s)",
localedir?localedir:"NULL");
return GP_OK;
}
const char *const actual_localedir = (localedir?localedir:LOCALEDIR);
const char *const gettext_domain = GETTEXT_PACKAGE_LIBGPHOTO2_PORT;
if (bindtextdomain (gettext_domain, actual_localedir) == NULL) {
if (errno == ENOMEM)
return GP_ERROR_NO_MEMORY;
return GP_ERROR;
}
gp_log(GP_LOG_DEBUG, "gp_port_init_localedir",
"localedir has been set to %s%s",
actual_localedir,
localedir?"":" (compile-time default)");
locale_initialized = 1;
return GP_OK;
}


/**
* \brief Specify codeset for translations
*
* This function specifies the codeset that are used for the translated
* This function specifies the codeset that is used for the translated
* strings that are passed back by the libgphoto2_port functions.
*
* This function is called by the gp_message_codeset() function, there is
* no need to call it yourself.
* This function is called by the gp_message_codeset() function, so
* there is no need to call it yourself.
*
* \param codeset new codeset to use
* \return the previous codeset
Expand Down Expand Up @@ -99,7 +151,7 @@ gp_port_info_list_new (GPPortInfoList **list)
* We put this in here because everybody needs to call this function
* before accessing ports...
*/
bindtextdomain (GETTEXT_PACKAGE_LIBGPHOTO2_PORT, LOCALEDIR);
gp_port_init_localedir (NULL);

C_MEM (*list = calloc (1, sizeof (GPPortInfoList)));

Expand Down Expand Up @@ -569,3 +621,11 @@ gp_port_info_new (GPPortInfo *info) {
C_MEM (*info = calloc (1, sizeof(struct _GPPortInfo)));
return GP_OK;
}


/*
* Local Variables:
* c-file-style:"linux"
* indent-tabs-mode:t
* End:
*/
9 changes: 5 additions & 4 deletions libgphoto2_port/libgphoto2_port/libgphoto2_port.ver
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ LIBGPHOTO2_5_0 {
gp_port_info_list_lookup_name;
gp_port_info_list_lookup_path;
gp_port_info_list_new;
gp_port_init_localedir;
gp_port_library_version;
gp_port_message_codeset;
gp_port_new;
Expand Down Expand Up @@ -66,7 +67,7 @@ LIBGPHOTO2_5_0 {
gp_system_rmdir;
local:
*;
};
}; # LIBGPHOTO2_5_0

# These are only supposed to be used by libgphoto2 internally.
LIBGPHOTO2_INTERNAL {
Expand All @@ -83,17 +84,17 @@ LIBGPHOTO2_INTERNAL {
gp_port_info_set_name;
gp_port_info_set_path;
gp_port_info_set_type;
};
}; # LIBGPHOTO2_INTERNAL

#LIBGPHOTO2_6_0 {
# global:
# # The asm(".symver ...") constructs will put stuff into this node,
# # it needs to stay.
# # Currently used by:
# # gp_port_set_info, gp_port_get_info,
# # gp_port_set_info, gp_port_get_info,
# # gp_port_info_list_append, gp_port_info_list_get_info
#
# # Add new exported functions here too!
#} LIBGPHOTO2_5_0;
#}; # LIBGPHOTO2_6_0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are weird?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, but i see its all commented out.


# here comes the next version...
10 changes: 10 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ test_camera_list_LDADD = \
$(INTLLIBS)


TESTS += test-init-localedir
check_PROGRAMS += test-init-localedir
test_init_localedir_LDADD =
test_init_localedir_LDADD += $(top_builddir)/libgphoto2/libgphoto2.la
test_init_localedir_LDADD += $(top_builddir)/libgphoto2_port/libgphoto2_port/libgphoto2_port.la
test_init_localedir_LDADD += $(LIBLTDL)
test_init_localedir_LDADD += $(LIBEXIF_LIBS)
test_init_localedir_LDADD += $(INTLLIBS)


########################################################################
# Test pedantic compilation for multiple language standard
########################################################################
Expand Down
66 changes: 66 additions & 0 deletions tests/test-init-localedir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/** \file tests/test-init-localedir.c
* \brief Exercise the *_init_localedir() functions
*
* Copyright 2022 Hans Ulrich Niedermann <hun@n-dimensional.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
*
* Usage:
* ./test-init-localedir /foo/share/locale
* ./test-init-localedir /foo/share/locale NULL
* ./test-init-localedir NULL /foo/share/locale /usr/share/locale
*
* Calls the gp_init_localedir() for each and every command line
* argument in sequence. Command line arguments are interpreted as
* directory paths (absolute or relative to the current working
* directory), but "NULL" is interpreted as a C NULL value.
*/

#include <stdio.h>
#include <string.h>

#include <gphoto2/gphoto2-port-log.h>
#include <gphoto2/gphoto2-abilities-list.h>


void log_func(GPLogLevel level, const char *domain,
const char *str, void *data)
{
printf("%d:%s:%s\n", level, domain, str);
}


int main(const int argc, const char *const argv[])
{
gp_log_add_func(GP_LOG_ALL, log_func, NULL);
for (int i=1; i<argc; ++i) {
const char *const arg = argv[i];
const char *const localedir =
(strcmp(arg, "NULL") == 0) ? NULL : argv[1];
printf("main: calling gp_init_localedir(%s)\n",
localedir?localedir:"(null)");
gp_init_localedir(localedir);
}
}


/*
* Local Variables:
* c-file-style:"linux"
* indent-tabs-mode:t
* End:
*/