Пример #1
0
int main(int argc, char *argv[])
{
	/* parse command-line options */
	char ch;
	int opt_short = 0;
	while ((ch = getopt(argc, argv, "s")) != -1) {
		switch (ch) {
			case 's':
				opt_short = 1;
				break;
			default:
				break;
		}
	}

	/* get battery status */
	long charge_full = read_number_from_file(BAT_PATH "charge_full");
	long charge_now = read_number_from_file(BAT_PATH "charge_now");
	if (charge_full < 0 || charge_now < 0) {
		if (opt_short)
			printf("!\n");
		else
			printf("battery not found!\n");
		exit(1);
	}

#ifdef IS_CHARGING_FILE
	int is_charging = (int)read_number_from_file(IS_CHARGING_FILE);
	if (is_charging < 0)
		is_charging = 0;
#endif

	int charge_percent = charge_now * 100 / charge_full;

	if (opt_short) /* short format (for tmux, zsh, ...) */
		printf("%d%c\n", charge_percent, is_charging ? '+' : '-');
	else { /* detailed report */
		/* charge level and status */
		const char *status = is_charging ? "charging" : "discharging";
		printf("%d%% (%s)\n", charge_percent, status);

		/* compute remaining time if the battery is discharging */
		long current_uA = read_number_from_file(BAT_PATH "current_now");
		if (!is_charging && current_uA > 0) {
			/* time = charge(uWh) * seconds_per_hour(s/h) / current(uA) seconds */
			int remaining_seconds = charge_full * 3600 / current_uA;
			char str_remaining_time[16];
			format_remaining_time(str_remaining_time, 16, remaining_seconds);
			printf("estimated remaining time: %s\n", str_remaining_time);
		}
	}

	return 0;
}
Пример #2
0
static void
check_space(int csave)
{
	struct statvfs fsb;
	int64_t spacefree, dumpsize, minfree, datasize;

	if (statvfs(".", &fsb) < 0)
		logprint(SC_SL_ERR | SC_EXIT_ERR, "statvfs: %s",
		    strerror(errno));

	dumpsize = dumphdr.dump_data - dumphdr.dump_start;
	datasize = dumphdr.dump_npages * pagesize;
	if (!csave)
		dumpsize += datasize;
	else
		dumpsize += datahdr.dump_data_csize;

	spacefree = (int64_t)fsb.f_bavail * fsb.f_frsize;
	minfree = 1024LL * read_number_from_file("minfree", 1024);
	if (spacefree < minfree + dumpsize) {
		logprint(SC_SL_ERR | SC_EXIT_ERR,
		    "not enough space in %s (%lld MB avail, %lld MB needed)",
		    savedir, spacefree >> 20, (minfree + dumpsize) >> 20);
	}
std::vector< CInDBMelody > CDataBase::getEverything() const {
    std::vector< CInDBMelody > result;
    std::ifstream index_file;
    std::ifstream id3_file;
    std::ifstream mel_file;
    index_file.open( index_filename, std::fstream::in | std::fstream::binary );
    id3_file.open( id3_filename, std::fstream::in );
    mel_file.open( mel_filename, std::fstream::in | std::fstream::binary );
    if( ! ( index_file.is_open() && id3_file.is_open() && mel_file.is_open() ) ) {
        std::cout << "ERROR: Couldn't open some DB file for writing in " << directory << '\n';
        return result;
    }

    uint64_t id3_start = 0, id3_end = 0, mel_start = 0, mel_end = 0;
    // get id3 and and melody addresses
    if( ! ( read_number_from_file( id3_start, id3_file_max_size_koeff, index_file )
         && read_number_from_file( mel_start, mel_file_max_size_koeff, index_file )
         ) ) {
        return result;
    }
    while( index_file.good() && index_file.peek() != EOF ) {
        // get id3 and and melody addresses
        if( ! ( read_number_from_file( id3_end, id3_file_max_size_koeff, index_file )
             && read_number_from_file( mel_end, mel_file_max_size_koeff, index_file )
             ) ) {
            return result;
        }
        // read id3 tags
        assert( id3_start < id3_end );
        assert( mel_start < mel_end );
        std::string artist, album, name, year;
        uint64_t record_size = id3_end - id3_start;
        if( ! id3_file.seekg( id3_start ).good() ) {
            std::cout << "ERROR: in id3 file\n";
            break;
        }
        std::getline( id3_file, artist );
        record_size -= id3_file.gcount();
        std::getline( id3_file, album );
        record_size -= id3_file.gcount();
        std::getline( id3_file, name );
        record_size -= id3_file.gcount();
        std::getline( id3_file, year );
        if( ! mel_file.seekg( mel_start ).good() ) {
            std::cout << "ERROR: in mel file\n";
            break;
        }
        std::vector< Raspoznavayka::interval_t > intervals( mel_end - mel_start );
        for( uint64_t i = 0; i < mel_end - mel_start; ++i ) {
            char interval;
            if( mel_file.get( interval ).fail() ) {
                std::cout << "ERROR: in mel file\n";
                break;
            }
            intervals[i] = static_cast< Raspoznavayka::interval_t >( interval );
        }
        CIDTag idtag( artist, album, name, std::atoi( year.c_str() ) );
        CInDBMelody new_melody( intervals, idtag );
        result.push_back( new_melody );
        id3_start = id3_end;
        mel_start = mel_end;
    } // end of found melodies' data read cycle

    id3_file.close();
    mel_file.close();
    index_file.close();
    return result;
}
std::vector< CHashMatch > CDataBase::searchByHash_offs( CHash hash ) const {
    std::vector< CHashMatch > result;
    std::ifstream hash_file;
    std::vector< std::pair< uint64_t, int64_t > > matches; // vector of pairs ( mel_id, offset )

    // hash offset cycle
    for( int64_t fixed_hash_offset = 0; 
            fixed_hash_offset < static_cast< int64_t >( hash.getLength() ) - static_cast< int64_t >( CFixedHash::length ); 
            ++fixed_hash_offset ) {

        CFixedHash fixed_hash( hash, fixed_hash_offset );
        std::string hash_filename = makeFilenameOfHash( fixed_hash );
        hash_file.open( makeFilenameOfHash( fixed_hash ),
                        std::fstream::in | std::fstream::binary );
        if( ! hash_file.is_open() ) {
            std::cout << "ERROR: Couldn't open hash file for reading: " << hash_filename << '\n';
            return result;
        }

        // fixed hash file read cycle
        while( hash_file.is_open() && hash_file.good() && hash_file.peek() != EOF ) {
            // read melody id
            uint64_t mel_id = 0;
            Raspoznavayka::mel_size_t mel_chm_offs = 0;
            if( ! ( read_number_from_file( mel_id, mel_number_size_koeff, hash_file ) // read melody id
                 && read_number_from_file( mel_chm_offs, mel_max_size_koeff, hash_file ) // read fixed hash match offset in melody
                 ) ) {
                return result;
            }
            
            int64_t total_offset = static_cast<int64_t>( mel_chm_offs ) - static_cast<int64_t>( fixed_hash_offset );
            // check distinct
            bool found = false;
            for( auto i = matches.begin(); i != matches.end(); ++i ) {
                if( i->first == mel_id && i->second == total_offset ) {
                    found = true;
                    break;
                }
            }
            if( !found ) {
                // push to matches
                matches.push_back( std::pair< uint64_t, int64_t >( mel_id, total_offset ) );
            }
        } // end of fixed hash file read cycle

        hash_file.close();
    } // end of hash offset cycle

    // read found melodies' data
    std::ifstream index_file;
    std::ifstream id3_file;
    std::ifstream mel_file;
    index_file.open( index_filename, std::fstream::in | std::fstream::binary );
    id3_file.open( id3_filename, std::fstream::in );
    mel_file.open( mel_filename, std::fstream::in | std::fstream::binary );
    if( ! ( index_file.is_open() && id3_file.is_open() && mel_file.is_open() ) ) {
        std::cout << "ERROR: Couldn't open some DB file for writing in " << directory << '\n';
        return result;
    }

    for( auto match = matches.begin(); match != matches.end(); ++match ) {
        int64_t mel_id = match->first, total_offset = match->second;
        // get index entry on this song
        index_file.seekg( mel_id * ( mel_number_size_koeff + mel_file_max_size_koeff ) );
        // get id3 and and melody addresses
        uint64_t id3_start = 0, id3_end = 0, mel_start = 0, mel_end = 0;
        if( ! ( read_number_from_file( id3_start, id3_file_max_size_koeff, index_file )
             && read_number_from_file( mel_start, mel_file_max_size_koeff, index_file )
             && read_number_from_file( id3_end, id3_file_max_size_koeff, index_file )
             && read_number_from_file( mel_end, mel_file_max_size_koeff, index_file )
             ) ) {
            return result;
        }
        // read id3 tags
        assert( id3_start < id3_end );
        assert( mel_start < mel_end );
        std::string artist, album, name, year;
        uint64_t record_size = id3_end - id3_start;
        if( ! id3_file.seekg( id3_start ).good() ) {
            std::cout << "ERROR: in id3 file\n";
            break;
        }
        std::getline( id3_file, artist );
        record_size -= id3_file.gcount();
        std::getline( id3_file, album );
        record_size -= id3_file.gcount();
        std::getline( id3_file, name );
        record_size -= id3_file.gcount();
        std::getline( id3_file, year );
        if( ! mel_file.seekg( mel_start ).good() ) {
            std::cout << "ERROR: in mel file\n";
            break;
        }
        std::vector< Raspoznavayka::interval_t > intervals( mel_end - mel_start );
        for( uint64_t i = 0; i < mel_end - mel_start; ++i ) {
            char interval;
            if( mel_file.get( interval ).fail() ) {
                std::cout << "ERROR: in mel file\n";
                break;
            }
            intervals[i] = static_cast< Raspoznavayka::interval_t >( interval );
        }
        CIDTag idtag( artist, album, name, std::atoi( year.c_str() ) );
        CInDBMelody new_melody( intervals, idtag );
        CHashMatch new_chm( &new_melody, total_offset );
        result.push_back( new_chm );
    } // end of found melodies' data read cycle

    id3_file.close();
    mel_file.close();
    index_file.close();
    return result;
}