Skip to content

Instantly share code, notes, and snippets.

View tkouba's full-sized avatar

Tomas Kouba tkouba

  • Prague, Czech Republic
View GitHub Profile
@tkouba
tkouba / ReadPassword.cs
Created June 19, 2015 12:39
Read password from console
/// <summary>
/// Read password from console
/// </summary>
/// <param name="prompt">Password prompt</param>
/// <param name="mask">Password mask char, usually '*'</param>
/// <returns>Password</returns>
public static string ReadPassword(string prompt, char mask)
{
Stack<String> pwdStack = new Stack<String>();
if (!String.IsNullOrEmpty(prompt))
@tkouba
tkouba / TimeSpanValueConverter.cs
Last active April 1, 2016 06:57
TimeSpanValueConverter for DevExpress XPO - save TimeSpan to database in seconds
public class TimeSpanValueConverter : ValueConverter
{
public override Type StorageType { get { return typeof(Int32); } }
public override object ConvertToStorageType(object value)
{
if (!(value is TimeSpan)) return null;
return Convert.ToInt32(Math.Truncate(((TimeSpan)value).TotalSeconds));
}
public override object ConvertFromStorageType(object value)
{
@tkouba
tkouba / ColorValueConverter.cs
Last active April 1, 2016 06:58
ColorValueConverter for DevExpress XPO - save Color to database as Int32
public class ColorValueConverter : ValueConverter
{
public override Type StorageType { get { return typeof(Int32); } }
public override object ConvertToStorageType(object value)
{
if (!(value is Color)) return null;
return ((Color)value).ToArgb() & 0x00FFFFFF;
}
public override object ConvertFromStorageType(object value)
{
@tkouba
tkouba / TimeService.cs
Created August 1, 2016 20:43
Windows CE time service control
using System;
using System.Runtime.InteropServices;
namespace Arci.WindowsCE
{
/// <summary>
/// NTP service controller
/// </summary>
public static class TimeService
{
@tkouba
tkouba / client_net_address.sql
Created January 12, 2017 08:40
Get connected client net addresses (T-SQL)
select distinct
net_transport,
client_net_address,
hostname,
program_name,
db.name as database_name,
sp.loginame
from master.sys.dm_exec_connections ec
inner join master.dbo.sysprocesses sp
on sp.spid = ec.session_id
@tkouba
tkouba / ReverseUInt64.cs
Created February 17, 2017 12:28
Reverse UInt64 with bits width of groups. Reverse bits (bits = 1), reverse half-bytes (bits = 4, same as reverse hexadecimal string value).
private static UInt64 ReverseUInt64(UInt64 x, int bits = 1)
{
UInt64 b = 0;
UInt64 mask = 0;
for (int i = 0; i < bits; i++)
{
mask = (mask << 1) | 0x1;
}
for (int i = 0; i < 64 / bits; i++)
{
@tkouba
tkouba / MultiLinkLabel.cs
Created May 16, 2017 07:39
Enhanced LinkLabel for Windows Forms
using System;
using System.Text.RegularExpressions;
namespace Arci.Windows.Forms
{
public class MultiLinkLabel : System.Windows.Forms.LinkLabel
{
protected override void OnTextChanged(EventArgs e)
{
CreateLinks();
@tkouba
tkouba / IEnumStringToArray.cs
Created August 29, 2017 17:31
COM interface IEnumString to string array
private string[] IEnumStringToArray(IEnumString enumerator)
{
const int S_OK = 0x00000000;
List<string> lst = new List<string>();
if (enumerator != null)
{
int cft;
string[] strF = new string[100];
int hresult;
IntPtr intPtr = Marshal.AllocCoTaskMem(sizeof(int));
@tkouba
tkouba / CreateName.cs
Created June 28, 2018 12:19
Create valid variable name in PascalCase from any text - remove invalid characters, add underscore prefix if needed
public static string CreateName(this string s) {
s = s.Normalize(NormalizationForm.FormD);
StringBuilder sb = new StringBuilder();
bool upper = true;
for (int i = 0; i < s.Length; i++)
{
var cat = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(s[i]);
//Console.WriteLine(cat);
if (cat == System.Globalization.UnicodeCategory.DecimalDigitNumber && sb.Length == 0)
sb.Append('_');
@tkouba
tkouba / DisposableList.cs
Created September 27, 2016 11:22
Generic list with automatic dispose
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyCollections
{
/// <summary>
/// Represents a strongly typed list of disposable objects that can be accessed by index.
/// Provides methods to search, sort, and manipulate lists and disposes all contained objects.