Thursday, September 22, 2016

My Moroccan Adventure

Check out the travel map I made with TripHappy

Thursday, December 1, 2011

Automatically Resize Text

This is a handy little function I wrote in jQuery I use in my new website, http://t3k.no. It will automatically resize the text within a div or span to fit the container.

Parameters:
containerID - The ID of the div or span you want the text to fit into.
resizeID - The ID of the text element you want to resize

function resizeText(containerID, resizeID )
{
    var size = 16;
    var desired_width = $('#' + containerID).width();
    $('#resizer').html($('#' + resizeID).html()); //setting resizer to desired text
    $('#resizer').css("font-size", size);
    var actual_width = $('#resizer').width();
    //alert(desired_width + ' - ' + actual_width);
    
    while(desired_width <= actual_width+10) //+10 for saftey net
    {
        size--;
        $('#resizer').css("font-size", size);
        actual_width = $('#resizer').width();
    }

    $('#' + resizeID).css("font-size", size);
}

Monday, February 28, 2011

Remove the Black "Theater Mode" from Facebook Pictures

Facebook has a new feature that is really annoying called theater mode. It creates a black screen to display pictures on and will not let you right-click pictures to copy the exact image address or do other fun things. If you want to disable it here is a easy trick I figured out.

1) Click the picture you want to view so that theater mode pops up (Like this)
2) Highlight the address bar
3) Delete the address starting with "&set" (Like this)
4) Click enter while still in the address bar to go to the new address without the stupid theater mode (Like this)

Easy right? Usually in a URL data after an '&' symbol is extra parameters that are being passed and not needed. So when copying a URL you can delete them.

Enjoy!

Tuesday, December 14, 2010

Simulated Memory and Disk w/ Different Paging Algorithms

Here is the code to one of my Operating Systems assignments, that is suppose to demonstrate the relationship between memory and disk, and test out the efficiency of 3 different paging algorithms: Least Recently Used, Most Recently Used, Completely Random.

3 Different Threads are created and are all constantly competing for CPU resources to add up various sums.

#include 
#include 
#include  
#include 
#include 
#include 


/*global variables*/
int N = 10000; //1000 Variables
int seq = 0; 
int k = 100;
int m = 200; //memory cells

/*structs*/
typedef struct {
 int memLookTable[200];
 int physicalTable[200];
 int metaTable[200];
} memoryStruct;

/*global variables part II */
int disk[10000];
memoryStruct memory;
pthread_mutex_t memLock;
FILE *output;


/*method declarations*/
void initMemory();
int fetch(int i);
void pageInLeast(int i);
void pageInMost(int i);
void pageInRandom(int i);
void* tOne(void *arg);
void* tTwo(void *arg);
void* tThree(void *arg);
void printThread();


int main()
{
 initMemory();

 output = fopen("output.txt" , "w");
 pthread_mutex_init(&memLock, NULL); //initializing the lock for memory

 int iret1, iret2, iret3;
 pthread_t thread1, thread2, thread3;


 pthread_create(&thread1, NULL, tOne, (void *) 10);
 pthread_create(&thread2, NULL, tTwo, (void *) 10);
 pthread_create(&thread3, NULL, tThree, (void *) 10);

 pthread_join(thread1, NULL);
 pthread_join(thread2, NULL);
 pthread_join(thread3, NULL);

 printf("Done!\n");
 fclose(output);
}

//Complete Random Order
void* tThree(void *arg)
{
 int task = 0;
 int done = 0;
 int i = 0;
 int adder;
 int hasLock = 0;
 int r; //random
 
 while(done == 0) //while the entire thread is not done
 {
  int sum = 0;
  while(i <= k)     //compute the sum of X_r2 to X_(rk) where r is a random number
  {
   r = rand() % N;
   if(hasLock == 0)   //if thread doesn't have lock (had to page previous iteration)
   {
    pthread_mutex_lock(&memLock);
    seq++;
    hasLock = 1;
   }

   adder = fetch(r);
   if (adder != -1)  //variable is in memory
   {
    sum = sum + adder;
    i++;
   }
   else   //variable needs to be paged
   {
    pageInRandom(r);
    adder = fetch(r);
    pthread_mutex_unlock(&memLock);
    hasLock = 0;
    if (adder != -1)
    {
     sum = sum + adder;
     i++;
    }
    else
     printf("ERROR in thread 3! r=%i\n", r);


   }
  }

  //task is done
  if(hasLock == 1) //if it still has the lock
  {
   pthread_mutex_unlock(&memLock);
   hasLock = 0;
  }

  printThread(3, task);
  task++;
  
  if(task > 2000)
   done = 1;

 }
}

//Sequential Order
void* tTwo(void *arg)
{
 int task = 0;
 int done = 0;
 int i = 0;
 int adder;
 int hasLock = 0;
 int a = 0; //what to add to k
 
 while(done == 0) //while the entire thread is not done
 {
  int sum = 0;
  while(i <= (k+a))     //compute the sum of X_1 to X_(k+a)
  {
   if(hasLock == 0)   //if thread doesn't have lock (had to page previous iteration)
   {
    pthread_mutex_lock(&memLock);
    seq++;
    hasLock = 1;
   }

   adder = fetch(i);
   if (adder != -1)  //variable is in memory
   {
    sum = sum + adder;
    i++;
   }
   else   //variable needs to be paged
   {
    pageInRandom(i);
    adder = fetch(i); 
    pthread_mutex_unlock(&memLock);
    hasLock = 0;
    if (adder != -1)
    {
     sum = sum + adder;
     i++;
    }
    else
     printf("ERROR in thread 2!\n");


   }
  }

  //task is done
  if(hasLock == 1) //if it still has the lock
  {
   pthread_mutex_unlock(&memLock);
   hasLock = 0;
  }

  printThread(2, task);
  task ++;
  a++;
  
  if(task > 2000)
   done = 1;
 }
} 

//Common Hot Set
void* tOne(void *arg)
{
 int task = 0;
 int done = 0;
 int i = 0;
 int adder;
 int hasLock = 0;
 while(done == 0) //while the entire thread is not done
 {
  int sum = 0;
  while(i < k) //compute the sum of 1 to k-1 variables
  {
   if(hasLock == 0)   //if thread doesn't have lock (had to page previous iteration)
   {
    pthread_mutex_lock(&memLock);
    seq++;
    hasLock = 1;
   }
   adder = fetch(i);
   if (adder != -1)  //variable is in memory
   {
    sum = sum + adder;
    i++;
   }
   else   //variable needs to be paged
   {
    pageInRandom(i);
    adder = fetch(i); 
    pthread_mutex_unlock(&memLock);
    hasLock = 0;
    if (adder != -1)
    {
     sum = sum + adder;
     i++;
    }
    else
     printf("ERROR in thread 1!\n");


   }

    
  }
  int r = rand() % (N-k);
  
  if(hasLock == 0)   //if thread doesn't have lock (had to page previous iteration)
  {
   pthread_mutex_lock(&memLock);
   seq++;
   hasLock = 1;
  }
  adder = fetch(r);
  if (adder != -1)  //variable is in memory
   sum = sum + adder;
  else   //variable needs to be paged
  {
   pageInRandom(r);
   adder = fetch(r);
   pthread_mutex_unlock(&memLock);
   hasLock = 0;
   if (adder != -1)
   {
    sum = sum + adder;
   }
   else
    printf("ERROR in Thread 1! r=%i\n", r);


  }

  //task is done
  printThread(1, task);
  task++;

    
  if(hasLock == 1) //if it still has the lock
  {
   pthread_mutex_unlock(&memLock);
   hasLock = 0;
  }


  //10 Tasks
  if(task > 2000)
   done = 1;
 }  
}

void printThread(int thread, int task)
{
 fprintf(output, "(%i), (%i), (%i)\n" , thread, task, seq);
 printf("(Thread: %i), (Task: %i), (Task Seq: %i)\n", thread, task, seq);
}

void initMemory()
{
 int i;
 for(i=0; i < m; i++) 
 {
  memory.memLookTable[i] = -1;
  memory.physicalTable[i] = -1;
 }
}

/* Check to see if X_i is in memory
 yes: return value
 no: return -1
*/
int fetch(int i)
{
 int b;
 for(b=0; b < m; b++)
 {
  if(memory.memLookTable[b] == i) //variable is not in memory
   return memory.physicalTable[b];
 }

 return -1;
 //else //variable IS in memory
  //return memory.physicalTable[i];
}


/* LEAST RECENTLY USED ALGORITHM
puts value of X_i from disk to memory by ^
 if a value in memory must be evicted, it must be written back to disk first
*/
void pageInLeast(int i)
{
 int index = checkEmptyMemorySlot();
 if(index != -1)//if nothing needs to be evicted
 {
  memory.memLookTable[index] = i;
  memory.physicalTable[index] = disk[i];
  memory.metaTable[index] = seq;
 }
 else //evict something
 {
  //find smallest value in metaTable
  int q, small, smallIndex;
  for(q=0; q < m; q++)     //after done, smallIndex is the smallest metaTable index
  {
   if(q==0)
   {
    small = memory.metaTable[0];  //initial value
    smallIndex = 0;
   } 
   else if (small > memory.metaTable[q])   //if a smaller meta value is found
   {
    small = memory.metaTable[q];
    smallIndex = q;
   }
  }
  
  //Place Evicted Value back in disk
  memory.metaTable[smallIndex] = seq;   
  int temp = memory.memLookTable[smallIndex];
  disk[temp] = memory.physicalTable[smallIndex];

  //Pagining in new value
  memory.memLookTable[smallIndex] = i;
  memory.physicalTable[smallIndex] = disk[i];
 }

}

//Returns -1 if memory is full, else returns the index of an open slot in memory
int checkEmptyMemorySlot()
{
 int i;
 for(i=0; i < m; i++)
 {
  if(memory.memLookTable[i] == -1)
   return i;
 }

 return -1;
}

/* Most RECENTLY USED ALGORITHM
puts value of X_i from disk to memory by ^
 if a value in memory must be evicted, it must be written back to disk first
*/
void pageInMost(int i)
{
 int index = checkEmptyMemorySlot();
 if(index != -1)//if nothing needs to be evicted
 {
  memory.memLookTable[index] = i;
  memory.physicalTable[index] = disk[i];
  memory.metaTable[index] = seq;
 }
 else //evict something
 {
  //find biggest value in metaTable
  int q, big, bigIndex;
  for(q=0; q < m; q++)     //after done, bigIndex is the biggest metaTable index
  {
   if(q==0)
    big = memory.metaTable[0];   //initial value
   else if (big < memory.metaTable[q])   //if a smaller meta value is found
   {
    big = memory.metaTable[q];
    bigIndex = q;
   }
  }
  
  //Place Evicted Value back in disk
  memory.metaTable[bigIndex] = seq;   
  int temp = memory.memLookTable[bigIndex];
  disk[temp] = memory.physicalTable[bigIndex];

  //Pagining in new value
  memory.memLookTable[bigIndex] = i;
  memory.physicalTable[bigIndex] = disk[i];
 }
}

/* Random ALGORITHM
puts value of X_i from disk to memory by ^
 if a value in memory must be evicted, it must be written back to disk first
*/
void pageInRandom(int i)
{
 int index = checkEmptyMemorySlot();
 if(index != -1)//if nothing needs to be evicted
 {
  memory.memLookTable[index] = i;
  memory.physicalTable[index] = disk[i];
 }
 else //evict something
 {
  
  //picking a random value to evict
  int r;
  r = rand() % m; //random number 0-999

  //Place Evicted Value back in disk   
  int temp = memory.memLookTable[r];
  disk[temp] = memory.physicalTable[r];

  //Pagining in new value
  memory.memLookTable[r] = i;
  memory.physicalTable[r] = disk[i];
 }
}

Monday, September 20, 2010

JPEG Crusher v1.3: An Easy JPEG Size Reducer - C#

That's right, my first official program release.

JPEG CRUSHER
Photobucket
Photobucket

Description:
Takes multiple JPEG files and quickly reduces quality to minimize file size.

Official Release:
v1.3 - https://sourceforge.net/projects/jpegcrusher/files/Published/JPEGCrusher%20v1.3.rar/download
Source:
v1.3 - https://sourceforge.net/projects/jpegcrusher/files/Source/JPEGCrusher%20v1.3%20Source.rar/download

Friday, July 30, 2010

Retrieving a File or Directory from a Path that is Too Long to Read - C#

Microsoft has a limitation of 260 characters for a path or file name. Usually Windows is smart enough and wont allow you to create a file or folder if the path will be too long, but sometimes, some way or another, files/folders get trapped and you can't access them because the name is too long. This can be a huge inconvenience and can cause your program to crash.

The following method overwrites the Windows API and allows you to move a folder or directory if its path is over 260 characters:
Note: Replace "H:\Calvin\kernel32.dll" with the path to kernel32.dll (can be found in your system32 folder)
Note2: You can call Move on a directory or file
[DllImport(@"H:\Calvin\kernel32.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool MoveFile(string lpExistingFileName, string lpNewFileName);

public static void Move(string fileName, string toPath) 
{
    string formattedName = @"\\?\" + fileName;
    MoveFile(formattedName, toPath);
            
}

If you have the problem where you don't the exact path you want to retrieve because you can't access it in the first place, ABTO created a nice little DLL that helps you with paths that are too long. The trial version has enough functionality to work. Download it HERE.

If you want to do other functions besides move a file, check out the File Management Functions documentation and alter the above code.

References:
Kim Hamilton

Thursday, July 22, 2010

EncodingTool Class: Detects, Checks for Consistency, and Converts Encoding - C#

Download the class here.
The following class can identify encoding in a file, check a file for consistent encoding, and convert encoding in a file.
class EncodingTool
    {
        //@Return: Returns the name of the encoding of the file at filePath
        public static string GetFileEncoding(string filePath)
        {
            FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            string encoding = null;

            try
            {

                if (file.CanSeek) //if file is readable
                {
                    byte[] bom = new byte[4]; //getting Byte-order mark

                    file.Read(bom, 0, 4);

                    if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)                     //utf-8
                        encoding = "UTF-8";
                    else if (bom[0] == 0xff && bom[1] == 0xfe)                                  // ucs-2le, ucs-4le, and ucs-16le
                        encoding = "UCS-21e, UCS-41e, and UCS-161e";
                    else if (bom[0] == 0xfe && bom[1] == 0xff)                                  // utf-16 and ucs-2
                        encoding = "UTF-16 and UCS-2";
                    else if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)    //ucs-4
                        encoding = "UCS-4";
                    else                                                                        //DEFAULT: ASCII
                        encoding = "ASCII";
                }

            }
            catch (Exception e)
            { Console.Error.WriteLine("ERROR: " + e.Message); }
            finally
            {
                if (file != null)
                    file.Close();
            }

            return encoding;
        }

        //@Return: Returns true if encoding is consistant throughout the file, else print all encodings
        public static bool ConsistantEncoding(string filePath)
        {
            FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            string encoding = null;
            bool consistant = true;

            try
            {

                if (file.CanSeek) //if file is readable
                {

                    byte[] bom = new byte[4]; //getting Byte-order mark

                    while (file.Read(bom, 0, 4) != 0)
                    {
                        if (encoding == null)
                        {
                            if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)                     //utf-8
                                encoding = "UTF-8";
                            else if (bom[0] == 0xff && bom[1] == 0xfe)                                  // ucs-2le, ucs-4le, and ucs-16le
                                encoding = "UCS-21e, UCS-41e, and UCS-161e";
                            else if (bom[0] == 0xfe && bom[1] == 0xff)                                  // utf-16 and ucs-2
                                encoding = "UTF-16 and UCS-2";
                            else if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)    //ucs-4
                                encoding = "UCS-4";
                            else                                                                        //DEFAULT: ASCII
                                encoding = "ASCII";
                        }
                        else
                        {
                            if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)                     //utf-8
                            {
                                if (encoding != "UTF-8")
                                    consistant = false;
                            }
                            else if (bom[0] == 0xff && bom[1] == 0xfe)                                  // ucs-2le, ucs-4le, and ucs-16le
                                if (encoding != "UCS-21e, UCS-41e, and UCS-161e")
                                    consistant = false;
                                else if (bom[0] == 0xfe && bom[1] == 0xff)                                  // utf-16 and ucs-2
                                    if (encoding != "UTF-16 and UCS-2")
                                        consistant = false;
                                    else if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)    //ucs-4
                                        if (encoding != "UCS-4")
                                            consistant = false;
                                        else                                                                        //DEFAULT: ASCII
                                            if (encoding != "ASCII")
                                                consistant = false;
                        }
                    }
                }

            }
            catch (Exception e)
            { Console.Error.WriteLine("ERROR: " + e.Message); }
            finally
            {
                if (file != null)
                    file.Close();
            }

            return consistant;

        }

        //@Done: Converts a file to specified unicode and names it: Name_Encoding_.txt
        public static void ConvertEncoding(string filePath, Encoding enc)
        {
            StreamReader sr = new StreamReader(filePath);
            StreamWriter sw = new StreamWriter(filePath.Substring(0, filePath.Length - 4) + "_" + enc.EncodingName + ".txt", true, enc);

            string line;
            using (sw)
            {
                while ((line = sr.ReadLine()) != null)
                {
                    sw.WriteLine(line);
                }
            }
        }
    }

Download the class here.