Sunday, October 14, 2012

Working with the console: Part 2 - Mac compiling

When compiling on my program on my Macbook I had a few problems. The first thing I ran into was the entire program would not compile using the terminal compiler. When I complied I would receive this error:

cio_test.cpp:959: error: cast from 'const char*' to 'int' loses precision

Well compiler, I think that's fine! Why did you not just do it? So, I thought to myself: what could be the problem? The only thing I could think of is the architecture (being x86_64) doesn't like that happening. Thus, I looked in the c++ manual file. lo and behold there is an option for architecture in it. It says there are only four different variables that it could be: ppc, ppc64, i386, and x86_64. Well, I know my current architecture is x86_64, so let's try i386 which is for 32-bit applications:

c++ cio_test.cpp bconsole.cpp console.cpp -lcurses -arch i386

 It worked!

Okay now how do I get Xcode to work? I know I can select i386 to my compile in the drop down box in the top left; however, when I compile, I receive 13 errors. Uh oh, what now? I know the program requires the lcurses library. If I could find the location of this library maybe I could add it to my project.

A quick Google search later tells me that all the library files are located in /usr/lib/. Thats great, but according to Finder the /usr directory doesn't exist, so I can't just drag and drop it in to my project. Another Google search tells me that /usr and a few other folders are considered hidden files in finder. So, by using the following command in the terminal you can unhide them:

sudo chflags nohidden /usr

Now I can see the files in finder. After looking in /usr/lib I find what I'm looking for, it is a file called libcurses.dylib. That file is then dragged into the source for my project and bam! No more compile errors. To run the file you have to click on the terminal application created under Products, but everything seems to work!

Huzzah!

Wednesday, October 10, 2012

Working with the console: Part 1

I spent some time trying to revise some of the code Fardad showed us in the edit() method. I decided that I would fiddle around with the switch case for left and right just for fun. While going through this editing, I went through a few iterations.
I started with this for the right arrow:

*curPosition += (*curPosition < fieldLength);

However, this did not work for shifting over the offset value. With that in mind, I attempted something along the lines of this:

*curPosition < fieldLength ? *curPosition++ : (*strOffset += (*strOffset < fieldLength));

Well, Let's just say this code did not work at all! Once it reached the end of the fieldLength the cursor would never move back. I'm sure it has something to do with it being an else case rather than an else if. Anyway, I don't think it would  be any faster than the regular format. I then dropped that idea all together and went with a more simple approach.  Here it is:

*curPosition += (*curPosition < fieldLength);
*strOffset += (*curPosition == fieldLength) && (*strOffset < fieldLength);

I might be able to refine it some more. Maybe even get it on one line, but for now I think it looks much more awesome.

Saturday, September 8, 2012