From 5da955e8f3f1b01378aeb570caea18249cd06099 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Sun, 31 May 2015 20:27:47 +0200 Subject: [PATCH] Improve code formating - get rid of C++ style This commit does not change any code logic and does not introduces any extra functionality. --- src/cache.c | 24 ++-- src/cache.h | 14 ++- src/config.c | 27 ++--- src/config.h | 4 +- src/libscrobbler2.c | 279 +++++++++++++++++++++++--------------------- src/libscrobbler2.h | 18 +-- src/notify.c | 20 ++-- src/utils.c | 44 +++---- 8 files changed, 224 insertions(+), 206 deletions(-) diff --git a/src/cache.c b/src/cache.c index 4fed38e..ce24806 100644 --- a/src/cache.c +++ b/src/cache.c @@ -33,7 +33,7 @@ #include "debug.h" -// Return the actual size of given cache record structure. +/* Return the actual size of given cache record structure. */ static size_t get_cache_record_size(const struct cmusfm_cache_record *record) { return sizeof(*record) + record->artist_len + record->album_len + record->album_artist_len + record->track_len + record->mbid_len; @@ -92,7 +92,7 @@ struct cmusfm_cache_record *get_cache_record(const scrobbler_trackinfo_t *sb_tin return cr; } -// Write data, which should be submitted later, to the cache file. +/* Write data, which should be submitted later, to the cache file. */ void cmusfm_cache_update(const scrobbler_trackinfo_t *sb_tinf) { FILE *f; @@ -113,7 +113,7 @@ void cmusfm_cache_update(const scrobbler_trackinfo_t *sb_tinf) { fclose(f); } -// Submit tracks saved in the cache file. +/* Submit tracks saved in the cache file. */ void cmusfm_cache_submit(scrobbler_session_t *sbs) { char rd_buff[4096]; @@ -128,12 +128,12 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) { if ((f = fopen(cmusfm_cache_file, "r")) == NULL) return; - // read file until EOF + /* read file until EOF */ while (!feof(f)) { rd_len = fread(rd_buff, 1, sizeof(rd_buff), f); record = (struct cmusfm_cache_record*)rd_buff; - // iterate while there is no enough data for full cache record header + /* iterate while there is no enough data for full cache record header */ while ((void*)record - (void*)rd_buff + sizeof(*record) <= rd_len) { if (record->signature != CMUSFM_CACHE_SIGNATURE) { @@ -145,11 +145,11 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) { record_size = get_cache_record_size(record); debug("record size: %ld", record_size); - // break if current record is truncated + /* break if current record is truncated */ if ((void*)record - (void*)rd_buff + record_size > rd_len) break; - // restore scrobbler track info structure from cache + /* restore scrobbler track info structure from cache */ memset(&sb_tinf, 0, sizeof(sb_tinf)); sb_tinf.timestamp = record->timestamp; sb_tinf.track_number = record->track_number; @@ -181,22 +181,22 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) { sb_tinf.artist, sb_tinf.album, sb_tinf.album_artist, sb_tinf.track_number, sb_tinf.track, sb_tinf.duration); - // submit tracks to Last.fm + /* submit tracks to Last.fm */ scrobbler_scrobble(sbs, &sb_tinf); - // point to next record + /* point to next record */ record = (struct cmusfm_cache_record*)((char*)record + record_size); } if ((unsigned)((void*)record - (void*)rd_buff) != rd_len) - // seek to the beginning of current record, because - // it is truncated, so we have to read it one more time + /* seek to the beginning of current record, because it is + * truncated, so we have to read it one more time */ fseek(f, (void*)record - (void*)rd_buff - rd_len, SEEK_CUR); } fclose(f); - // remove cache file + /* remove cache file */ unlink(cmusfm_cache_file); } diff --git a/src/cache.h b/src/cache.h index b149263..560f29a 100644 --- a/src/cache.h +++ b/src/cache.h @@ -27,17 +27,19 @@ #define CMUSFM_CACHE_SIGNATURE 0x6643 -// cache record header structure +/* cache record header structure */ struct __attribute__((__packed__)) cmusfm_cache_record { uint32_t signature; uint32_t timestamp, track_number, duration; uint16_t artist_len, album_len, album_artist_len; uint16_t track_len, mbid_len; - //char artist[]; // NULL-terminated - //char album[]; // NULL-terminated - //char album_artist[]; // NULL-terminated - //char track[]; // NULL-terminated - //char mbid[]; // NULL-terminated + /* NULL-terminated strings + char artist[]; + char album[]; + char album_artist[]; + char track[]; + char mbid[]; + */ }; diff --git a/src/config.c b/src/config.c index 39e94a6..6697d3c 100644 --- a/src/config.c +++ b/src/config.c @@ -36,23 +36,24 @@ #include "cmusfm.h" -// Return the pointer to the configuration value substring. This function -// strips all white-spaces and optional quotation marks. +/* Return the pointer to the configuration value substring. This function + * strips all white-spaces and optional quotation marks. */ static char *get_config_value(char *str) { char *end; - // seek to the beginning of a value + /* seek to the beginning of a value */ str = strchr(str, '=') + 1; - // trim leading spaces and optional quotation + /* trim leading spaces and optional quotation */ while (isspace(*str)) str++; if (*str == '"') str++; - if (*str == 0) // edge case handling + /* edge case handling */ + if (*str == 0) return str; - // trim trailing spaces and optional quotation + /* trim trailing spaces and optional quotation */ end = str + strlen(str) - 1; while (end > str && isspace(*end)) end--; if (*end == '"') end--; @@ -69,19 +70,19 @@ static int decode_config_bool(const char *value) { return strcmp(value, "yes") == 0; } -// Read cmusfm configuration from the file. +/* Read cmusfm configuration from the file. */ int cmusfm_config_read(const char *fname, struct cmusfm_config *conf) { FILE *f; char line[128]; - // initialize configuration defaults + /* initialize configuration defaults */ memset(conf, 0, sizeof(*conf)); strcpy(conf->format_localfile, "^(?A.+) - (?T.+)\\.[^.]+$"); strcpy(conf->format_shoutcast, "^(?A.+) - (?T.+)$"); #ifdef ENABLE_LIBNOTIFY - // set the MS Windows name convention as a default - compatible with most - // sailors from the pirate bay + /* set the MS Windows name convention as a default - compatible with most + * sailors from the pirate bay */ strcpy(conf->format_coverfile, "^folder\\.jpg$"); #endif conf->nowplaying_localfile = 1; @@ -120,16 +121,16 @@ int cmusfm_config_read(const char *fname, struct cmusfm_config *conf) { return fclose(f); } -// Write cmusfm configuration to the file. +/* Write cmusfm configuration to the file. */ int cmusfm_config_write(const char *fname, struct cmusfm_config *conf) { FILE *f; - // create configuration file (truncate previous one) + /* create configuration file (truncate previous one) */ if ((f = fopen(fname, "w")) == NULL) return -1; - // protect session key from exposure + /* protect session key from exposure */ chmod(fname, S_IWUSR | S_IRUSR); fprintf(f, "# authentication\n"); diff --git a/src/config.h b/src/config.h index 060568f..8572a96 100644 --- a/src/config.h +++ b/src/config.h @@ -26,7 +26,7 @@ #endif -// Configuration file key definitions +/* Configuration file key definitions */ #define CMCONF_USER_NAME "user" #define CMCONF_SESSION_KEY "key" #define CMCONF_FORMAT_LOCALFILE "format-localfile" @@ -43,7 +43,7 @@ struct cmusfm_config { char user_name[64]; char session_key[16 * 2 + 1]; - // regular expressions for name parsers + /* regular expressions for name parsers */ char format_localfile[64]; char format_shoutcast[64]; #ifdef ENABLE_LIBNOTIFY diff --git a/src/libscrobbler2.c b/src/libscrobbler2.c index 2d143dc..0e8411d 100644 --- a/src/libscrobbler2.c +++ b/src/libscrobbler2.c @@ -24,37 +24,38 @@ #include "libscrobbler2.h" +#include #include #include #include -#include #include #include #include "debug.h" -// used as a buffer for GET/POST server response +char *mem2hex(const unsigned char *mem, int len, char *str); +unsigned char *hex2mem(const char *str, int len, unsigned char *mem); + + +/* used as a buffer for GET/POST server response */ struct sb_response_data { char *data; int len; }; -// used for quick URL and signature creation process +/* used for quick URL and signature creation process */ struct sb_getpost_data { char *name; char data_format; void *data; }; -char *mem2hex(const unsigned char *mem, int len, char *str); -unsigned char *hex2mem(const char *str, int len, unsigned char *mem); - -// CURL write callback function. +/* CURL write callback function. */ static size_t sb_curl_write_callback(char *ptr, size_t size, size_t nmemb, - void *data) -{ + void *data) { + struct sb_response_data *resp = (struct sb_response_data*)data; int len = size * nmemb; @@ -66,12 +67,11 @@ static size_t sb_curl_write_callback(char *ptr, size_t size, size_t nmemb, return len; } -// Initialize CURL handler for internal usage. -CURL *sb_curl_init(CURLoption method, struct sb_response_data *response) -{ +/* Initialize CURL handler for internal usage. */ +CURL *sb_curl_init(CURLoption method, struct sb_response_data *response) { CURL *curl; - if((curl = curl_easy_init()) == NULL) + if ((curl = curl_easy_init()) == NULL) return NULL; #ifdef CURLOPT_PROTOCOLS @@ -89,51 +89,54 @@ CURL *sb_curl_init(CURLoption method, struct sb_response_data *response) return curl; } -// Cleanup CURL handler and free allocated response buffer. -void sb_curl_cleanup(CURL *curl, struct sb_response_data *response) -{ +/* Cleanup CURL handler and free allocated response buffer. */ +void sb_curl_cleanup(CURL *curl, struct sb_response_data *response) { curl_easy_cleanup(curl); free(response->data); } -// Check scrobble API response status (and curl itself). -int sb_check_response(struct sb_response_data *response, int curl_status, - scrobbler_session_t *sbs) -{ - char *ptr; +/* Check scrobble API response status (and curl itself). */ +int sb_check_response(struct sb_response_data *response, + int curl_status, scrobbler_session_t *sbs) { debug("check: status: %d, body: %s", curl_status, response->data); - if(curl_status != 0) { - // network transfer failure (curl error) + + /* network transfer failure (curl error) */ + if (curl_status != 0) { sbs->error_code = curl_status; return SCROBBERR_CURLPERF; } - if(strstr(response->data, "data, "data, " no need for escaping + curl_free(escaped_data); + } + else + /* non-string content -> no need for escaping */ offset += sprintf(str_buffer + offset, format, sb_data[x].name, sb_data[x].data); } - str_buffer[offset - 1] = 0; //strip '&' at the end of string + /* strip '&' from the end of the string */ + str_buffer[offset - 1] = 0; debug("params: %s", str_buffer); + return str_buffer; } -// Scrobble a track. -int scrobbler_scrobble(scrobbler_session_t *sbs, scrobbler_trackinfo_t *sbt) -{ +/* Scrobble a track. */ +int scrobbler_scrobble(scrobbler_session_t *sbs, scrobbler_trackinfo_t *sbt) { + CURL *curl; int status; uint8_t sign[MD5_DIGEST_LENGTH]; - char api_key_hex[sizeof(sbs->api_key)*2 + 1]; - char session_key_hex[sizeof(sbs->session_key)*2 + 1]; - char sign_hex[sizeof(sign)*2 + 1]; + char api_key_hex[sizeof(sbs->api_key) * 2 + 1]; + char session_key_hex[sizeof(sbs->session_key) * 2 + 1]; + char sign_hex[sizeof(sign) * 2 + 1]; char post_data[2048]; struct sb_response_data response; - // data in alphabetical order sorted by name field (except api_sig) + /* data in alphabetical order sorted by name field (except api_sig) */ struct sb_getpost_data sb_data[] = { {"album", 's', sbt->album}, {"albumArtist", 's', sbt->album_artist}, {"api_key", 's', api_key_hex}, {"artist", 's', sbt->artist}, - //{"context", 's', NULL}, - {"duration", 'd', (void*)(long)sbt->duration}, + /* {"context", 's', NULL}, */ + {"duration", 'd', (void *)(long)sbt->duration}, {"mbid", 's', sbt->mbid}, {"method", 's', "track.scrobble"}, {"sk", 's', session_key_hex}, - {"timestamp", 'd', (void*)sbt->timestamp}, + {"timestamp", 'd', (void *)sbt->timestamp}, {"track", 's', sbt->track}, - {"trackNumber", 'd', (void*)(long)sbt->track_number}, - //{"streamId", 's', NULL}, - {"api_sig", 's', sign_hex}}; + {"trackNumber", 'd', (void *)(long)sbt->track_number}, + /* {"streamId", 's', NULL}, */ + {"api_sig", 's', sign_hex}, + }; debug("scrobble: %ld", sbt->timestamp); debug("payload: %s - %s (%s) - %d. %s (%ds)", sbt->artist, sbt->album, sbt->album_artist, sbt->track_number, sbt->track, sbt->duration); - if(sbt->artist == NULL || sbt->track == NULL || sbt->timestamp == 0) + if (sbt->artist == NULL || sbt->track == NULL || sbt->timestamp == 0) return SCROBBERR_TRACKINF; - if((curl = sb_curl_init(CURLOPT_POST, &response)) == NULL) + if ((curl = sb_curl_init(CURLOPT_POST, &response)) == NULL) return SCROBBERR_CURLINIT; mem2hex(sbs->api_key, sizeof(sbs->api_key), api_key_hex); mem2hex(sbs->session_key, sizeof(sbs->session_key), session_key_hex); - // make signature for track.updateNowPlaying API call + /* make signature for track.scrobble API call */ sb_generate_method_signature(sb_data, 11, sbs->secret, sign); mem2hex(sign, sizeof(sign), sign_hex); - // make track.updateNowPlaying POST request + /* make track.scrobble POST request */ sb_make_curl_getpost_string(curl, post_data, sb_data, 12); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); curl_easy_setopt(curl, CURLOPT_URL, SCROBBLER_URL); @@ -231,51 +242,52 @@ int scrobbler_scrobble(scrobbler_session_t *sbs, scrobbler_trackinfo_t *sbt) return status; } -// Notify Last.fm that a user has started listening to a track. -// This is engine function (without required argument check) +/* Notify Last.fm that a user has started listening to a track. This + * is an engine function (without required argument check). */ int sb_update_now_playing(scrobbler_session_t *sbs, - scrobbler_trackinfo_t *sbt) -{ + scrobbler_trackinfo_t *sbt) { + CURL *curl; int status; uint8_t sign[MD5_DIGEST_LENGTH]; - char api_key_hex[sizeof(sbs->api_key)*2 + 1]; - char session_key_hex[sizeof(sbs->session_key)*2 + 1]; - char sign_hex[sizeof(sign)*2 + 1]; + char api_key_hex[sizeof(sbs->api_key) * 2 + 1]; + char session_key_hex[sizeof(sbs->session_key) * 2 + 1]; + char sign_hex[sizeof(sign) * 2 + 1]; char post_data[2048]; struct sb_response_data response; - // data in alphabetical order sorted by name field (except api_sig) + /* data in alphabetical order sorted by name field (except api_sig) */ struct sb_getpost_data sb_data[] = { {"album", 's', sbt->album}, {"albumArtist", 's', sbt->album_artist}, {"api_key", 's', api_key_hex}, {"artist", 's', sbt->artist}, - //{"context", 's', NULL}, - {"duration", 'd', (void*)(long)sbt->duration}, + /* {"context", 's', NULL}, */ + {"duration", 'd', (void *)(long)sbt->duration}, {"mbid", 's', sbt->mbid}, {"method", 's', "track.updateNowPlaying"}, {"sk", 's', session_key_hex}, {"track", 's', sbt->track}, - {"trackNumber", 'd', (void*)(long)sbt->track_number}, - {"api_sig", 's', sign_hex}}; + {"trackNumber", 'd', (void *)(long)sbt->track_number}, + {"api_sig", 's', sign_hex}, + }; debug("now playing: %ld", sbt->timestamp); debug("payload: %s - %s (%s) - %d. %s (%ds)", sbt->artist, sbt->album, sbt->album_artist, sbt->track_number, sbt->track, sbt->duration); - if((curl = sb_curl_init(CURLOPT_POST, &response)) == NULL) + if ((curl = sb_curl_init(CURLOPT_POST, &response)) == NULL) return SCROBBERR_CURLINIT; mem2hex(sbs->api_key, sizeof(sbs->api_key), api_key_hex); mem2hex(sbs->session_key, sizeof(sbs->session_key), session_key_hex); - // make signature for track.updateNowPlaying API call + /* make signature for track.updateNowPlaying API call */ sb_generate_method_signature(sb_data, 10, sbs->secret, sign); mem2hex(sign, sizeof(sign), sign_hex); - // make track.updateNowPlaying POST request + /* make track.updateNowPlaying POST request */ sb_make_curl_getpost_string(curl, post_data, sb_data, 11); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); curl_easy_setopt(curl, CURLOPT_URL, SCROBBLER_URL); @@ -287,21 +299,20 @@ int sb_update_now_playing(scrobbler_session_t *sbs, return status; } -// Update "Now playing" notification. +/* Update "Now playing" notification. */ int scrobbler_update_now_playing(scrobbler_session_t *sbs, - scrobbler_trackinfo_t *sbt) -{ + scrobbler_trackinfo_t *sbt) { debug("now playing wrapper"); - if(sbt->artist == NULL || sbt->track == NULL) + if (sbt->artist == NULL || sbt->track == NULL) return SCROBBERR_TRACKINF; return sb_update_now_playing(sbs, sbt); } -// Hard-codded method for validating session key. This approach uses the -// updateNotify method call with the wrong number of parameters as a test -// call. -int scrobbler_test_session_key(scrobbler_session_t *sbs) -{ +/* Hard-codded method for validating session key. This approach uses the + * updateNotify method call with the wrong number of parameters as a test + * call. */ +int scrobbler_test_session_key(scrobbler_session_t *sbs) { + scrobbler_trackinfo_t sbt; int status; @@ -309,64 +320,66 @@ int scrobbler_test_session_key(scrobbler_session_t *sbs) memset(&sbt, 0, sizeof(sbt)); status = sb_update_now_playing(sbs, &sbt); - // 'invalid parameters' is not the error in this case :) - if(status == SCROBBERR_SBERROR && sbs->error_code == 6) return 0; - else return status; + /* 'invalid parameters' is not an error in this case :) */ + if (status == SCROBBERR_SBERROR && sbs->error_code == 6) + return 0; + + return status; } -// Return session key in string hex dump. The memory block pointed by the -// *str has to be big enough to contain sizeof(session_key)*2 + 1. -char *scrobbler_get_session_key_str(scrobbler_session_t *sbs, char *str) -{ +/* Return the session key in the string hex dump. The memory block pointed by + * the str has to be big enough to contain sizeof(session_key) * 2 + 1. */ +char *scrobbler_get_session_key_str(scrobbler_session_t *sbs, char *str) { return mem2hex(sbs->session_key, sizeof(sbs->session_key), str); } -// Set session key by parsing the hex dump of this key. -void scrobbler_set_session_key_str(scrobbler_session_t *sbs, const char *str) -{ +/* Set session key by parsing the hex dump of this key. */ +void scrobbler_set_session_key_str(scrobbler_session_t *sbs, const char *str) { hex2mem(str, sizeof(sbs->session_key), sbs->session_key); } -// Perform scrobbler service authentication process. +/* Perform scrobbler service authentication process. */ int scrobbler_authentication(scrobbler_session_t *sbs, - scrobbler_authuser_callback_t callback) -{ + scrobbler_authuser_callback_t callback) { + CURL *curl; int status; uint8_t sign[MD5_DIGEST_LENGTH]; - char api_key_hex[sizeof(sbs->api_key)*2 + 1]; - char sign_hex[sizeof(sign)*2 + 1], token_hex[33]; + char api_key_hex[sizeof(sbs->api_key) * 2 + 1]; + char sign_hex[sizeof(sign) * 2 + 1], token_hex[33]; char get_url[1024], *ptr; struct sb_response_data response; - // data in alphabetical order sorted by name field (except api_sig) + /* data in alphabetical order sorted by name field (except api_sig) */ struct sb_getpost_data sb_data_token[] = { {"api_key", 's', api_key_hex}, {"method", 's', "auth.getToken"}, - {"api_sig", 's', sign_hex}}; + {"api_sig", 's', sign_hex}, + }; struct sb_getpost_data sb_data_session[] = { {"api_key", 's', api_key_hex}, {"method", 's', "auth.getSession"}, {"token", 's', token_hex}, - {"api_sig", 's', sign_hex}}; + {"api_sig", 's', sign_hex}, + }; - if((curl = sb_curl_init(CURLOPT_HTTPGET, &response)) == NULL) + if ((curl = sb_curl_init(CURLOPT_HTTPGET, &response)) == NULL) return SCROBBERR_CURLINIT; mem2hex(sbs->api_key, sizeof(sbs->api_key), api_key_hex); - // make signature for auth.getToken API call + /* make signature for auth.getToken API call */ sb_generate_method_signature(sb_data_token, 2, sbs->secret, sign); mem2hex(sign, sizeof(sign), sign_hex); - // make auth.getToken GET request + /* make auth.getToken GET request */ strcpy(get_url, SCROBBLER_URL "?"); sb_make_curl_getpost_string(curl, get_url + strlen(get_url), sb_data_token, 3); curl_easy_setopt(curl, CURLOPT_URL, get_url); status = curl_easy_perform(curl); status = sb_check_response(&response, status, sbs); - if(status != 0) { + if (status != 0) { sb_curl_cleanup(curl, &response); return status; } @@ -374,22 +387,22 @@ int scrobbler_authentication(scrobbler_session_t *sbs, memcpy(token_hex, strstr(response.data, "") + 7, 32); token_hex[32] = 0; - // perform user authorization (callback function) + /* perform user authorization (callback function) */ sprintf(get_url, SCROBBLER_USERAUTH_URL "?api_key=%s&token=%s", api_key_hex, token_hex); - if(callback(get_url) != 0) { + if (callback(get_url) != 0) { sb_curl_cleanup(curl, &response); return SCROBBERR_CALLBACK; } - - // make signature for auth.getSession API call + + /* make signature for auth.getSession API call */ sb_generate_method_signature(sb_data_session, 3, sbs->secret, sign); mem2hex(sign, sizeof(sign), sign_hex); - // reinitialize response buffer + /* reinitialize response buffer */ response.len = 0; - // make auth.getSession GET request + /* make auth.getSession GET request */ strcpy(get_url, SCROBBLER_URL "?"); sb_make_curl_getpost_string(curl, get_url + strlen(get_url), sb_data_session, 4); curl_easy_setopt(curl, CURLOPT_URL, get_url); @@ -397,7 +410,7 @@ int scrobbler_authentication(scrobbler_session_t *sbs, status = sb_check_response(&response, status, sbs); debug("authentication status: %d", status); - if(status != 0) { + if (status != 0) { sb_curl_cleanup(curl, &response); return status; } @@ -405,7 +418,8 @@ int scrobbler_authentication(scrobbler_session_t *sbs, strncpy(sbs->user_name, strstr(response.data, "") + 6, sizeof(sbs->user_name)); sbs->user_name[sizeof(sbs->user_name) - 1] = 0; - if((ptr = strchr(sbs->user_name, '<')) != NULL) *ptr = 0; + if ((ptr = strchr(sbs->user_name, '<')) != NULL) + *ptr = 0; memcpy(get_url, strstr(response.data, "") + 5, 32); hex2mem(get_url, sizeof(sbs->session_key), sbs->session_key); @@ -413,19 +427,19 @@ int scrobbler_authentication(scrobbler_session_t *sbs, return 0; } -// Initialize scrobbler session. On success this function returns pointer -// to allocated scrobbler_session structure, which must be freed by call -// to scrobbler_free. On error NULL is returned. +/* Initialize scrobbler session. On success this function returns pointer + * to allocated scrobbler_session structure, which must be freed by call + * to scrobbler_free. On error NULL is returned. */ scrobbler_session_t *scrobbler_initialize(uint8_t api_key[16], - uint8_t secret[16]) -{ + uint8_t secret[16]) { + scrobbler_session_t *sbs; - // allocate space for scrobbler session structure - if((sbs = calloc(1, sizeof(scrobbler_session_t))) == NULL) + /* allocate space for scrobbler session structure */ + if ((sbs = calloc(1, sizeof(scrobbler_session_t))) == NULL) return NULL; - if(curl_global_init(CURL_GLOBAL_NOTHING) != 0) { + if (curl_global_init(CURL_GLOBAL_NOTHING) != 0) { free(sbs); return NULL; } @@ -436,14 +450,13 @@ scrobbler_session_t *scrobbler_initialize(uint8_t api_key[16], return sbs; } -void scrobbler_free(scrobbler_session_t *sbs) -{ +void scrobbler_free(scrobbler_session_t *sbs) { curl_global_cleanup(); free(sbs); } -// Dump memory into hexadecimal string format. Note that *str has to be -// big enough to contain 2*len+1. +/* Dump memory into hexadecimal string format. Note that *str has to be + * big enough to contain 2*len+1. */ char *mem2hex(const unsigned char *mem, int len, char *str) { char hexchars[] = "0123456789abcdef", *ptr; @@ -457,8 +470,8 @@ char *mem2hex(const unsigned char *mem, int len, char *str) return str; } -// Opposite to mem2hex. Len is the number of bytes in hex representation, -// so strlen(str) should be 2*len. +/* Opposite to mem2hex. Len is the number of bytes in hex representation, + * so strlen(str) should be 2*len. */ unsigned char *hex2mem(const char *str, int len, unsigned char *mem) { int x; diff --git a/src/libscrobbler2.h b/src/libscrobbler2.h index d96e614..cd58eed 100644 --- a/src/libscrobbler2.h +++ b/src/libscrobbler2.h @@ -31,10 +31,10 @@ #define SCROBBLER_USERAUTH_URL "http://www.last.fm/api/auth/" typedef struct scrobbler_session_tag { - uint8_t api_key[16]; //128-bit API key - uint8_t secret[16]; //128-bit secter + uint8_t api_key[16]; /* 128-bit API key */ + uint8_t secret[16]; /* 128-bit secret */ - uint8_t session_key[16]; //128-bit session key (authentication) + uint8_t session_key[16]; /* 128-bit session key */ char user_name[64]; int error_code; @@ -46,12 +46,12 @@ typedef struct scrobbler_trackinfo_tag { time_t timestamp; } scrobbler_trackinfo_t; -// ----- scrobbler_* error types definitions ----- -#define SCROBBERR_CURLINIT 1 //curl initialization error -#define SCROBBERR_CURLPERF 2 //curl perform error - network issue -#define SCROBBERR_SBERROR 3 //scrobbler API error -#define SCROBBERR_CALLBACK 4 //callback error (authentication) -#define SCROBBERR_TRACKINF 5 //missing required field(s) in trackinfo structure +/* ----- scrobbler_* error definitions ----- */ +#define SCROBBERR_CURLINIT 1 /* curl initialization error */ +#define SCROBBERR_CURLPERF 2 /* curl perform error - network issue */ +#define SCROBBERR_SBERROR 3 /* scrobbler API error */ +#define SCROBBERR_CALLBACK 4 /* callback error (authentication) */ +#define SCROBBERR_TRACKINF 5 /* missing required field in trackinfo */ scrobbler_session_t *scrobbler_initialize(uint8_t api_key[16], uint8_t secret[16]); diff --git a/src/notify.c b/src/notify.c index 83473ee..18e649e 100644 --- a/src/notify.c +++ b/src/notify.c @@ -25,28 +25,28 @@ #include -// global notification handler +/* global notification handler */ static NotifyNotification *cmus_notify; -// Show track information via the notification system. +/* Show track information via the notification system. */ void cmusfm_notify_show(const scrobbler_trackinfo_t *sb_tinf, const char *icon) { char *body; size_t art_len, alb_len; - if(cmus_notify) { - // forcefully close previous notification + if (cmus_notify) { + /* forcefully close previous notification */ notify_notification_close(cmus_notify, NULL); g_object_unref(G_OBJECT(cmus_notify)); } - // concatenate artist and album (when applicable) + /* concatenate artist and album (when applicable) */ art_len = strlen(sb_tinf->artist); alb_len = strlen(sb_tinf->album); - body = (char*)malloc(art_len + alb_len + sizeof(" ()") + 1); + body = (char *)malloc(art_len + alb_len + sizeof(" ()") + 1); strcpy(body, sb_tinf->artist); - if(alb_len > 0) + if (alb_len > 0) sprintf(&body[art_len], " (%s)", sb_tinf->album); cmus_notify = notify_notification_new(sb_tinf->track, body, icon); @@ -54,15 +54,15 @@ void cmusfm_notify_show(const scrobbler_trackinfo_t *sb_tinf, const char *icon) free(body); } -// Initialize notification system. +/* Initialize notification system. */ void cmusfm_notify_initialize() { cmus_notify = NULL; notify_init("cmusfm"); } -// Free notification system resources. +/* Free notification system resources. */ void cmusfm_notify_free() { - if(cmus_notify) + if (cmus_notify) g_object_unref(G_OBJECT(cmus_notify)); notify_uninit(); } diff --git a/src/utils.c b/src/utils.c index ec76911..44ba8e4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -76,10 +76,10 @@ char *get_cmus_home_file(const char *file) { } #ifdef ENABLE_LIBNOTIFY -// Return an album cover file based on the current location. Location should -// be either a local file name or an URL. When cover file can not be found, -// NULL is returned (URL case, or when coverfile ERE match failed). In case -// of wild-card match, the first one is returned. +/* Return an album cover file based on the current location. Location should + * be either a local file name or an URL. When cover file can not be found, + * NULL is returned (URL case, or when coverfile ERE match failed). In case + * of wild-card match, the first one is returned. */ char *get_album_cover_file(const char *location, const char *format) { static char fname[256]; @@ -92,9 +92,9 @@ char *get_album_cover_file(const char *location, const char *format) { if (location == NULL) return NULL; - // NOTE: We can support absolute paths only. The reason for this, is, that - // cmus might report file name according to its current directory, - // which is not known. Hence this obstruction. + /* NOTE: We can support absolute paths only. The reason for this, is, that + * cmus might report file name according to its current directory, + * which is not known. Hence this obstruction. */ if (location[0] != '/') return NULL; @@ -110,7 +110,7 @@ char *get_album_cover_file(const char *location, const char *format) { return NULL; } - // scan given directory for cover file name pattern + /* scan given directory for cover file name pattern */ while ((dp = readdir(dir)) != NULL) { debug("cover lookup: %s", dp->d_name); if (!regexec(®ex, dp->d_name, 0, NULL, 0)) { @@ -130,15 +130,15 @@ char *get_album_cover_file(const char *location, const char *format) { } #endif -// Get track information substrings from the given string. Matching is done -// according to the provided format, which is a ERE pattern with customized -// placeholders. Placeholder is defined as a marked subexpression with the -// `?X` marker, where X can be one the following characters: -// A - artist, B - album, T - title, N - track number -// e.g.: ^(?A.+) - (?N[:digits:]+)\. (?T.+)$ -// In order to get a single match structure, one should use `get_regexp_match` -// function. When matches are not longer needed, is should be freed by the -// standard `free` function. When something goes wrong, NULL is returned. +/* Get track information substrings from the given string. Matching is done + * according to the provided format, which is a ERE pattern with customized + * placeholders. Placeholder is defined as a marked subexpression with the + * `?X` marker, where X can be one the following characters: + * A - artist, B - album, T - title, N - track number + * e.g.: ^(?A.+) - (?N[:digits:]+)\. (?T.+)$ + * In order to get a single match structure, one should use `get_regexp_match` + * function. When matches are not longer needed, is should be freed by the + * standard `free` function. When something goes wrong, NULL is returned. */ struct format_match *get_regexp_format_matches(const char *str, const char *format) { #define MATCHES_SIZE FORMAT_MATCH_TYPE_COUNT + 1 @@ -151,8 +151,8 @@ struct format_match *get_regexp_format_matches(const char *str, const char *form debug("matching: %s: %s", format, str); - // allocate memory for up to FORMAT_MATCH_TYPE_COUNT matches - // with one extra always empty terminating structure + /* allocate memory for up to FORMAT_MATCH_TYPE_COUNT matches + * with one extra always empty terminating structure */ matches = (struct format_match *)calloc(MATCHES_SIZE, sizeof(*matches)); regexp = strdup(format); @@ -185,8 +185,10 @@ struct format_match *get_regexp_format_matches(const char *str, const char *form return matches; } -// Return pointer to the single format match structure with given match type. -struct format_match *get_regexp_match(struct format_match *matches, enum format_match_type type) { +/* Return pointer to the single format match structure with given + * match type. */ +struct format_match *get_regexp_match(struct format_match *matches, + enum format_match_type type) { while (matches->type) { if (matches->type == type) break;