[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-dev] [approve] standardizePath & other unix Prim



committed...


On 7 Dec 2005, at 12:21, Christopher Frauenberger wrote:

stefan kersten wrote:
On Wed, Nov 30, 2005 at 01:20:09PM +0100, Christopher Frauenberger wrote:
The patch below also implements basename, dirname, localtime and gmttime primitives for windows.
is there a reason for declaring the replacement functions
with a win32_ prefix? i think it's a bad idea cluttering
source files with #ifdef's when a compatible implementation
exists. i'd either remove the prefix or create platform
independent wrapper functions instead.
I removed all possible ifdefs and added #define wrappers in SC_Win32Utils.h for all unix replacements so that the code is not that fragmented anymore...

compiles fine when adding #include "SC_DirUtils.h" to
SC_LanguageClient.cpp.
added this, please test the patches below so that I could commit those changes...

thanks, best
Christopher

Index: headers/common/SC_DirUtils.h
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/headers/common/ SC_DirUtils.h,v
retrieving revision 1.6
diff -u -r1.6 SC_DirUtils.h
--- headers/common/SC_DirUtils.h	16 Nov 2005 13:18:43 -0000	1.6
+++ headers/common/SC_DirUtils.h	30 Nov 2005 10:55:45 -0000
@@ -41,6 +41,9 @@
 // Add 'component' to 'path' using the platform path separator.
 void sc_AppendToPath(char *path, const char *component);

+// Returns the expanded path with users home directory (also in newpath2)
+char *sc_StandardizePath(const char *path, char *newpath2);
+
 // Return TRUE iff 'dirname' is an existing directory.
 bool sc_DirectoryExists(const char *dirname);

Index: headers/common/SC_Win32Utils.h
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/headers/common/ SC_Win32Utils.h,v
retrieving revision 1.1
diff -u -r1.1 SC_Win32Utils.h
--- headers/common/SC_Win32Utils.h	16 Nov 2005 12:57:55 -0000	1.1
+++ headers/common/SC_Win32Utils.h	7 Dec 2005 10:36:35 -0000
@@ -24,11 +24,18 @@
 #include <winsock2.h>
 #include <pthread.h>

+// wrappers for unix replacements
+#define gettimeofday win32_gettimeofday
+#define basename win32_basename
+#define dirname win32_dirname
+#define nanosleep win32_nanosleep
+
void win32_ReplaceCharInString(char* string, int len, char src, char dst); void win32_ExtractContainingFolder(char* folder,const char* pattern,int maxChars);
 void win32_gettimeofday(timeval* tv, void*);
 void win32_GetHomeFolder(char* homeFolder, int bufLen);
 char* win32_basename(char* path);
+char* win32_dirname(char* path);
int win32_nanosleep (const struct timespec *requested_time, struct timespec *remaining);


Index: headers/lang/AdvancingAllocPool.h
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/headers/lang/ AdvancingAllocPool.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 AdvancingAllocPool.h
--- headers/lang/AdvancingAllocPool.h 6 Sep 2002 07:27:26 -0000 1.1.1.1
+++ headers/lang/AdvancingAllocPool.h	6 Dec 2005 14:22:47 -0000
@@ -28,8 +28,8 @@
 #ifndef _AdvancingAllocPool_
 #define _AdvancingAllocPool_

-#include <stdexcept>
 #include <stdlib.h>
+#include <stdexcept>

 class AllocPool;

Index: headers/lang/PyrSignal.h
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/headers/lang/ PyrSignal.h,v
retrieving revision 1.2
diff -u -r1.2 PyrSignal.h
--- headers/lang/PyrSignal.h	25 Dec 2004 21:39:58 -0000	1.2
+++ headers/lang/PyrSignal.h	6 Dec 2005 16:54:31 -0000
@@ -18,8 +18,7 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

-
-#pragma once on
+#pragma once // the value on here causes MSVC8 not to compile, is it needed elsewhere?

 #include "PyrObject.h"
 #include "GC.h"
Index: headers/lang/SC_LibraryConfig.h
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/headers/lang/ SC_LibraryConfig.h,v
retrieving revision 1.3
diff -u -r1.3 SC_LibraryConfig.h
--- headers/lang/SC_LibraryConfig.h	29 Sep 2004 08:52:59 -0000	1.3
+++ headers/lang/SC_LibraryConfig.h	30 Nov 2005 10:53:35 -0000
@@ -103,8 +103,5 @@
 };

 extern SC_LibraryConfig* gLibraryConfig;
-#ifndef SC_WIN32
-extern char *unixStandardizePath(const char *path, char *newpath);
-#endif

 #endif // SC_LIBRARYCONFIG_H_INCLUDED
Index: headers/plugin_interface/SC_Constants.h
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/headers/ plugin_interface/SC_Constants.h,v
retrieving revision 1.2
diff -u -r1.2 SC_Constants.h
--- headers/plugin_interface/SC_Constants.h 11 May 2005 15:30:15 -0000 1.2
+++ headers/plugin_interface/SC_Constants.h	6 Dec 2005 13:20:07 -0000
@@ -38,8 +38,8 @@
 const double rsqrt2 = 1. / sqrt2;

 // used to truncate precision
-const float truncFloat = (float)(3. * pow(2,22));
-const double truncDouble = 3. * pow(2,51);
+const float truncFloat = (float)(3. * pow(2.0,22));
+const double truncDouble = 3. * pow(2.0,51);

const float kBadValue = 1e20f; // used in the secant table for values very close to 1/0

Index: source/common/SC_DirUtils.cpp
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/source/common/ SC_DirUtils.cpp,v
retrieving revision 1.7
diff -u -r1.7 SC_DirUtils.cpp
--- source/common/SC_DirUtils.cpp	16 Nov 2005 13:18:43 -0000	1.7
+++ source/common/SC_DirUtils.cpp	30 Nov 2005 11:35:21 -0000
@@ -55,6 +55,44 @@
 	strncat(path, component, PATH_MAX);
 }

+char *sc_StandardizePath(const char *path, char *newpath2) {
+	char newpath1[MAXPATHLEN];
+
+	newpath1[0] = '\0';
+	newpath2[0] = '\0';
+
+	size_t pathLen = strlen(path);
+
+ if ((pathLen >= 2) && (path[0] == '~') && ((path[1] == '/') || (path[1] == '\\'))) {
+      char home[PATH_MAX];
+      sc_GetUserHomeDirectory(home, PATH_MAX);
+
+	    if (home != 0) {
+			if ((pathLen - 1 + strlen(home)) >= MAXPATHLEN) {
+				return 0;
+			}
+			strcpy(newpath1, home);
+			strcat(newpath1, path + 1);
+		} else {
+			if (pathLen >= MAXPATHLEN) {
+				return 0;
+			}
+			strcpy(newpath1, path);
+			newpath1[0] = '.';
+		}
+	} else {
+		if (pathLen >= MAXPATHLEN) {
+			return 0;
+		}
+		strcpy(newpath1, path);
+	}
+
+	bool isAlias = false;
+	sc_ResolveIfAlias(newpath1, newpath2, isAlias, PATH_MAX);
+
+	return newpath2;
+}
+

 // Returns TRUE iff dirname is an existing directory.

@@ -125,6 +163,10 @@
 			}
 		}
 	}
+#elif defined(SC_LINUX)
+	if (!realpath(path, returnPath))
+		strcpy(returnPath, path);
+	return;
 #endif
 	strcpy(returnPath, path);
 	return;
Index: source/common/SC_Win32Utils.cpp
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/source/common/ SC_Win32Utils.cpp,v
retrieving revision 1.2
diff -u -r1.2 SC_Win32Utils.cpp
--- source/common/SC_Win32Utils.cpp	16 Nov 2005 12:59:55 -0000	1.2
+++ source/common/SC_Win32Utils.cpp	30 Nov 2005 10:11:29 -0000
@@ -87,21 +87,45 @@
 }

 char* win32_basename(char* path)
-{
+{
   int pathLen = strlen(path);
   int lastPathSepFoundPos = -1;
   int pos = 0;
+  while (path[pathLen-1] == '\\' || path[pathLen-1] == '/') {
+	  path[pathLen-1]=0; pathLen--;
+  };
   while(path[pos] != 0) {
-    if (path[pos] == '/' || path[pos] == '/') {
+    if (path[pos] == '\\' || path[pos] == '/') {
       lastPathSepFoundPos = pos;
     }
+	pos++;
   }
-  if (lastPathSepFoundPos = -1)
+  if (lastPathSepFoundPos == -1)
     return path;
   else
     return path + lastPathSepFoundPos + 1;
 }

+char* win32_dirname(char* path)
+{
+  int pathLen = strlen(path);
+  int lastPathSepFoundPos = -1;
+  int pos = 0;
+  while (path[pathLen-1] == '\\' || path[pathLen-1] == '/') {
+	  path[pathLen-1]=0; pathLen--;
+  };
+  while(path[pos] != 0) {
+    if (path[pos] == '\\' || path[pos] == '/') {
+      lastPathSepFoundPos = pos;
+    }
+	pos++;
+  }
+  if (lastPathSepFoundPos != -1)
+	path[lastPathSepFoundPos]=0;
+
+  return path;
+}
+
 int win32_nanosleep (const struct timespec *requested_time,
 		  struct timespec *remaining)
 {
Index: source/lang/LangPrimSource/PyrUnixPrim.cpp
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/source/lang/ LangPrimSource/PyrUnixPrim.cpp,v
retrieving revision 1.14
diff -u -r1.14 PyrUnixPrim.cpp
--- source/lang/LangPrimSource/PyrUnixPrim.cpp 24 Nov 2005 09:03:22 -0000 1.14 +++ source/lang/LangPrimSource/PyrUnixPrim.cpp 7 Dec 2005 10:35:43 -0000
@@ -29,6 +29,7 @@
 #include "VMGlobals.h"
 #include "GC.h"
 #include "SC_RGen.h"
+#include "SC_DirUtils.h"

 #ifdef SC_WIN32
 #include <stdio.h>
@@ -62,7 +63,6 @@
 	return errNone;
 }

-#ifndef SC_WIN32
 int prString_Basename(struct VMGlobals *g, int numArgsPushed);
 int prString_Basename(struct VMGlobals *g, int numArgsPushed)
 {
@@ -72,7 +72,7 @@
         int err = slotStrVal(a, path, PATH_MAX);
         if (err) return err;

-        char *basename0 = basename(path);
+		char *basename0 = basename(path);

         int size = strlen(basename0);
         PyrString *strobj = newPyrStringN(g->gc, size, 0, true);
@@ -102,8 +102,6 @@

         return errNone;
 }
-#endif
-

 void* string_popen_thread_func(void *data);
 void* string_popen_thread_func(void *data)
@@ -166,7 +164,6 @@

 double bootSeconds();

-#ifndef SC_WIN32
 int prLocalTime(struct VMGlobals *g, int numArgsPushed);
 int prLocalTime(struct VMGlobals *g, int numArgsPushed)
 {
@@ -176,7 +173,6 @@
 	struct timeval tv;
 	gettimeofday(&tv, 0);
 	
-	
 	struct tm* tm = localtime((const time_t*)&tv.tv_sec);
 	
 	SetInt(slots+0, tm->tm_year + 1900);
@@ -201,7 +197,6 @@
 	struct timeval tv;
 	gettimeofday(&tv, 0);
 	
-	
 	struct tm* tm = gmtime((const time_t*)&tv.tv_sec);
 	
 	SetInt(slots+0, tm->tm_year + 1900);
@@ -216,7 +211,6 @@
 	
 	return errNone;
 }
-#endif

 int prAscTime(struct VMGlobals *g, int numArgsPushed);
 int prAscTime(struct VMGlobals *g, int numArgsPushed)
@@ -304,8 +298,7 @@
 	return errNone;
 }

-#ifdef SC_LINUX
-#include "SC_LibraryConfig.h"
+#ifndef SC_DARWIN

 // should be unified for OSX and linux
 int prString_StandardizePath(struct VMGlobals* g, int numArgsPushed);
@@ -319,14 +312,14 @@
 	err = slotStrVal(arg, ipath, PATH_MAX);
 	if (err) return err;

-	if (unixStandardizePath(ipath, opath)) {
+	if (sc_StandardizePath(ipath, opath)) {
 		PyrString* pyrString = newPyrString(g->gc, opath, 0, true);
 		SetObject(arg, pyrString);
 	}

 	return errNone;
 }
-#endif // SC_LINUX
+#endif // not SC_DARWIN

 void initUnixPrimitives();
 void initUnixPrimitives()
@@ -336,22 +329,18 @@
 	base = nextPrimitiveIndex();
 	
definePrimitive(base, index++, "_String_System", prString_System, 1, 0);
-#ifndef SC_WIN32
- definePrimitive(base, index++, "_String_Basename", prString_Basename, 1, 0); - definePrimitive(base, index++, "_String_Dirname", prString_Dirname, 1, 0);
-#endif
- definePrimitive(base, index++, "_String_POpen", prString_POpen, 1, 0); + definePrimitive(base, index++, "_String_Basename", prString_Basename, 1, 0); + definePrimitive(base, index++, "_String_Dirname", prString_Dirname, 1, 0); + definePrimitive(base, index++, "_String_POpen", prString_POpen, 1, 0);
 	definePrimitive(base, index++, "_Unix_Errno", prUnix_Errno, 1, 0);
-#ifndef SC_WIN32
 	definePrimitive(base, index++, "_LocalTime", prLocalTime, 1, 0);
 	definePrimitive(base, index++, "_GMTime", prGMTime, 1, 0);
-#endif
-  definePrimitive(base, index++, "_AscTime", prAscTime, 1, 0);
-  definePrimitive(base, index++, "_prStrFTime", prStrFTime, 2, 0);
+	definePrimitive(base, index++, "_AscTime", prAscTime, 1, 0);
+	definePrimitive(base, index++, "_prStrFTime", prStrFTime, 2, 0);
 	definePrimitive(base, index++, "_TimeSeed", prTimeSeed, 1, 0);
-#ifdef SC_LINUX
+#ifndef SC_DARWIN
definePrimitive(base, index++, "_Cocoa_StandardizePath", prString_StandardizePath, 1, 0);
-#endif // SC_LINUX
+#endif // not SC_DARWIN
 }


Index: source/lang/LangSource/PyrInterpreter3.cpp
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/source/lang/ LangSource/PyrInterpreter3.cpp,v
retrieving revision 1.40
diff -u -r1.40 PyrInterpreter3.cpp
--- source/lang/LangSource/PyrInterpreter3.cpp 16 Nov 2005 13:18:43 -0000 1.40 +++ source/lang/LangSource/PyrInterpreter3.cpp 7 Dec 2005 10:30:37 -0000
@@ -58,11 +58,7 @@
 int32 timeseed()
 {
 	struct timeval tv;
-#ifdef SC_WIN32
-  win32_gettimeofday(&tv, 0);
-#else
   gettimeofday(&tv, 0);
-#endif
   return tv.tv_sec ^ tv.tv_usec;
 }

Index: source/lang/LangSource/PyrLexer.cpp
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/source/lang/ LangSource/PyrLexer.cpp,v
retrieving revision 1.59
diff -u -r1.59 PyrLexer.cpp
--- source/lang/LangSource/PyrLexer.cpp 16 Nov 2005 13:18:43 -0000 1.59
+++ source/lang/LangSource/PyrLexer.cpp	6 Dec 2005 16:59:29 -0000
@@ -164,7 +164,7 @@
else if (c >= 'A' && c <= 'A' + sc_min(36,base) - 11) z = z * base + c - 'A' + 10;
 		else if (c == '.') decptpos = i;
 	}
-	z = z / pow(base, n - decptpos);
+	z = z / pow((double)base, n - decptpos);
 	return z;
 }

Index: source/lang/LangSource/PyrSched.cpp
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/source/lang/ LangSource/PyrSched.cpp,v
retrieving revision 1.23
diff -u -r1.23 PyrSched.cpp
--- source/lang/LangSource/PyrSched.cpp 16 Nov 2005 13:18:43 -0000 1.23
+++ source/lang/LangSource/PyrSched.cpp	7 Dec 2005 10:30:53 -0000
@@ -187,11 +187,7 @@
 double GetTimeOfDay()
 {
 	struct timeval tv;
-#ifdef SC_WIN32
-  win32_gettimeofday(&tv, 0);
-#else
   gettimeofday(&tv, 0);
-#endif
 	return (double)tv.tv_sec + 1.0e-6 * (double)tv.tv_usec;
 }
 #endif // SC_DARWIN
Index: source/lang/LangSource/SC_LanguageClient.cpp
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/source/lang/ LangSource/SC_LanguageClient.cpp,v
retrieving revision 1.13
diff -u -r1.13 SC_LanguageClient.cpp
--- source/lang/LangSource/SC_LanguageClient.cpp 13 Jul 2005 13:18:42 -0000 1.13 +++ source/lang/LangSource/SC_LanguageClient.cpp 7 Dec 2005 10:45:12 -0000
@@ -42,6 +42,7 @@
 #include "PyrPrimitive.h"
 #include "GC.h"
 #include "VMGlobals.h"
+#include "SC_DirUtils.h"

 void closeAllGUIScreens();
 void initGUI();
@@ -124,7 +125,7 @@

 	for (int i=0; i < 3; i++) {
 		snprintf(ipath, PATH_MAX, paths[i]);
-		if (unixStandardizePath(ipath, opath)) {
+		if (sc_StandardizePath(ipath, opath)) {
 			bool success = readLibraryConfig(opath, ipath);
 			if (success) return true;
 		}
Index: source/lang/LangSource/SC_LibraryConfig.cpp
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/source/lang/ LangSource/SC_LibraryConfig.cpp,v
retrieving revision 1.7
diff -u -r1.7 SC_LibraryConfig.cpp
--- source/lang/LangSource/SC_LibraryConfig.cpp 16 Nov 2005 13:18:43 -0000 1.7 +++ source/lang/LangSource/SC_LibraryConfig.cpp 7 Dec 2005 10:33:45 -0000
@@ -1,6 +1,7 @@
 #include "SC_LibraryConfig.h"
 #include "SCBase.h"
 #include "SC_StringBuffer.h"
+#include "SC_DirUtils.h"

 #include <assert.h>
 #include <ctype.h>
@@ -10,7 +11,6 @@
 #include <limits.h>
 #ifdef SC_WIN32
 # include "SC_Win32Utils.h"
-# define MAXPATHLEN _MAX_PATH
 #else
 # include <sys/param.h>
 # include <unistd.h>
@@ -139,54 +139,6 @@
 	}
 }

-#ifndef SC_WIN32
-// sk: slightly improved robustness for path lengths exceeding MAXPATHLEN
-//     newpath2 should be a buffer of size MAXPATHLEN
-
-char *unixStandardizePath(const char *path, char *newpath2) {
-	char newpath1[MAXPATHLEN];
-
-	newpath1[0] = '\0';
-	newpath2[0] = '\0';
-
-	size_t pathLen = strlen(path);
-
-	if ((pathLen >= 2) && (path[0] == '~') && (path[1] == '/')) {
-  #ifndef SC_WIN32
-      const char *home = getenv("HOME");
-  #else
-      char* home[_PATH_MAX];
-      win32_GetHomeFolder(home, _PATH_MAX);
-  #endif
-
-		if (home != 0) {
-			if ((pathLen - 1 + strlen(home)) >= MAXPATHLEN) {
-				return 0;
-			}
-			strcpy(newpath1, home);
-			strcat(newpath1, path + 1);
-		} else {
-			if (pathLen >= MAXPATHLEN) {
-				return 0;
-			}
-			strcpy(newpath1, path);
-			newpath1[0] = '.';
-		}
-	} else {
-		if (pathLen >= MAXPATHLEN) {
-			return 0;
-		}
-		strcpy(newpath1, path);
-	}
-
-	if (realpath(newpath1, newpath2) == 0) {
-		return 0;
-	}
-
-	return newpath2;
-}
-#endif
-
// =====================================================================
 // SC_LibraryConfigFile
// =====================================================================
@@ -345,18 +297,11 @@
 	path.finish();
   char realPath[MAXPATHLEN];

-#ifdef SC_WIN32
-  if (strlen(path.getData()) + 1 > MAXPATHLEN) {
- (*mErrorFunc)("%s,%d: couldn't resolve path %s\n", fileName, lineNumber, path.getData());
-		return false;
-	}
-  strcpy(realPath,path.getData());
-#else
-	if (unixStandardizePath(path.getData(), realPath) == 0) {
+	if (sc_StandardizePath(path.getData(), realPath) == 0) {
(*mErrorFunc)("%s,%d: couldn't resolve path %s\n", fileName, lineNumber, path.getData());
 		return false;
 	}
-#endif
+
 	if (action == ':') {
 		if (++depth > kMaxIncludeDepth) {
(*mErrorFunc)("%s,%d: maximum include depth of %d exceeded\n", fileName, lineNumber, kMaxIncludeDepth);
@@ -364,11 +309,7 @@
 		}
 		SC_LibraryConfigFile file(mErrorFunc);
 		if (!file.open(realPath)) return true;
-#ifdef SC_WIN32
-    const char* fileName = win32_basename(realPath);
-#else
     const char* fileName = basename(realPath);
-#endif
     bool success = file.read(depth, fileName, libConf);
 		file.close();
 		return success;
Index: source/server/SC_CoreAudio.cpp
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/source/server/ SC_CoreAudio.cpp,v
retrieving revision 1.51
diff -u -r1.51 SC_CoreAudio.cpp
--- source/server/SC_CoreAudio.cpp	16 Nov 2005 13:11:39 -0000	1.51
+++ source/server/SC_CoreAudio.cpp	7 Dec 2005 10:30:43 -0000
@@ -135,11 +135,7 @@
 {
 	struct timeval tv;
 	uint64 s, f;
-#ifdef SC_WIN32
-	win32_gettimeofday(&tv, 0);
-#else
 	gettimeofday(&tv, 0);
-#endif
 	s = (uint64)tv.tv_sec + (uint64)kSECONDS_FROM_1900_to_1970;
 	f = (uint64)((double)tv.tv_usec * kMicrosToOSCunits);

Index: source/server/SC_World.cpp
===================================================================
RCS file: /cvsroot/supercollider/SuperCollider3/source/server/ SC_World.cpp,v
retrieving revision 1.66
diff -u -r1.66 SC_World.cpp
--- source/server/SC_World.cpp	28 Nov 2005 10:21:26 -0000	1.66
+++ source/server/SC_World.cpp	6 Dec 2005 13:29:56 -0000
@@ -66,7 +66,7 @@
 }

 bool sc_UseVectorUnit();
-bool sc_SetDenormalFlags();
+void sc_SetDenormalFlags();

///////////////////////////////////////////////////////////////////// ///////////

_______________________________________________
sc-dev mailing list
sc-dev@xxxxxxxxxxxxxxx
http://www.create.ucsb.edu/mailman/listinfo/sc-dev