quinta-feira, 26 de fevereiro de 2009

Swat - Community Ubuntu Documentation

From the swat man page: swat allows a Samba administrator to configure the complex smb.conf file via a Web browser. In addition, a swat configuration page has help links to all the configurable options in the smb.conf file allowing an administrator to easily look up the effects of any change.

Installing Swat

  1. sudo apt-get install swat xinetd

  2. sudo nano /etc/xinetd.d/swat

  3. Insert the following text (borrowed from http://www.samba.org/samba/docs/man/Samba-HOWTO-Collection/SWAT.html#xinetd):

# default: off
# description: SWAT is the Samba Web Admin Tool. Use swat \
# to configure your Samba server. To use SWAT, \
# connect to port 901 with your favorite web browser.
service swat
{
port = 901
socket_type = stream
wait = no
# Use only_from if you want to restrict access
# only_from = localhost
user = root
server = /usr/sbin/swat
log_on_failure += USERID
disable = no
}
  1. Exit and Save

Running Swat

  1. sudo /etc/init.d/xinetd restart

  2. Point your browser to http://localhost:901/

  3. Enter the username and password of a user with proper privileges

Questions

Q: my feisty system doesn't have /etc/xinetd.d/, now what?

A: you will need to install a dependency beforehand: xinetd

sudo apt-get install xinetd
sudo update-inetd --enable 'swat'

then create the /etc/xinetd.d/swat as above, and now sudo dpkg-reconfigure xinetd to restart with the new configuration.

kudos to fabioleitao for the answer, http://ubuntuforums.org/showpost.php?p=980625&postcount=8

Q: The swat help links do not work. How do I tell swat where to find the man pages?

A: You don’t have to tell swat where they are you have to install them.

sudo apt-get install samba-doc

Q: When I open the web page I only see four boxes (Home, Status, View, Password) but none of these boxes give me the ability to configure Samba. What should I do?

A: You do not have the necessary permissions. You will need to ensure you are a user of the administration group (‘admin’) and that the adm group has sufficient access rights to the Samba configuration file (‘smb.conf’). Note the user created during the installation is automatically a member of the adm group.

To ensure the adm group has proper permissions over ‘smb.conf’ use ‘chmod’ and ‘chgrp’ tools to change the file access permissions and group permissions respectively:

sudo chmod g+w /etc/samba/smb.conf
sudo chgrp adm /etc/samba/smb.conf

Another method is to grant all users—the whole world essentially—complete access to ‘smb.conf’. This is not recommended for obvious security reasons.

sudo chmod 777 /etc/samba/smb.conf

Now refresh your browser window and you should see additional boxes for Globals, Shares, Printers and Wizard.

Q: On my 6.06 LTS server clients don't see the swat page. It is just a blank page. What now?

A: Edit the /etc/samba/smb.conf file so that the file contains a line for allowed hosts like:

sudo vi /etc/samba/smb.conf 

or, if you are not that comfortable with vi:

sudo nano /etc/samba/smb.conf

Add or change the following line:

[global]
......
.......
hosts allow = 192.168.1.0/255.255.255.0

[printers]

Of course this has to match your own network settings. After this you have to restart the samba subsystem.

sudo /etc/init.d/samba restart

terça-feira, 17 de fevereiro de 2009

Format an enumeration


/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/



// Format an enumeration.

using System;

public class EnumFmtDemo {
enum Direction { North, South, East, West }
[Flags] enum Status { Ready=0x1, OffLine=0x2,
Waiting=0x4, TransmitOK=0x8,
RecieveOK=

0x10, OnLine=0x20 }

public static void Main() {
Direction d = Direction.West;

Console.WriteLine("{0:G}", d);
Console.WriteLine("{0:F}", d);
Console.W

riteLine("{0:D}", d);
Console.WriteLine("{0:X}", d);

Status s = Status.Ready | Status.TransmitOK;

Console.W

riteLine("{0:G}", s);
Console.WriteLine("{0:F}", s);
Console.WriteLine("{0:D}", s);
Console.WriteLine("{0:X}", s);
}
}





Use String.Format() to format a value



/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Use String.Format() to format a value.

using System;

public class FormatDemo1 {
public static void Main() {
double v = 17688.65849;
double v2 = 0.15;
int x = 21;

string str = String.Format("{0:F2}", v);
Console.WriteLine(str);

str = String.Format("{0:N5}", v);
Console.WriteLine(str);

str = String.Format("{0:e}", v);
Console.WriteLine(str);

str = String.Format("{0:r}", v);
Console.WriteLine(str);

str = String.Format("{0:p}", v2);
Console.WriteLine(str);

str = String.Format("{0:X}", x);
Console.WriteLine(str);

str = String.Format("{0:D12}", x);
Console.WriteLine(str);

str = String.Format("{0:C}", 189.99);
Console.WriteLine(str);
}
}


A closer look at Format()



/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// A closer look at Format().

using System;

public class FormatDemo2 {
public static void Main() {
int i;
int sum = 0;
int prod = 1;
string str;

/* Display the running sum and product
for the numbers 1 through 10. */
for(i=1; i <= 10; i++) {
sum += i;
prod *= i;
str = String.Format("Sum:{0,3:D} Product:{1,8:D}",
sum, prod);
Console.WriteLine(str);
}
}
}


Use ToString() to format values



/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Use ToString() to format values.

using System;

public class ToStringDemo {
public static void Main() {
double v = 17688.65849;
double v2 = 0.15;
int x = 21;

string str = v.ToString("F2");
Console.WriteLine(str);

str = v.ToString("N5");
Console.WriteLine(str);

str = v.ToString("e");
Console.WriteLine(str);

str = v.ToString("r");
Console.WriteLine(str);

str = v2.ToString("p");
Console.WriteLine(str);

str = x.ToString("X");
Console.WriteLine(str);

str = x.ToString("D12");
Console.WriteLine(str);

str = 189.99.ToString("C");
Console.WriteLine(str);
}
}


Numeric Formatting:Custom Format Strings:Decimal Point


using System;

public class DecimalPoint
{
public static void Main()
{
Console.WriteLine("{0:#####.000}", 75928.3);
Console.WriteLine("{0:##.000}", 1456.456456);
}
}


Numeric Formatting:Custom Format Strings:Digit or Space Placeholder



using System;

public class DigitorSpacePlaceholder
{
public static void Main()
{
Console.WriteLine("{0:#####}", 255);
Console.WriteLine("{0:#####}", 1456);
Console.WriteLine("{0:###}", 32767);
}
}


Numeric Formatting:Custom Format Strings:Digit or Zero Placeholder



using System;

public class DigitorZeroPlaceholder
{
public static void Main()
{
Console.WriteLine("{0:000}", 55);
Console.WriteLine("{0:000}", 1456);
}
}


Numeric Formatting:Custom Format Strings:Escapes and Literals



using System;

public class EscapesandLiterals
{
public static void Main()
{
Console.WriteLine("{0:###\\#}", 255);
Console.WriteLine(@"{0:###\#}", 255);
Console.WriteLine("{0:###'#0%;'}", 1456);
}
}



Numeric Formatting:Custom Format Strings:Exponential Notation



using System;

public class ExponentialNotation
{
public static void Main()
{
Console.WriteLine("{0:###.000E-00}", 3.1415533E+04);
Console.WriteLine("{0:#.0000000E+000}", 2.553939939E+101);
}
}


Numeric Formatting:Custom Format Strings:Group Separator



using System;

public class GroupSeparator
{
public static void Main()
{
Console.WriteLine("{0:##,###}", 2555634323);
Console.WriteLine("{0:##,000.000}", 14563553.593993);
Console.WriteLine("{0:#,#.000}", 14563553.593993);
}
}


Numeric Formatting:Custom Format Strings:Number Prescaler



using System;

public class NumberPrescaler
{
public static void Main()
{
Console.WriteLine("{0:000,.##}", 158847);
Console.WriteLine("{0:000,,,.###}", 1593833);
}
}

Numeric Formatting:Custom Format Strings:Percent Notation



using System;

public class PercentNotation
{
public static void Main()
{
Console.WriteLine("{0:##.000%}", 0.89144);
Console.WriteLine("{0:00%}", 0.01285);
}
}


Numeric Formatting:Custom Format Strings:Section Separator


using System;

public class SectionSeparator
{
public static void Main()
{
Console.WriteLine("{0:###.00;0;(###.00)}", -456.55);
Console.WriteLine("{0:###.00;0;(###.00)}", 0);
Console.WriteLine("{0:###.00;0;(###.00)}", 456.55);
}
}


Numeric Formatting:Standard Format Strings:Currency



using System;

public class Currency
{
public static void Main()
{
Console.WriteLine("{0:C}", 33345.8977);
Console.WriteLine("{0:C}", -33345.8977);
}
}




Numeric Formatting:Standard Format Strings:Decimal



using System;

public class StandardFormatStringsDecimal {
public static void Main()
{
Console.WriteLine("{0:D}", 33345);
Console.WriteLine("{0:D7}", 33345);
}
}


Numeric Formatting:Standard Format Strings:Fixed-Point



using System;

public class FixedPoint {
public static void Main()
{
Console.WriteLine("{0:F}", 33345.8977);
Console.WriteLine("{0:F0}", 33345.8977);
Console.WriteLine("{0:F5}", 33345.8977);
}
}

Numeric Formatting:Standard Format Strings:General



using System;

public class StandardFormatStringsGeneral
{
public static void Main()
{
Console.WriteLine("{0:G}", 33345.8977);
Console.WriteLine("{0:G7}", 33345.8977);
Console.WriteLine("{0:G4}", 33345.8977);
}
}

Numeric Formatting:Standard Format Strings:Hexadecimal



using System;

public class Hexadecimal
{
public static void Main()
{
Console.WriteLine("{0:X}", 255);
Console.WriteLine("{0:x8}", 1456);
}
}


Numeric Formatting:Standard Format Strings:Number



using System;

public class StandardFormatStringsNumber
{
public static void Main()
{
Console.WriteLine("{0:N}", 33345.8977);
Console.WriteLine("{0:N4}", 33345.8977);
}
}


Numeric Formatting:Standard Format Strings:Scientific (Exponential)



using System;

public class ScientificExponential
{
public static void Main()
{
Console.WriteLine("{0:E}", 33345.8977);
Console.WriteLine("{0:E10}", 33345.8977);
Console.WriteLine("{0:e4}", 33345.8977);
}
}


Illustrates formatting numbers



/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example2_15.cs illustrates formatting numbers
*/

public class Example2_151
{

public static void Main()
{

// formatting integers
int myInt = 12345;
int myInt2 = 67890;
System.Console.WriteLine("myInt = {0, 6}, myInt2 = {1, 5}",
myInt, myInt2);
System.Console.WriteLine("myInt using 10:d = {0, 10:d}",
myInt);
System.Console.WriteLine("myInt using 10:x = {0, 10:x2}",
myInt);

// formatting floating-point numbers
double myDouble = 1234.56789;
System.Console.WriteLine("myDouble using 10:f3 = {0, 10:f3}",
myDouble);
float myFloat = 1234.56789f;
System.Console.WriteLine("myFloat using 10:f3 = {0, 10:f3}",
myFloat);
decimal myDecimal = 1234.56789m;
System.Console.WriteLine("myDecimal using 10:f3 = {0, 10:f3}",
myDecimal);
System.Console.WriteLine("myFloat using 10:e3 = {0, 10:e3}",
myFloat);
System.Console.WriteLine("myFloat using 10:p2 = {0, 10:p2}",
myFloat);
System.Console.WriteLine("myFloat using 10:n2 = {0, 10:n2}",
myFloat);
System.Console.WriteLine("myFloat using 10:g2 = {0, 10:g2}",
myFloat);

// formatting currency values
decimal myMoney = 15123.45m;
System.Console.WriteLine("myMoney using 10:c2 = {0, 10:c2}",
myMoney);

}

}






http://www.demo2s.com/Code/CSharp/Development-Class/Usingcustomformats.htm


/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Using custom formats.

using System;

public class PictureFormatDemo {
public static void Main() {
double num = 64354.2345;

Console.WriteLine("Default format: " + num);

// Display with 2 decimal places.
Console.WriteLine("Value with two decimal places: " +
"{0:#.##}", num);

// Display with commas and 2 decimal places.
Console.WriteLine("Add commas: {0:#,###.##}", num);

// Display using scientific notation.
Console.WriteLine("Use scientific notation: " +
"{0:#.###e+00}", num);

// Scale the value by 1000.
Console.WriteLine("Value in 1,000s: " +
"{0:#0,}", num);

/* Display positive, negative, and zero
values differently. */
Console.WriteLine("Display positive, negative, " +
"and zero values differently.");
Console.WriteLine("{0:#.#;(#.##);0.00}", num);
num = -num;
Console.WriteLine("{0:#.##;(#.##);0.00}", num);
num = 0.0;
Console.WriteLine("{0:#.##;(#.##);0.00}", num);

// Display a percentage.
num = 0.17;
Console.WriteLine("Display a pecentage: {0:#%}", num);
}
}

String Format for Double [C#]

The following examples show how to format float numbers to string in C#. You can use static method String.Format or instance methods double.ToString and float.ToString.

Digits after decimal point

This example formats double to string with fixed number of decimal places. For two decimal places use pattern „0.00“. If a float number has less decimal places, the rest digits on the right will be zeroes. If it has more decimal places, the number will be rounded.

[C#]
// just two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"

Next example formats double to string with floating number of decimal places. E.g. for maximal two decimal places use pattern „0.##“.

[C#]
// max. two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"

Digits before decimal point

If you want a float number to have any minimal number of digits before decimal point use N-times zero before decimal point. E.g. pattern „00.0“ formats a float number to string with at least two digits before decimal point and one digit after that.

[C#]
// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567); // "123.5"
String.Format("{0:00.0}", 23.4567); // "23.5"
String.Format("{0:00.0}", 3.4567); // "03.5"
String.Format("{0:00.0}", -3.4567); // "-03.5"

Thousands separator

To format double to string with use of thousands separator use zero and comma separator before an usual float formatting pattern, e.g. pattern „0,0.0“ formats the number to use thousands separators and to have one decimal place.

[C#]
String.Format("{0:0,0.0}", 12345.67);     // "12,345.7"
String.Format("{0:0,0}", 12345.67); // "12,346"

Zero

Float numbers between zero and one can be formatted in two ways, with or without leading zero before decimal point. To format number without a leading zero use # before point. For example „#.0“ formats number to have one decimal place and zero to N digits before decimal point (e.g. „.5“ or „123.5“).

Following code shows how can be formatted a zero (of double type).

[C#]
String.Format("{0:0.0}", 0.0);            // "0.0"
String.Format("{0:0.#}", 0.0); // "0"
String.Format("{0:#.0}", 0.0); // ".0"
String.Format("{0:#.#}", 0.0); // ""

Align numbers with spaces

To align float number to the right use comma „,“ option before the colon. Type comma followed by a number of spaces, e.g. „0,10:0.0“ (this can be used only in String.Format method, not in double.ToString method). To align numbers to the left use negative number of spaces.

[C#]
String.Format("{0,10:0.0}", 123.4567);    // "     123.5"
String.Format("{0,-10:0.0}", 123.4567); // "123.5 "
String.Format("{0,10:0.0}", -123.4567); // " -123.5"
String.Format("{0,-10:0.0}", -123.4567); // "-123.5 "

Custom formatting for negative numbers and zero

If you need to use custom format for negative float numbers or zero, use semicolon separator;“ to split pattern to three sections. The first section formats positive numbers, the second section formats negative numbers and the third section formats zero. If you omit the last section, zero will be formatted using the first section.

[C#]
String.Format("{0:0.00;minus 0.00;zero}", 123.4567);   // "123.46"
String.Format("{0:0.00;minus 0.00;zero}", -123.4567); // "minus 123.46"
String.Format("{0:0.00;minus 0.00;zero}", 0.0); // "zero"

Some funny examples

As you could notice in the previous example, you can put any text into formatting pattern, e.g. before an usual pattern „my text 0.0“. You can even put any text between the zeroes, e.g. „0aaa.bbb0“.

[C#]
String.Format("{0:my number is 0.0}", 12.3);   // "my number is 12.3"
String.Format("{0:0aaa.bbb0}", 12.3); // "12aaa.bbb3"

See also


Double..::.ToString Method (String)

Updated: October 2008

Converts the numeric value of this instance to its equivalent string representation, using the specified format.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function ToString ( _
format As String _
) As String
Visual Basic (Usage)
Dim instance As Double
Dim format As String
Dim returnValue As String

returnValue = instance.ToString(format)
C#
public string ToString(
string format
)
Visual C++
public:
String^ ToString(
String^ format
)
JScript
public function ToString(
format : String
) : String

Return Value

Type: System..::.String
The string representation of the value of this instance as specified by format.
ExceptionCondition
FormatException

format is invalid.

The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of a number, as specified by format.

The format parameter can be any valid standard numeric format specifier except for D and X, as well as any combination of custom numeric format specifiers. If format is nullNothingnullptra null reference (Nothing in Visual Basic) or an empty string, the return value is formatted with the general numeric format specifier ("G").

The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

The return value is formatted using the NumberFormatInfo object for the current culture. To apply the formatting conventions of a specified culture, call the Double..::.ToString(String, IFormatProvider) method.

By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.

The return value is formatted with NumberFormatInfo data for the current culture.

The following example displays several Double values using each of the supported standard numeric format specifiers together with two custom numeric format strings. One of those custom format strings illustrates how to pad a Single value with leading zeroes. In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture.

Visual Basic
Dim numbers() As Double = {1054.32179, -195489100.8377, 1.0437E21, _
-1.0573e-05}
Dim specifiers() As String = { "C", "E", "e", "F", "G", "N", "P", _
"R", "#,000.000", "0.###E-000", _
"000,000,000,000.00###" }
For Each number As Double In numbers
Console.WriteLine("Formatting of {0}:", number)
For Each specifier As String In specifiers
Console.WriteLine(" {0,5}: {1}", _
specifier, number.ToString(specifier))
Next
Console.WriteLine()
Next
' The example displays the following output to the console:
' Formatting of 1054.32179:
' C: $1,054.32
' E: 1.054322E+003
' e: 1.054322e+003
' F: 1054.32
' G: 1054.32179
' N: 1,054.32
' P: 105,432.18 %
' R: 1054.32179
' #,000.000: 1,054.322
' 0.###E-000: 1.054E003
' 000,000,000,000.00###: 000,000,001,054.322
'
' Formatting of -195489100.8377:
' C: ($195,489,100.84)
' E: -1.954891E+008
' e: -1.954891e+008
' F: -195489100.84
' G: -195489100.8377
' N: -195,489,100.84
' P: -19,548,910,083.77 %
' R: -195489100.8377
' #,000.000: -195,489,100.838
' 0.###E-000: -1.955E008
' 000,000,000,000.00###: -000,195,489,100.00
'
' Formatting of 1.0437E+21:
' C: $1,043,700,000,000,000,000,000.00
' E: 1.043700E+021
' e: 1.043700e+021
' F: 1043700000000000000000.00
' G: 1.0437E+21
' N: 1,043,700,000,000,000,000,000.00
' P: 104,370,000,000,000,000,000,000.00 %
' R: 1.0437E+21
' #,000.000: 1,043,700,000,000,000,000,000.000
' 0.###E-000: 1.044E021
' 000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
'
' Formatting of -1.0573E-05:
' C: $0.00
' E: -1.057300E-005
' e: -1.057300e-005
' F: 0.00
' G: -1.0573E-05
' N: 0.00
' P: 0.00 %
' R: -1.0573E-05
' #,000.000: 000.000
' 0.###E-000: -1.057E-005
' 000,000,000,000.00###: -000,000,000,000.00001

C#
double[] numbers= {1054.32179, -195489100.8377, 1.0437E21,
-1.0573e-05};
string[] specifiers = { "C", "E", "e", "F", "G", "N", "P",
"R", "#,000.000", "0.###E-000",
"000,000,000,000.00###" };
foreach (double number in numbers)
{
Console.WriteLine("Formatting of {0}:", number);
foreach (string specifier in specifiers)
Console.WriteLine(" {0,5}: {1}",
specifier, number.ToString(specifier));

Console.WriteLine();
}
// The example displays the following output to the console:
// Formatting of 1054.32179:
// C: $1,054.32
// E: 1.054322E+003
// e: 1.054322e+003
// F: 1054.32
// G: 1054.32179
// N: 1,054.32
// P: 105,432.18 %
// R: 1054.32179
// #,000.000: 1,054.322
// 0.###E-000: 1.054E003
// 000,000,000,000.00###: 000,000,001,054.322
//
// Formatting of -195489100.8377:
// C: ($195,489,100.84)
// E: -1.954891E+008
// e: -1.954891e+008
// F: -195489100.84
// G: -195489100.8377
// N: -195,489,100.84
// P: -19,548,910,083.77 %
// R: -195489100.8377
// #,000.000: -195,489,100.838
// 0.###E-000: -1.955E008
// 000,000,000,000.00###: -000,195,489,100.00
//
// Formatting of 1.0437E+21:
// C: $1,043,700,000,000,000,000,000.00
// E: 1.043700E+021
// e: 1.043700e+021
// F: 1043700000000000000000.00
// G: 1.0437E+21
// N: 1,043,700,000,000,000,000,000.00
// P: 104,370,000,000,000,000,000,000.00 %
// R: 1.0437E+21
// #,000.000: 1,043,700,000,000,000,000,000.000
// 0.###E-000: 1.044E021
// 000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//
// Formatting of -1.0573E-05:
// C: $0.00
// E: -1.057300E-005
// e: -1.057300e-005
// F: 0.00
// G: -1.0573E-05
// N: 0.00
// P: 0.00 %
// R: -1.0573E-05
// #,000.000: 000.000
// 000,000,000,000.00###: -000,000,000,000.00001

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date