Пример #1
0
	/** read write bytes **/
	Bytes* read_bytes(const char* path){
		if(!path) return 0;
		if(FILE* fp =fopen(path, "rb")){
			if(0 != fseek(fp, 0, SEEK_END)){
				ERROR("fail read %s, %s", path, get_last_error_desc());
				fclose(fp);
				return 0;
			}

			const int64_t s =ftell(fp);
			if(s == -1){
				ERROR("fail read %s, %s", path, get_last_error_desc());
				fclose(fp);
				return 0;
			}

			if(0 != fseek(fp, 0, SEEK_SET)){
				ERROR("fail read %s, %s", path, get_last_error_desc());
				fclose(fp);
				return 0;
			}

			Bytes* content =SafeNew<Bytes>();
			content->resize(s);
			char* ptr =content->c_str();
			size_t cursor =0;
			while(cursor < (size_t)s){
				const size_t n =fread(ptr+cursor, 1, (size_t)s-cursor, fp);
				if(n > 0){
					cursor +=n;
				}
				else if(n == 0){
					if(ferror(fp)){
						return 0;
					}
					if(feof(fp)){
						return content;
					}
				}
				else{
					ERROR("fail read %s, %s", path, get_last_error_desc());
					fclose(fp);
					return 0;
				}
			}
			fclose(fp);
			ASSERT(cursor == (size_t)s);
			return content;
		}
		else{
			ERROR("fail to open %s, %s", path, get_last_error_desc());
			return 0;
		}
	}
Пример #2
0
	Array* String::split(const char* sep){
		// check & prepare
		if(!sep) return 0;
		if(m_length <= 0) return 0;
		Bytes* bs =SafeNew<Bytes>();
		bs->set(m_data, m_length+1);
		char* sz =bs->c_str();
		Array* arr =SafeNew<Array>();
		char* saveptr =0;

		// split
		char* token =strtok_r(sz, sep, &saveptr);
		while(token){
			arr->push_back(String::NewString(token));
			token =strtok_r(0, sep, &saveptr);
		}
		return arr;
	}
Пример #3
0
	String* BitSet::toString(){
		Bytes* bs =SafeNew<Bytes>();
		uint64_t set_cnt =0;
		const uint64_t max_set_cnt =count();
		const uint64_t bits_count =m_bits_holder_count * 64;
		bs->reserve(bits_count);
		for(uint64_t i=0; i<bits_count; ++i){
			if(test(i)){
				bs->write("1", 1);
				++set_cnt;
				if(set_cnt == max_set_cnt){
					break;
				}
			}
			else{
				bs->write("0", 1);
			}
		}
		return String::New(bs->c_str(), bs->size());
	}