开发者

What are the minimum changes necessary to port ANSI C to Objective C?

开发者 https://www.devze.com 2023-03-26 23:26 出处:网络
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 ch

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 = 开发者_StackOverflow社区'%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;
        }
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号