Archive

Archive for the ‘Coding’ Category

Cleaning up those CRT warnings in Visual Studio

June 17th, 2010

When you need to clean up the CRT warnings about unsecure functions, like printf or sprintf, in favor for secure functions like printf_s or sprintf_s, you don’t have to scour through all of your code to clean up the warnings.

You can declare the following for Visual Studio builds and the functions are overridden with templated versions that are secure:
#ifdef _MSC_VER
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#endif

This is useful if you have a C++ cross platform file where you don’t want to create macros to have one function for the Mac and the other version for Windows.

Jaime Coding , , ,

Using regex to replace a path

February 8th, 2010

This is for me, not you, a reference for me. So if you are wondering why I wrote this… it’s because it’s for me.

Here is a regex string I used in TextWrangler to search and replace paths in a Xcode project file… you know, the one that is inside the .xcodeproj bundle…

Search string:
\.\./\.\./development/.+/mac/macos_10\.4/dylib/

Replace with:
Whatever you like…

Jaime Coding ,

What’s gonna work?

February 7th, 2010

My son used to love the TV show, “The Wonder Pets”. In that show they have a recurring theme and a song to go along with it: “What’s gonna work… team-work!”.

Now for those who are new here, or who don’t already know, I work in software development. These days, almost all types of development require some form of team work, except for the poor guys who used to work at 3D Realms that were expected to code like they were living in the 90’s.

Splitting work between team members who have the same skill set is an difficult task. The reason being is that most developers tend to have a different style of programming between one another. So when one developer encounters another developers code, they may break “Rule 0″ from the “C++ Coding Standards : Rules, Guidelines, and Best Practices” book, written by Herb Shutter and Andrei Alexandrescu.

Rule 0: Don’t sweat the small stuff
Wow! This rule is so simple… so powerful… So, why is it so hard to live by?

My guess is that for many developers perusing through code, the setup and layout of the code makes it easy, or hard, to read. So, if you are used to prefixing your pointer variables with a lower-case p and someone on your team is using plain words to describe the variable, I can understand how it hurts trying to read someone else’s code that may or may not jive with your brain. Comfy code formatting is like the cozy blue blanket for developers… Stop sucking your thumb!

Dev’ies are willing to spend the time, and shamefully submit code, that is may only be different by a couple of spaces or tabs. I will admit that I have broken this rule myself… with a big grin on my face while I’m committing such offense, but in the few cases where I have done this, it has been with code that has had it’s ownership transferred to me and I have authoritative rights to do with it as I please :) So there!

Now back to team-work…

Working with other team members can be tough since it means we have to give up a part of ourselves to work with others and sometimes compromise our workflow so that everyone is happy and that no one feels negated.

Likewise, the ‘OTHERS’ have to give a little to receive a little… but remember that they can’t read your mind, so, you too have to let them know how you feel and respectfully tell them your ideas. I’m not suggesting that you have a Jerry Springer like meeting where people show how they feel deep down inside when you altered their code or comments and start flinging chairs at each other. Save that for TV where you can get paid for doing that!

I’m suggesting that if something doesn’t jive, let your team know, but think before you speak… Ask yourself, was there a good reason that developer took out your code or changed your comments or decided to change the spacing? You may be surprised with the answer. And if the answer is in violation of Rule 0, then smack them over the head with official “Offenses to Coding” baseball bat! It’s both fun and theraputic ;)

Till next time, happy batting… I mean… Happy Coding!

Jaime Coding, What's New

“If the comments are ugly, the code is ugly”

November 17th, 2009

I read this and said to myself, this is so true.

Thinking about this, if you don’t take the time to think about the comments, there is a good chance that the code below/next to/above the comments wasn’t well thought out either.

Jaime Coding , , ,

Xcode and static libs

October 19th, 2009

I’m writing this blog entry just in case other people come across the same problem and can’t seem to figure out what’s going on.

I found it in Xcode 2.5 that hasn’t been fixed in the latest version of Xcode that I have, version 3.1.3.

The bug I’m referring to is when you create a target as a static lib output, then you change the target later to output a dylib. The problem is that everything looks fine… heck, it even puts a dylib in the file name extension, but the file is actually a static library.

You have to go to Terminal.app and use the file command to see that the file is in fact, a static lib and not a dylib. You may not notice the problem at first until you go to debug the project and notice some weirdness.

Hopefully, you haven’t gotten bit by this problem, but if you have, the only solution I found was to create a new target from scratch.

Jaime Coding, Not-so-funny , , , , ,

Dumping file names to a file

August 12th, 2009

I have a PC and a Mac that I work with, but mostly I use the Mac.

Of course on the Mac, you can do a lot with the Terminal.app and Bash. For example, you can dump out a file listing into a text file:

ls *.cpp > ../implementation_files.txt

Jaime Coding

Print to PDF with AppleScript and Microsoft Word

April 5th, 2009

Recently, I needed to automate printing a Word document to a PDF file and have it saved to a predefined location. There were some good articles on the web about how to do the printing part, or the PDF part but not how to save the output.

So, here’s my stab at printing a Word document to a PDF file in a known location:

?View Code APPLESCRIPT
-- This AppleScript will open up our Word doc and save it as a PDF
 
set myDoc to "/Users/jaimerios/Documents/Developer notes.doc"
set pdfSavePath to "/Users/jaimerios/Documents/PDFs/"
 
tell application "Microsoft Word"
	activate
	open myDoc
end tell
 
tell application "System Events"
	tell process "Microsoft Word"
		-- Press command+p to open our print dialog
		keystroke "p" using command down
 
		-- Let's make sure our print dialog is up
		repeat until exists window "Print"
		end repeat
 
		-- Click the PDF menu button
		click menu button "PDF" of window "Print"
 
		-- Make sure the menu is up
		repeat until exists menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
		end repeat
		-- Select the "Save as PDF" menu item
		click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
 
		-- Make sure the save dialog is visible
		repeat until exists window "Save"
		end repeat
 
		-- Press command+shift+g to show the "Go" drop down sheet
		keystroke "g" using {command down, shift down}
		-- Set our location field to our pdfSavePath
		set value of text field 1 of sheet of window "Save" to pdfSavePath
		-- Now click the Go button
		click button "Go" of sheet of window "Save"
 
		-- Now that we are in our desired folder, set the file name and save
		set value of text field 1 of window "Save" to "Special Developer Notes.pdf"
 
		click button "Save" of window "Save"
	end tell
end tell

Now the only limitation to this script is that it doesn’t handle the condition where the PDF file might already exist in our output location.

I have to give credit to the original author who came up with tis script. Their AppleScript code is located at this website link.

Well, I hope this helps you and if you have any comments to make this script better, let me know.

Happy coding!

Jaime Coding , , ,

Xcode 3.1, Perforce and Scripts

March 9th, 2009

In my previous post, I showed how you can leverage AppleScript to check out a file or a project that resides on a Perforce server. The reason for this is that Xcode 3.x does not work well with Perforce.

Now, I figured out how to do this with the build in scripts menu in Xcode. The limitations I found so far is that you can only check out a file, can’t do a project yet, and that the cursor has to be in the file that you want to check out.

So I created a script, named it “Check out file” and put it in a section named “Gravy” since that is what I work with most. The script code is:

#!/bin/sh
# This shell script checks out the current file from Perforce, so long as it has the cursor in it
 
# Get the file's full path
FULL_FILE_PATH="%%%{PBXFilePath}%%%"
 
# Check to make sure it exists
if [ -f "$FULL_FILE_PATH" ]
then
	# Separate the filename and the path
	SRC_FILE=${FULL_FILE_PATH##*/}
	FILE_PATH=${FULL_FILE_PATH%/*}
 
	# Go to the folder and invoke the p4 command line app with the appropriate arguments
	cd "$FILE_PATH"
	/usr/local/bin/p4 -cjrios_My_Perforce_ClientName -pperforce.jaimerios.com:1666 -PHah -ujaimer edit "$SRC_FILE"
 
# This is a hack to get Xcode to recognize that the file was checked out
# Notice that the code is not indented
osascript - "$1" << ENDOFSCRIPT
tell application "Xcode"
	set myFile to associated file name of front window
	save myFile
end tell
tell application "Finder"
	activate
end tell
tell application "Xcode"
	activate
end tell
ENDOFSCRIPT
 
else
	echo File not found:"$SRC_FILE"
fi

For the output and error options, I set them to “Display in Alert”.

And that’s it. I hope this helps you and if you have any comments for improving this, post a comment and let me know.

Happy coding!

Reference:
Xcode shell scripting

Jaime Coding , ,

Xcode 3.1 and Perforce

March 7th, 2009

I use Perforce and I love it! Over the years I’ve used Visual SourceSafe, CVS, SVN and I experimented with other tools, but none of them have come close to the usefulness of Perforce.

I also use Xcode for my Mac coding but when you use Xcode with Perforce, you get some wierd behaviour. Xcode get’s hung, for no reason whatsoever and although Apple seems to know this is a problem, it doesn’t seem like they are fixing this problem any time soon.

Lucky for me you can use AppleScript from within Xcode! I usually work on one project at a time, so I can create an AppleScript that checks out the frontmost project file for me and another AppleScript to check out the currently selected source code file.

When you open Xcode you’ll notice an AppleScript icon in the menubar between Window and Help, but this is not an AppleScript drop down. This is just for the General Scripts that can be invoked from Xcode. So, if you want to run a shell script from Xcode, you can use this scripts menu for doing that, but for me, I wanted to leverage AppleScript for what I want to do.

You think this is for AppleScript, but it's not.

So, to make the AppleScript menu available, you have to open the AppleScript Utility in “/Applications/AppleScript/”. Open that program and click the “Show Script menu in menu bar” checkbox.

Use this to make the Script menu available

Once you do that, you should see an AppleScript menu item by the other icons on the left side of your menubar.

Now, open the “Script Editor” program within the “/Applications/AppleScript” folder. Copy the code below, which attempts to check out the frontmost project file from Perforce:

?View Code APPLESCRIPT
-- Check out the frontmost project
tell application "Xcode"
set myProj to (active project document)
set myCmd to full path of myProj
end tell
 
set myCmd to "/usr/local/bin/p4 -cjrios_My_Perforce_ClientName -pperforce.jaimerios.com:1666 -PHah -ujaimer edit " & myCmd & "/..."
 
do shell script myCmd

Basically, the script gets the frontmost project file, sets a variable to the full path to the project file and then I create a string with the command that I would normally invoke in Terminal.app. The changes you need to make to the command line are:

  • The -c option which tells p4 which client you are working in
  • The -p option which is the name, or ip, with TCP port to the perforce server
  • The -P option which is your password to the server and
  • The -u option which is your username

Notice that I don’t have a space between the switch and the value and that I have the full path to p4 spelled out.

Paste the code into a new script in “Script Editor” and save the script, with the name of your choice, into the “/Users/jaimerios/Library/Scripts/Applications/Xcode” folder. Once you go back to Xcode, you will see your script in the menu item.

Now the following script is almost the same thing, except for it checks out the current file that is selected in the project. Repeat the same steps for creating the project check out script for the script code below:

?View Code APPLESCRIPT
-- Check out the currently selected file
tell application "Xcode"
 
set myFile to associated file name of front window
set textDocuments to text documents
repeat with i in textDocuments
if path of i is myFile then
 
set myCmd to path of i
set myCmd to "/usr/local/bin/p4 -cjrios_My_Perforce_ClientName -pperforce.jaimerios.com:1666 -PHah -ujaimer edit " & myCmd
do shell script myCmd
end if
end repeat
 
end tell

I hope these scripts help you out. If you have any suggestions for making these scripts better, post a comment and let me know.

Happy coding!

Jaime Coding , ,

Weird EXC_BAD_ACCESS error with NSPopAutoreleasePool

February 25th, 2009

Recently I worked on a project that had a NIB and XIB in it and for some strange reason I was getting a EXC_BAD_ACCESS error message in GDB with NSPopAutoReleasepool being the culprit. The best part about the problem was that the only indicator I had for the call stack was was four question marks ‘????’. That didn’t make too much sense.

Luckily, I came across this posting on cocoadev: click here

The point to the article was how to intelligently look at the error and get a call stack from code that is normally hidden from you in Cocoa. This article shows you how to turn on those extra debugging options so that you can figure out where you are causing the problem.

In my case, I was double-free-ing an object that was already being handled by NSAutoreleasePool.

Jaime Coding , , ,