Win32OperatingSystem.st
changeset 6304 c51a3d81ba5b
parent 6297 7d0a04fb50be
child 6336 7accea3927f8
equal deleted inserted replaced
6303:ef753226450d 6304:c51a3d81ba5b
  1161                 typ = @symbol(nonexistentSignal);
  1161                 typ = @symbol(nonexistentSignal);
  1162                 break;
  1162                 break;
  1163 
  1163 
  1164             case ERROR_PATH_NOT_FOUND:
  1164             case ERROR_PATH_NOT_FOUND:
  1165                 sym = @symbol(ERROR_PATH_NOT_FOUND);
  1165                 sym = @symbol(ERROR_PATH_NOT_FOUND);
  1166                 typ = @symbol(unavailableReferentSignal);
  1166                 typ = @symbol(nonexistentSignal);
  1167                 break;
  1167                 break;
  1168 
  1168 
  1169             case ERROR_TOO_MANY_OPEN_FILES:
  1169             case ERROR_TOO_MANY_OPEN_FILES:
  1170                 sym = @symbol(ERROR_TOO_MANY_OPEN_FILES);
  1170                 sym = @symbol(ERROR_TOO_MANY_OPEN_FILES);
  1171                 typ = @symbol(noResourcesSignal);
  1171                 typ = @symbol(noResourcesSignal);
  3241     h = CreateFile(name, access, share, 0 /* sa */, create, attr, 0 /* hTempl */);
  3241     h = CreateFile(name, access, share, 0 /* sa */, create, attr, 0 /* hTempl */);
  3242     if (h != INVALID_HANDLE_VALUE) {
  3242     if (h != INVALID_HANDLE_VALUE) {
  3243         __externalAddressVal(fileHandle) = (void *)h;  
  3243         __externalAddressVal(fileHandle) = (void *)h;  
  3244     } else {
  3244     } else {
  3245         fileHandle = nil;
  3245         fileHandle = nil;
  3246         errorNumber = __MKSMALLINT( __WIN32_ERR(GetLastError()) );
  3246         errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) );
  3247     }
  3247     }
  3248 
  3248 
  3249 badArgument: ;
  3249 badArgument: ;
  3250 %}.
  3250 %}.
  3251     fileHandle notNil ifTrue:[
  3251     fileHandle notNil ifTrue:[
  3275 openFileForReadWrite:pathName
  3275 openFileForReadWrite:pathName
  3276      ^ self openFile:pathName attributes:#(#'GENERIC_READ' #'GENERIC_WRITE')
  3276      ^ self openFile:pathName attributes:#(#'GENERIC_READ' #'GENERIC_WRITE')
  3277 !
  3277 !
  3278 
  3278 
  3279 openFileForWrite:pathName
  3279 openFileForWrite:pathName
  3280      ^ self openFile:pathName attributes:#(#'GENERIC_WRITE')
  3280      ^ self openFile:pathName attributes:#(#'GENERIC_WRITE' #'OPEN_EXISTING')
  3281 !
  3281 !
  3282 
  3282 
  3283 recursiveCopyDirectory:sourcePathName to:destination
  3283 recursiveCopyDirectory:sourcePathName to:destination
  3284     "copy the directory named 'sourcePathName' and all contained files/directories to 'destination'.
  3284     "copy the directory named 'sourcePathName' and all contained files/directories to 'destination'.
  3285      Return true if successful."
  3285      Return true if successful."
  8530     libraries...
  8530     libraries...
  8531 "
  8531 "
  8532 !
  8532 !
  8533 
  8533 
  8534 version
  8534 version
  8535     ^ '$Header: /cvs/stx/stx/libbasic/Win32OperatingSystem.st,v 1.103 2001-12-11 18:43:22 cg Exp $'
  8535     ^ '$Header: /cvs/stx/stx/libbasic/Win32OperatingSystem.st,v 1.104 2001-12-11 20:13:22 cg Exp $'
  8536 ! !
  8536 ! !
  8537 
  8537 
  8538 !Win32OperatingSystem::Win32FILEHandle methodsFor:'release'!
  8538 !Win32OperatingSystem::Win32FILEHandle methodsFor:'release'!
  8539 
  8539 
  8540 closeFile
  8540 closeFile
  8557     I represent a generic HANDLE, which can be closed via CloseHandle.
  8557     I represent a generic HANDLE, which can be closed via CloseHandle.
  8558 "
  8558 "
  8559 !
  8559 !
  8560 
  8560 
  8561 version
  8561 version
  8562     ^ '$Header: /cvs/stx/stx/libbasic/Win32OperatingSystem.st,v 1.103 2001-12-11 18:43:22 cg Exp $'
  8562     ^ '$Header: /cvs/stx/stx/libbasic/Win32OperatingSystem.st,v 1.104 2001-12-11 20:13:22 cg Exp $'
  8563 ! !
  8563 ! !
  8564 
  8564 
  8565 !Win32OperatingSystem::Win32Handle methodsFor:'io'!
  8565 !Win32OperatingSystem::Win32Handle methodsFor:'io'!
  8566 
  8566 
  8567 readBytes:count into:aByteBuffer startingAt:firstIndex
  8567 readBytes:count into:aByteBuffer startingAt:firstIndex
  8568     "read count bytes into a byte-buffer;
  8568     "read count bytes into a byte-buffer;
  8569      Return the number of bytes read (negative on error)"
  8569      Return the number of bytes read (negative on error)"
  8570 
  8570 
       
  8571     |errSym errorNumber|
       
  8572 
  8571 %{
  8573 %{
  8572 #if 0
       
  8573     unsigned char *extPtr;
  8574     unsigned char *extPtr;
  8574     int nRead = -1;
  8575     int nRead = -1;
  8575     INT fd = (INT)(__externalAddressVal(self));
  8576     HANDLE hFile = (HANDLE)(__externalAddressVal(self));
  8576     INT cnt, offs;
  8577     DWORD cntWanted, offs, cntRead;
  8577     int nInstBytes, objSize;
  8578     int bufferSize;
  8578 
  8579     OBJ sz;
       
  8580 
       
  8581     if ((hFile == 0) || (hFile == INVALID_HANDLE_VALUE)) {
       
  8582         errSym = @symbol(errorNotOpen);
       
  8583         goto bad;
       
  8584     }
  8579     if (! __bothSmallInteger(count, firstIndex)) {
  8585     if (! __bothSmallInteger(count, firstIndex)) {
       
  8586         errSym = @symbol(badArgument);
  8580         goto bad;
  8587         goto bad;
  8581     }
  8588     }
  8582     cnt = __smallIntegerVal(count);
  8589     cntWanted = __smallIntegerVal(count);
  8583     offs = __smallIntegerVal(firstIndex) - 1;
  8590     offs = __smallIntegerVal(firstIndex) - 1;
  8584 
  8591 
  8585     if (fd < 0) {
  8592     if (! __isExternalBytes(aByteBuffer)) {
       
  8593         errSym = @symbol(badBuffer);
  8586         goto bad;
  8594         goto bad;
  8587     }
  8595     }
  8588     if (__isExternalBytes(aByteBuffer)) {
  8596         
  8589         OBJ sz;
  8597     extPtr = (char *)(__externalBytesAddress(aByteBuffer));
  8590 
  8598     sz = __externalBytesSize(aByteBuffer);
  8591         nInstBytes = 0;
  8599     if (! __isSmallInteger(sz)) {
  8592         extPtr = (char *)(__externalBytesAddress(aByteBuffer));
  8600         errSym = @symbol(badBufferSize);
  8593         sz = __externalBytesSize(aByteBuffer);
  8601         goto bad;
  8594         if (__isSmallInteger(sz)) {
  8602     }
  8595             objSize = __smallIntegerVal(sz);
  8603     bufferSize = __smallIntegerVal(sz);
  8596         } else {
  8604 
  8597             objSize = -1; /* unknown */
  8605     if ((offs >= 0)
       
  8606      && (cntWanted >= 0)
       
  8607      && (bufferSize >= (cntWanted + offs))) {
       
  8608         BOOL ok;
       
  8609 
       
  8610         ok = ReadFile(hFile, extPtr+offs, cntWanted, &cntRead, 0 /* lpOverlapped */);
       
  8611         if (ok == TRUE) {
       
  8612             RETURN (__mkSmallInteger(cntRead));
  8598         }
  8613         }
  8599     } else {
  8614         errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) );
  8600         OBJ oClass;
       
  8601         int nInstVars;
       
  8602 
       
  8603         oClass = __Class(aByteBuffer);
       
  8604         switch (__smallIntegerVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
       
  8605             case BYTEARRAY:
       
  8606             case WORDARRAY:
       
  8607             case LONGARRAY:
       
  8608             case SWORDARRAY:
       
  8609             case SLONGARRAY:
       
  8610             case FLOATARRAY:
       
  8611             case DOUBLEARRAY:
       
  8612                 break;
       
  8613             default:
       
  8614                 goto bad;
       
  8615         }
       
  8616         extPtr = (char *)0;
       
  8617         nInstVars = __smallIntegerVal(__ClassInstPtr(oClass)->c_ninstvars);
       
  8618         nInstBytes = __OBJS2BYTES__(nInstVars);
       
  8619         objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes;
       
  8620     }
       
  8621     if ((offs >= 0)
       
  8622      && (cnt >= 0)
       
  8623      && ((objSize == -1) || (objSize >= (cnt + offs)))) {
       
  8624         nRead = 0;
       
  8625 
       
  8626         do {
       
  8627             int n;
       
  8628 
       
  8629             if (extPtr) {
       
  8630                 n = read(fd, extPtr+offs, cnt);
       
  8631             } else {
       
  8632                 char *bp;
       
  8633 
       
  8634                 /*
       
  8635                  * on interrupt, anObject may be moved to another location.
       
  8636                  * So we recompute the byte-address here.
       
  8637                  */
       
  8638                 bp = __ByteArrayInstPtr(aByteBuffer)->ba_element + nInstBytes;
       
  8639 
       
  8640                 n = read(fd, bp + offs, cnt);
       
  8641             }
       
  8642             if (n > 0) {
       
  8643                 cnt -= n;
       
  8644                 offs += n;
       
  8645                 nRead += n;
       
  8646             } else {
       
  8647                 if (n < 0) {
       
  8648                     if (errno == EINTR) {
       
  8649                         continue;
       
  8650                     }
       
  8651                     break;
       
  8652                 }
       
  8653             }
       
  8654         } while (cnt > 0);
       
  8655 
       
  8656         RETURN (__mkSmallInteger(nRead));
       
  8657     }
  8615     }
  8658 bad: ;   
  8616 bad: ;   
  8659 #endif
       
  8660 %}.
  8617 %}.
  8661     ^ self primitiveFailed
  8618 
       
  8619     errorNumber isNil ifTrue:[
       
  8620         self error:'invalid argument(s): ', errSym.
       
  8621     ] ifFalse:[
       
  8622         (OperatingSystem errorHolderForNumber:errorNumber) reportError
       
  8623     ].
  8662 
  8624 
  8663     "
  8625     "
  8664      |h buff n|
  8626      |h buff n|
  8665 
  8627 
  8666      h := self basicNew.
  8628      h := self basicNew.
  8667      h setFileDescriptor:0.
  8629      buff := ByteArray new:10. 
  8668      buff := ByteArray new:10. buff inspect.
       
  8669      n := h readBytes:10 into:buff startingAt:1.
  8630      n := h readBytes:10 into:buff startingAt:1.
  8670      Transcript show:n; space; showCR:buff.
  8631      Transcript show:n; space; showCR:buff.
  8671     "
  8632     "
  8672 !
  8633 !
  8673 
  8634 
  8674 writeBytes:count from:aByteBuffer startingAt:firstIndex
  8635 writeBytes:count from:aByteBuffer startingAt:firstIndex
  8675     "write count bytes from a byte-buffer;
  8636     "write count bytes from a byte-buffer;
  8676      Return the number of bytes written (negative on error)"
  8637      Return the number of bytes written (negative on error)"
  8677 
  8638 
       
  8639     |errSym errorNumber|
  8678 %{
  8640 %{
  8679 #if 0
       
  8680     unsigned char *extPtr;
  8641     unsigned char *extPtr;
  8681     int nWritten = -1;
  8642     int nWritten = -1;
  8682     INT fd = (INT)(__externalAddressVal(self));
  8643     HANDLE hFile = (HANDLE)(__externalAddressVal(self));
  8683     INT cnt, offs;
  8644     DWORD cntWanted, offs, cntWritten;
  8684     int nInstBytes, objSize;
  8645     int bufferSize;
  8685 
  8646 
       
  8647     if ((hFile == 0) || (hFile == INVALID_HANDLE_VALUE)) {
       
  8648         errSym = @symbol(errorNotOpen);
       
  8649         goto bad;
       
  8650     }
  8686     if (! __bothSmallInteger(count, firstIndex)) {
  8651     if (! __bothSmallInteger(count, firstIndex)) {
       
  8652         errSym = @symbol(badArgument);
  8687         goto bad;
  8653         goto bad;
  8688     }
  8654     }
  8689     cnt = __smallIntegerVal(count);
  8655     cntWanted = __smallIntegerVal(count);
  8690     offs = __smallIntegerVal(firstIndex) - 1;
  8656     offs = __smallIntegerVal(firstIndex) - 1;
  8691 
  8657 
  8692     if (fd < 0) {
  8658     if (! __isExternalBytes(aByteBuffer)) {
       
  8659         errSym = @symbol(badBuffer);
  8693         goto bad;
  8660         goto bad;
  8694     }
  8661     }
  8695     if (__isExternalBytes(aByteBuffer)) {
  8662 
  8696         OBJ sz;
  8663     extPtr = (char *)(__externalBytesAddress(aByteBuffer));
  8697 
  8664     bufferSize = __externalBytesSize(aByteBuffer);
  8698         nInstBytes = 0;
  8665     if (! __isSmallInteger(bufferSize)) {
  8699         extPtr = (char *)(__externalBytesAddress(aByteBuffer));
  8666         errSym = @symbol(badBufferSize);
  8700         sz = __externalBytesSize(aByteBuffer);
  8667         goto bad;
  8701         if (__isSmallInteger(sz)) {
  8668     }
  8702             objSize = __smallIntegerVal(sz);
  8669     bufferSize = __smallIntegerVal(bufferSize);
  8703         } else {
  8670 
  8704             objSize = -1; /* unknown */
  8671     if ((offs >= 0)
       
  8672      && (cntWanted >= 0)
       
  8673      && (bufferSize >= (cntWanted + offs))) {
       
  8674         BOOL ok;
       
  8675 
       
  8676         ok = WriteFile(hFile, extPtr+offs, cntWanted, &cntWritten, 0 /* lpOverlapped */);
       
  8677         if (ok == TRUE) {
       
  8678             RETURN (__mkSmallInteger(cntWritten));
  8705         }
  8679         }
  8706     } else {
  8680         errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) );
  8707         OBJ oClass;
       
  8708         int nInstVars;
       
  8709 
       
  8710         oClass = __Class(aByteBuffer);
       
  8711         switch (__smallIntegerVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
       
  8712             case BYTEARRAY:
       
  8713             case WORDARRAY:
       
  8714             case LONGARRAY:
       
  8715             case SWORDARRAY:
       
  8716             case SLONGARRAY:
       
  8717             case FLOATARRAY:
       
  8718             case DOUBLEARRAY:
       
  8719                 break;
       
  8720             default:
       
  8721                 goto bad;
       
  8722         }
       
  8723         extPtr = (char *)0;
       
  8724         nInstVars = __smallIntegerVal(__ClassInstPtr(oClass)->c_ninstvars);
       
  8725         nInstBytes = __OBJS2BYTES__(nInstVars);
       
  8726         objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes;
       
  8727     }
       
  8728     if ((offs >= 0)
       
  8729      && (cnt >= 0)
       
  8730      && ((objSize == -1) || (objSize >= (cnt + offs)))) {
       
  8731         nWritten = 0;
       
  8732 
       
  8733         do {
       
  8734             int n;
       
  8735 
       
  8736             if (extPtr) {
       
  8737                 n = write(fd, extPtr+offs, cnt);
       
  8738             } else {
       
  8739                 char *bp;
       
  8740 
       
  8741                 /*
       
  8742                  * on interrupt, anObject may be moved to another location.
       
  8743                  * So we recompute the byte-address here.
       
  8744                  */
       
  8745                 bp = __ByteArrayInstPtr(aByteBuffer)->ba_element + nInstBytes;
       
  8746 
       
  8747                 n = write(fd, bp + offs, cnt);
       
  8748             }
       
  8749             if (n > 0) {
       
  8750                 cnt -= n;
       
  8751                 offs += n;
       
  8752                 nWritten += n;
       
  8753             } else {
       
  8754                 if (n < 0) {
       
  8755                     if (errno == EINTR) {
       
  8756                         continue;
       
  8757                     }
       
  8758                     break;
       
  8759                 }
       
  8760             }
       
  8761         } while (cnt > 0);
       
  8762 
       
  8763         RETURN (__mkSmallInteger(nWritten));
       
  8764     }
  8681     }
  8765 bad: ;   
  8682 bad: ;   
  8766 #endif
       
  8767 %}.
  8683 %}.
  8768     ^ self primitiveFailed
  8684     errorNumber isNil ifTrue:[
       
  8685         self error:'invalid argument(s): ', errSym.
       
  8686     ] ifFalse:[
       
  8687         (OperatingSystem errorHolderForNumber:errorNumber) reportError
       
  8688     ].
  8769 
  8689 
  8770     "
  8690     "
  8771      |h buff n|
  8691      |h buff n|
  8772 
  8692 
  8773      h := self basicNew.
  8693      h := self basicNew.
  8799 ! !
  8719 ! !
  8800 
  8720 
  8801 !Win32OperatingSystem class methodsFor:'documentation'!
  8721 !Win32OperatingSystem class methodsFor:'documentation'!
  8802 
  8722 
  8803 version
  8723 version
  8804     ^ '$Header: /cvs/stx/stx/libbasic/Win32OperatingSystem.st,v 1.103 2001-12-11 18:43:22 cg Exp $'
  8724     ^ '$Header: /cvs/stx/stx/libbasic/Win32OperatingSystem.st,v 1.104 2001-12-11 20:13:22 cg Exp $'
  8805 ! !
  8725 ! !
  8806 Win32OperatingSystem initialize!
  8726 Win32OperatingSystem initialize!