ExternalStream.st
changeset 15361 ff526d39bb07
parent 15342 d89884677c76
child 15435 fc902ce16af0
child 18066 89d51443ba6f
equal deleted inserted replaced
15360:7bf49766c324 15361:ff526d39bb07
  3002 !
  3002 !
  3003 
  3003 
  3004 reset
  3004 reset
  3005     "set the read position to the beginning of the collection"
  3005     "set the read position to the beginning of the collection"
  3006 
  3006 
  3007     self position:ZeroPosition
  3007     self position:0
  3008 !
  3008 !
  3009 
  3009 
  3010 setToEnd
  3010 setToEnd
  3011     "redefined since it must be implemented differently"
  3011     "redefined since it must be implemented differently"
  3012 
  3012 
  4379     FILE *fdopen();
  4379     FILE *fdopen();
  4380     int fd = -2;
  4380     int fd = -2;
  4381 
  4381 
  4382     if (__isStringLike(openmode)) {
  4382     if (__isStringLike(openmode)) {
  4383 #ifdef WIN32
  4383 #ifdef WIN32
  4384 	__stxWrapApiEnterCritical();
  4384         __stxWrapApiEnterCritical();
  4385 #endif
  4385 #endif
  4386 	if (__isSmallInteger(aFileDescriptor)) {
  4386         if (__isSmallInteger(aFileDescriptor)) {
  4387 	    fd = __intVal(aFileDescriptor);
  4387             fd = __intVal(aFileDescriptor);
  4388 	}
  4388         }
  4389 #ifdef WIN32
  4389 #ifdef WIN32
  4390 	else if (__isExternalAddressLike(aFileDescriptor)) {
  4390         else if (__isExternalAddressLike(aFileDescriptor)) {
  4391 	    fd = _open_osfhandle((long)__externalAddressVal(aFileDescriptor), O_BINARY);
  4391             fd = _open_osfhandle((long)__externalAddressVal(aFileDescriptor), O_BINARY);
  4392 	    if (fd < 0) {
  4392             if (fd < 0) {
  4393 		if (__threadErrno == 0) {
  4393                 if (__threadErrno == 0) {
  4394 		    // no more file descriptors
  4394                     // no more file descriptors
  4395 		    __threadErrno = EMFILE;
  4395                     __threadErrno = EMFILE;
  4396 		}
  4396                 }
  4397 		error = __mkSmallInteger(__threadErrno);
  4397                 error = __mkSmallInteger(__threadErrno);
  4398 		__stxWrapApiLeaveCritical();
  4398                 __stxWrapApiLeaveCritical();
  4399 		goto out;
  4399                 goto out;
  4400 	    }
  4400             }
  4401 	}
  4401         }
  4402 #endif
  4402 #endif
  4403 	f = (FILEPOINTER) fdopen(fd, (char *)__stringVal(openmode));
  4403         f = (FILEPOINTER) fdopen(fd, (char *)__stringVal(openmode));
  4404 #ifdef WIN32
  4404 #ifdef WIN32
  4405 	__stxWrapApiLeaveCritical();
  4405         __stxWrapApiLeaveCritical();
  4406 #endif
  4406 #endif
  4407 	if (f == NULL) {
  4407         if (f == NULL) {
  4408 	    error =__mkSmallInteger(__threadErrno);
  4408             error =__mkSmallInteger(__threadErrno);
  4409 	} else {
  4409         } else {
  4410 	    if (@global(FileOpenTrace) == true) {
  4410             if (@global(FileOpenTrace) == true) {
  4411 		fprintf(stderr, "fdopen [ExternalStream] %d (%x) -> %x\n", fd, fd, f);
  4411                 fprintf(stderr, "fdopen [ExternalStream] %d (%x) -> %x\n", fd, fd, f);
  4412 	    }
  4412             }
  4413 
  4413 
  4414 	    fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
  4414             fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
  4415 	    retVal = self;
  4415             retVal = self;
  4416 	}
  4416         }
  4417     }
  4417     }
  4418 out:;
  4418 out:;
  4419 %}.
  4419 %}.
  4420     retVal notNil ifTrue:[
  4420     retVal notNil ifTrue:[
  4421 	position := ZeroPosition.
  4421         position := 0.
  4422 	buffered isNil ifTrue:[
  4422         buffered isNil ifTrue:[
  4423 	    buffered := true.       "default is buffered"
  4423             buffered := true.       "default is buffered"
  4424 	].
  4424         ].
  4425 	handleType := handleTypeSymbol.
  4425         handleType := handleTypeSymbol.
  4426 	Lobby register:self.
  4426         Lobby register:self.
  4427 	^ retVal
  4427         ^ retVal
  4428     ].
  4428     ].
  4429     error notNil ifTrue:[
  4429     error notNil ifTrue:[
  4430 	OperatingSystem closeFd:aFileDescriptor.
  4430         OperatingSystem closeFd:aFileDescriptor.
  4431 	lastErrorNumber := error.
  4431         lastErrorNumber := error.
  4432 	position := nil.
  4432         position := nil.
  4433 	"
  4433         "
  4434 	 the open failed for some reason ...
  4434          the open failed for some reason ...
  4435 	"
  4435         "
  4436 	^ self openError:error
  4436         ^ self openError:error
  4437     ].
  4437     ].
  4438 !
  4438 !
  4439 
  4439 
  4440 dupFd
  4440 dupFd
  4441     "duplicate my file descriptor"
  4441     "duplicate my file descriptor"
  4480     if (__isStringLike(aPath) && __isStringLike(openModeString)) {
  4480     if (__isStringLike(aPath) && __isStringLike(openModeString)) {
  4481 #ifdef WIN32
  4481 #ifdef WIN32
  4482 # if 1
  4482 # if 1
  4483        f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4483        f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4484 # else
  4484 # else
  4485 	__BEGIN_INTERRUPTABLE__
  4485         __BEGIN_INTERRUPTABLE__
  4486 	do {
  4486         do {
  4487 	    f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4487             f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4488 	} while ((f == NULL) && (__threadErrno == EINTR));
  4488         } while ((f == NULL) && (__threadErrno == EINTR));
  4489 	__END_INTERRUPTABLE__
  4489         __END_INTERRUPTABLE__
  4490 # endif
  4490 # endif
  4491 #else /* UNIX */
  4491 #else /* UNIX */
  4492 	__BEGIN_INTERRUPTABLE__
  4492         __BEGIN_INTERRUPTABLE__
  4493 	do {
  4493         do {
  4494 	    f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4494             f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4495 	} while ((f == NULL) && (__threadErrno == EINTR));
  4495         } while ((f == NULL) && (__threadErrno == EINTR));
  4496 	__END_INTERRUPTABLE__
  4496         __END_INTERRUPTABLE__
  4497 #endif /* UNIX */
  4497 #endif /* UNIX */
  4498 	if (f == NULL) {
  4498         if (f == NULL) {
  4499 	    __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
  4499             __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
  4500 	} else {
  4500         } else {
  4501 	    if (@global(FileOpenTrace) == true) {
  4501             if (@global(FileOpenTrace) == true) {
  4502 		fprintf(stderr, "fopen %s [ExternalStream] -> %x\n", __stringVal(aPath), f);
  4502                 fprintf(stderr, "fopen %s [ExternalStream] -> %x\n", __stringVal(aPath), f);
  4503 	    }
  4503             }
  4504 	    fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
  4504             fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
  4505 	    __INST(handleType) = @symbol(filePointer);
  4505             __INST(handleType) = @symbol(filePointer);
  4506 	    ok = true;
  4506             ok = true;
  4507 	}
  4507         }
  4508     }
  4508     }
  4509 %}.
  4509 %}.
  4510     ok ifFalse:[
  4510     ok ifFalse:[
  4511 	"
  4511         "
  4512 	 the open failed for some reason ...
  4512          the open failed for some reason ...
  4513 	"
  4513         "
  4514 	^ self openError:lastErrorNumber.
  4514         ^ self openError:lastErrorNumber.
  4515     ].
  4515     ].
  4516     position := ZeroPosition.
  4516     position := 0.
  4517     Lobby register:self.
  4517     Lobby register:self.
  4518 !
  4518 !
  4519 
  4519 
  4520 reOpen
  4520 reOpen
  4521     "USERS WILL NEVER INVOKE THIS METHOD
  4521     "USERS WILL NEVER INVOKE THIS METHOD
  5760 ! !
  5760 ! !
  5761 
  5761 
  5762 !ExternalStream class methodsFor:'documentation'!
  5762 !ExternalStream class methodsFor:'documentation'!
  5763 
  5763 
  5764 version
  5764 version
  5765     ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.363 2013-05-31 12:50:39 stefan Exp $'
  5765     ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.364 2013-06-03 18:41:42 cg Exp $'
  5766 !
  5766 !
  5767 
  5767 
  5768 version_CVS
  5768 version_CVS
  5769     ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.363 2013-05-31 12:50:39 stefan Exp $'
  5769     ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.364 2013-06-03 18:41:42 cg Exp $'
  5770 ! !
  5770 ! !
  5771 
  5771 
  5772 
  5772 
  5773 ExternalStream initialize!
  5773 ExternalStream initialize!