Skip to content

Commit

Permalink
convert/refactor to/in c++
Browse files Browse the repository at this point in the history
  • Loading branch information
hotbso committed Jan 1, 2025
1 parent 6062549 commit 574697f
Show file tree
Hide file tree
Showing 21 changed files with 891 additions and 1,005 deletions.
11 changes: 7 additions & 4 deletions Makefile.lin64
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ TARGET_XP12=$(OBJDIR)/openSAM.xpl
TARGET_XP11=$(OBJDIR)/openSAM.xpl_xp11

HEADERS=$(wildcard *.h)
SOURCES=openSAM.c os_dgs.c os_jw.c os_ui.c os_anim.c sam_xml.c log_msg.c read_wav.c
OBJECTS=$(addprefix $(OBJDIR)/, $(SOURCES:.c=.o))
SOURCES=openSAM.cpp os_dgs.cpp os_jw.cpp os_ui.cpp os_anim.cpp sam_xml.cpp log_msg.cpp read_wav.cpp
OBJECTS:=$(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
OBJECTS_XP12=$(OBJECTS) $(OBJDIR)/os_sound.o
OBJECTS_XP11=$(OBJECTS) $(OBJDIR)/os_sound_xp11.o

CXX=g++
LD=g++

CFLAGS=-DVERSION=\"$(VERSION)\" \
-Wall -Wextra -Wno-format-overflow -Wno-format-truncation \
-I$(SDK)/CHeaders/XPLM -I$(SDK)/CHeaders/Widgets -O2 -fPIC -DLIN=1 -fno-stack-protector
Expand All @@ -24,8 +27,8 @@ LIBS=-lexpat
all: $(TARGET_XP12) $(TARGET_XP11)
$(shell [ -d $(OBJDIR) ] || mkdir $(OBJDIR))

$(OBJDIR)/%.o: %.c version.mak
$(CC) $(CFLAGS) -o $@ -c $<
$(OBJDIR)/%.o: %.cpp version.mak
$(CXX) $(CFLAGS) -o $@ -c $<

$(TARGET_XP12): $(OBJECTS_XP12)
$(CC) -o $(TARGET_XP12) $(LNFLAGS) $(OBJECTS_XP12) $(LIBS)
Expand Down
16 changes: 8 additions & 8 deletions Makefile.mac64
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ TARGET_XP12=$(OBJDIR)/openSAM.xpl
TARGET_XP11=$(OBJDIR)/openSAM.xpl_xp11

HEADERS=$(wildcard *.h)
SOURCES=openSAM.c os_dgs.c os_jw.c os_ui.c os_anim.c sam_xml.c log_msg.c read_wav.c
OBJECTS_XP12_arm=$(addprefix $(OBJDIR)/, $(SOURCES:.c=.o_arm)) $(OBJDIR)/os_sound.o_arm
SOURCES=openSAM.cpp os_dgs.cpp os_jw.cpp os_ui.cpp os_anim.cpp sam_xml.cpp log_msg.cpp read_wav.cpp
OBJECTS_XP12_arm=$(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o_arm)) $(OBJDIR)/os_sound.o_arm
OBJECTS_XP12_x86=$(OBJECTS_XP12_arm:.o_arm=.o_x86)

OBJECTS_XP11_arm=$(addprefix $(OBJDIR)/, $(SOURCES:.c=.o_arm)) $(OBJDIR)/os_sound_xp11.o_arm
OBJECTS_XP11_arm=$(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o_arm)) $(OBJDIR)/os_sound_xp11.o_arm
OBJECTS_XP11_x86=$(OBJECTS_XP11_arm:.o_arm=.o_x86)

CCx=clang -target x86_64-apple-macos11
CCa=clang -target arm64-apple-macos11
CCx=clang++ -target x86_64-apple-macos11
CCa=clang++ -target arm64-apple-macos11

DEFS= -DAPL=1 -fPIC -fno-stack-protector -fvisibility=hidden -ffast-math -DNDEBUG -Wno-deprecated-declarations

CFLAGS+=-O2 -Wall -I$(SDK)/CHeaders/XPLM -I$(SDK)/CHeaders/Widgets -DVERSION=\"$(VERSION)\" $(DEFS)
CFLAGS+=-std=c++17 -O2 -Wall -I$(SDK)/CHeaders/XPLM -I$(SDK)/CHeaders/Widgets -DVERSION=\"$(VERSION)\" $(DEFS)

LNFLAGS+=-dynamiclib -shared -rdynamic -fvisibility=hidden -Wl,-exported_symbols_list -Wl,linkscript.mac

Expand All @@ -42,10 +42,10 @@ LIBS+=/opt/local/lib/libexpat.a
all: $(TARGET_XP12) $(TARGET_XP11)
$(shell [ -d $(OBJDIR) ] || mkdir $(OBJDIR))

$(OBJDIR)/%.o_arm: %.c version.mak
$(OBJDIR)/%.o_arm: %.cpp version.mak
$(CCa) $(CFLAGS) -o $@ -c $<

$(OBJDIR)/%.o_x86: %.c version.mak
$(OBJDIR)/%.o_x86: %.cpp version.mak
$(CCx) $(CFLAGS) -o $@ -c $<

$(TARGET_XP12)_arm: $(OBJECTS_XP12_arm)
Expand Down
24 changes: 12 additions & 12 deletions Makefile.mgw64
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ TARGET_XP12=$(OBJDIR)/openSAM.xpl
TARGET_XP11=$(OBJDIR)/openSAM.xpl_xp11

HEADERS=$(wildcard *.h)
SOURCES=openSAM.c os_dgs.c os_jw.c os_ui.c os_anim.c sam_xml.c log_msg.c read_wav.c
OBJECTS:=$(addprefix $(OBJDIR)/, $(SOURCES:.c=.o))
SOURCES=openSAM.cpp os_dgs.cpp os_jw.cpp os_ui.cpp os_anim.cpp sam_xml.cpp log_msg.cpp read_wav.cpp
OBJECTS:=$(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
OBJECTS_XP12=$(OBJECTS) $(OBJDIR)/os_sound.o
OBJECTS_XP11=$(OBJECTS) $(OBJDIR)/os_sound_xp11.o

# all sources to the dep files
SOURCES_DEP=$(SOURCES) os_sound.c os_sound_xp11.c
DEPFILES=$(SOURCES_DEP:%.c=$(DEPDIR)/%.d)
SOURCES_DEP=$(SOURCES) os_sound.cpp os_sound_xp11.cpp
DEPFILES=$(SOURCES_DEP:%.cpp=$(DEPDIR)/%.d)

DEPDIR := $(OBJDIR)/.deps
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.d

CC=gcc
LD=gcc
CXX=g++
LD=g++

CFLAGS+=-DVERSION=\"$(VERSION)\" -g0 -O2 -Wextra -Wall -fdiagnostics-color -Wno-format-overflow -Wno-format-truncation \
-DWINDOWS -DWIN32 -DIBM=1 \
-I$(SDK)/CHeaders/XPLM -I$(SDK)/CHeaders/Widgets -I$(OPENAL)

COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) -c
COMPILE.cpp = $(CXX) $(DEPFLAGS) $(CFLAGS) -c

LDFLAGS=-shared -static-libgcc -static -lpthread
LIBS=-L$(SDK)/Libraries/Win -lXPLM_64 -lXPWidgets_64 -l:libexpat.a

$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d version.mak | $(DEPDIR)
$(COMPILE.c) -o $@ $<
$(OBJDIR)/%.o: %.cpp $(DEPDIR)/%.d version.mak | $(DEPDIR)
$(COMPILE.cpp) -o $@ $<

install: install_xp12 install_xp11

Expand Down Expand Up @@ -64,7 +64,7 @@ install_xp11: $(TARGET_XP11)
clean:
rm -f ./$(OBJDIR)/* sam_xml_test.exe

sam_xml_test.exe: sam_xml_test.c sam_xml.c log_msg.c $(HEADERS)
$(CC) -Wall -fdiagnostics-color -Wno-format-overflow -I$(SDK)/CHeaders/XPLM -DIBM=1 \
sam_xml_test.exe: sam_xml_test.cpp sam_xml.cpp log_msg.cpp $(HEADERS)
$(CXX) -Wall -fdiagnostics-color -Wno-format-overflow -I$(SDK)/CHeaders/XPLM -DIBM=1 \
-DWINDOWS -DWIN32 -DLOCAL_DEBUGSTRING -o sam_xml_test.exe \
sam_xml_test.c sam_xml.c log_msg.c -l:libexpat.a
sam_xml_test.cpp sam_xml.cpp log_msg.cpp -l:libexpat.a
14 changes: 7 additions & 7 deletions Makefile.osxcross
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ OBJDIR=./OBJ_osx
TARGET_XP12=$(OBJDIR)/openSAM.xpl
TARGET_XP11=$(OBJDIR)/openSAM.xpl_xp11
HEADERS=$(wildcard *.h)
SOURCES=openSAM.c os_dgs.c os_jw.c os_ui.c os_anim.c sam_xml.c log_msg.c read_wav.c
OBJECTS_XP12_arm=$(addprefix $(OBJDIR)/, $(SOURCES:.c=.o_arm)) $(OBJDIR)/os_sound.o_arm
SOURCES=openSAM.cpp os_dgs.cpp os_jw.cpp os_ui.cpp os_anim.cpp sam_xml.cpp log_msg.cpp read_wav.cpp
OBJECTS_XP12_arm=$(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o_arm)) $(OBJDIR)/os_sound.o_arm
OBJECTS_XP12_x86=$(OBJECTS_XP12_arm:.o_arm=.o_x86)

OBJECTS_XP11_arm=$(addprefix $(OBJDIR)/, $(SOURCES:.c=.o_arm)) $(OBJDIR)/os_sound_xp11.o_arm
OBJECTS_XP11_arm=$(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o_arm)) $(OBJDIR)/os_sound_xp11.o_arm
OBJECTS_XP11_x86=$(OBJECTS_XP11_arm:.o_arm=.o_x86)

CCx=o64-clang -mmacosx-version-min=12.0
CCa=oa64-clang -mmacosx-version-min=12.0
CCx=o64-clang++ -mmacosx-version-min=12.0
CCa=oa64-clang++ -mmacosx-version-min=12.0

DEFS= -DAPL=1 -fPIC -fno-stack-protector -fvisibility=hidden -ffast-math -DNDEBUG

Expand All @@ -42,10 +42,10 @@ LIBS+=
all: $(TARGET_XP12) $(TARGET_XP11)
$(shell [ -d $(OBJDIR) ] || mkdir $(OBJDIR))

$(OBJDIR)/%.o_arm: %.c version.mak
$(OBJDIR)/%.o_arm: %.cpp version.mak
$(CCa) $(CFLAGS) -o $@ -c $<

$(OBJDIR)/%.o_x86: %.c version.mak
$(OBJDIR)/%.o_x86: %.cpp version.mak
$(CCx) $(CFLAGS) -o $@ -c $<

$(TARGET_XP12)_arm: $(OBJECTS_XP12_arm)
Expand Down
6 changes: 3 additions & 3 deletions log_msg.c → log_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*/

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <cstdio>
#include <cstdarg>
#include <cstring>

#ifdef LOCAL_DEBUGSTRING
void
Expand Down
109 changes: 48 additions & 61 deletions openSAM.c → openSAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
*/

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <fstream>

#include "openSAM.h"
#include "os_dgs.h"
Expand Down Expand Up @@ -76,9 +75,8 @@
*/

static int init_done, init_fail;
static char xp_dir[512];
static char pref_path[512];
static const char *psep;
static std::string xp_dir;
static std::string pref_path;
static XPLMMenuID seasons_menu;
static int auto_item, season_item[4];
static int auto_season;
Expand Down Expand Up @@ -112,8 +110,8 @@ int auto_select_jws;
float parked_x, parked_z;
int parked_ngen;

float now; // current timestamp
char base_dir[512]; // base directory of openSAM
float now; // current timestamp
std::string base_dir; // base directory of openSAM

int beacon_state, beacon_last_pos; // beacon state, last switch_pos, ts of last switch actions
float beacon_off_ts, beacon_on_ts;
Expand All @@ -132,14 +130,14 @@ unsigned long long stat_sc_far_skip, stat_far_skip, stat_near_skip,
stat_acc_called, stat_jw_match, stat_dgs_acc, stat_dgs_acc_last,
stat_anim_acc_called, stat_auto_drf_called;

XPLMProbeInfo_t probeinfo = {.structSize = sizeof(XPLMProbeInfo_t)};
XPLMProbeInfo_t probeinfo;
XPLMProbeRef probe_ref;
XPLMMenuID anim_menu;

static void
save_pref()
{
FILE *f = fopen(pref_path, "w");
FILE *f = fopen(pref_path.c_str(), "w");
if (NULL == f)
return;

Expand All @@ -160,7 +158,7 @@ load_pref()
season = 1;
auto_select_jws = 1;

FILE *f = fopen(pref_path, "r");
FILE *f = fopen(pref_path.c_str(), "r");
if (NULL == f)
return;

Expand Down Expand Up @@ -315,7 +313,7 @@ flight_loop_cb(float inElapsedSinceLastCall,
anim_next_ts = now + anim_loop_delay;
}

return MIN(anim_loop_delay, MIN(jw_loop_delay, dgs_loop_delay));
return std::min(anim_loop_delay, std::min(jw_loop_delay, dgs_loop_delay));
}

// set season according to date
Expand Down Expand Up @@ -387,34 +385,27 @@ menu_cb(void *menu_ref, void *item_ref)
save_pref();
}

static int
find_icao_in_file(const char *acf_icao, const char *dir, const char *fn,
char *line, int len)
static bool
find_icao_in_file(const char *acf_icao, const std::string& fn, std::string& line)
{
char fn_full[512];
snprintf(fn_full, sizeof(fn_full) - 1, "%s%s", dir, fn);

int res = 0;
FILE *f = fopen(fn_full, "r");
line[len-1] = '\0';
if (f) {
log_msg("check whether acf '%s' is in file %s", acf_icao, fn_full);
while (fgets(line, len-1, f)) {
char *cptr = strchr(line, '\r');
if (cptr)
*cptr = '\0';
cptr = strchr(line, '\n');
if (cptr)
*cptr = '\0';

if (line == strstr(line, acf_icao)) {
log_msg("found acf %s in %s", acf_icao, fn);
res = 1;
bool res = false;
std::ifstream f(fn);
if (f.is_open()) {
log_msg("check whether acf '%s' is in file %s", acf_icao, fn.c_str());

while (std::getline(f, line)) {
size_t i = line.find('\r');
if (i != std::string::npos)
line.resize(i);

if (line.find(acf_icao) == 0) {
log_msg("found acf %s in %s", acf_icao, fn.c_str());
res = true;
break;
}
}

fclose(f);
f.close();
}

return res;
Expand All @@ -426,6 +417,7 @@ XPluginStart(char *out_name, char *out_sig, char *out_desc)
{
log_msg("Startup " VERSION);

probeinfo.structSize = sizeof(XPLMProbeInfo_t);
strcpy(out_name, "openSAM " VERSION);
strcpy(out_sig, "openSAM.hotbso");
strcpy(out_desc, "A plugin that emulates SAM");
Expand All @@ -434,24 +426,17 @@ XPluginStart(char *out_name, char *out_sig, char *out_desc)
XPLMEnableFeature("XPLM_USE_NATIVE_PATHS", 1);
XPLMEnableFeature("XPLM_USE_NATIVE_WIDGET_WINDOWS", 1);

XPLMGetSystemPath(xp_dir);
psep = XPLMGetDirectorySeparator();
char buffer[2048];
XPLMGetSystemPath(buffer);
xp_dir = std::string(buffer);

// set pref path
XPLMGetPrefsPath(pref_path);
XPLMExtractFileAndPath(pref_path);
strcat(pref_path, psep);
strcat(pref_path, "openSAM.prf");
XPLMGetPrefsPath(buffer);
XPLMExtractFileAndPath(buffer);
pref_path = std::string(buffer) + "/openSAM.prf";

// get my base dir
XPLMGetPluginInfo(XPLMGetMyID(), NULL, base_dir, NULL, NULL);
char *cptr = strrchr(base_dir, '/');
if (cptr)
*cptr = '\0';

cptr = strrchr(base_dir, '/');
if (cptr)
*(cptr + 1) = '\0'; // keep /
base_dir = xp_dir + "Resources/plugins/openSAM/";

date_day_dr = XPLMFindDataRef("sim/time/local_date_days");

Expand Down Expand Up @@ -492,10 +477,13 @@ XPluginStart(char *out_name, char *out_sig, char *out_desc)

load_pref();

if (!collect_sam_xml(xp_dir))
SceneryPacks scp(xp_dir);
if (! scp.valid) {
log_msg("Error collecting scenery_packs.ini!");
} else if (!collect_sam_xml(scp))
log_msg("Error collecting sam.xml files!");

log_msg("%d sceneries with sam jetways found", n_sceneries);
log_msg("%d sceneries with sam jetways found", (int)sceneries.size());

// if commands or dataref accessors are already registered it's to late to
// fail XPluginStart as the dll is unloaded and X-Plane crashes
Expand Down Expand Up @@ -658,13 +646,13 @@ XPluginReceiveMessage(XPLMPluginID in_from, long in_msg, void *in_param)
return;

// check whether acf is listed in exception files
char line[200];
if (find_icao_in_file(acf_icao, base_dir, "acf_use_engine_running.txt", line, sizeof(line))) {
std::string line; line.reserve(200);
if (find_icao_in_file(acf_icao, base_dir + "acf_use_engine_running.txt", line)) {
use_engine_running = 1;
log_msg("found");
}

if (find_icao_in_file(acf_icao, base_dir, "acf_dont_connect_jetway.txt", line, sizeof(line))) {
if (find_icao_in_file(acf_icao, base_dir + "acf_dont_connect_jetway.txt", line)) {
dont_connect_jetway = 1;
log_msg("found");
}
Expand All @@ -682,11 +670,10 @@ XPluginReceiveMessage(XPLMPluginID in_from, long in_msg, void *in_param)

// check for a second door, seems to be not available by dataref
// data in the acf file is often bogus, so check our own config file first
line[sizeof(line) - 1] = '\0';
if (find_icao_in_file(acf_icao, base_dir, "acf_door_position.txt", line, sizeof(line))) {
if (find_icao_in_file(acf_icao, base_dir + "acf_door_position.txt", line)) {
int d;
float x, y, z;
if (4 == sscanf(line + 4, "%d %f %f %f", &d, &x, &y, &z)) {
if (4 == sscanf(line.c_str() + 4, "%d %f %f %f", &d, &x, &y, &z)) {
if (d == 2) { // only door 2 for now
d--;
door_info[d].x = x;
Expand Down Expand Up @@ -769,7 +756,7 @@ XPluginReceiveMessage(XPLMPluginID in_from, long in_msg, void *in_param)
log_msg("A321 detected, checking door config");

char path[512];
strcpy(path, xp_dir);
strcpy(path, xp_dir.c_str());
int len = strlen(path);
int n = XPLMGetDatab(acf_livery_path, path + len, 0, sizeof(path) - len - 50);
path[len + n] = '\0';
Expand Down
Loading

0 comments on commit 574697f

Please sign in to comment.