File njam-1.25-leveledit.patch of Package njam

--- njam-1.25-src/src/njamutils.h.leveledit	2005-12-13 12:56:43.000000000 +0100
+++ njam-1.25-src/src/njamutils.h	2006-05-09 19:40:37.000000000 +0200
@@ -25,6 +25,10 @@
 void NjamSetRect(SDL_Rect& rect, int x, int y, int w=0, int h=0);
 SDLKey NjamGetch(bool Wait);
 int NjamRandom(int MaxValue);
+#ifdef __linux__
+char *NjamGetHomeDir();
+int NjamCheckAndCreateDir(const char *name);
+#endif
 
 typedef enum { fxBlackWhite, fxDarken } tEffect;
 bool SurfaceEffect(SDL_Surface *surface, SDL_Rect& r, tEffect Effect = fxBlackWhite);
--- njam-1.25-src/src/njam.cpp.leveledit	2006-05-09 19:40:37.000000000 +0200
+++ njam-1.25-src/src/njam.cpp	2006-05-09 19:40:37.000000000 +0200
@@ -350,6 +350,12 @@
 bool NjamEngine::Init(bool Fullscreen, bool SoftwareSurface, bool UseDGA)
 {
 #ifdef __linux__
+	// Check if $HOME/.njam-levels exists and if it doesn't create it
+	char buf[512];
+	char *home = NjamGetHomeDir();
+	snprintf(buf, sizeof(buf), "%s/.njam-levels", home? home:"");
+	NjamCheckAndCreateDir(buf);
+	
 	sprintf(linux_sdl_driver, "x11\0");
 	char *driver_name = getenv("SDL_VIDEODRIVER");
 	if (driver_name)
@@ -624,7 +630,9 @@
 
 	// Load user settings from .conf file
     LogFile("Opening njam.conf file.\n");
+#ifndef __linux__
     char *home = getenv("HOME");
+#endif
     std::string njamconf("njam.conf");
     if (home)
         njamconf = std::string(home) + "/.njamconf";
--- njam-1.25-src/src/njam.h.leveledit	2006-05-09 21:06:37.000000000 +0200
+++ njam-1.25-src/src/njam.h	2006-05-09 21:07:42.000000000 +0200
@@ -98,6 +98,7 @@
 struct list_item
 {
 	char item_text[50];
+	char item_path[512];
 	struct list_item *next;
 	struct list_item *prev;
 };
@@ -192,7 +193,7 @@
 	void LevelEditor();				// level editor (njamedit.cpp)
 	void RenderEditor(bool, int, int, int);
 	bool EnterFileName(char *file_name);
-	int SelectMap(char type, char *filename = NULL);
+	int SelectMap(char type, char *filename = NULL, int filename_size = 0);
 	struct list_item *SelectFromList(struct list_item *first);
 	void Message(char *text);
 	bool Query(char *text);
--- njam-1.25-src/src/njamedit.cpp.leveledit	2005-12-13 12:56:42.000000000 +0100
+++ njam-1.25-src/src/njamedit.cpp	2006-05-09 21:06:08.000000000 +0200
@@ -41,7 +41,7 @@
 {
 	int level_type = 0;	// cooperative
 	int level_type_was = 0;
-	char filename[250];
+	char filename[512];
 	filename[0] = '\0';
 
 	int x=2, y=1;
@@ -90,7 +90,7 @@
 
 		if (key == SDLK_l)	// select file from directory
 		{
-			if (SelectMap('A', filename) > -1)
+			if (SelectMap('A', filename, sizeof(filename)) > -1)
 			{
 				int w=0;
 				while (filename[w])
@@ -127,12 +127,20 @@
 			{
 				if (EnterFileName(filename))
 				{
-					char buf[250];
+					char buf[512];
 					char types[2][5] = { "COOP", "DUEL" };
-					sprintf(buf, "levels/%s.%s\0", filename, types[level_type]);
-					sprintf(filename, "%s\0", buf);
+#ifdef __linux__
+					char *home = NjamGetHomeDir();
+					snprintf(buf, sizeof(buf),
+						"%s/.njam-levels/%s.%s",
+						home? home:"",
+#else
+					snprintf(buf, sizeof(buf),
+						"levels/%s.%s",
+#endif					
+						filename, types[level_type]);
 					LogFile::LogFile("Saving maps");
-					m_Maps.Save(filename);
+					m_Maps.Save(buf);
 					level_type_was = level_type;
 					changed = false;
 				}
@@ -458,51 +466,72 @@
 	}
 }
 //---------------------------------------------------------------------------
-int NjamEngine::SelectMap(char type, char *filename)
+int NjamEngine::SelectMap(char type, char *filename, int filename_size)
 {
 	struct list_item *first = NULL;
 
-	// open folder, load all entries
+	// open folders, load all entries
 	DIR *dir;
 	struct dirent *ent;
+	int i;
+	char dirname[512] = "levels";
 
-	if ((dir = opendir("levels")) == NULL)
+#ifdef __linux__
+	for (i=0; i<2; i++)
 	{
-		printf("Unable to open directory\n");
-		return -1;
-	}
+		if (i==0)
+		{
+#endif
+			if ((dir = opendir(dirname)) == NULL)
+			{
+				printf("Unable to open directory\n");
+				return -1;
+			}
+#ifdef __linux__
+		}
+		else
+		{
+			char *home = NjamGetHomeDir();
 
-	while ((ent = readdir(dir)) != NULL)
-	{
-		// check if last letter of name maches:
-		int p = 0;
-		while (ent->d_name[p])
-			p++;
+			snprintf(dirname, sizeof(dirname), "%s/.njam-levels",
+				home? home:"");
+			if ((dir = opendir(dirname)) == NULL)
+				break;
+		}
+#endif
+		while ((ent = readdir(dir)) != NULL)
+		{
+			// check if last letter of name maches:
+			int p = 0;
+			while (ent->d_name[p])
+				p++;
 
-		char c = ent->d_name[p-1];
-		if (c != 'P' && c != 'L')
-			continue;
+			char c = ent->d_name[p-1];
+			if (c != 'P' && c != 'L')
+				continue;
 
-		if (type != 'A' && c != type)
-			continue;
+			if (type != 'A' && c != type)
+				continue;
 
-		struct list_item *new_file = new struct list_item;
-		new_file->next = first;
-		new_file->prev = NULL;
-		if (first)
-			first->prev = new_file;
-		first = new_file;
+			struct list_item *new_file = new struct list_item;
+			new_file->next = first;
+			new_file->prev = NULL;
+			if (first)
+				first->prev = new_file;
+			first = new_file;
 
-		// copy file name
-		for (int i=0; ent->d_name[i] && i<29; i++)
-		{
-			first->item_text[i] = ent->d_name[i];
-			first->item_text[i+1] = '\0';
+			// copy file name
+			snprintf(first->item_text, sizeof(first->item_text),
+			  "%s", ent->d_name);
+			snprintf(first->item_path, sizeof(first->item_path),
+			  "%s/%s", dirname, ent->d_name);
 		}
-	}
 
-	if (closedir(dir) != 0)
-		printf("Unable to close directory\n");
+		if (closedir(dir) != 0)
+			printf("Unable to close directory\n");
+#ifdef __linux__
+	}
+#endif
 
 	// show and let player select if more than one levelset
 	struct list_item *selected;
@@ -514,11 +543,10 @@
 	int result = 0;	// no levels loaded (so far)
 	if (selected)
 	{
-		char file_name[200];
-		sprintf(file_name, "levels/%s\0", selected->item_text);
-		result = m_Maps.Load(file_name);
+		result = m_Maps.Load(selected->item_path);
 		if (filename)
-			sprintf(filename, "%s\0", file_name);
+			snprintf(filename, filename_size, "%s",
+				selected->item_path);
 	}
 
 	while (first)	// free memory
--- njam-1.25-src/src/njamutils.cpp.leveledit	2005-12-13 12:56:43.000000000 +0100
+++ njam-1.25-src/src/njamutils.cpp	2006-05-09 19:40:37.000000000 +0200
@@ -162,3 +162,83 @@
 	return true;
 }
 //-----------------------------------------------------------------------------
+#ifdef __linux__
+#include <pwd.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#if defined(__DECC) && defined(VMS)
+#include <unixlib.h>
+static char *vms_to_unix_buffer = NULL;
+static int convert_vms_to_unix(char *vms_dir_name)
+{
+	vms_to_unix_buffer = vms_dir_name;
+}
+#endif
+
+char *NjamGetHomeDir()
+{
+	struct passwd *pw;
+
+	if (!(pw = getpwuid(getuid())))
+	{ 
+		fprintf(stderr, "Who are you? Not found in passwd database!!\n");
+		return NULL;
+	}
+
+#if defined(__DECC) && defined(VMS)
+	/* Convert The OpenVMS Formatted "$HOME" Directory Path Into Unix
+	   Format. */
+	decc$from_vms(pw->pw_dir, convert_vms_to_unix, 1);
+	return vms_to_unix_buffer;
+#else
+	return pw->pw_dir;
+#endif
+}
+//-----------------------------------------------------------------------------
+int NjamCheckAndCreateDir(const char *name)
+{
+	struct stat stat_buffer;
+
+	if (stat(name, &stat_buffer))
+	{
+		/* error check if it doesn't exist or something else is wrong */
+		if (errno == ENOENT)
+		{
+			/* doesn't exist letts create it ;) */
+#ifdef BSD43
+			if (mkdir(name, 0775))
+#else
+				if (mkdir(name, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH))
+#endif
+				{
+					fprintf(stderr, "Error creating dir %s", name);
+					perror(" ");
+					return -1;
+				}
+		}
+		else
+		{
+			/* something else went wrong yell about it */
+			fprintf(stderr, "Error opening %s", name);
+			perror(" ");
+			return -1;
+		}
+	}
+	else
+	{
+		/* file exists check it's a dir otherwise yell about it */
+#ifdef BSD43
+		if (!(S_IFDIR & stat_buffer.st_mode))
+#else
+			if (!S_ISDIR(stat_buffer.st_mode))
+#endif
+			{
+				fprintf(stderr,"Error %s exists but isn't a dir\n", name);
+				return -1;
+			}
+	}
+	return 0;
+}
+#endif
openSUSE Build Service is sponsored by