Exemplo n.º 1
0
void Printer::Print(const map<string, string>& variables, const char* text) {
  int size = strlen(text);
  int pos = 0;  // The number of bytes we've written so far.

  for (int i = 0; i < size; i++) {
    if (text[i] == '\n') {
      // Saw newline.  If there is more text, we may need to insert an indent
      // here.  So, write what we have so far, including the '\n'.
      WriteRaw(text + pos, i - pos + 1);
      pos = i + 1;

      // Setting this true will cause the next WriteRaw() to insert an indent
      // first.
      at_start_of_line_ = true;

    } else if (text[i] == variable_delimiter_) {
      // Saw the start of a variable name.

      // Write what we have so far.
      WriteRaw(text + pos, i - pos);
      pos = i + 1;

      // Find closing delimiter.
      const char* end = strchr(text + pos, variable_delimiter_);
      if (end == NULL) {
        GOOGLE_LOG(DFATAL) << " Unclosed variable name.";
        end = text + pos;
      }
      int endpos = end - text;

      string varname(text + pos, endpos - pos);
      if (varname.empty()) {
        // Two delimiters in a row reduce to a literal delimiter character.
        WriteRaw(&variable_delimiter_, 1);
      } else {
        // Replace with the variable's value.
        map<string, string>::const_iterator iter = variables.find(varname);
        if (iter == variables.end()) {
          GOOGLE_LOG(DFATAL) << " Undefined variable: " << varname;
        } else {
          WriteRaw(iter->second.data(), iter->second.size());
        }
      }

      // Advance past this variable.
      i = endpos;
      pos = endpos + 1;
    }
  }

  // Write the rest.
  WriteRaw(text + pos, size - pos);
}
Exemplo n.º 2
0
void Printer::WriteRaw(const char* data, int size) {
  if (failed_) return;
  if (size == 0) return;

  if (at_start_of_line_) {
    // Insert an indent.
    at_start_of_line_ = false;
    WriteRaw(indent_.data(), indent_.size());
    if (failed_) return;
  }

  while (size > buffer_size_) {
    // Data exceeds space in the buffer.  Copy what we can and request a
    // new buffer.
    memcpy(buffer_, data, buffer_size_);
    data += buffer_size_;
    size -= buffer_size_;
    void* void_buffer;
    failed_ = !output_->Next(&void_buffer, &buffer_size_);
    if (failed_) return;
    buffer_ = reinterpret_cast<char*>(void_buffer);
  }

  // Buffer is big enough to receive the data; copy it.
  memcpy(buffer_, data, size);
  buffer_ += size;
  buffer_size_ -= size;
}
Exemplo n.º 3
0
void WriteELF::Write(const vector<uint64_t>& instructions, const DebugInfo& info, const char* filename)
{
	// File name symbol
	{	auto& sym = AddSymbol(filename);
		sym.st_info = (STB_LOCAL<<4) | STT_FILE;
		sym.st_shndx = SHN_ABS;
	}

	const size_t code_size = instructions.size() * sizeof(uint64_t);

	// Code fragment symbol, name = file name
	if (!NoStandardSymbols)
	{	string name = stripextension(strippath(filename));
		replacenonalphanum(name);
		AddGlobalSymbol(name).st_size = code_size;
		// End Symbol
		name.append("_end");
		AddGlobalSymbol(name).st_value = code_size;
		// Size symbol
		name.erase(name.size() - 4);
		name.append("_size");
		auto& sym = AddGlobalSymbol(name);
		sym.st_shndx = SHN_ABS;
		sym.st_value = code_size;
	}

	// Export global symbol table
	for (auto sym : info.GlobalsByName)
	{	auto& value = AddGlobalSymbol(sym.first);
		value.st_value = (Elf32_Addr)sym.second.iValue;
		if (sym.second.Type == V_INT)
			value.st_shndx = SHN_ABS;
	}

	// write header
	WriteRaw(Hdr);
	// write section table
	WriteRaw(Sect0);
	WriteRaw(SectSNST);
	Elf32_Shdr sect = SectCode;
	sect.sh_size = code_size;
	WriteRaw(sect);
	sect = SectSym;
	sect.sh_offset += code_size;
	size_t sym_size = Symbols.size() * sizeof(Elf32_Sym);
	sect.sh_size = sym_size;
	WriteRaw(sect);
	sect = SectSymST;
	sect.sh_offset += code_size + sym_size;
	sect.sh_size = SymbolNames.size() + 1;
	WriteRaw(sect);
	// write section names
	WriteRaw(SNST);
	// write code
	checkedwrite(Target, &instructions.front(), code_size);
	// write symbol table
	checkedwrite(Target, &Symbols.front(), sizeof(Elf32_Sym) * Symbols.size());
	// write symbol names
	checkedwrite(Target, SymbolNames.c_str(), SymbolNames.size() + 1);
}
Exemplo n.º 4
0
void Mask::UpdateBeadFindOutcomes( Region &wholeChip, char const *maskFileName, bool not_single_beadfind, int update_stats, char const *maskStatsName)
{
  // write the beadmask in maskPtr to file maskFileName
  if (!update_stats)
  {
    DumpStats (wholeChip, (char *)maskStatsName, not_single_beadfind);
  }
  WriteRaw (maskFileName);
  validateMask();
}
Exemplo n.º 5
0
size_t wxBZipOutputStream::OnSysWrite(const void* buffer, size_t bufsize)
{
    bz_stream* hZip = (bz_stream*)m_hZip;

    hZip->next_in = (char*)buffer;
    hZip->avail_in = bufsize;
	hZip->next_out = m_pBuffer;
	hZip->avail_out = WXBZBS;

    size_t nWrote = 0;
    int nRet = BZ_RUN_OK;  // for the 'while' statement

    while( nRet== BZ_RUN_OK && hZip->avail_in > 0 )
    {
        // Compress the data in buffer, resulting data -> pbuffer
        nRet = BZ2_bzCompress(hZip, BZ_RUN);  
        if (nRet != BZ_RUN_OK)
        { 
            wxLogDebug(wxT("Error from BZ2_bzCompress in Write()")); 
            return 0; 
        }

        // This is how much newly compressed data is available
        size_t nCurWrite = WXBZBS - hZip->avail_out;  
        if (nCurWrite != 0)
		{
            // Deliver the compressed data to the parent stream
            WriteRaw(m_pBuffer, nCurWrite);    
            if (m_parent_o_stream->LastWrite() != nCurWrite)
            { 
                wxLogDebug(wxT("Error writing to underlying stream")); 
                break; 
            }

            //Reset the buffer
            hZip->avail_out = WXBZBS;  
            hZip->next_out = m_pBuffer;
            nWrote += nCurWrite;
		}
	}

    // I'm not sure if this is necessary as, if it worked, 
    // the loop continued until avail_in was zero
    nWrote = bufsize - hZip->avail_in ;  
    return nWrote;	
}
Exemplo n.º 6
0
void Log::EndFrame()
{
    MutexLock lock(logMutex);

    // Process messages accumulated from other threads (if any)
    while (!threadMessages.IsEmpty())
    {
        const StoredLogMessage& stored = threadMessages.Front();

        if (stored.level != LOG_RAW)
            Write(stored.level, stored.message);
        else
            WriteRaw(stored.message, stored.error);

        threadMessages.PopFront();
    }
}
Exemplo n.º 7
0
/**
 * @brief Internal function for writing the messages.
 * @param [in] level Message's level.
 * @param [in] pszFormat Message's format specifier.
 * @param [in] arglist Message argumets to format string.
 */
void CLogFile::WriteV(UINT level, LPCTSTR pszFormat, va_list arglist)
{
	CString msg;
	msg.FormatV(pszFormat, arglist);
	msg.Insert(0, GetPrefix(level));
	if (level & LOSERROR)
	{
		TCHAR cause[5120];
		CInternetException(GetLastError()).GetErrorMessage(cause, countof(cause));
		msg += cause;
	}
	msg.TrimRight(_T("\r\n"));
	msg += _T("\n");
	WriteRaw(msg);
	if (level & LDEBUG)
	{
		OutputDebugString(msg);
	}
}
Exemplo n.º 8
0
bool wxBZipOutputStream::Close() // Flushes any remaining compressed data
{
    bz_stream* hZip = (bz_stream*)m_hZip;
    int nRet = BZ_FINISH_OK;

    while ( nRet == BZ_FINISH_OK )
	{
		hZip->next_out = m_pBuffer;
		hZip->avail_out = WXBZBS;

        // Call BZ2_bzCompress with the parameter BZ_FINISH
		int nRet = BZ2_bzCompress(hZip, BZ_FINISH);
		if (nRet != BZ_FINISH_OK && nRet != BZ_STREAM_END)
		{ 
            wxLogDebug(wxT("BZ2_bzCompress error in Close()")); 
            break; 
        }
		
		size_t nCurWrite = WXBZBS - hZip->avail_out;

        if (nCurWrite != 0)
		{
			WriteRaw(m_pBuffer, nCurWrite);

			if (m_parent_o_stream->LastWrite() != nCurWrite)
            { 
                wxLogDebug(wxT("Error writing to underlying ")
                           wxT("stream during the Close() phase")); 
                break; 
            }
        }

        if (nRet == BZ_STREAM_END)
		{
            return true; //hrm, restructure here???
		}
	}
 
    return false;
}
Exemplo n.º 9
0
int TelnetProtocol::Write(const void *pBuf, int count)
{
  for (int i = 0 ; i < count ; i++) {
    BYTE ch = ((const BYTE *)pBuf)[i];

    switch (state) {
      case stData:
        if (ch == cdIAC)
          state = stCode;
        else
          WriteRaw(&ch, 1);
        break;
      case stCode:
        switch (ch) {
          case cdIAC:
            WriteRaw(&ch, 1);
            state = stData;
            break;
          case cdSB:
          case cdWILL:
          case cdWONT:
          case cdDO:
          case cdDONT:
            code = ch;
            state = stOption;
            break;
          default:
            printf("RECV: unknown code %u\n", (unsigned)ch);
            state = stData;
        }
        break;
      case stOption:
        printf("RECV: %s %u\n", code2name(code), (unsigned)ch);
        switch (code) {
          case cdSB:
            option = ch;
            params.clear();
            state = stSubParams;
            break;
          case cdWILL:
            switch (options[ch].remoteOptionState) {
              case OptionState::osCant:
                SendOption(cdDONT, ch);
                break;
              case OptionState::osNo:
                options[ch].remoteOptionState = OptionState::osYes;
                SendOption(cdDO, ch);
                break;
              case OptionState::osYes:
                break;
            }
            break;
          case cdWONT:
            switch (options[ch].remoteOptionState) {
              case OptionState::osCant:
              case OptionState::osNo:
                break;
              case OptionState::osYes:
                options[ch].remoteOptionState = OptionState::osNo;
                SendOption(cdDONT, ch);
                break;
            }
            break;
          case cdDO:
            switch (options[ch].localOptionState) {
              case OptionState::osCant:
                SendOption(cdWONT, ch);
                break;
              case OptionState::osNo:
                options[ch].localOptionState = OptionState::osYes;
                SendOption(cdWILL, ch);
                break;
              case OptionState::osYes:
                break;
            }
            break;
          case cdDONT:
            switch (options[ch].localOptionState) {
              case OptionState::osCant:
              case OptionState::osNo:
                break;
              case OptionState::osYes:
                options[ch].localOptionState = OptionState::osNo;
                SendOption(cdWONT, ch);
                break;
            }
            break;
          default:
            printf("  ignored\n");
        };
        if (state == stOption)
          state = stData;
        break;
      case stSubParams:
        if (ch == cdIAC)
          state = stSubCode;
        else
          params.push_back(ch);
        break;
      case stSubCode:
        switch (ch) {
          case cdIAC:
            state = stSubParams;
            break;
          case cdSE:
            printf("  ");
            {
              for (BYTE_vector::const_iterator i = params.begin() ; i != params.end() ; i++)
                printf("%u ", (unsigned)*i);
            }
            printf("SE\n");

            switch (option) {
              case opTerminalType:
                params.clear();
                params.push_back(0);
                params.insert(params.end(), terminalType.begin(), terminalType.end());
                SendSubNegotiation(option, params);
                break;
              default:
                printf("  ignored\n");
            }

            state = stData;
            break;
          default:
            printf("RECV: unknown sub code %u\n", (unsigned)ch);
            state = stData;
        };
        break;
    }
  }

  return count;
}
Exemplo n.º 10
0
size_t wxBZipOutputStream::OnSysWrite(const void* buffer, size_t bufsize)
{
	size_t nWrote = 0;
	int n;

	
	((bz_stream*&)hZip)->next_in = &(((char*&)buffer)[nWrote]);
	((bz_stream*&)hZip)->avail_in = bufsize - nWrote;
	((bz_stream*&)hZip)->next_out = &pBuffer[0];
	((bz_stream*&)hZip)->avail_out = WXBZBS;

	do {
	if ((n=BZ2_bzCompress((bz_stream*&)hZip, BZ_RUN)) != BZ_RUN_OK)
	{

		wxMessageBox(wxString::Format("BrokeC %i", n));
		break;
	}
	if (((bz_stream*&)hZip)->avail_out < WXBZBS)
	{
		((bz_stream*&)hZip)->next_out = &pBuffer[0];
		((bz_stream*&)hZip)->avail_out = WXBZBS;
		size_t nCurWrite = WXBZBS - ((bz_stream*&)hZip)->avail_out;
	//	wxMessageBox(wxString::Format("%i", nCurWrite));
		WriteRaw(pBuffer, nCurWrite);
		nWrote += m_parent_o_stream->LastWrite();


		if (m_parent_o_stream->LastWrite() != nCurWrite)
		{
			wxMessageBox("Broke");
			break;
		}
	}
	} while(((bz_stream*&)hZip)->avail_in != 0);

	while (nWrote != bufsize)
	{
		((bz_stream*&)hZip)->next_out = &pBuffer[0];
		((bz_stream*&)hZip)->avail_out = WXBZBS;

		int nRet = BZ2_bzCompress((bz_stream*&)hZip, BZ_FINISH);
		
		if (nRet != BZ_FINISH_OK && nRet != BZ_STREAM_END)
		{
			wxMessageBox("ErrorFK");
			break;
		}

		
		size_t nCurWrite = WXBZBS - ((bz_stream*&)hZip)->avail_out;
	//	wxMessageBox(wxString::Format("%i", nCurWrite));
		if (nCurWrite != 0)
		{
			WriteRaw(pBuffer, nCurWrite);
			nWrote += m_parent_o_stream->LastWrite();


			if (m_parent_o_stream->LastWrite() != nCurWrite)
			{
				wxMessageBox("Broke");
				break;
			}
		}

		if (nRet == BZ_STREAM_END)
		{
			wxMessageBox("GOOD");
			break;
		}

	}
 
//	wxMessageBox(wxString::Format("%i %i", nWrote, bufsize);
	return nWrote;	
}
Exemplo n.º 11
0
void JsonWriter::WriteRaw(const char *name, const char *raw,
                          size_t raw_length) {
  WriteRaw(name, std::string(raw, raw_length));
}
Exemplo n.º 12
0
void Printer::PrintRaw(const char* data) {
  if (failed_) return;
  WriteRaw(data, strlen(data));
}
Exemplo n.º 13
0
void Printer::PrintRaw(const string& data) {
  WriteRaw(data.data(), data.size());
}
Exemplo n.º 14
0
static void
e5_export_bov(
    const char* bov_file, 
    eid_t e5_file_id, 
    e5_grid_dataset* grid)
{
    static const int min_bricklet = 1;
    static const int max_bricklet = 32;
    
    int i;
    int d;
    
    bov_header_t header;
    e5_bov_attrs_t attrs;
    
    memset(&header, 0, sizeof(bov_header_t));
    memset(&attrs, 0, sizeof(e5_bov_attrs_t));
    
    size_t len = strlen(bov_file) + 8;
    char *data_file = malloc(len);
    snprintf(data_file, len, "%s.data", e5_basename(bov_file));
    
    eid_t e5_group_id = e5_open_group(e5_file_id, "/Emissivity/real scalars");           
    e5_attr real_scalars_attr[] = { {"time", &(attrs.time), E5_TYPE_DOUBLE, 0}, {0} };
    estatus_t status = e5_read_attr_list(e5_group_id, real_scalars_attr);
    E5_EXIT_ON_ERROR(status);
    e5_close_group(e5_group_id);
    
    e5_group_id = e5_open_group(e5_file_id, "/Emissivity/real runtime parameters");           
    e5_attr real_param_attr[] = { 
        {"xmin", &(attrs.xmin), E5_TYPE_DOUBLE, 0}, 
        {"xmax", &(attrs.xmax), E5_TYPE_DOUBLE, 0}, 
        {"ymin", &(attrs.ymin), E5_TYPE_DOUBLE, 0}, 
        {"ymax", &(attrs.ymax), E5_TYPE_DOUBLE, 0}, 
        {"zmin", &(attrs.zmin), E5_TYPE_DOUBLE, 0}, 
        {"zmax", &(attrs.zmax), E5_TYPE_DOUBLE, 0}, 
        {0} 
    };
    
    status = e5_read_attr_list(e5_group_id, real_param_attr);
    E5_EXIT_ON_ERROR(status);
    e5_close_group(e5_group_id);
    
    header.variable = grid->name;
    header.time = (float)attrs.time;
    header.data_file = data_file;
    header.data_size[0] = (int)grid->dim[0];
    header.data_size[1] = (int)grid->dim[1];
    header.data_size[2] = (int)grid->dim[2];
    header.centering = "zonal";
    header.data_components = 1;
    header.brick_origin[0] = (attrs.xmax + attrs.xmin) * 0.5;
    header.brick_origin[1] = (attrs.ymax + attrs.ymin) * 0.5;
    header.brick_origin[2] = (attrs.zmax + attrs.zmin) * 0.5;
    header.brick_size[0] = attrs.xmax - attrs.xmin;
    header.brick_size[1] = attrs.ymax - attrs.ymin;
    header.brick_size[2] = attrs.zmax - attrs.zmin;
    
    for(i=min_bricklet; i <= max_bricklet; i++)
    {
        for(d=0; d<3; d++)
            header.data_bricklets[d] = ((header.data_size[d] % i) == 0) ? i : header.data_bricklets[d];
    }

    if(grid->type == E5_TYPE_FLOAT)
        header.data_format = "FLOAT";
    else if(grid->type == E5_TYPE_DOUBLE)
        header.data_format = "DOUBLE";
    else if(grid->type == E5_TYPE_INT)
        header.data_format = "INT";        
        
    FILE* fd = CreateTextFile(bov_file); 
    if(!fd)
    {
        printf("Failed to create BOV header file '%s'! Exiting...\n", bov_file);
        exit(EXIT_FAILURE);
    }

    printf("Writing header to '%s'...\n", bov_file);
    fprintf(fd, "TIME: %f\n", header.time);
    fprintf(fd, "DATA_FILE: %s\n", header.data_file);
    fprintf(fd, "DATA_SIZE: %d %d %d\n", 
        header.data_size[0], header.data_size[1], header.data_size[2]);
    fprintf(fd, "DATA_FORMAT: %s\n", header.data_format);
    fprintf(fd, "VARIABLE: %s\n", header.variable);
    fprintf(fd, "DATA_ENDIAN: LITTLE\n");
    fprintf(fd, "CENTERING: %s\n", header.centering);
    fprintf(fd, "BRICK_ORIGIN: %f %f %f\n",
        header.brick_origin[0], header.brick_origin[1], header.brick_origin[2]);
    fprintf(fd, "BRICK_SIZE: %f %f %f\n",
        header.brick_size[0], header.brick_size[1], header.brick_size[2]);
    fprintf(fd, "DIVIDE_BRICK: true\n");
    fprintf(fd, "DATA_BRICKLETS: %d %d %d\n",
        header.data_bricklets[0], header.data_bricklets[1], header.data_bricklets[2]);
    fprintf(fd, "DATA_COMPONENTS: %d\n", 1);
    CloseTextFile(fd);

    snprintf(data_file, len, "%s.data", (bov_file));
    fd = CreateRawFile(header.data_file);
    if(!fd)
    {
        printf("Failed to create BOV data file '%s'! Exiting...\n", bov_file);
        exit(EXIT_FAILURE);
    }

    printf("Writing data to '%s'...\n", header.data_file);
    WriteRaw(fd, grid->data, e5_sizeof(grid->type), grid->dim[0] * grid->dim[1] * grid->dim[2]);
    CloseRawFile(fd);
    printf("DONE!\n");
}
Exemplo n.º 15
0
void VFSHandle_file::WriteString( char *s, unsigned int length )
{
	WriteRaw( ( unsigned char * ) s, length );
}