Care for UTF8 filenams in linux/unix
authorStefan Vogel <sv@exept.de>
Thu, 06 Nov 2008 11:11:18 +0100
changeset 11317 c634e925111f
parent 11316 0b2757774461
child 11318 fc07976462e8
Care for UTF8 filenams in linux/unix
DirectoryStream.st
--- a/DirectoryStream.st	Wed Nov 05 17:38:45 2008 +0100
+++ b/DirectoryStream.st	Thu Nov 06 11:11:18 2008 +0100
@@ -509,39 +509,43 @@
 nextLine
     "return the next filename as a string"
 
-    |prevEntry nextEntry|
+    |prevEntry nextEntry isUnix|
 %{
+#ifdef unix
+    isUnix = true;
+#endif
+
 #ifdef HAS_OPENDIR
     DIR *d;
     DIRENT_STRUCT *dp;
     OBJ dirP;
 
     if (__INST(hitEOF) != true && (dirP = __INST(dirPointer)) != nil) {
-	__INST(lastErrorNumber) = nil;
-	d = (DIR *)__FILEVal(dirP);
+        __INST(lastErrorNumber) = nil;
+        d = (DIR *)__FILEVal(dirP);
 
-	__BEGIN_INTERRUPTABLE__
-	do {
-	    do {
-		__threadErrno = 0;
-		dp = readdir(d);
-		/*
-		 * for compatibility with ST-80,
-		 * skip entries for '.' and '..'.
-		 * If wanted, these must be added synthetically.
-		 */
-	    } while (dp && ((strcmp(dp->d_name, ".")==0) || (strcmp(dp->d_name, "..")==0)));
-	} while ((dp == NULL) && (__threadErrno == EINTR));
-	__END_INTERRUPTABLE__
+        __BEGIN_INTERRUPTABLE__
+        do {
+            do {
+                __threadErrno = 0;
+                dp = readdir(d);
+                /*
+                 * for compatibility with ST-80,
+                 * skip entries for '.' and '..'.
+                 * If wanted, these must be added synthetically.
+                 */
+            } while (dp && ((strcmp(dp->d_name, ".")==0) || (strcmp(dp->d_name, "..")==0)));
+        } while ((dp == NULL) && (__threadErrno == EINTR));
+        __END_INTERRUPTABLE__
 
-	if (dp != NULL) {
-	    nextEntry = __MKSTRING((char *)(dp->d_name));
-	} else {
-	    if (__threadErrno) {
-		__INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
-	    } else {
-		__INST(hitEOF) = true;
-	    }
+        if (dp != NULL) {
+            nextEntry = __MKSTRING((char *)(dp->d_name));
+        } else {
+            if (__threadErrno) {
+                __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
+            } else {
+                __INST(hitEOF) = true;
+            }
        }
     }
 #else /* no HAS_OPENDIR */
@@ -552,29 +556,34 @@
     int rslt;
 
     if (__INST(hitEOF) != true && (dirP = __INST(dirPointer)) != nil) {
-	__INST(lastErrorNumber) = nil;
-	d = __HANDLEVal(dirP);
+        __INST(lastErrorNumber) = nil;
+        d = __HANDLEVal(dirP);
 
-	do {
-	    __threadErrno = 0;
-	    rslt = STX_API_CALL2( "FindNextFileW", FindNextFileW, d, &data );
-	} while ((rslt < 0) && (__threadErrno == EINTR));
+        do {
+            __threadErrno = 0;
+            rslt = STX_API_CALL2( "FindNextFileW", FindNextFileW, d, &data );
+        } while ((rslt < 0) && (__threadErrno == EINTR));
 
-	if (rslt > 0) {
-	    nextEntry = __MKU16STRING( data.cFileName );
-	} else {
-	   __INST(hitEOF) = true;
-	}
+        if (rslt > 0) {
+            nextEntry = __MKU16STRING( data.cFileName );
+        } else {
+           __INST(hitEOF) = true;
+        }
     }
 # endif /* WIN32 */
 #endif /* HAS_OPENDIR */
 %}.
     lastErrorNumber notNil ifTrue:[^ self ioError].
+    nextEntry notNil ifTrue:[
+        isUnix == true ifTrue:[
+            "linux strings are in UTF8"
+            nextEntry := nextEntry utf8Decoded.
+        ] ifFalse:[
+            nextEntry := nextEntry asSingleByteStringIfPossible.
+        ]
+    ].
     prevEntry := readAheadEntry.
     readAheadEntry := nextEntry.
-    readAheadEntry notNil ifTrue:[
-	readAheadEntry := readAheadEntry asSingleByteStringIfPossible.
-    ].
     prevEntry isNil ifTrue:[^ self pastEndRead].
     ^ prevEntry
 ! !
@@ -782,5 +791,5 @@
 !DirectoryStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/DirectoryStream.st,v 1.66 2008-10-31 14:03:42 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/DirectoryStream.st,v 1.67 2008-11-06 10:11:18 stefan Exp $'
 ! !