001    package org.bridj;
002    
003    import java.io.FileNotFoundException;
004    import org.bridj.ann.*;
005    
006    //import static org.bridj.LastError.Windows.*;
007    //import static org.bridj.LastError.Unix.*;
008    import static org.bridj.Pointer.*;
009    
010    /**
011     * Native error that correspond to the <a href="http://en.wikipedia.org/wiki/Errno.h">errno</a> or <a href="http://msdn.microsoft.com/en-us/library/ms679360(v=vs.85).aspx">GetLastError()</a> mechanism.<br>
012     * Some C functions declare errors by marking an error code in <a href="http://en.wikipedia.org/wiki/Errno.h">errno</a> or through <a href="http://msdn.microsoft.com/en-us/library/ms680627(v=vs.85).aspx">SetLastError(int)</a>.<br> 
013     * If you want their corresponding bindings to throw an exception whenever such an error was marked, simply make them throw this exception explicitly.<br>
014     * On Windows, BridJ will first check <a href="http://msdn.microsoft.com/en-us/library/ms679360(v=vs.85).aspx">GetLastError()</a>, then if no error was found it will check <a href="http://en.wikipedia.org/wiki/Errno.h">errno</a> (on the other platforms only <a href="http://en.wikipedia.org/wiki/Errno.h">errno</a> is available).<br>
015     * For instance, look at the following binding of the C-library <a href="http://www.cplusplus.com/reference/clibrary/cstdlib/strtoul/">strtoul</a> function :
016     * <pre>
017     * &#064;Library("c")
018     * {@code
019     * public static native long strtoul(Pointer<Byte> str, Pointer<Pointer<Byte>> endptr, int base) throws LastError;
020     * }</pre> 
021     * @author Olivier Chafik
022     */
023    public class LastError extends NativeError {
024        final int code;
025        final String description;
026        
027        LastError(int code, String description) {
028                    super((description == null ? "?" : description.trim()) + " (error code = " + code + ")");//toString(code));
029                    this.code = code;
030                this.description = description;
031                if (BridJ.verbose)
032                    BridJ.info("Last error detected : " + getMessage());
033        }
034    
035        /**
036         * Native error code (as returned by <a href="http://en.wikipedia.org/wiki/Errno.h">errno</a> or <a href="http://msdn.microsoft.com/en-us/library/ms679360(v=vs.85).aspx">GetLastError()</a>).
037         */
038        public int getCode() {
039            return code;
040        }
041    
042        /**
043         * Native error description (as returned by <a href="http://www.cplusplus.com/reference/clibrary/cstring/strerror/">strerror</a> or <a href="http://msdn.microsoft.com/en-us/library/ms680582(v=vs.85).aspx">FormatMessage</a>).
044         */
045        public String getDescription() {
046            return description;
047        }
048        
049        static void throwNewInstance(int code, String description) {
050            if (code == 0)
051                return;
052            
053            throw new LastError(code, description);
054        }
055    }