I have this code and I wont to optimize them:
here is a scrolling menu with max 15 line and loop must run for all station from database ( num_stations = number of stations from data base ).
Code:
#define MENU_SIZE 15
int i;
char text[ 32 ];
const int num_stations = station_get_num_stations( cmd->station );
osd_list_set_lines( cmd->osd, num_stations );
osd_list_set_text( cmd->osd, 0, _("Station List") );
for( i = 0; i < MENU_SIZE; i++ ) {
if( cmd->curmenupos < MENU_SIZE ) {
/* Station number + name */
snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i ), station_get_name( cmd->station, i ) );
osd_list_set_text( cmd->osd, i + 1, text );
} else if ( cmd->curmenupos == MENU_SIZE ) {
/* Station number + name */
snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i + 1 ), station_get_name( cmd->station, i + 1 ) );
osd_list_set_text( cmd->osd, i + 1, text );
} else if ( cmd->curmenupos == MENU_SIZE + 1 ) {
/* Station number + name */
snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i + 2 ), station_get_name( cmd->station, i + 2 ) );
osd_list_set_text( cmd->osd, i + 1, text );
} else if ( cmd->curmenupos == MENU_SIZE + 2 ) {
/* Station number + name */
snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i + 3 ), station_get_name( cmd->station, i + 3 ) );
osd_list_set_text( cmd->osd, i + 1, text );
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/* n = num_stations */
} else if ( cmd->curmenupos == MENU_SIZE + n ) {
/* Station number + name */
snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i + n + 1 ), station_get_name( cmd->station, i + n + 1 ) );
osd_list_set_text( cmd->osd, i + 1, text );
}
cmd->curmenusize = MENU_SIZE + 1
my attempt:
Code:
#define MENU_SIZE 15
int i, j;
char text[ 32 ];
const int num_stations = station_get_num_stations( cmd->station );
osd_list_set_lines( cmd->osd, num_stations );
osd_list_set_text( cmd->osd, 0, _("Station List") );
for( i = 0; i < MENU_SIZE; i++ ) {
for( j = 0; j < num_stations - MENU_SIZE; j++ ) {
if( cmd->curmenupos < MENU_SIZE ) {
/* Station number + name */
snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i ), station_get_name( cmd->station, i ) );
osd_list_set_text( cmd->osd, i + 1, text );
} else if( cmd->curmenupos == MENU_SIZE + j ) {
/* Station number + name */
snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i + j + 1 ), station_get_name( cmd->station, i + j + 1 ) );
osd_list_set_text( cmd->osd, i + 1, text );
}
}
}
cmd->curmenusize = MENU_SIZE + 1;
Thanks