Skip to content

Instantly share code, notes, and snippets.

View nikanos's full-sized avatar

Nikanos Polykarpou nikanos

  • ::1, Nicosia, Cyprus
View GitHub Profile
@nikanos
nikanos / add10usingbind.cpp
Last active November 29, 2022 17:14
Add 10 to all input numbers using bind2nd, bind with placeholder (C++11) and generic lambda (C++14)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
ifstream inf("input.txt");
@nikanos
nikanos / PrintServerVars.cs
Last active November 29, 2022 17:14
ASP.NET MVC 4 Print server variables snippet
public class ServerVar
{
public string Key { get; set; }
public string Value { get; set; }
}
public class ServerVarsViewModel
{
public IList<ServerVar> ServerVariables { get; set; }
}
@nikanos
nikanos / Ensure.cs
Created November 10, 2022 21:24
C# class for method parameter checks
public static class Ensure
{
public static void ArgumentNotNull<T>(T argument, string argumentName) where T : class
{
if (argument == null)
throw new ArgumentNullException(argumentName);
}
public static void ArgumentNotNull<T>(Nullable<T> argument, string argumentName) where T : struct
{
@nikanos
nikanos / tsql_loop.sql
Last active November 29, 2022 17:07
SQL Server TSQL Loop example
DECLARE @cnt INT = 0;
WHILE @cnt < 10
BEGIN
PRINT 'Doing Something in loop #' + Convert(VARCHAR(10),@cnt);
--Use RAISEERROR to flush PRINT buffer
RAISERROR(N'', --message
0, --severity
1) --state
WITH NOWAIT;
@nikanos
nikanos / forkex1.c
Last active December 4, 2022 21:12
linux fork() C example - parent process starts a child process that sleeps for 3 seconds and either waits or terminates it.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int pid;
int status;
@nikanos
nikanos / shellforkex1.cpp
Created December 4, 2022 21:30
linux C++ shell example using fork()
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <algorithm>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
@nikanos
nikanos / Animal.c
Created December 4, 2022 21:43
Inheritance example using C
#include <stdio.h>
#include <string.h>
#include "Animal.h"
void Animal_Ctor(Animal* pa)
{
pa->pfspeak=Animal_Speaking;
}
void Animal_Speaking(Animal* pa)
@nikanos
nikanos / StringUtils.cs
Created January 3, 2023 20:19
C# string SafeFormat method - used as a wrapper to string.Format() and passing Undefined to any missing arguments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class StringUtils
{
public static string SafeFormat(string format, params object[] parameters)
{
const int MAX_NUMBER_OF_UNPASSED_PARAMETERS = 50;
@nikanos
nikanos / RemoveEmptyFolders.ps1
Created March 7, 2023 09:47
Powershell script to remove empty folders under the current directory
Get-ChildItem -Recurse .|where {$_.PSISContainer -and @($_ | Get-ChildItem).Count -eq 0 }|Remove-Item
@nikanos
nikanos / MachineKeyProtector.cs
Created April 18, 2023 19:07
.NET Framework WebApi Bearer Token Decode
public class MachineKeyProtector : IDataProtector
{
private readonly string[] _purpose =
{
typeof(OAuthAuthorizationServerMiddleware).Namespace,
"Access_Token",
"v1"
};
public byte[] Protect(byte[] userData)