+ hdr->home = UINT_MAX;
+ hdr->roaming = UINT_MAX;
+
+ stats_file_update_cache(file);
+ }
+
+ return 0;
+}
+
+
+static struct stats_record *get_next_record(struct stats_iter *iter)
+{
+ if (iter->it != iter->end) {
+ struct stats_record *tmp;
+
+ tmp = iter->it;
+ iter->it = get_next(iter->file, iter->it);
+
+ return tmp;
+ }
+
+ return NULL;
+}
+
+static int append_record(struct stats_file *file,
+ struct stats_record *rec)
+{
+ struct stats_record *cur, *next;
+ int err;
+
+ if (file->last == get_end(file)) {
+ err = stats_file_remap(file, file->len +
+ sysconf(_SC_PAGESIZE));
+ if (err < 0)
+ return err;
+
+ stats_file_update_cache(file);
+ }
+
+ cur = get_end(file);
+ next = get_next(file, cur);
+
+ memcpy(next, rec, sizeof(struct stats_record));
+
+ set_end(file, next);
+
+ return 0;
+}
+
+static struct stats_record *process_file(struct stats_iter *iter,
+ struct stats_file *temp_file,
+ struct stats_record *cur,
+ GDate *date_change_step_size,
+ int account_period_offset)
+{
+ struct stats_record *home, *roaming;
+ struct stats_record *next;
+
+ home = NULL;
+ roaming = NULL;
+
+ if (cur == NULL)
+ cur = get_next_record(iter);
+ next = get_next_record(iter);
+
+ while (next != NULL) {
+ GDate date_cur;
+ GDate date_next;
+ int append;
+
+ append = FALSE;
+
+ if (cur->roaming == TRUE)
+ roaming = cur;
+ else
+ home = cur;
+
+ g_date_set_time_t(&date_cur, cur->ts);
+ g_date_set_time_t(&date_next, next->ts);
+
+ if (g_date_compare(&date_cur, date_change_step_size) < 0) {
+ /* month period size */
+ GDateDay day_cur, day_next;
+ GDateMonth month_cur, month_next;
+
+ month_cur = g_date_get_month(&date_cur);
+ month_next = g_date_get_month(&date_next);
+
+ day_cur = g_date_get_day(&date_cur);
+ day_next = g_date_get_day(&date_next);
+
+ if (day_cur == day_next && month_cur != month_next) {
+ append = TRUE;
+ } else if (day_cur < account_period_offset &&
+ day_next >= account_period_offset) {
+ append = TRUE;
+ }
+ } else {
+ /* day period size */
+ if (g_date_days_between(&date_cur, &date_next) > 0)
+ append = TRUE;
+ }
+
+ if (append == TRUE) {
+ if (home != NULL) {
+ append_record(temp_file, home);
+ home = NULL;
+ }
+
+ if (roaming != NULL) {
+ append_record(temp_file, roaming);
+ roaming = NULL;
+ }
+ }
+
+ cur = next;
+ next = get_next_record(iter);