Core Function SetBit

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
m (1 revision)
 
(4 intermediate revisions by one user not shown)
Line 40: Line 40:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 +
Enum Privs
 +
{
 +
$DeleteFile = 0,
 +
$UploadFile = 1,
 +
$DownloadFile = 2,
 +
$RenameFile = 3,
 +
$MoveFile = 4,
 +
$CreateFolder = 5,
 +
$DeleteFolder = 6,
 +
$RenameFolder = 7,
 +
$MoveFolder = 8,
 +
$ReadChat = 9,
 +
$SendChat = 10,
 +
$CreateChat = 11,
 +
$CloseChat = 12
 +
};
 +
 
// Create a binary variable to hold the Bits
 
// Create a binary variable to hold the Bits
 
// It can be any size we want but for now 4 bytes
 
// It can be any size we want but for now 4 bytes
Line 170: Line 187:
 
$DeleteFile = 0,
 
$DeleteFile = 0,
 
$UploadFile = 1,
 
$UploadFile = 1,
$DownloadFile = 2,
+
$DownloadFile = 2,
 
$RenameFile = 3,
 
$RenameFile = 3,
 
$MoveFile = 4,
 
$MoveFile = 4,
$CreateFolder = 5,
+
$CreateFolder = 5,
$DeleteFolder = 6,
+
$DeleteFolder = 6,
$RenameFolder = 7
+
$RenameFolder = 7,
 +
$cat = 8
 
};
 
};
  
  
$Access = BinaryCreate(4, 0x00);
+
$Access = BinaryCreate(1, 0x00);
 
FillBit($Access, false);
 
FillBit($Access, false);
  
Line 186: Line 204:
 
MySetBit($Access, Privs->$RenameFolder, true);
 
MySetBit($Access, Privs->$RenameFolder, true);
  
printr $Access;
+
say "--- Privs (Enabled 3) ---";
 +
PrintPrivs($Access);
 +
say;
  
say "--- Privs ---";
+
say "--- Privs (Enabled all) ---";
 +
FillBit($Access, true);
 
PrintPrivs($Access);
 
PrintPrivs($Access);
 
say;
 
say;
  
Function MySetBit( $data , $index, $value = false )
+
Function MySetBit( $data, $index, $value = false )
 
{
 
{
 
if ($value)
 
if ($value)
Line 200: Line 221:
 
}
 
}
  
 
+
Function MyGetBit( $data, $index )
 +
{
 +
return ($data[$index >> 3] >> (7 - ($index & 7))) & 1;
 +
}
  
 
Function PrintPrivs( $data )
 
Function PrintPrivs( $data )
Line 207: Line 231:
 
foreach( Enumerate('Privs') as my $Key => my $Value )
 
foreach( Enumerate('Privs') as my $Key => my $Value )
 
{
 
{
my $Status = GetBit($data, $Value);
+
my $Status = MyGetBit($data, $Value);
 
if($Status) // Only show privs we can use
 
if($Status) // Only show privs we can use
 
{
 
{
Line 219: Line 243:
 
}
 
}
 
}
 
}
 +
// --- Privs (Enabled 3) ---
 +
// Priv 'downloadfile' Access is 'true'
 +
// Priv 'createfolder' Access is 'true'
 +
// Priv 'renamefolder' Access is 'true'
 +
//
 +
// --- Privs (Enabled all) ---
 +
// Priv 'deletefile' Access is 'true'
 +
// Priv 'uploadfile' Access is 'true'
 +
// Priv 'downloadfile' Access is 'true'
 +
// Priv 'renamefile' Access is 'true'
 +
// Priv 'movefile' Access is 'true'
 +
// Priv 'createfolder' Access is 'true'
 +
// Priv 'deletefolder' Access is 'true'
 +
// Priv 'renamefolder' Access is 'true'
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:37, 14 June 2015

SetBit( <binary-array>, <index>, <value> )

Contents

Description

Set the bit at a given index.

Parameters

binary-array

The binary variable to use.

index

Index of the bit to set (This is not the index of the bytes).

value

Optional; Value to set the bit to. (Should be TRUE or FALSE)

Default: FALSE

Return Value

Success: Returns true if operation was a success (even if the bit was already set to what we wanted to change them to)

Failure: Returns false

Remarks

This is a very good way to store a ton of stuff inside bytes since you could store an array of about 8 individual true/false states in just 1 byte or an array of about 32 individual true/false states in just 4 bytes and so on.

This would use up an incredibly less amount of ram and processing power compared to using a dictionary directly.

See example to learn how to use this.

Example

Enum Privs
{
	$DeleteFile			= 0,
	$UploadFile			= 1,
	$DownloadFile			= 2,
	$RenameFile			= 3,
	$MoveFile			= 4,
	$CreateFolder			= 5,
	$DeleteFolder			= 6,
	$RenameFolder			= 7,
	$MoveFolder			= 8,
	$ReadChat			= 9,
	$SendChat			= 10,
	$CreateChat			= 11,
	$CloseChat			= 12
};
 
// Create a binary variable to hold the Bits
// It can be any size we want but for now 4 bytes
// is enough to hold the above privs
$Access = BinaryCreate(4, 0x00);
// Make sure all privs are off
FillBit($Access, false);
 
// Print privs to see default state
// (Should all be disabled)
say "--- Privs ---";
PrintPrivs($Access);
say; // Blank new line
 
// Lets add some Privs
SetBit($Access, Privs->$UploadFile, true);
SetBit($Access, Privs->$CreateFolder, true);
SetBit($Access, Privs->$ReadChat, true);
SetBit($Access, Privs->$CloseChat, true);
 
// Print privs again to see changes
say "--- Privs After Change ---";
say "(UploadFile, CreateFolder, ReadChat and CloseChat will be enabled)";
PrintPrivs($Access);
say; // Blank new line
 
// Lets add some Privs again
SetBit($Access, Privs->$SendChat, true);
// Print privs again to see changes
say "--- Privs After Change (1 added) ---";
say "(in addition to above SendChat will be enabled)";
PrintPrivs($Access);
say; // Blank new line
 
// Lets remove some Privs
SetBit($Access, Privs->$SendChat, false);
SetBit($Access, Privs->$CreateFolder, false);
// Print privs again to see changes
say "--- Privs After Change (2 removed) ---";
say "(SendChat and CreateFolder will be removed)";
PrintPrivs($Access);
say; // Blank new line
 
// Now lets just turn all privs on
FillBit($Access, true);
say "--- Privs After Change ---";
say "(All Enabled)";
PrintPrivs($Access);
say; // Blank new line
 
// Finally lets just turn all privs off
FillBit($Access, false);
say "--- Privs After Change ---";
say "(All Disabled)";
PrintPrivs($Access);
say; // Blank new line
 
// This function will print all the privs that
// are available to us every time its called
Function PrintPrivs( $data )
{
	my $Allowed = 0;
	foreach( Enumerate('Privs') as my $Key => my $Value )
	{
		my $Status = GetBit($data, $Value);
		if($Status) // Only show privs we can use
		{
			say "Priv '$Key' Access is '$Status'";
			$Allowed++;
		}
	}
	if(!$Allowed)
	{
		say "You have no privs";
	}
}
 
// Prints
//
// --- Privs ---
// You have no privs
//
// --- Privs After Change ---
// (UploadFile, CreateFolder, ReadChat and CloseChat will be enabled)
// Priv 'uploadfile' Access is 'true'
// Priv 'createfolder' Access is 'true'
// Priv 'readchat' Access is 'true'
// Priv 'closechat' Access is 'true'
//
// --- Privs After Change (1 added) ---
// (in addition to above SendChat will be enabled)
// Priv 'uploadfile' Access is 'true'
// Priv 'createfolder' Access is 'true'
// Priv 'readchat' Access is 'true'
// Priv 'sendchat' Access is 'true'
// Priv 'closechat' Access is 'true'
//
// --- Privs After Change (2 removed) ---
// (SendChat and CreateFolder will be removed)
// Priv 'uploadfile' Access is 'true'
// Priv 'readchat' Access is 'true'
// Priv 'closechat' Access is 'true'
//
// --- Privs After Change ---
// (All Enabled)
// Priv 'deletefile' Access is 'true'
// Priv 'uploadfile' Access is 'true'
// Priv 'downloadfile' Access is 'true'
// Priv 'renamefile' Access is 'true'
// Priv 'movefile' Access is 'true'
// Priv 'createfolder' Access is 'true'
// Priv 'deletefolder' Access is 'true'
// Priv 'renamefolder' Access is 'true'
// Priv 'movefolder' Access is 'true'
// Priv 'readchat' Access is 'true'
// Priv 'sendchat' Access is 'true'
// Priv 'createchat' Access is 'true'
// Priv 'closechat' Access is 'true'
//
// --- Privs After Change ---
// (All Disabled)
// You have no privs

If you are wondering how to do SetBit() using Sputnik code only and not using any __Bit() functions then here is how its done

Enum Privs
{
	$DeleteFile			= 0,
	$UploadFile			= 1,
	$DownloadFile			= 2,
	$RenameFile			= 3,
	$MoveFile			= 4,
	$CreateFolder			= 5,
	$DeleteFolder			= 6,
	$RenameFolder			= 7,
	$cat			= 8
};
 
 
$Access = BinaryCreate(1, 0x00);
FillBit($Access, false);
 
MySetBit($Access, Privs->$DownloadFile, true);
MySetBit($Access, Privs->$CreateFolder, true);
MySetBit($Access, Privs->$RenameFolder, true);
 
say "--- Privs (Enabled 3) ---";
PrintPrivs($Access);
say;
 
say "--- Privs (Enabled all) ---";
FillBit($Access, true);
PrintPrivs($Access);
say;
 
Function MySetBit( $data, $index, $value = false )
{
	if ($value)
		$data[$index >> 3] |= (byte)(1 << (7 - ($index & 7)));
	else
		$data[$index >> 3] &= (byte)(~(1 << (7 - ($index & 7))));
}
 
Function MyGetBit( $data, $index )
{
	return ($data[$index >> 3] >> (7 - ($index & 7))) & 1;
}
 
Function PrintPrivs( $data )
{
	my $Allowed = 0;
	foreach( Enumerate('Privs') as my $Key => my $Value )
	{
		my $Status = MyGetBit($data, $Value);
		if($Status) // Only show privs we can use
		{
			say "Priv '$Key' Access is '$Status'";
			$Allowed++;
		}
	}
	if(!$Allowed)
	{
		say "You have no privs";
	}
}
// --- Privs (Enabled 3) ---
// Priv 'downloadfile' Access is 'true'
// Priv 'createfolder' Access is 'true'
// Priv 'renamefolder' Access is 'true'
//
// --- Privs (Enabled all) ---
// Priv 'deletefile' Access is 'true'
// Priv 'uploadfile' Access is 'true'
// Priv 'downloadfile' Access is 'true'
// Priv 'renamefile' Access is 'true'
// Priv 'movefile' Access is 'true'
// Priv 'createfolder' Access is 'true'
// Priv 'deletefolder' Access is 'true'
// Priv 'renamefolder' Access is 'true'
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox