以下のコードは、宛先ディレクトリ、ラジオ局名、および現在の時刻に基づいてファイル名を生成します。
static int start_recording(const gchar *destination, const char* station, const char* time)
{
Recording* recording;
char *filename;
filename = g_strdup_printf(_("%s/%s_%s"),
destination,
station,
time);
recording = recording_start(filename);
g_free(filename);
if (!recording)
return -1;
recording->station = g_strdup(station);
record_status_window(recording);
run_status_window(recording);
return 1;
}
出力例:
/home/ubuntu/Desktop/Europa FM_07042012-111705.ogg
問題:
同じステーション名には、タイトルに空白が含まれる場合があります:
Europa FM
Paprika Radio
Radio France Internationale
...........................
Rock FM
生成されたファイル名の出力から空白を削除して、次のようになるようにしたいと思います。
/home/ubuntu/Desktop/EuropaFM_07042012-111705.ogg
(より複雑な要件は、ファイル名からすべての不正な char. を削除することです)
ありがとうございました。
アップデート
これを書く場合:
static int start_recording(const gchar *destination, const char* station, const char* time)
{
Recording* recording;
char *filename;
char* remove_whitespace(station)
{
char* result = malloc(strlen(station)+1);
char* i;
int temp = 0;
for( i = station; *i; ++i)
if(*i != ' ')
{
result[temp] = (*i);
++temp;
}
result[temp] = '\0';
return result;
}
filename = g_strdup_printf(_("%s/%s_%s"),
destination,
remove_whitespace(station),
time);
recording = recording_start(filename);
g_free(filename);
if (!recording)
return -1;
recording->station = g_strdup(station);
tray_icon_items_set_sensible(FALSE);
record_status_window(recording);
run_status_window(recording);
return 1;
}
次の警告が表示されます。
警告: 'strlen' の引数 1 を渡すと、キャストなしで整数からポインターが作成されます [デフォルトで有効] 警告: 代入は、キャストなしで整数からポインターを作成します [デフォルトで有効]