Skip to content

Commit

Permalink
Improve code formating - get rid of C++ style
Browse files Browse the repository at this point in the history
This commit does not change any code logic and does not introduces
any extra functionality.
  • Loading branch information
arkq committed May 31, 2015
1 parent 73c800a commit 5da955e
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 206 deletions.
24 changes: 12 additions & 12 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
14 changes: 8 additions & 6 deletions src/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
*/
};


Expand Down
27 changes: 14 additions & 13 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
Loading

0 comments on commit 5da955e

Please sign in to comment.