Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

EndianFilter.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file EndianFilter.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032         
00033         @version        Initial version; November 2005
00034 
00035         @author         Kris
00036 
00037 
00038 *******************************************************************************/
00039 
00040 module mango.io.EndianFilter;
00041 
00042 private import  mango.io.Buffer,
00043                 mango.io.Conduit;
00044 
00045 private import  mango.sys.ByteSwap;
00046 
00047 
00048 class EndianFilter : ConduitFilter
00049 {
00050         private uint mask;
00051 
00052         private this (uint width)
00053         {
00054                 mask = ~(width - 1);
00055         }
00056 
00057         abstract void swap (void[] x, uint bytes);
00058 
00059         uint reader (void[] dst)
00060         {
00061                 //notify ("byte-swapping input ...\n"c);
00062 
00063                 dst = dst [0..(length & mask)];
00064 
00065                 int ret = next.reader (dst);
00066                 if (ret == Conduit.Eof)
00067                     return ret;
00068 
00069                 int ret1;
00070                 if (ret & ~mask)
00071                     do {
00072                        ret1 = next.reader (dst[ret..ret+1]);
00073                        if (ret1 == Conduit.Eof)
00074                            error ("odd number of bytes!");
00075                        } while (ret1 == 0);
00076 
00077                 swap (dst, ret + ret1);
00078                 return ret;
00079         }
00080 
00081         uint writer (void[] src)
00082         {
00083                 //notify ("byte-swapping output ...\n"c);
00084 
00085                 int     written;
00086                 int     ret, 
00087                         len = src.length & mask;
00088 
00089                 src = src [0..len];
00090                 swap (src, len);
00091 
00092                 do {
00093                    ret = next.writer (src[written..len]);
00094                    if (ret == Conduit.Eof)
00095                        return ret;
00096                    } while ((written += ret) < len);
00097 
00098                 return len;                        
00099         }
00100 }
00101 
00102 class EndianFilter16 : EndianFilter
00103 {
00104         this () {super (2);}
00105 
00106         final void swap (void[] x, uint bytes) {ByteSwap.swap16 (x, bytes);}
00107 }
00108 
00109 class EndianFilter32 : EndianFilter
00110 {
00111         this () {super (4);}
00112 
00113         final void swap (void[] x, uint bytes) {ByteSwap.swap32 (x, bytes);}
00114 }
00115 

Generated on Sat Dec 24 17:28:32 2005 for Mango by  doxygen 1.4.0