mirror of
https://git.zap.org.au/git/trader.git
synced 2024-10-13 18:03:39 -04:00
Add various header files
Add the system.h header file for all system header includes, and utils.c and utils.h for various low-level utility functions.
This commit is contained in:
parent
ced3697fe1
commit
6910370557
@ -28,4 +28,10 @@
|
|||||||
|
|
||||||
|
|
||||||
bin_PROGRAMS = trader
|
bin_PROGRAMS = trader
|
||||||
trader_SOURCES = trader.c trader.h globals.c globals.h
|
|
||||||
|
trader_SOURCES = \
|
||||||
|
trader.c trader.h \
|
||||||
|
globals.c globals.h \
|
||||||
|
utils.c utils.h \
|
||||||
|
system.h
|
||||||
|
trader_LDADD = @CURSES_LIB@
|
||||||
|
53
src/system.h
Normal file
53
src/system.h
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/************************************************************************
|
||||||
|
* *
|
||||||
|
* Star Traders: A Game of Interstellar Trading *
|
||||||
|
* Copyright (C) 1990-2011, John Zaitseff *
|
||||||
|
* *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Author: John Zaitseff <J.Zaitseff@zap.org.au>
|
||||||
|
$Id$
|
||||||
|
|
||||||
|
This file, system.h, contains system #include directives for Star
|
||||||
|
Traders.
|
||||||
|
|
||||||
|
|
||||||
|
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 3 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, see http://www.gnu.org/licenses/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef included_SYSTEM_H
|
||||||
|
#define included_SYSTEM_H 1
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************************
|
||||||
|
* Portability definitions *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
#ifdef HAVE_CONFIG_H
|
||||||
|
# include "config.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************************
|
||||||
|
* System header files *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* included_SYSTEM_H */
|
@ -32,7 +32,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "trader.h"
|
#include "trader.h"
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
|
|
||||||
/************************************************************************
|
/************************************************************************
|
||||||
@ -41,5 +40,12 @@
|
|||||||
|
|
||||||
int main (int argc, char *argv[])
|
int main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
init_program_name(argv);
|
||||||
|
|
||||||
|
// Testing...
|
||||||
|
printf("program_name: %s\n", program_name());
|
||||||
|
printf("home_directory: %s\n", home_directory());
|
||||||
|
printf("data_directory: %s\n", data_directory());
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
15
src/trader.h
15
src/trader.h
@ -9,7 +9,7 @@
|
|||||||
Author: John Zaitseff <J.Zaitseff@zap.org.au>
|
Author: John Zaitseff <J.Zaitseff@zap.org.au>
|
||||||
$Id$
|
$Id$
|
||||||
|
|
||||||
This file, trader.h, contains various definitions for Star Traders.
|
This file, trader.h, contains overall definitions for Star Traders.
|
||||||
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify it
|
This program is free software: you can redistribute it and/or modify it
|
||||||
@ -31,7 +31,20 @@
|
|||||||
#define included_TRADER_H 1
|
#define included_TRADER_H 1
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************************
|
||||||
|
* Global definitions *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
#define GAME_FILE_API_VERSION "7.0" /* For game loads and saves */
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************************
|
||||||
|
* Program-specific header files *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
#include "system.h"
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
|
||||||
#endif /* included_TRADER_H */
|
#endif /* included_TRADER_H */
|
||||||
|
150
src/utils.c
Normal file
150
src/utils.c
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
/************************************************************************
|
||||||
|
* *
|
||||||
|
* Star Traders: A Game of Interstellar Trading *
|
||||||
|
* Copyright (C) 1990-2011, John Zaitseff *
|
||||||
|
* *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Author: John Zaitseff <J.Zaitseff@zap.org.au>
|
||||||
|
$Id$
|
||||||
|
|
||||||
|
This file, utils.c, contains the implementation of various utility
|
||||||
|
function for Star Traders.
|
||||||
|
|
||||||
|
|
||||||
|
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 3 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, see http://www.gnu.org/licenses/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "system.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************************
|
||||||
|
* Module variables *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
static char *program_name_str = NULL; // Canonical program name
|
||||||
|
static char *home_directory_str = NULL; // Full pathname to home
|
||||||
|
static char *data_directory_str = NULL; // Writable data dir pathname
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************************
|
||||||
|
* Utility function definitions *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------
|
||||||
|
Function: init_program_name - Make the program name "canonical"
|
||||||
|
Arguments: argv - Same as passed to main()
|
||||||
|
Returns: (nothing)
|
||||||
|
|
||||||
|
This function modifies the argv[0] pointer to eliminate the leading
|
||||||
|
path name from the program name. In other words, it strips any leading
|
||||||
|
directories and leaves just the base name of the program. It also
|
||||||
|
assigns the module variable "program_name_str".
|
||||||
|
*/
|
||||||
|
|
||||||
|
void init_program_name (char *argv[])
|
||||||
|
{
|
||||||
|
if ((argv == NULL) || (argv[0] == NULL) || (*(argv[0]) == '\0')) {
|
||||||
|
program_name_str = PACKAGE;
|
||||||
|
} else {
|
||||||
|
char *p = strrchr(argv[0], '/');
|
||||||
|
|
||||||
|
if ((p != NULL) && (*++p != '\0')) {
|
||||||
|
argv[0] = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
program_name_str = argv[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------
|
||||||
|
Function: program_name - Return the canonical program name
|
||||||
|
Arguments: (none)
|
||||||
|
Returns: const char * - Pointer to program name
|
||||||
|
|
||||||
|
This function returns the canonical program name (the program name as
|
||||||
|
invoked on the command line, without any leading pathname). NULL
|
||||||
|
should never be returned.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const char *program_name (void)
|
||||||
|
{
|
||||||
|
if (program_name_str == NULL) {
|
||||||
|
init_program_name(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return program_name_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------
|
||||||
|
Function: home_directory - Return home directory pathname
|
||||||
|
Arguments: (none)
|
||||||
|
Returns: const char * - Pointer to home directory
|
||||||
|
|
||||||
|
This function returns the full pathname to the user's home directory,
|
||||||
|
using the HOME environment variable. Note that the existance or
|
||||||
|
writability of this pathname is NOT checked by this function. NULL is
|
||||||
|
returned if the home directory cannot be determined.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const char *home_directory (void)
|
||||||
|
{
|
||||||
|
if (home_directory_str == NULL) {
|
||||||
|
// Use the HOME environment variable where possible
|
||||||
|
char *p = getenv("HOME");
|
||||||
|
if ((p != NULL) && (*p != '\0')) {
|
||||||
|
home_directory_str = strdup(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return home_directory_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------
|
||||||
|
Function: data_directory - Return writable data directory pathname
|
||||||
|
Arguments: (none)
|
||||||
|
Returns: const char * - Pointer to data directory
|
||||||
|
|
||||||
|
This function returns the full pathname to a writable subdirectory
|
||||||
|
within the user's home directory. Essentially, home_directory() + "/."
|
||||||
|
+ program_name() is returned. Note that this path is NOT created by
|
||||||
|
this function, nor is the writability of this path checked. NULL is
|
||||||
|
returned if this path cannot be determined.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const char *data_directory (void)
|
||||||
|
{
|
||||||
|
if (data_directory_str == NULL) {
|
||||||
|
const char *name = program_name();
|
||||||
|
const char *home = home_directory();
|
||||||
|
if ((name != NULL) && (home != NULL)) {
|
||||||
|
char *p = malloc(strlen(home) + strlen(name) + 3);
|
||||||
|
if (p != NULL) {
|
||||||
|
strcpy(p, home);
|
||||||
|
strcat(p, "/.");
|
||||||
|
strcat(p, name);
|
||||||
|
data_directory_str = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return data_directory_str;
|
||||||
|
}
|
46
src/utils.h
Normal file
46
src/utils.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/************************************************************************
|
||||||
|
* *
|
||||||
|
* Star Traders: A Game of Interstellar Trading *
|
||||||
|
* Copyright (C) 1990-2011, John Zaitseff *
|
||||||
|
* *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Author: John Zaitseff <J.Zaitseff@zap.org.au>
|
||||||
|
$Id$
|
||||||
|
|
||||||
|
This file, utils.h, contains various utility function declarations for
|
||||||
|
Star Traders.
|
||||||
|
|
||||||
|
|
||||||
|
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 3 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, see http://www.gnu.org/licenses/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef included_UTILS_H
|
||||||
|
#define included_UTILS_H 1
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************************
|
||||||
|
* Utility function declarations *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
extern void init_program_name (char *argv[]);
|
||||||
|
extern const char *program_name (void);
|
||||||
|
|
||||||
|
extern const char *home_directory (void);
|
||||||
|
extern const char *data_directory (void);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* included_UTILS_H */
|
Loading…
Reference in New Issue
Block a user