From f1e9814334e0dfd2757af87a1da102ec9f93ea58 Mon Sep 17 00:00:00 2001 From: David Capello Date: Sat, 18 Sep 2010 01:03:56 -0300 Subject: [PATCH] Move get_errno_string() to src/base/errno_string. --- src/CMakeLists.txt | 1 + src/base/errno_string.cpp | 80 +++++++++++++++++++++++++++++++++++++++ src/base/errno_string.h | 26 +++++++++++++ src/core/core.cpp | 65 ------------------------------- src/core/core.h | 2 - 5 files changed, 107 insertions(+), 67 deletions(-) create mode 100644 src/base/errno_string.cpp create mode 100644 src/base/errno_string.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 18dc8d078..54c2b99a6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -52,6 +52,7 @@ add_library(aseprite-library xml_widgets.cpp app/color.cpp app/color_utils.cpp + base/errno_string.cpp base/mem_utils.cpp commands/cmd_about.cpp commands/cmd_advanced_mode.cpp diff --git a/src/base/errno_string.cpp b/src/base/errno_string.cpp new file mode 100644 index 000000000..be85862c2 --- /dev/null +++ b/src/base/errno_string.cpp @@ -0,0 +1,80 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2010 David Capello + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#include + +// Like 'strerror' but thread-safe. +std::string get_errno_string(int errnum) +{ + static const char *errors[] = { + "No error", /* errno = 0 */ + "Operation not permitted", /* errno = 1 (EPERM) */ + "No such file or directory", /* errno = 2 (ENOFILE) */ + "No such process", /* errno = 3 (ESRCH) */ + "Interrupted function call", /* errno = 4 (EINTR) */ + "Input/output error", /* errno = 5 (EIO) */ + "No such device or address", /* errno = 6 (ENXIO) */ + "Arg list too long", /* errno = 7 (E2BIG) */ + "Exec format error", /* errno = 8 (ENOEXEC) */ + "Bad file descriptor", /* errno = 9 (EBADF) */ + "No child processes", /* errno = 10 (ECHILD) */ + "Resource temporarily unavailable", /* errno = 11 (EAGAIN) */ + "Not enough space", /* errno = 12 (ENOMEM) */ + "Permission denied", /* errno = 13 (EACCES) */ + "Bad address", /* errno = 14 (EFAULT) */ + NULL, + "Resource device", /* errno = 16 (EBUSY) */ + "File exists", /* errno = 17 (EEXIST) */ + "Improper link", /* errno = 18 (EXDEV) */ + "No such device", /* errno = 19 (ENODEV) */ + "Not a directory", /* errno = 20 (ENOTDIR) */ + "Is a directory", /* errno = 21 (EISDIR) */ + "Invalid argument", /* errno = 22 (EINVAL) */ + "Too many open files in system", /* errno = 23 (ENFILE) */ + "Too many open files", /* errno = 24 (EMFILE) */ + "Inappropriate I/O control operation", /* errno = 25 (ENOTTY) */ + NULL, + "File too large", /* errno = 27 (EFBIG) */ + "No space left on device", /* errno = 28 (ENOSPC) */ + "Invalid seek", /* errno = 29 (ESPIPE) */ + "Read-only file system", /* errno = 30 (EROFS) */ + "Too many links", /* errno = 31 (EMLINK) */ + "Broken pipe", /* errno = 32 (EPIPE) */ + "Domain error", /* errno = 33 (EDOM) */ + "Result too large", /* errno = 34 (ERANGE) */ + NULL, + "Resource deadlock avoided", /* errno = 36 (EDEADLOCK) */ + NULL, + "Filename too long", /* errno = 38 (ENAMETOOLONG) */ + "No locks available", /* errno = 39 (ENOLCK) */ + "Function not implemented", /* errno = 40 (ENOSYS) */ + "Directory not empty", /* errno = 41 (ENOTEMPTY) */ + "Illegal byte sequence", /* errno = 42 (EILSEQ) */ + }; + + if (errnum >= 0 + && errnum < (int)(sizeof(errors)/sizeof(char *)) + && errors[errnum] != NULL) { + return errors[errnum]; + } + else { + return "Unknown error"; + } +} diff --git a/src/base/errno_string.h b/src/base/errno_string.h new file mode 100644 index 000000000..6f3395016 --- /dev/null +++ b/src/base/errno_string.h @@ -0,0 +1,26 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2010 David Capello + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef BASE_ERRNO_STRING_H_INCLUDED +#define BASE_ERRNO_STRING_H_INCLUDED + +#include + +std::string get_errno_string(int errnum); + +#endif diff --git a/src/core/core.cpp b/src/core/core.cpp index f0511e243..8956871c0 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -117,68 +117,3 @@ bool is_interactive() { return ase_mode & MODE_GUI ? true: false; } - -/** - * Like 'strerror' but thread-safe. - * - * @return The same @a buf pointer. - */ -char *get_errno_string(int errnum, char *buf, int size) -{ - static const char *errors[] = { - "No error", /* errno = 0 */ - "Operation not permitted", /* errno = 1 (EPERM) */ - "No such file or directory", /* errno = 2 (ENOFILE) */ - "No such process", /* errno = 3 (ESRCH) */ - "Interrupted function call", /* errno = 4 (EINTR) */ - "Input/output error", /* errno = 5 (EIO) */ - "No such device or address", /* errno = 6 (ENXIO) */ - "Arg list too long", /* errno = 7 (E2BIG) */ - "Exec format error", /* errno = 8 (ENOEXEC) */ - "Bad file descriptor", /* errno = 9 (EBADF) */ - "No child processes", /* errno = 10 (ECHILD) */ - "Resource temporarily unavailable", /* errno = 11 (EAGAIN) */ - "Not enough space", /* errno = 12 (ENOMEM) */ - "Permission denied", /* errno = 13 (EACCES) */ - "Bad address", /* errno = 14 (EFAULT) */ - NULL, - "Resource device", /* errno = 16 (EBUSY) */ - "File exists", /* errno = 17 (EEXIST) */ - "Improper link", /* errno = 18 (EXDEV) */ - "No such device", /* errno = 19 (ENODEV) */ - "Not a directory", /* errno = 20 (ENOTDIR) */ - "Is a directory", /* errno = 21 (EISDIR) */ - "Invalid argument", /* errno = 22 (EINVAL) */ - "Too many open files in system", /* errno = 23 (ENFILE) */ - "Too many open files", /* errno = 24 (EMFILE) */ - "Inappropriate I/O control operation", /* errno = 25 (ENOTTY) */ - NULL, - "File too large", /* errno = 27 (EFBIG) */ - "No space left on device", /* errno = 28 (ENOSPC) */ - "Invalid seek", /* errno = 29 (ESPIPE) */ - "Read-only file system", /* errno = 30 (EROFS) */ - "Too many links", /* errno = 31 (EMLINK) */ - "Broken pipe", /* errno = 32 (EPIPE) */ - "Domain error", /* errno = 33 (EDOM) */ - "Result too large", /* errno = 34 (ERANGE) */ - NULL, - "Resource deadlock avoided", /* errno = 36 (EDEADLOCK) */ - NULL, - "Filename too long", /* errno = 38 (ENAMETOOLONG) */ - "No locks available", /* errno = 39 (ENOLCK) */ - "Function not implemented", /* errno = 40 (ENOSYS) */ - "Directory not empty", /* errno = 41 (ENOTEMPTY) */ - "Illegal byte sequence", /* errno = 42 (EILSEQ) */ - }; - - if (errnum >= 0 - && errnum < (int)(sizeof(errors)/sizeof(char *)) - && errors[errnum] != NULL) { - ustrncpy(buf, errors[errnum], size); - } - else { - ustrncpy(buf, "Unknown error", size); - } - - return buf; -} diff --git a/src/core/core.h b/src/core/core.h index 826829f35..f4c569ec7 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -39,6 +39,4 @@ public: void verbose_printf(const char *format, ...); bool is_interactive(); -char *get_errno_string(int errnum, char *buf, int size); - #endif