0

As a Mac Newbie I need to convert about 5,000 lines of ANSI C to Objective C to be used in an iPad app. Most of the code is similar to the example below. To save time and minimize bugs I want to only change the original C code when absolutely necessary to port over to Obj C. To help me understand the conversion, what portions of the code below must be changed to connect into the iPhone/iPad User Interface? Any guidance would really be appreciated. Thanks for the help.

void GetLandingSpeeds(LandingSpeeds *mySpeeds, int PhenomType, LandingInterpolationParameters *InterParams, char *FlapLand, char *WingStab)
{

    mySpeeds->LandingSpeedsOK = No;

    sprintf(query_string,"SELECT * FROM Landing_Speeds WHERE Weight = %d AND FlapLand = '%s' AND WingStab = '%s'", InterParams->Weight_lower, FlapLand, WingStab);
    Query* GetFlapsSpeeds_Lower = sql_select_query(query_string, AircraftDatabase);

    if (InterParams->Weight_Interpolation_Percent == 0)
    {
        // single table

        if (GetFlapsSpeeds_Lower->RecordCount == 1) 
        {
            mySpeeds->Vac = atoi(sql_item(GetFlapsSpeeds_Lower, "Vac"));
            mySpeeds->Vref = atoi(sql_item(GetFlapsSpeeds_Lower, "Vref"));
            if (PhenomType == P300)
                mySpeeds->Vfs = atoi(sql_item(GetFlapsSpeeds_Lower, "Vfs"));
            mySpeeds->LandingSpeedsOK = Yes;
        }
    }
    else
    {
        // simple linear interpolation        
        sprintf(query_string,"SELECT * FROM Landing_Speeds WHERE Weight = %d AND FlapLand = '%s' AND WingStab = '%s'", InterParams->Weight_upper, FlapLand, WingStab);
        Query* GetFlapsSpeeds_Upper = sql_select_query(query_string, AircraftDatabase);

        if ( (GetFlapsSpeeds_Lower->RecordCount == 1) && (GetFlapsSpeeds_Upper->RecordCount == 1) )
        {
            mySpeeds->Vac = atoi(sql_item(GetFlapsSpeeds_Lower, "Vac")) * (1-InterParams->Weight_Interpolation_Percent) + atoi(sql_item(GetFlapsSpeeds_Upper, "Vac")) * InterParams->Weight_Interpolation_Percent;
            mySpeeds->Vref = atoi(sql_item(GetFlapsSpeeds_Lower, "Vref")) * (1-InterParams->Weight_Interpolation_Percent) + atoi(sql_item(GetFlapsSpeeds_Upper, "Vref")) * InterParams->Weight_Interpolation_Percent;
            if (PhenomType == P300)
                mySpeeds->Vfs = atoi(sql_item(GetFlapsSpeeds_Lower, "Vfs")) * (1 - InterParams->Weight_Interpolation_Percent) + atoi(sql_item(GetFlapsSpeeds_Upper, "Vfs")) * InterParams->Weight_Interpolation_Percent;
            mySpeeds->LandingSpeedsOK = Yes;
        }
    }
}
MonkeyBusiness
  • 379
  • 1
  • 4
  • 16

1 Answers1

10

Objective-C is a pure superset of C. You should not have to change anything. What problems are you seeing?

EDIT

Do a search for "Model-View-Controller" or "MVC." This is the heart of iOS programming. The Model classes are highly reusable across platforms, and can be in C without any trouble. What you posted above is classic Model code. You ask it for data; it gives you data.

View classes are highly platform-specific, and you get most of them from iOS itself. These are the buttons and graphs and images and the like. They just know how to draw data on the screen.

The Controller classes glue the two together and are what you will need to write from scratch in Objective-C. They ask for data from the Model (C) code, and update the Views (ObjC).

Provided your 5000 lines of C are mostly Model code (and it sounds like it is from your description), it should drop in easily. You just need to write Objective-C to manage the UI.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Well, I'm just getting started but don't I have to change functions to methods using the correct syntax, reorganize my structures into instantiated variables, etc. I was also told I would need to re-write the sql queries. I would love to find a simple guide for porting C to Obj C. – MonkeyBusiness Aug 11 '11 at 03:06
  • 2
    There is no requirement to port C to Objective-C. Objective-C is exactly a superset of C. Everything you can do in C, will run exactly the same in Objective-C with no changes. I can't imagine why you'd need to rewrite SQL. That's neither C nor Objective-C. You'd only need to do that if you're changing your SQL backend. You should not just convert functions to methods. Methods are part of object-oriented programming. If you're convert to methods, you need to re-architect, not port. But unless you want OOP, there's no reason to do that. – Rob Napier Aug 11 '11 at 03:12
  • Note that you must write anything that talks to Cocoa in Objective-C, which means you should definitely write your UI in Objective-C, but that's not portable code anyway, so there's no need to port anything. – Rob Napier Aug 11 '11 at 03:14
  • Interesting. That's not been my understanding. I simply want to get my C code to communicate with the iPad User Interface SDK. So I can have my C code communicate directly with View Controllers, etc? That would save me a lot of time if it could. But I was told iPad apps must be Objective C. That's wrong? – MonkeyBusiness Aug 11 '11 at 03:21
  • 3
    I have a square, but somebody told me I need a rectangle. Do I need to replace my square? – R.. GitHub STOP HELPING ICE Aug 11 '11 at 03:27