Move get_errno_string() to src/base/errno_string.

This commit is contained in:
David Capello 2010-09-18 01:03:56 -03:00
parent 0674fbe069
commit f1e9814334
5 changed files with 107 additions and 67 deletions

View File

@ -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

80
src/base/errno_string.cpp Normal file
View File

@ -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 <string>
// 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";
}
}

26
src/base/errno_string.h Normal file
View File

@ -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 <string>
std::string get_errno_string(int errnum);
#endif

View File

@ -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;
}

View File

@ -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