示例#1
0
// Create new DataFile that's below file_size_limit in size.
DataFile *DataWriter::createNewDataFile(size_t headroom)
{
    DataFile *datafile = 0;
    int serial=0;
    stdString data_file_name;
    try
    {   // Keep opening existing data files until we find
        // one below the file limit.
        // We might have to create a new one.
        while (true)
        {
            makeDataFileName(serial, data_file_name);
            datafile = DataFile::reference(directory,
                                           data_file_name, true);
            FileOffset file_size = datafile->getSize();
            if (file_size+headroom < file_size_limit)
                return datafile;
            if (datafile->is_new_file)
            {
                LOG_MSG ("Warning: %s: "
                         "Cannot create a new data file within file size limit\n"
                         "type %d, count %d, %zu samples, file limit: %d bytes.\n",
                         channel_name.c_str(),
                         dbr_type, dbr_count, next_buffer_size, file_size_limit);
                return datafile; // Use anyway
            }
            // Try the next one.
            ++serial;
            datafile->release();
            datafile = 0;
        }
    }
    catch (GenericException &e)
    {
        if (datafile)
        {
            datafile->release();
            datafile = 0;
        }
        throw GenericException(__FILE__, __LINE__,
                               "Reference new datafile '%s':\n%s",
                               data_file_name.c_str(), e.what());
    }
    throw GenericException(__FILE__, __LINE__,
                           "createNewDataFile(%zu) failed", headroom);
    return 0;
}
示例#2
0
	//virtual method that will be executed                                                                                                           
	void executeAlgorithm(AlgorithmData& data, AlgorithmContext& context) {

		//****define algorithm here****

		//open image file
		// input name specified in configuration file
		Dataset* input = data.getInputDataset("imageIn");
		std::set<DataKey> keys = input->getAllFileKeys();
	
		//... iterate through input keys and do work ...
		for ( std::set<DataKey>::iterator i = keys.begin(); i != keys.end(); i++ ) {	
			DataKey myKey = *i;
			DataFile* myFile = input->getDataFile(myKey);
			int fileSize=myFile->getSize();
			char * fileBytes=myFile->getBytes();

			//open native message block	
			std::string nativeHdr="\n*************************NATIVE_OUTPUT*************************\n"; 
			std::cout<<nativeHdr<<std::endl;		
			
			//report image info to stdout
			std::cout<<"  Image Loaded"<<std::endl;
			std::cout<<"  "<<fileSize<<" bytes"<<std::endl;
			
			//print original file contents
			std::cout<<"  original file contents: "<<fileBytes;

			std::vector<WordCount> counter;

			char *tok;
			WordCount wc;
			char mark[] = " .,\n";
			int p = 0;
			int matchflag = 0;
			int k=0;

			tok = strtok(fileBytes, mark);
			while( tok != NULL ){
				if(k%2 == 0){
					wc.word = tok;
				}
				else if(k%2 == 1){
					wc.count = atoi(tok);

					for(p=0; p < counter.size(); p++){
						if(strcmp(counter[p].word, wc.word) == 0){
							counter[p].count += wc.count;
							matchflag = 1;
							break;
						}
					}

					if(matchflag == 0){
						counter.push_back(wc);
					}
					matchflag = 0;
				}
				tok = strtok( NULL, mark);  /* 2回目以降 */
				k++;
			}

			size_t len = counter.size() * (20 + 2);
			char * textout = (char *)malloc(len + 1);
			memset(textout, 0, sizeof(textout));
			char buf[22];

			printf("Answer(size = %d): \n", counter.size());
			for(p=0; p < counter.size() - 1; p++){
				sprintf(buf, "%s,%d\n", counter[p].word, counter[p].count);
				strcat(textout, buf);
				memset(buf, 0, sizeof(buf));
			}

			//print new output file contents
			printf("%s", textout);
			//std::cout<<"  output file contents: "<<fileBytes;

			//close message block		
			std::cout<<nativeHdr<<std::endl;
			
			// output file in the output folder
			Dataset* output = data.getOutputDataset("imageOut");
			DataFile* fileData = new DataFile(textout, strlen(textout), "testOut.bin");
			output->addDataFile(myKey, fileData);
		}
	}