Random programming things I'd want to remember

Saturday, March 3, 2012

XCode 4.2: how to provide arguments to a command-line app

Here is how to provide arguments to a program in XCode 4.2:
Product -> Edit Scheme

Click on "Run" in the left-side part of the dialog box. If needed, switch to "Arguments" tab in the middle part of the dialog box. Under "Arguments Passed On Launch", press "+" and add a value. Then you can access the value using the following code:
NSProcessInfo *proc = [NSProcessInfo processInfo];
NSArray *args = [proc arguments];
NSString *myArg = [args objectAtPosition: 1];

One note worth mentioning: if your argument contains spaces, enclose it in quotes: "my argument".

Thursday, March 1, 2012

Compile Objective-C code From Terminal With ARC Using xcodebuild

I was perfectly happy compiling Objective-C from the command line until I hit a snag. As a result, I found out about xcodebuild from Joshua Smith . I am still in the process of researching this tool, but so far I use it this way:

xcodebuild -project yourprojectname.xcodeproj -alltargets


and then I use the
./yourprojectname 

command to start the app (I can customize the name of the executable in the project settings).

One thing to note: the xcodebuild command needs the .xcodeproj file to run, so you need to provide the path to the .xcodeproj file when issuing the command.

Wednesday, February 29, 2012

Changing current directory with NSFileManager




NSFileManager *fm;

fm = [NSFileManager defaultManager];


if ([fm changeCurrentDirectoryPath: @"/Users/yourUserNameHere/Documents/folder1/folder2"] == NO)
   NSLog (@"Something went wrong");
//if you don't see the "Something went wrong" message, then the current directory has beed changed, here is how to check it:

NSLog(@"%@", [fm currentDirectoryPath]);




Taking screenshots on a Mac

Tuesday, February 28, 2012

Where XCode keeps compiled files

If using XCode (4.2) and needing to work with path-sensitive code, here is how you can find out where the executables are kept:

File -> Project Settings...

Then click the little arrow by the path under "Derived Data Location" (closer to Advanced button in XCode 4.2). There you will see folders with compiled files. If needed to add some path-sensitive data, insert it under the following path:

Project Name + some gibberish/Build/Products/Debug

Monday, February 27, 2012

Compile Objective-C code From Terminal With ARC Using Clang

In order to compile Objective-C in the terminal, here is what needs to be done: 1. Open Terminal window 2. If using XCode to get the autocompletion and syntax highlighting, navigate to the project folder (using "cd" commands to change directory and "ls" command to see what's in the current folder; use "cd .." to go up one level if went somewhere wrong). 2. If using terminal to write code as well, open vi and type in the code. 3. Enter the following line of code at the command line while the terminal window is in the same folder as your .m and .h files:
clang -fobjc-arc -framework Foundation main.m -o yourAppName

This will compile the code into an executable file. To run, type
open yourAppName

at the command line. Alternatively, you can run your program by typing the program name preceded with ./ like so:
./yourAppName

Or you can add the name and path to your program to the PATH variable of your system.

Sunday, February 26, 2012

Objective-C: String to number to object conversion notes

So you need to add a double into an NSMutableArray, but you cannot do it directly because double is not an object. Here is how you wrap a double into an object type of NSNumber and add it to the array:
//create and initialize array:
NSMutableArray *arr = [NSMutableArray array];

//add your double to the array:
[arr addObject:[NSNumber numberWithDouble:yourNumber]];

//Here is how you unwrap it back into a double:
NSNumber *someNumber = [arr lastObject];

double d = [someNumber doubleValue];

Here is another handy trick, it turns out that NSString has a method that tries to return a double from the string itself. Here is how you write it:
NSString *myString = @"5.0";
double result = [myString doubleValue]; //I bet there is also a method to return an integer in the same manner

Here is how a double is converted to an NSString object:
NSString *resultString = [NSString stringWithFormat:@"%g", result];

Saturday, February 25, 2012

Objective-C, display object info using %@ format

If needed to display the object info in an informative manner using %@ format, implement the "description" method, like so:

-(NSString *) description
{
   return [NSString stringWithFormat: @"%i/%i/%i", _month, _day, _year];
}


The idea is the same as, for example, overriding ToString() method in C#.

Thursday, February 23, 2012

Create new object with some default parameters and NSMutable array to hold items


@interface Playlist : NSObject
@property (nonatomic, copy) NSString *playListName;
@property (nonatomic, strong) NSMutableArray *songs;

-(void)addSong:(Song *) aSong;
-(void)removeSong:(Song *)aSong;
-(id)initWithPlayListName:(NSString *)newPlayListName;
@end

//#import "Playlist.h"

@implementation Playlist
@synthesize playListName = _playListName, songs=_songs;

-(void)addSong:(Song *)aSong
{
    [_songs addObject:aSong];
}

-(void)removeSong:(Song *)aSong
{
    [_songs removeObject:aSong];
}

-(id)initWithPlayListName:(NSString *)newPlayListName
{
    self = [super init];
    if(self)
    {
        _playListName = newPlayListName;
        _songs = [NSMutableArray array];
    }
    
    return self;
}
@end

Objective-C tidbits

Sort an NSArray of NSIntegers:

NSArray *sorted = [arr sortedArrayUsingSelector:@selector(compare:)];


Iterate over items in NSCountedSet:

for(id i in set)
{
      NSLog(@"Item %@, count: %lu", i, (unsigned long)[set countForObject:i]);
}

Wednesday, December 28, 2011

Extract data from Excel into binary text file

In an Excel document (.xlsx, for .xls use
connStr
as the connection string), name one worksheet "Questions" and another "Answers" (no quotes in both cases). All columns are in Excel General format; the allowable format is noted in parentheses: Questions worksheet: QuestionID (Number), QuestionText (Text) Answers worksheet: AnswerID (Number), AnswerText (Text), QuestionID (Number), Correct (Text, but the only acceptable values are TRUE and FALSE).
var fileName = string.Format("{0}\\dbase.xlsx", Directory.GetCurrentDirectory());
var connStr = string.Format(
  "Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);
var connectionString = string.Format(
  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;", fileName);
var adapter1 = new OleDbDataAdapter("SELECT * FROM [Questions$]", connectionString);
var adapter2 = new OleDbDataAdapter("SELECT * FROM [Answers$]", connectionString);

var ds = new DataSet();

adapter1.Fill(ds, "Questions");
adapter2.Fill(ds, "Answers");

var answersTable = ds.Tables["Answers"].AsEnumerable();
var questionsTable = ds.Tables["Questions"].AsEnumerable();

List answers = answersTable.Where(
  x => x.Field("AnswerID") > 0.0).Select(x =>
     new TestQuestionAnswer
     {
         AnswerID = Convert.ToInt32(x.Field("AnswerID")),
         AnswerText = x.Field("AnswerText"),
         AnswerCorrect = x.Field("Correct"),
         QuestionID = Convert.ToInt32(x.Field("QuestionID"))
     }).ToList();

List questions = questionsTable.Where(
  x => x.Field("QuestionID") > 0.0).Select(x =>
    new TestQuestion
    {
        QuestionID = Convert.ToInt32(x.Field("QuestionID")),
        QuestionText = x.Field("QuestionText")
    }).ToList();


foreach (TestQuestion tq in questions)
{
    List a = answers.Where(x => x.QuestionID == tq.QuestionID).ToList();
    tq.Answers = a;
}

using (FileStream fs = new FileStream("data.dat", FileMode.Create))
{
    using (var writer = new BinaryWriter(fs))
    {
        writer.WriteList(questions);
    }
}
//fix for Syntax Highlighter plugin glitch:
//

Adjust the label text size of radiobutton or checkbox


txtQuestion.Text = "Question text";
int answerBreak = panel1.Height / NUMBEROFANSWERS;
int runningHeightTotal = 0;

panel1.Controls.Clear();

for (int i = 0; i < NUMBEROFANSWERS; i++)
{

  Font stringFont = new System.Drawing.Font(
    SystemFonts.DefaultFont.ToString(), 12, System.Drawing.FontStyle.Regular);

  //Gotcha 1: MeasureText does not produce accurate results
  Size textSize = TextRenderer.MeasureText(
    testQuestions[currentlyTestedQuestionID].Answers[i].AnswerText, stringFont);


  RadioButton rb = new RadioButton();
  rb.Tag = testQuestions[currentlyTestedQuestionID].Answers[i].AnswerID;

  //shift down if the previous answer took too much room
  rb.Location = new System.Drawing.Point(5, 
    (i * answerBreak >= runningHeightTotal ? i * answerBreak : runningHeightTotal + 5));
  rb.Text = testQuestions[currentlyTestedQuestionID].Answers[i].AnswerText;
  rb.Size = new System.Drawing.Size(textSize.Width, textSize.Height);
  rb.Font = stringFont;

  using (Graphics g = rb.CreateGraphics())
  {
    bool height = false;
    int linesNumber = 1;
    //so the text size is measured using the Graphics object
    Size s = g.MeasureString(rb.Text, rb.Font).ToSize();
    if (rb.Size.Height <= s.Height)
      height = true;

    if (s.Width > PANEL1WIDTH)
    {
      linesNumber = s.Width / 750;
      if (s.Width % PANEL1WIDTH > 0)
        linesNumber += 1;
    }

    rb.Size = new Size(PANEL1WIDTH, 
      height ? (s.Height + 5) * linesNumber : rb.Size.Height * linesNumber);
  }

  runningHeightTotal += rb.Height;

  panel1.Controls.Add(rb);
}
If checkbox's size needs to be adjusted, declare rb as a CheckBox, and everything will work just as well.

Get Stream for a BinaryReader for a file embedded in exe


public Stream GetDataFileStream(string resourceName)
  {
    foreach (string currentResource in System.Reflection.Assembly.
        GetExecutingAssembly().GetManifestResourceNames())
      if (currentResource.LastIndexOf(resourceName) != -1)
        {
          return System.Reflection.Assembly.GetExecutingAssembly().
            GetManifestResourceStream(currentResource);
        }

        throw new Exception("Resource not found : " + resourceName);
  }


//Then call this code:

using (BinaryReader reader = new BinaryReader(GetDataFileStream("data.dat")))
  {
    yourDataContainter = reader.ReadOrWhatever();
  }

In solution explorer, right-click on the text file that you want to embed, select "Properties". Under "Build Action", select "Embedded Resource", build solution.

How to extract embedded image/Как достать картинку из скомпилированного exe

Image _image;
List resourcenames = new List();//syntax highlighter glitch fix: 
resourcenames.AddRange(Assembly.GetExecutingAssembly().GetManifestResourceNames());
string resourcename = resourcenames.Find(
   delegate(string item) 
      { return item.EndsWith("template1.jpg"); });
Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcename);
BinaryReader br = new BinaryReader(s);

MemoryStream ms = new MemoryStream(br.ReadBytes((int)s.Length));
_image = Image.FromStream(ms);
br.Close();
s.Close();
br.Dispose();
s.Dispose();

In solution explorer, right-click on the text file that you want to embed, select "Properties". Under "Build Action", select "Embedded Resource", build solution.

How to extract embedded text file/Как достать текстовый файл, скомпилированный в exe

        public static string ExtractResource(string resourceName)
        {
            foreach (string currentResource in System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames())
                if (currentResource.LastIndexOf(resourceName) != -1)
                {
                    string fqnTempFile = System.IO.Path.GetTempFileName();
                    string path = System.IO.Path.GetDirectoryName(fqnTempFile);
                    string rootName = System.IO.Path.GetFileNameWithoutExtension(fqnTempFile);
                    string destFile = path + @"\" + rootName + "." +
                    System.IO.Path.GetExtension(currentResource);

                    System.IO.Stream fs = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(currentResource);

                    byte[] buff = new byte[fs.Length];
                    fs.Read(buff, 0, (int)fs.Length);
                    fs.Close();

                    System.IO.FileStream destStream = new System.IO.FileStream(destFile,FileMode.Create);
                    destStream.Write(buff, 0, buff.Length);
                    destStream.Close();

                    return destFile;
                }

            throw new Exception("Resource not found : " + resourceName);

        }
In solution explorer, right-click on the text file that you want to embed, select "Properties". Under "Build Action", select "Embedded Resource", build solution.

Thursday, September 22, 2011

How to set Google Chrome's search engine to Google US English by default

Instructions are taken from here: http://www.google.com/support/forum/p/Chrome/thread?tid=581638f51350589b&hl=en
1. Go to Wrench > Options (Preferences on Mac) > Basics > Search > Manage
2. Select Add ("+" sign on Mac)
3. For "Name" and "Keyword," type whatever
4. For "URL," enter http://www.google.com/search?q=%s
5. Click O.K. 6. Select your new search engine from the list and click "Make default"
7. Click "Close"

Generate random text in Microsoft Word

If you type in =rand(10,10) in Microsoft Word, it will generate 10 paragraphs of text with 10 sentences in each paragraph. Looks like the text is coming from Word help files, but it is a great feature.

Thursday, September 15, 2011

How to make bootable USB flash drive with just Windows

diskpart
select disk 1
clean
create partition primary
select partition 1
active
format fs=fat32
assign
exit

xcopy d:\*.* /s/e/f e:\

From: http://www.techmixer.com/install-windows-vista-from-bootable-usb-flash-memory-drive/

Sunday, July 3, 2011

Firefox 5 tab scrolling issue

I like seeing all my tabs at once in my browser, so here is how I did it in Firefox 5:

1. Open Firefox menu -> Help -> Troubleshooting information.
2. Click "Open Containing Folder" button.
3. Create "chrome" folder inside the folder that had just opened up after step 2.
4. Create a text file, call it "userChrome.css" and paste the following into it:

@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); 

.tabbrowser-tab {min-width: 016px !important;}
.tabbrowser-tab {clip-width: 016px !important;} 

Saturday, July 2, 2011

Windows Phone 7: Iterate over ListBoxItems in a list box to focus on a control

I am developing a small Windows Phone 7 application and I ran into a wall. I am updating items within a ListBox and I need to set focus on a control after the update. It turns out that it is not a trivial task -- I need to iterate over the entire list box to get to the control and then set focus on it.

Here is the XAML of my list box:

     
                
                    
                        
                            
                            
                            
                        
                    
                

            

I was using the VisualTreeHelper class to iterate over the ListBox, but I was not getting to the ListBoxItems. I was getting ListBox, ScrollViewer, Border, Grid, ContentPresenter, ItemsPresenter, VirtualizingStackPanel, and other controls, but no ListBoxItems. After reading this, the problem was fixed by calling the
UpdateLayout
method. The rest was simple. Here is my code.

void SetFocusOnTextBox(DependencyObject element, int primaryKey)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
            {
                if (foundItem == false) //global flag that stops processing once the control is found
                {
                    DependencyObject child = VisualTreeHelper.GetChild(element, i);
                    StackPanel s = child as StackPanel;
                    if (s != null)
                        FindAllChildrenOfAStackPanel(child, primaryKey);
                    if (foundItem == false)
                        SetFocusOnTextBox(child, primaryKey);
                    else
                        break;
                }
            }
            foundItem = false;
        }

        void FindAllChildrenOfAStackPanel(DependencyObject element, int primaryKey)
        {
            StackPanel s = element as StackPanel;
            if (s != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
                {
                    TextBox t = VisualTreeHelper.GetChild(element, i) as TextBox;
                    if (t != null)
                        if (t.Name == primaryKey)
                        {
                            t.Focus();
                            foundItem = true;
                            break;
                        }
                }
            }
        }