dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/samples .cvsignore,NONE,1.1 Makefile


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/samples .cvsignore,NONE,1.1 Makefile.am,NONE,1.1 README,NONE,1.1 codepage.cs,NONE,1.1 except.cs,NONE,1.1 fib.cs,NONE,1.1 getenv.cs,NONE,1.1 hello.cs,NONE,1.1 httpsrv.cs,NONE,1.1 ilrun.sh.in,NONE,1.1 samples.build,NONE,1.1
Date: Sat, 07 Dec 2002 23:30:01 -0500

Update of /cvsroot/dotgnu-pnet/pnetlib/samples
In directory subversions:/tmp/cvs-serv9601/samples

Added Files:
        .cvsignore Makefile.am README codepage.cs except.cs fib.cs 
        getenv.cs hello.cs httpsrv.cs ilrun.sh.in samples.build 
Log Message:


Copy the C# samples from pnet to pnetlib.


--- NEW FILE ---
Makefile
Makefile.in
.deps
*.exe
*.dll
ilrun.sh

--- NEW FILE ---

.PHONY: samples

all-local: samples

samples:
        "$(CSANT)" $(CSANT_FLAGS) -f samples.build all

CLEANFILES = *.exe

--- NEW FILE ---

Use the "ilrun.sh" script to run the samples with the correct paths
set up to find the library dependencies.  Otherwise, you must do a
"make install" on pnet and pnetlib first before running the samples.

--- NEW FILE ---
// This sample prints information about the code pages that are present
// in the system library.  Usage as follows:
//
//    ilrun codepage.exe
//       -- List all code pages using a short one line per page form.
//
//    ilrun codepage.exe -v
//       -- List all code pages using a verbose form.
//
//    ilrun codepage.exe nnn
//       -- Print verbose information about code page "nnn".
//
//    ilrun codepage.exe name
//       -- Print verbose information about web encoding "name".
//
//    ilrun codepage.exe -u nnn
//       -- Dump mappings from Unicode to code page "nnn".
//
//    ilrun codepage.exe -u name
//       -- Dump mappings from Unicode to web encoding "name".

using System;
using System.Text;

class CodePage
{
        // Print information about an encoding.
        private static void PrintEncoding(Encoding enc, bool verbose, bool 
isdef)
        {
                if(verbose)
                {
                        Console.Write("{0}: ", enc.CodePage);
                        Console.Write(enc.EncodingName);
                        if(isdef)
                        {
                                Console.Write(" [default encoding]");
                        }
                        Console.WriteLine();
                        Console.Write("\tBodyName=");
                        Console.WriteLine(enc.BodyName);
                        Console.Write("\tHeaderName=");
                        Console.WriteLine(enc.HeaderName);
                        Console.Write("\tWebName=");
                        Console.WriteLine(enc.WebName);
                        Console.Write("\tIsBrowserDisplay=");
                        Console.WriteLine(enc.IsBrowserDisplay);
                        Console.Write("\tIsBrowserSave=");
                        Console.WriteLine(enc.IsBrowserSave);
                        Console.Write("\tIsMailNewsDisplay=");
                        Console.WriteLine(enc.IsMailNewsDisplay);
                        Console.Write("\tIsMailNewsSave=");
                        Console.WriteLine(enc.IsMailNewsSave);
                        Console.Write("\tWindowsCodePage=");
                        Console.WriteLine(enc.WindowsCodePage);
                }
                else
                {
                        Console.Write("{0,5}: ", enc.CodePage);
                        Console.Write(enc.EncodingName);
                        if(isdef)
                        {
                                Console.Write(" [default encoding]");
                        }
                        Console.WriteLine();
                }
        }

        // Print all code pages in the system.
        private static void PrintAll(bool verbose)
        {
                int page;
                int defaultPage;
                Encoding enc;
                enc = Encoding.Default;
                defaultPage = enc.CodePage;
                for(page = 1; page < 65536; ++page)
                {
                        try
                        {
                                enc = Encoding.GetEncoding(page);
                        }
                        catch(NotSupportedException)
                        {
                                enc = null;
                        }
                        if(enc != null)
                        {
                                PrintEncoding(enc, verbose, (page == 
defaultPage));
                                if(verbose)
                                {
                                        Console.WriteLine();
                                }
                        }
                }
        }

        private static String hexchars = "0123456789abcdef";

        // Dump a value in hexadecimal.
        private static void DumpHex(int value, int numDigits)
        {
                int shift = (numDigits * 4) - 4;
                while(shift >= 0)
                {
                        Console.Write(hexchars[(value >> shift) & 0x0F]);
                        shift -= 4;
                }
        }

        // Names for the first 33 characters of the ASCII character set.
        private static String[] ctrlNames =
                {"NUL ", "SOH ", "STX ", "ETX ", "EOT ", "ENQ ",
                 "ACK ", "BEL ", "BS  ", "HT  ", "LF  ", "VT  ",
                 "FF  ", "CR  ", "SO  ", "SI  ", "DLE ", "DC1 ",
                 "DC2 ", "DC3 ", "DC4 ", "NAK ", "SYN ", "ETB ",
                 "CAN ", "EM  ", "SUB ", "ESC ", "FS  ", "GS  ",
                 "RS  ", "US  ", "SP  "};

        // Dump the 8-bit byte to Unicode character mappings
        // for an encoding.
        private static void Dump8BitMappings(Encoding enc)
        {
                byte[] buf = new byte [1];
                char[] chars = new char [enc.GetMaxCharCount(1)];
                int value, numChars, ch;
                for(value = 0; value < 256; ++value)
                {
                        if((value % 8) == 0)
                        {
                                Console.WriteLine();
                                DumpHex(value, 2);
                                Console.Write(':');
                        }
                        buf[0] = (byte)value;
                        try
                        {
                                numChars = enc.GetChars(buf, 0, 1, chars, 0);
                        }
                        catch(ArgumentException)
                        {
                                numChars = 0;
                        }
                        Console.Write(' ');
                        if(numChars == 1)
                        {
                                ch = chars[0];
                                if(ch <= 0x20)
                                {
                                        Console.Write(ctrlNames[ch]);
                                }
                                else if(ch < 0x7F)
                                {
                                        if(ch != '\'')
                                        {
                                                Console.Write('\'');
                                                Console.Write((char)ch);
                                                Console.Write('\'');
                                                Console.Write(' ');
                                        }
                                        else
                                        {
                                                Console.Write("\"'\" ");
                                        }
                                }
                                else if(ch == 0x7F)
                                {
                                        Console.Write("DEL ");
                                }
                                else
                                {
                                        DumpHex(ch, 4);
                                }
                        }
                        else
                        {
                                Console.Write("????");
                        }
                        if((value % 4) == 3)
                        {
                                Console.Write(' ');
                        }
                }
                Console.WriteLine();
        }

        // Dump the Unicode character mappings for an encoding.
        private static void DumpEncoding(Encoding enc)
        {
                byte[] buf = new byte [enc.GetMaxByteCount(1)];
                char[] chars = new char [1];
                int value, numBytes, index;
                for(value = 0; value < 65536; ++value)
                {
                        chars[0] = (char)value;
                        try
                        {
                                numBytes = enc.GetBytes(chars, 0, 1, buf, 0);
                        }
                        catch(ArgumentException)
                        {
                                numBytes = 0;
                        }
                        if(numBytes > 0 &&
                           (numBytes != 1 || buf[0] != (byte)'?' || value == 
(int)'?'))
                        {
                                DumpHex(value, 4);
                                Console.Write(':');
                                for(index = 0; index < numBytes; ++index)
                                {
                                        Console.Write(' ');
                                        DumpHex(buf[index], 2);
                                }
                                Console.WriteLine();
                        }
                }
        }

        // Print information about a specific code page.
        private static void PrintPage(int page)
        {
                Encoding enc;
                try
                {
                        enc = Encoding.GetEncoding(page);
                }
                catch(NotSupportedException)
                {
                        enc = null;
                }
                if(enc != null)
                {
                        PrintEncoding(enc, true, false);
                        Dump8BitMappings(enc);
                }
                else
                {
                        Console.Write(page);
                        Console.WriteLine(": unknown code page");
                }
        }

        // Print information about a specific web encoding.
        private static void PrintWebEncoding(String name)
        {
                Encoding enc;
                try
                {
                        enc = Encoding.GetEncoding(name);
                }
                catch(NotSupportedException)
                {
                        enc = null;
                }
                if(enc != null)
                {
                        PrintEncoding(enc, true, false);
                        Dump8BitMappings(enc);
                }
                else
                {
                        Console.Write(name);
                        Console.WriteLine(": unknown encoding name");
                }
        }

        // Dump Unicodoe mappings for a specific code page.
        private static void DumpPage(int page)
        {
                Encoding enc;
                try
                {
                        enc = Encoding.GetEncoding(page);
                }
                catch(NotSupportedException)
                {
                        enc = null;
                }
                if(enc != null)
                {
                        DumpEncoding(enc);
                }
                else
                {
                        Console.Write(page);
                        Console.WriteLine(": unknown code page");
                }
        }

        // Dump Unicode mappings for a specific web encoding.
        private static void DumpWebEncoding(String name)
        {
                Encoding enc;
                try
                {
                        enc = Encoding.GetEncoding(name);
                }
                catch(NotSupportedException)
                {
                        enc = null;
                }
                if(enc != null)
                {
                        DumpEncoding(enc);
                }
                else
                {
                        Console.Write(name);
                        Console.WriteLine(": unknown encoding name");
                }
        }

        public static void Main(String[] args)
        {
                if(args.Length > 0)
                {
                        if(args[0] == "-v")
                        {
                                PrintAll(true);
                        }
                        else if(args[0] == "-u" && args.Length > 1)
                        {
                                if(args[1][0] >= '0' && args[1][0] <= '9')
                                {
                                        DumpPage(Int32.Parse(args[1]));
                                }
                                else
                                {
                                        DumpWebEncoding(args[1]);
                                }
                        }
                        else if(args[0][0] >= '0' && args[0][0] <= '9')
                        {
                                PrintPage(Int32.Parse(args[0]));
                        }
                        else
                        {
                                PrintWebEncoding(args[0]);
                        }
                }
                else
                {
                        PrintAll(false);
                }
        }
}

--- NEW FILE ---
// This example demonstrates catching exceptions and then
// reporting the complete stack trace of the throw point.

using System;

class Hello
{
        public static void ThrowUp(int x)
        {
                throw new Exception();
        }

        public static int ThrowUpDivZero(int x)
        {
                return 3 / x;
        }

        public static void Main()
        {
                Console.WriteLine("Testing user-created exception:");
                Console.WriteLine();
                try
                {
                        ThrowUp(0);
                }
                catch(Exception e)
                {
                        Console.Write("Caught: ");
                        Console.WriteLine(e.ToString());
                }
                Console.WriteLine("Testing system-created exception:");
                Console.WriteLine();
                try
                {
                        ThrowUpDivZero(0);
                }
                catch(Exception e)
                {
                        Console.Write("Caught: ");
                        Console.WriteLine(e.ToString());
                }
        }
}

--- NEW FILE ---

using System;

public class Fib
{
        // Get the n'th Fibonacci number by iteration,
        // where fib(0) = 0, fib(1) = 1.
        public static uint fib(uint n)
        {
                uint a = 0;
                uint b = 1;
                uint temp;
                if(n == 0)
                {
                        return 0;
                }
                while(n > 0)
                {
                        temp = a + b;
                        a = b;
                        b = temp;
                        --n;
                }
                return b;
        }

        // Program entry point.
        public static void Main()
        {
                uint n;
                for(n = 1; n <= 10; ++n)
                {
                        Console.Write(fib(n));
                        Console.Write(" ");
                }
                Console.WriteLine();
        }
}

--- NEW FILE ---
// An example that demonstrates access to environment variables.
//
// "getenv" with no command-line arguments will print the values
// of all environment variables.
//
// "getenv" with an argument will print the value of just that
// environment variable.
//
// "getenv" with two arguments, where the first is "-d", will
// access environment variables by way of a dictionary indexer.

using System;
using System.Collections;

public class getenv
{
        public static void Main(String[] args)
        {
                String value;
                IDictionary vars;

                if(args.Length == 2 && args[0] == "-d")
                {
                        // Access the value through a dictionary indexer.
                        vars = Environment.GetEnvironmentVariables();
                        value = (String)(vars[args[1]]);
                        if(value != null)
                        {
                                Console.WriteLine(value);
                        }
                        else
                        {
                                Console.Write(args[1]);
                                Console.WriteLine(" does not exist in the 
environment");
                        }
                }
                else if(args.Length == 1)
                {
                        // Access the value through "GetEnvironmentVariable".
                        value = Environment.GetEnvironmentVariable(args[0]);
                        if(value != null)
                        {
                                Console.WriteLine(value);
                        }
                        else
                        {
                                Console.Write(args[0]);
                                Console.WriteLine(" does not exist in the 
environment");
                        }
                }
                else
                {
                        // Dump the contents of all environment variables.
                        vars = Environment.GetEnvironmentVariables();
                        IDictionaryEnumerator e = vars.GetEnumerator();
                        while(e.MoveNext())
                        {
                                Console.Write((String)(e.Key));
                                Console.Write("=");
                                Console.WriteLine((String)(e.Value));
                        }
                }
        }
}

--- NEW FILE ---

using System;

public class Hello
{
        public static void Main(String[] args)
        {
                if(args.Length == 0)
                {
                        Console.WriteLine("Hello World!");
                }
                else
                {
                        Console.Write("Hello");
                        foreach(String value in args)
                        {
                                Console.Write(' ');
                                Console.Write(value);
                        }
                        Console.WriteLine("!");
                }
        }
}

--- NEW FILE ---

using System;
using System.IO;
using System.Net.Sockets;
using System.Net;
class SocketListener
{
        public static byte[] GetBytes(String msg)
        {
                byte[] b=new byte[msg.Length];
                for(int i=0;i<msg.Length;i++)
                {
                        b[i]=(byte)msg[i];
                }
                return b;
        }
        public static void Main(String []argv)
        {
                IPAddress ip = IPAddress.Loopback;
                Int32 port=1800;
                if(argv.Length>0)
                        port = Int32.Parse(argv[0]);    
                IPEndPoint ep = new IPEndPoint(ip,port);
                Socket ss = new Socket(AddressFamily.InterNetwork , 
                        SocketType.Stream , ProtocolType.Tcp);
                try
                {
                        ss.Bind(ep);
                }
                catch(SocketException err)
                {
                        Console.WriteLine("** Error : socket already in use 
:"+err);
                        Console.WriteLine("           Please wait a few secs & 
try");   
                }
                ss.Listen(-1);
                Console.WriteLine("Server started and running on port 
{0}.....",port);
                Console.WriteLine("Access URL http://localhost:{0}",port);
                Socket client = null;
                while(true)
                {
                        client=ss.Accept();
                        SocketMessenger sm=new SocketMessenger(client);
                        sm.Start();
                }
                Console.WriteLine(client.LocalEndPoint.ToString()+" CONNECTED 
");
                ss.Close();
        }
}

class SocketMessenger
{
        private String index_file="httpsrv.cs";
        public SocketMessenger(Socket client)
        {
                this.client=client;
        }
        public void Start()
        {
                byte[] buffer=new byte[300];
                int read=client.Receive(buffer,0,300,SocketFlags.None);
                this.request=GetString(buffer);
                if(read>0)
                {
                        String 
fname=this.request.Substring(this.request.IndexOf("/"));
                        fname=fname.Substring(0,fname.IndexOf(" HTTP/1"));
                        if(fname=="/")
                                fname="/"+index_file;

                        fname="."+fname;
                        SendFile(fname);
                }
                client.Shutdown(SocketShutdown.Both);
                client.Close();
        }
        
        public void SendFile(String fname)
        {
                const String CRLF="\r\n";
                String header="HTTP/1.0 200 OK"+CRLF+"Keep-Alive: 
Close"+CRLF+CRLF;
                Console.WriteLine(fname);
                FileStream fin=new FileStream(fname,FileMode.Open,
                        FileAccess.Read);
                client.Send(GetBytes(header));
                byte []buffer=new byte[1024];
                int read=0;
                read=fin.Read(buffer,0,1024);
                while(read>0)
                {
                        client.Send(buffer,0,read,SocketFlags.None);
                        read=fin.Read(buffer,0,1024);
                }       
        }
        public static String GetString(byte[] msg)
        {
                char[] b=new char[msg.Length];
                for(int i=0;i<msg.Length;i++)
                {
                        b[i]=(char)msg[i];
                }
                return new String(b);
        }
        public static byte[] GetBytes(String msg)
        {
                byte[] b=new byte[msg.Length];
                for(int i=0;i<msg.Length;i++)
                {
                        b[i]=(byte)msg[i];
                }
                return b;
        }
        Socket client;
        String request;
}

--- NEW FILE ---
#!/bin/sh
#
# ilrun.sh - Run samples using "ilrun" and the correct paths.
#
# Copyright (C) 2002  Southern Storm Software, Pty Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Get the incoming configuration variables.
ILRUN="@ILRUN@"

# Run the engine, with the assembly paths set correct.
exec "$ILRUN" -L../runtime -L../I18N -L../System -L../System.Xml $*

--- NEW FILE ---
<?xml version="1.0"?>
<project name="samples" default="all">
        <target name="all">

                <!-- Build the codepage.exe program -->
                <compile output="codepage.exe"
                                 target="exe"
                                 unsafe="true"
                                 nostdlib="true"
                                 optimize="true"
                                 debug="true">

                        <sources>
                                <file name="codepage.cs"/>
                        </sources>

                        <references>
                                <file name="../runtime/mscorlib.dll"/>
                        </references>

                        <arg compiler="cscc" value="-Wno-empty-input"/>
                        <arg compiler="csc" value="/nowarn:626"/>
                        <arg compiler="csc" value="/nowarn:649"/>
                        <arg compiler="csc" value="/nowarn:168"/>
                        <arg compiler="csc" value="/nowarn:67"/>
                        <arg compiler="csc" value="/nowarn:169"/>
                </compile>

                <!-- Build the except.exe program -->
                <compile output="except.exe"
                                 target="exe"
                                 unsafe="true"
                                 nostdlib="true"
                                 optimize="true"
                                 debug="true">

                        <sources>
                                <file name="except.cs"/>
                        </sources>

                        <references>
                                <file name="../runtime/mscorlib.dll"/>
                        </references>

                        <arg compiler="cscc" value="-Wno-empty-input"/>
                        <arg compiler="csc" value="/nowarn:626"/>
                        <arg compiler="csc" value="/nowarn:649"/>
                        <arg compiler="csc" value="/nowarn:168"/>
                        <arg compiler="csc" value="/nowarn:67"/>
                        <arg compiler="csc" value="/nowarn:169"/>
                </compile>

                <!-- Build the fib.exe program -->
                <compile output="fib.exe"
                                 target="exe"
                                 unsafe="true"
                                 nostdlib="true"
                                 optimize="true"
                                 debug="true">

                        <sources>
                                <file name="fib.cs"/>
                        </sources>

                        <references>
                                <file name="../runtime/mscorlib.dll"/>
                        </references>

                        <arg compiler="cscc" value="-Wno-empty-input"/>
                        <arg compiler="csc" value="/nowarn:626"/>
                        <arg compiler="csc" value="/nowarn:649"/>
                        <arg compiler="csc" value="/nowarn:168"/>
                        <arg compiler="csc" value="/nowarn:67"/>
                        <arg compiler="csc" value="/nowarn:169"/>
                </compile>

                <!-- Build the getenv.exe program -->
                <compile output="getenv.exe"
                                 target="exe"
                                 unsafe="true"
                                 nostdlib="true"
                                 optimize="true"
                                 debug="true">

                        <sources>
                                <file name="getenv.cs"/>
                        </sources>

                        <references>
                                <file name="../runtime/mscorlib.dll"/>
                        </references>

                        <arg compiler="cscc" value="-Wno-empty-input"/>
                        <arg compiler="csc" value="/nowarn:626"/>
                        <arg compiler="csc" value="/nowarn:649"/>
                        <arg compiler="csc" value="/nowarn:168"/>
                        <arg compiler="csc" value="/nowarn:67"/>
                        <arg compiler="csc" value="/nowarn:169"/>
                </compile>

                <!-- Build the hello.exe program -->
                <compile output="hello.exe"
                                 target="exe"
                                 unsafe="true"
                                 nostdlib="true"
                                 optimize="true"
                                 debug="true">

                        <sources>
                                <file name="hello.cs"/>
                        </sources>

                        <references>
                                <file name="../runtime/mscorlib.dll"/>
                        </references>

                        <arg compiler="cscc" value="-Wno-empty-input"/>
                        <arg compiler="csc" value="/nowarn:626"/>
                        <arg compiler="csc" value="/nowarn:649"/>
                        <arg compiler="csc" value="/nowarn:168"/>
                        <arg compiler="csc" value="/nowarn:67"/>
                        <arg compiler="csc" value="/nowarn:169"/>
                </compile>

                <!-- Build the httpsrv.exe program -->
                <compile output="httpsrv.exe"
                                 target="exe"
                                 unsafe="true"
                                 nostdlib="true"
                                 optimize="true"
                                 debug="true">

                        <sources>
                                <file name="httpsrv.cs"/>
                        </sources>

                        <references>
                                <file name="../System/System.dll"/>
                                <file name="../runtime/mscorlib.dll"/>
                        </references>

                        <arg compiler="cscc" value="-Wno-empty-input"/>
                        <arg compiler="csc" value="/nowarn:626"/>
                        <arg compiler="csc" value="/nowarn:649"/>
                        <arg compiler="csc" value="/nowarn:168"/>
                        <arg compiler="csc" value="/nowarn:67"/>
                        <arg compiler="csc" value="/nowarn:169"/>
                </compile>

        </target>
</project>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]