;; SS:ReadBinaryStream V2.0
;;
;; function : Read binarystream from file(full path) by given charset , return a List or a String .
;;
;; Args:
;; file -- file name with full path , The file size must be less than amount of memory can be used for the current AUTOCAD .
;;
;; char -- charset name , it must register in your system , see the Windows registry sub-key in \\HKEY_CLASSES_ROOT\\MIME\\Database\\Charset
;; such as "unicode","ascii","us-ascii","chinese","GB2132", etc.
;; nil & "Unicode" , allways return a list like by vlisp function Read-char .
;; Oterwise , the char will be used to convert the stream into text string .
;;
;; pos -- position for start read byte , first start pos is 0 , second is 1,
;; for Bigfont-Char it maybe use two bytes , if so the second text start pos is 2 , 4 ... 2n .
;; len -- Numbits for read or readtext , if nil or beyond the size , will be set to suit Maximum value .
;;
;; Returns -- if char is nil or "Unicode" , returns a ascii code list ;after Acad2008 , The system expansion Charset beyond the 256 limit ,
;; so it may return number like 18714 or -18918(after Acad2013) ,for this case , you can use (rem 18714 256)
;; or (rem (+ 256(rem -18714 256)) 256) to get the ascii code like old version .
;; if char is not nil and "Unicode" , and it's supported in your window system , returns a string .
;;
;; by GSLS(SS) 10.23-2011 ~
;;--------------------------------------------------------------------------;;
;; Refrence :
;; adodb.stream from http://baike.baidu.com/link?url=HkCWIQo1pSmTMN76tTipNkSUR5O_s7_8ctpH_XXuo06y2qhd-TCAkCCuxDOtNi55zA5V9eTPO4fobMbeYDNjHK
;; Microsoft Data Access Components
;; from http://en.wikipedia.org/wiki/Microsoft_Data_Access_Components
;; Nonsmall's from http://bbs.mjtd.com/forum.php?mod=viewthread&tid=78782&extra=page%3D2%26filter%3Dtypeid%26typeid%3D110&page=1
;; Michale's from http://www.theswamp.org/index.php?topic=17465.0
;; LeeMac's from http://www.theswamp.org/index.php?topic=39814.0
;; Highflybird's from http://www.theswamp.org/index.php?topic=36656.0
;; this vbs can't run in my used case : ACAD2011 Win7 64Bit.
;;--------------------------------------------------------------------------;;
;; Method of Adodb.Stream object . ;;
;; Open , Close , Write , WriteText , Read , ReadText, ;;
;; Flush , CopyTo , Cancel , SkipLine, SetEOS ;;
;; Property of Adodb.Stream object . ;;
;; LineSeparator, SaveToFile, LoadFromFile , Charset , ;;
;; Size , Position , Type , State , Mode , EOS , ;;
;;--------------------------------------------------------------------------;;
;; e.g. (SS:ReadBinaryStream (findfile "gslsshp.shx") "us-ascii" 23 32) -->
;; "\032\010\000\010\000\001\000\010\000\022\000HELL\000\r\031^d\035z6q(`\000\000\000EOF"
;; (SS:ReadBinaryStream (findfile "gslsshp.shx") "Unicode" 23 4)--> '(18714 18824 18688 18824) '(-18918 -18808 -18944 -18808)--of ACAD2015
;; (SS:ReadBinaryStream (findfile "gslsshp.shx") nil 23 4)--> '(18714 18824 18688 18824)
;; (SS:ReadBinaryStream (findfile "gslsshp.shx") "ascii" 23 4)-->"\032\010\000\010"
(defun SS:ReadBinaryStream
(file char pos len / adostream ret size str)
;_(setq file "gslsshp.shx" file (findfile file) char "us-ascii" pos 0 len nil)
;_(setq file "gslsshp.shx" file (findfile file) char nil pos 23 len nil)
(if (and (findfile file
) ;_check file exists , No rebuild file name by (findfile file) , because it maybe not you want truely . )
;_Type -- The Type property is to read / write only when the current postion located
;_ at the beginning (Position 0) of stream , at other location is read-only.
;_ The default value is adTypeText. However, if the binary data was originally
;_ written in a new empty Stream, Type will be changed to adTypeBinary.
;_ *** adTypeBinary =1 adTypeText =2 ***
;_Open -- Stream.Open Source, Mode, OpenOptions, UserName, Password
;_ Source -- Optional. Variant value that specifies Stream data source. Source may contain
;_ an absolute URL string that points to the existing nodes of the well-known tree structure
;_ (such as email or file system). Use URL keyword ("URL = http://server/folder") to specify the URL.
;_ In addition, Source may also contain open the Record object reference to the object to open the Record
;_ associated with the default stream. If not specified Source, Stream will be instantiated and open,
;_ by default it is not associated with the underlying source.
;_
;_ Mode -- Optional. ConnectModeEnum value that specifies Access Mode (for example, read / write or read-only) of the gotten Stream .
;_ The default value is adModeUnknown. If Mode is not specified, it is inherited source Stream Variant.
;_ E.G, if you open the source Record in read-only mode, then Stream will also be open by read-only mode in default case .
;_ *** adModeRead =1 adModeReadWrite =3 adModeRecursive =4194304 adModeShareDenyNone =16 adModeShareDenyRead =4
;_ adModeShareDenyWrite =8 adModeShareExclusive =12 adModeUnknown =0 adModeWrite =2 ***
;_
;_ OpenOptions -- Optional. StreamOpenOptionsEnum value that's default value is -1 .
;_ *** adOpenStreamAsync =1 adOpenStreamFromRecord =4 adOpenStreamUnspecified=-1 ***
;_ UserName -- Optional. String value that contains (when necessary) the identity of the user to access a Stream object.
;_ Password -- Optional. String value that contains (when required) to access a Stream object password.
adostream
file)
;_LoadFromFile -- Load the contents of an existing file into the Stream, or upload the contents of the lacal file to the server .
;_ FileName can contain any UNC format valid path and name . If the specified file does not exist, a runtime error will occur.
;_ Stream object must be open before calling LoadFromFile . This method does not change the binding Stream object ,
;_ it is still bound to the object specified by URL of the original open Stream . LoadFromFile overwrite the current
;_ contents of the Stream object with the data read from the file .
(if (and (numberp pos
) (< 0 pos size
));_check pos(position) suitable (if (and (numberp len
) (< 0 len size
));_check len(length) suitable ;_Position -- For read-only Stream object, If the Position value you set exceed the Size of Stream ,
;_ ADO will not return an error. This does not change the size of the Stream,
;_ Stream will not change the content in any way. However, you should avoid this operation
;_ because it may produce meaningless Position value.
adostream
(quote Write
) ;_overite stream contents by binary though Unicode Charset from memory . ;_Write -- Write the binary data to Stream object . method : Stream.Write Buffer
;_ Buffer -- Variant, contains a bytes safearray to be written.
;_ The specified bytes written to Stream object, are no space between eachother .
;_ Position is set to 1+ byte of the writen data . Write method does not cut off the flow of the remaining data.
;_ If you want to cut these bytes, call SetEOS.
;_ If you write more than the current EOS position, Stream's Size will increase to include the new byte,
;_ EOS will also be moved to the last byte of new Stream .
adostream
;_ Read -- Reads the specified number of bytes or whole from the Stream object and return the Variant .
;_ specified number of bytes read from binary Stream object . Variant = Stream.Read ( NumBytes )
;_ NumBytes -- optional. default value is adReadAll .
len)) ;_read binary
))) ;_end new stream
(null char
);_check charset (eq (strcase char
) "UNICODE"));_equal default char ;_(setq char "us-ascii")
"HKEY_CLASSES_ROOT\\MIME\\Database\\Charset")) ;_check whether character set is supported in system .
)) ;_dermine return ascii list or string
nil ;_no rebuild ret .
;_ Charset -- Specifies the character set used to convert the text contents of Stream .
;_ Such as , "Unicode" , "ascii" , "iso-8859-1" ,"Windows-1252",etc. .
;_ For systems supported character set , see the Windows registry sub-key in \\HKEY_CLASSES_ROOT\\MIME\\Database\\Charset .
;_
;_ For text Stream object , the text object is stored as Unicode. The data read from the Stream is converted by the character
;_ set of the specified Charset property. Similarly, to write the data into the Stream object, the character set is specified to Unicode .
;_
;_ For open Stream, it's current Position shall be at the beginning of located Stream (0) , so as to set Charset.
;_ Charset only used with text Stream object (Type of adTypeText) together. If Type is adTypeBinary, this property is ignored.
adostream
len) ;_ReadText
;_ ReadText -- Reads the specified number of characters from a text Stream object . Method : Stream.ReadText ( NumChars ) .
;_ NumChars is Optional , Default value is adReadAll , If NumChar over the remaining number of characters in the stream,
;_ will only return the remaining characters; Sring read is not consistent with the NumChar filled to a specified length;
;_ If there is no remaining characters can be read, it will return a value of Null variant.
;_ ***ReadText not be used to read backwards.
;_ ReadText method used with text stream (Type of adTypeText=2) . For a binary stream, please use Read.
))))
;_ Close -- Close any open objects and related objects.
;_ Use the Close method to close the Connection, Record, Recordset or Stream object to release any system resources
;_ associated with it. Close object does not remove it from memory; then you can change its property settings and
;_ turn it on again. Completely remove an object from memory, please set object variable to Nothing (Visual Basic)
;_ or nil (Visual Lisp) after close object .
ret))))
ret ;_by ReadText
));_catch error of Null SQL Variant .
ret
) ;_returns
);_if , end result deal
);_progn
);_if
);_defun