/**
 * Convert character vector between marked encodings and the encoding provided
 *
 * @param str     input character vector or list of raw vectors
 * @param to    target encoding, \code{NULL} or \code{""} for default enc
 * @param to_raw single logical, should list of raw vectors be returned?
 * @return a converted character vector or list of raw vectors
 *
 * @version 0.1-?? (Marek Gagolewski, 2013-11-12)
 *
 * @version 0.2-1 (Marek Gagolewski, 2014-03-28)
 *          use StriUcnv
 *
 * @version 0.2-1 (Marek Gagolewski, 2014-04-01)
 *          calc required buf size a priori
 *
 * @version 0.3-1 (Marek Gagolewski, 2014-11-04)
 *    Issue #112: str_prepare_arg* retvals were not PROTECTed from gc
 */
SEXP stri_encode_from_marked(SEXP str, SEXP to, SEXP to_raw)
{
    PROTECT(str = stri_prepare_arg_string(str, "str"));
    const char* selected_to   = stri__prepare_arg_enc(to, "to", true); /* this is R_alloc'ed */
    bool to_raw_logical = stri__prepare_arg_logical_1_notNA(to_raw, "to_raw");

    STRI__ERROR_HANDLER_BEGIN(1)
    R_len_t str_n = LENGTH(str);
    StriContainerUTF16 str_cont(str, str_n);

    // get the number of strings to convert; if == 0, then you know what's the result
    if (str_n <= 0) return Rf_allocVector(to_raw_logical?VECSXP:STRSXP, 0);

    // Open converters
    StriUcnv ucnv(selected_to);
    UConverter* uconv_to = ucnv.getConverter(true /*register_callbacks*/);

    // Get target encoding mark
    cetype_t encmark_to = to_raw_logical?CE_BYTES:ucnv.getCE();

    // Prepare out val
    SEXP ret;
    STRI__PROTECT(ret = Rf_allocVector(to_raw_logical?VECSXP:STRSXP, str_n));

    // calculate required buf size
    R_len_t bufsize = 0;
    for (R_len_t i=0; i<str_n; ++i) {
        if (!str_cont.isNA(i) && str_cont.get(i).length() > bufsize)
            bufsize = str_cont.get(i).length();
    }
    bufsize = UCNV_GET_MAX_BYTES_FOR_STRING(bufsize, ucnv_getMaxCharSize(uconv_to));
    // "The calculated size is guaranteed to be sufficient for this conversion."
    String8buf buf(bufsize);

    for (R_len_t i=0; i<str_n; ++i) {
        if (str_cont.isNA(i)) {
            if (to_raw_logical) SET_VECTOR_ELT(ret, i, R_NilValue);
            else                SET_STRING_ELT(ret, i, NA_STRING);
            continue;
        }

        R_len_t curn_tmp = str_cont.get(i).length();
        const UChar* curs_tmp = str_cont.get(i).getBuffer(); // The buffer contents is (probably) not NUL-terminated.
        if (!curs_tmp)
            throw StriException(MSG__INTERNAL_ERROR);

        UErrorCode status = U_ZERO_ERROR;
        ucnv_resetFromUnicode(uconv_to);
        R_len_t bufneed = ucnv_fromUChars(uconv_to, buf.data(), buf.size(),
                                          curs_tmp, curn_tmp, &status);
        if (bufneed <= buf.size()) {
            STRI__CHECKICUSTATUS_THROW(status, {/* do nothing special on err */})
        }
        else {// larger buffer needed [this shouldn't happen?]
            buf.resize(bufneed, false/*destroy contents*/);
            status = U_ZERO_ERROR;
            bufneed = ucnv_fromUChars(uconv_to, buf.data(), buf.size(),
                                      curs_tmp, curn_tmp, &status);
            STRI__CHECKICUSTATUS_THROW(status, {/* do nothing special on err */})
        }

        if (to_raw_logical) {
            SEXP outobj;
            STRI__PROTECT(outobj = Rf_allocVector(RAWSXP, bufneed));
            memcpy(RAW(outobj), buf.data(), (size_t)bufneed);
            SET_VECTOR_ELT(ret, i, outobj);
            STRI__UNPROTECT(1);
        }
        else {
            SET_STRING_ELT(ret, i,
                           Rf_mkCharLenCE(buf.data(), bufneed, encmark_to));
        }
    }

    STRI__UNPROTECT_ALL
    return ret;

    STRI__ERROR_HANDLER_END({/* nothing special on error */})
}
Beispiel #2
0
void cv::mixChannels( const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts, const int* fromTo, size_t npairs )
{
    if( npairs == 0 )
        return;
    CV_Assert( src && nsrcs > 0 && dst && ndsts > 0 && fromTo && npairs > 0 );
    
    size_t i, j, k, esz1 = dst[0].elemSize1();
    int depth = dst[0].depth();

    AutoBuffer<uchar> buf((nsrcs + ndsts + 1)*(sizeof(Mat*) + sizeof(uchar*)) + npairs*(sizeof(uchar*)*2 + sizeof(int)*6));
    const Mat** arrays = (const Mat**)(uchar*)buf;
    uchar** ptrs = (uchar**)(arrays + nsrcs + ndsts);
    const uchar** srcs = (const uchar**)(ptrs + nsrcs + ndsts + 1);
    uchar** dsts = (uchar**)(srcs + npairs);
    int* tab = (int*)(dsts + npairs);
    int *sdelta = (int*)(tab + npairs*4), *ddelta = sdelta + npairs;
    
    for( i = 0; i < nsrcs; i++ )
        arrays[i] = &src[i];
    for( i = 0; i < ndsts; i++ )
        arrays[i + nsrcs] = &dst[i];
    ptrs[nsrcs + ndsts] = 0;
    
    for( i = 0; i < npairs; i++ )
    {
        int i0 = fromTo[i*2], i1 = fromTo[i*2+1];
        if( i0 >= 0 )
        {
            for( j = 0; j < nsrcs; i0 -= src[j].channels(), j++ )
                if( i0 < src[j].channels() )
                    break;
            CV_Assert(j < nsrcs && src[j].depth() == depth);
            tab[i*4] = (int)j; tab[i*4+1] = (int)(i0*esz1);
            sdelta[i] = src[j].channels();
        }
        else
        {
            tab[i*4] = (int)(nsrcs + ndsts); tab[i*4+1] = 0;
            sdelta[i] = 0;
        }
        
        for( j = 0; j < ndsts; i1 -= dst[j].channels(), j++ )
            if( i1 < dst[j].channels() )
                break;
        CV_Assert(i1 >= 0 && j < ndsts && dst[j].depth() == depth);
        tab[i*4+2] = (int)(j + nsrcs); tab[i*4+3] = (int)(i1*esz1);
        ddelta[i] = dst[j].channels();
    }

    NAryMatIterator it(arrays, ptrs, (int)(nsrcs + ndsts));
    int total = (int)it.size, blocksize = std::min(total, (int)((BLOCK_SIZE + esz1-1)/esz1));
    MixChannelsFunc func = mixchTab[depth];
    
    for( i = 0; i < it.nplanes; i++, ++it )
    {
        for( k = 0; k < npairs; k++ )
        {
            srcs[k] = ptrs[tab[k*4]] + tab[k*4+1];
            dsts[k] = ptrs[tab[k*4+2]] + tab[k*4+3];
        }
        
        for( int j = 0; j < total; j += blocksize )
        {
            int bsz = std::min(total - j, blocksize);
            func( srcs, sdelta, dsts, ddelta, bsz, (int)npairs );
            
            if( j + blocksize < total )
                for( k = 0; k < npairs; k++ )
                {
                    srcs[k] += blocksize*sdelta[k]*esz1;
                    dsts[k] += blocksize*ddelta[k]*esz1;
                }
        }
    }
}
Beispiel #3
0
// this function is called a *lot* of times (as I learned after seeing from
// profiler output that it is called ~12000 times from Mahogany start up code!)
// so it is important to optimize it - in particular, avoid using generic
// string functions here and do everything manually because it is faster
//
// I still kept the old version to be able to check that the optimized code has
// the same output as the non optimized version.
void wxRegConfig::SetPath(const wxString& strPath)
{
    // remember the old path
    wxString strOldPath = m_strPath;

#ifdef WX_DEBUG_SET_PATH // non optimized version kept here for testing
    wxString m_strPathAlt;

    {
        wxArrayString aParts;

        // because GetPath() returns "" when we're at root, we must understand
        // empty string as "/"
        if ( strPath.empty() || (strPath[0] == wxCONFIG_PATH_SEPARATOR) ) {
            // absolute path
            wxSplitPath(aParts, strPath);
        }
        else {
            // relative path, combine with current one
            wxString strFullPath = GetPath();
            strFullPath << wxCONFIG_PATH_SEPARATOR << strPath;
            wxSplitPath(aParts, strFullPath);
        }

        // recombine path parts in one variable
        wxString strRegPath;
        m_strPathAlt.Empty();
        for ( size_t n = 0; n < aParts.Count(); n++ ) {
            strRegPath << '\\' << aParts[n];
            m_strPathAlt << wxCONFIG_PATH_SEPARATOR << aParts[n];
        }
    }
#endif // 0

    // check for the most common case first
    if ( strPath.empty() )
    {
        m_strPath = wxCONFIG_PATH_SEPARATOR;
    }
    else // not root
    {
        // construct the full path
        wxString strFullPath;
        if ( strPath[0u] == wxCONFIG_PATH_SEPARATOR )
        {
            // absolute path
            strFullPath = strPath;
        }
        else // relative path
        {
            strFullPath.reserve(2*m_strPath.length());

            strFullPath << m_strPath;
            if ( strFullPath.Len() == 0 ||
                 strFullPath.Last() != wxCONFIG_PATH_SEPARATOR )
                strFullPath << wxCONFIG_PATH_SEPARATOR;
            strFullPath << strPath;
        }

        // simplify it: we need to handle ".." here

        // count the total number of slashes we have to know if we can go upper
        size_t totalSlashes = 0;

        // position of the last slash to be able to backtrack to it quickly if
        // needed, but we set it to -1 if we don't have a valid position
        //
        // we only remember the last position which means that we handle ".."
        // quite efficiently but not "../.." - however the latter should be
        // much more rare, so it is probably ok
        int posLastSlash = -1;

        const wxChar *src = strFullPath.c_str();
        size_t len = strFullPath.length();
        const wxChar *end = src + len;

        wxStringBufferLength buf(m_strPath, len);
        wxChar *dst = buf;
        wxChar *start = dst;

        for ( ; src < end; src++, dst++ )
        {
            if ( *src == wxCONFIG_PATH_SEPARATOR )
            {
                // check for "/.."

                // note that we don't have to check for src < end here as
                // *end == 0 so can't be '.'
                if ( src[1] == wxT('.') && src[2] == wxT('.') &&
                     (src + 3 == end || src[3] == wxCONFIG_PATH_SEPARATOR) )
                {
                    if ( !totalSlashes )
                    {
                        wxLogWarning(_("'%s' has extra '..', ignored."),
                                     strFullPath.c_str());
                    }
                    else // return to the previous path component
                    {
                        // do we already have its position?
                        if ( posLastSlash == -1 )
                        {
                            // no, find it: note that we are sure to have one
                            // because totalSlashes > 0 so we don't have to
                            // check the boundary condition below

                            // this is more efficient than strrchr()
                            dst--;
                            while ( *dst != wxCONFIG_PATH_SEPARATOR )
                            {
                                dst--;
                            }
                        }
                        else // the position of last slash was stored
                        {
                            // go directly there
                            dst = start + posLastSlash;

                            // invalidate posLastSlash
                            posLastSlash = -1;
                        }

                        // we must have found a slash one way or another!
                        wxASSERT_MSG( *dst == wxCONFIG_PATH_SEPARATOR,
                                      wxT("error in wxRegConfig::SetPath") );

                        // stay at the same position
                        dst--;

                        // we killed one
                        totalSlashes--;
                    }

                    // skip both dots
                    src += 2;
                }
                else // not "/.."
                {
                    if ( (dst == start) || (dst[-1] != wxCONFIG_PATH_SEPARATOR) )
                    {
                        *dst = wxCONFIG_PATH_SEPARATOR;

                        posLastSlash = dst - start;

                        totalSlashes++;
                    }
                    else // previous char was a slash too
                    {
                        // squeeze several subsequent slashes into one: i.e.
                        // just ignore this one
                        dst--;
                    }
                }
            }
            else // normal character
            {
                // just copy
                *dst = *src;
            }
        }

        // NUL terminate the string
        if ( dst[-1] == wxCONFIG_PATH_SEPARATOR && (dst != start + 1) )
        {
            // if it has a trailing slash we remove it unless it is the only
            // string character
            dst--;
        }

        *dst = wxT('\0');
        buf.SetLength(dst - start);
    }

#ifdef WX_DEBUG_SET_PATH
    wxASSERT( m_strPath == m_strPathAlt );
#endif

    if ( m_strPath == strOldPath )
        return;

    // registry APIs want backslashes instead of slashes
    wxString strRegPath;
    if ( !m_strPath.empty() )
    {
        size_t len = m_strPath.length();

        const wxChar *src = m_strPath.c_str();
        wxStringBufferLength buf(strRegPath, len);
        wxChar *dst = buf;

        const wxChar *end = src + len;
        for ( ; src < end; src++, dst++ )
        {
            if ( *src == wxCONFIG_PATH_SEPARATOR )
                *dst = wxT('\\');
            else
                *dst = *src;
        }

        buf.SetLength(len);
    }

    // this is not needed any longer as we don't create keys unnecessarily any
    // more (now it is done on demand, i.e. only when they're going to contain
    // something)
#if 0
    // as we create the registry key when SetPath(key) is done, we can be left
    // with plenty of empty keys if this was only done to try to read some
    // value which, in fact, doesn't exist - to prevent this from happening we
    // automatically delete the old key if it was empty
    if ( m_keyLocal.Exists() && LocalKey().IsEmpty() )
    {
        m_keyLocal.DeleteSelf();
    }
#endif // 0

    // change current key(s)
    m_keyLocal.SetName(m_keyLocalRoot, strRegPath);

    if ( GetStyle() & wxCONFIG_USE_GLOBAL_FILE )
    {
      m_keyGlobal.SetName(m_keyGlobalRoot, strRegPath);

      wxLogNull nolog;
      m_keyGlobal.Open(wxRegKey::Read);
    }
}
StorageReply::UP
ProtocolSerialization::decodeReply(mbus::BlobRef data, const api::StorageCommand& cmd) const
{
    LOG(spam, "Decode %d bytes of data.", data.size());
    if (data.size() < sizeof(int32_t)) {
        std::ostringstream ost;
        ost << "Request of size " << data.size() << " is not big enough to be "
            "able to store a request.";
        throw vespalib::IllegalArgumentException(ost.str(), VESPA_STRLOC);
    }

    document::ByteBuffer buf(data.data(), data.size());
    int type;
    buf.getIntNetwork(type);
    SRep::UP reply;
    switch (type) {
    case api::MessageType::PUT_REPLY_ID:
        reply = onDecodePutReply(cmd, buf); break;
    case api::MessageType::UPDATE_REPLY_ID:
        reply = onDecodeUpdateReply(cmd, buf); break;
    case api::MessageType::GET_REPLY_ID:
        reply = onDecodeGetReply(cmd, buf); break;
    case api::MessageType::REMOVE_REPLY_ID:
        reply = onDecodeRemoveReply(cmd, buf); break;
    case api::MessageType::REVERT_REPLY_ID:
        reply = onDecodeRevertReply(cmd, buf); break;
    case api::MessageType::CREATEBUCKET_REPLY_ID:
        reply = onDecodeCreateBucketReply(cmd, buf); break;
    case api::MessageType::DELETEBUCKET_REPLY_ID:
        reply = onDecodeDeleteBucketReply(cmd, buf); break;
    case api::MessageType::MERGEBUCKET_REPLY_ID:
        reply = onDecodeMergeBucketReply(cmd, buf); break;
    case api::MessageType::GETBUCKETDIFF_REPLY_ID:
        reply = onDecodeGetBucketDiffReply(cmd, buf); break;
    case api::MessageType::APPLYBUCKETDIFF_REPLY_ID:
        reply = onDecodeApplyBucketDiffReply(cmd, buf); break;
    case api::MessageType::REQUESTBUCKETINFO_REPLY_ID:
        reply = onDecodeRequestBucketInfoReply(cmd, buf); break;
    case api::MessageType::NOTIFYBUCKETCHANGE_REPLY_ID:
        reply = onDecodeNotifyBucketChangeReply(cmd, buf); break;
    case api::MessageType::SPLITBUCKET_REPLY_ID:
        reply = onDecodeSplitBucketReply(cmd, buf); break;
    case api::MessageType::JOINBUCKETS_REPLY_ID:
        reply = onDecodeJoinBucketsReply(cmd, buf); break;
    case api::MessageType::VISITOR_CREATE_REPLY_ID:
        reply = onDecodeCreateVisitorReply(cmd, buf); break;
    case api::MessageType::VISITOR_DESTROY_REPLY_ID:
        reply = onDecodeDestroyVisitorReply(cmd, buf); break;
    case api::MessageType::REMOVELOCATION_REPLY_ID:
        reply = onDecodeRemoveLocationReply(cmd, buf); break;
    case api::MessageType::SETBUCKETSTATE_REPLY_ID:
        reply = onDecodeSetBucketStateReply(cmd, buf); break;
    default:
    {
        std::ostringstream ost;
        ost << "Unknown message type " << type;
        throw vespalib::IllegalArgumentException(ost.str(), VESPA_STRLOC);
    }
    }
    return std::make_unique<StorageReply>(std::move(reply));
}
Beispiel #5
0
Buffer AbstractData::encode() const {
  Buffer buf(get_buffers_size());
  encode_buffers(0, &buf);
  return buf;
}
Beispiel #6
0
// Parse a TrueType VDMX table.
//   yMax: (output) the ascender value from the table
//   yMin: (output) the descender value from the table (negative!)
//   vdmx: the table bytes
//   vdmxLength: length of @vdmx, in bytes
//   targetPixelSize: the pixel size of the font (e.g. 16)
//
// Returns true iff a suitable match are found. Otherwise, *yMax and *yMin are
// untouched. size_t must be 32-bits to avoid overflow.
//
// See http://www.microsoft.com/opentype/otspec/vdmx.htm
bool parseVDMX(int* yMax, int* yMin,
               const uint8_t* vdmx, size_t vdmxLength,
               unsigned targetPixelSize)
{
    Buffer buf(vdmx, vdmxLength);

    // We ignore the version. Future tables should be backwards compatible with
    // this layout.
    uint16_t numRatios;
    if (!buf.skip(4) || !buf.readU16(&numRatios))
        return false;

    // Now we have two tables. Firstly we have @numRatios Ratio records, then a
    // matching array of @numRatios offsets. We save the offset of the beginning
    // of this second table.
    //
    // Range 6 <= x <= 262146
    unsigned long offsetTableOffset =
        buf.offset() + 4 /* sizeof struct ratio */ * numRatios;

    unsigned desiredRatio = 0xffffffff;
    // We read 4 bytes per record, so the offset range is
    //   6 <= x <= 524286
    for (unsigned i = 0; i < numRatios; ++i) {
        uint8_t xRatio, yRatio1, yRatio2;

        if (!buf.skip(1)
            || !buf.readU8(&xRatio)
            || !buf.readU8(&yRatio1)
            || !buf.readU8(&yRatio2))
            return false;

        // This either covers 1:1, or this is the default entry (0, 0, 0)
        if ((xRatio == 1 && yRatio1 <= 1 && yRatio2 >= 1)
            || (xRatio == 0 && yRatio1 == 0 && yRatio2 == 0)) {
            desiredRatio = i;
            break;
        }
    }

    if (desiredRatio == 0xffffffff) // no ratio found
        return false;

    // Range 10 <= x <= 393216
    buf.setOffset(offsetTableOffset + sizeof(uint16_t) * desiredRatio);

    // Now we read from the offset table to get the offset of another array
    uint16_t groupOffset;
    if (!buf.readU16(&groupOffset))
        return false;
    // Range 0 <= x <= 65535
    buf.setOffset(groupOffset);

    uint16_t numRecords;
    if (!buf.readU16(&numRecords) || !buf.skip(sizeof(uint16_t)))
        return false;

    // We read 6 bytes per record, so the offset range is
    //   4 <= x <= 458749
    for (unsigned i = 0; i < numRecords; ++i) {
        uint16_t pixelSize;
        if (!buf.readU16(&pixelSize))
            return false;
        // the entries are sorted, so we can abort early if need be
        if (pixelSize > targetPixelSize)
            return false;

        if (pixelSize == targetPixelSize) {
            int16_t tempYMax, tempYMin;
            if (!buf.readS16(&tempYMax)
                || !buf.readS16(&tempYMin))
                return false;
            *yMin = tempYMin;
            *yMax = tempYMax;
            return true;
        }
        if (!buf.skip(2 * sizeof(int16_t)))
            return false;
    }

    return false;
}
Beispiel #7
0
static void
selection_handler( GtkWidget *WXUNUSED(widget),
                   GtkSelectionData *selection_data,
                   guint WXUNUSED(info),
                   guint WXUNUSED(time),
                   gpointer signal_data )
{
    wxClipboard * const clipboard = wxTheClipboard;
    if ( !clipboard )
        return;

    wxDataObject * const data = clipboard->GTKGetDataObject(
        gtk_selection_data_get_selection(selection_data));
    if ( !data )
        return;

    // ICCCM says that TIMESTAMP is a required atom.
    // In particular, it satisfies Klipper, which polls
    // TIMESTAMP to see if the clipboards content has changed.
    // It shall return the time which was used to set the data.
    if (gtk_selection_data_get_target(selection_data) == g_timestampAtom)
    {
        guint timestamp = GPOINTER_TO_UINT (signal_data);
        gtk_selection_data_set(selection_data,
                               GDK_SELECTION_TYPE_INTEGER,
                               32,
                               (guchar*)&(timestamp),
                               sizeof(timestamp));
        wxLogTrace(TRACE_CLIPBOARD,
                   wxT("Clipboard TIMESTAMP requested, returning timestamp=%u"),
                   timestamp);
        return;
    }

    wxDataFormat format(gtk_selection_data_get_target(selection_data));

    wxLogTrace(TRACE_CLIPBOARD,
               wxT("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s timestamp=%u"),
               format.GetId().c_str(),
               wxString::FromAscii(wxGtkString(gdk_atom_name(gtk_selection_data_get_target(selection_data)))).c_str(),
               wxString::FromAscii(wxGtkString(gdk_atom_name(gtk_selection_data_get_data_type(selection_data)))).c_str(),
               wxString::FromAscii(wxGtkString(gdk_atom_name(gtk_selection_data_get_selection(selection_data)))).c_str(),
               GPOINTER_TO_UINT( signal_data )
               );

    if ( !data->IsSupportedFormat( format ) )
        return;

    int size = data->GetDataSize( format );
    if ( !size )
        return;

    wxCharBuffer buf(size - 1); // it adds 1 internally (for NUL)

    // text data must be returned in UTF8 if format is wxDF_UNICODETEXT
    if ( !data->GetDataHere(format, buf.data()) )
        return;

    // use UTF8_STRING format if requested in Unicode build but just plain
    // STRING one in ANSI or if explicitly asked in Unicode
#if wxUSE_UNICODE
    if (format == wxDataFormat(wxDF_UNICODETEXT))
    {
        gtk_selection_data_set_text(
            selection_data,
            (const gchar*)buf.data(),
            size );
    }
    else
#endif // wxUSE_UNICODE
    {
        gtk_selection_data_set(
            selection_data,
            GDK_SELECTION_TYPE_STRING,
            8*sizeof(gchar),
            (const guchar*)buf.data(),
            size );
    }
}
Beispiel #8
0
void HostListItem::paintCell(QPainter * p, const QColorGroup & cg, int column, int width, int align )
{
  QColorGroup m_cg( cg );

  // TODO: reuse icons?
  if( column == HostListItem::Video )
  {
    if( m_video ) { // video ?
      if( m_read_only )
        setPixmap( HostListItem::Video, SmallIcon("nmm_option_on_readonly")  );
      else
        setPixmap( HostListItem::Video, SmallIcon("nmm_option_on")  );
    }
    else
      if( ! m_read_only)
        setPixmap( HostListItem::Video, SmallIcon("nmm_option_off") );
  }
  else if( column == HostListItem::Audio )
  {
    if( m_audio ) {// audio ?
      if( m_read_only )
        setPixmap( HostListItem::Audio, SmallIcon("nmm_option_on_readonly")  );
      else
        setPixmap( HostListItem::Audio, SmallIcon("nmm_option_on")  );
    }
    else
      if( ! m_read_only)
        setPixmap( HostListItem::Audio, SmallIcon("nmm_option_off") );
  }
  else if( column ==  HostListItem::Status )
  {
    QFont font( p->font() );
    if( ! m_status  ) // Unknown
    {
      font.setBold( false );
      setText( HostListItem::Status , i18n("Unknown") );
    }
    else if( m_status == NmmEngine::STATUS_OK )
    {
      font.setBold( false );
      m_cg.setColor( QColorGroup::Text, Qt::darkGreen );
      setText( HostListItem::Status , i18n("OK") );
    }
    else { // error
      font.setBold( true );
      m_cg.setColor( QColorGroup::Text, Qt::red );
      setText( HostListItem::Status , i18n("Failed") );
    }
    p->setFont( font );
  }
  else if( column == HostListItem::Volume )
  {
    QPixmap buf( width, height() );
    QColor bg = listView()->viewport()->backgroundColor();
    buf.fill( bg );

    bitBlt( &buf, 0, 0, pixmapVolume( PixInset ) );

    // Draw gradient
    static int padding = 7;
    static int vol; // pixelposition
    if( this == ((HostList*)listView())->m_hoveredVolume )
    {
      vol = listView()->viewportToContents( listView()->viewport()->mapFromGlobal( QCursor::pos() ) ).x();
      vol -= listView()->header()->sectionPos( HostListItem::Volume );
    }
    else
      vol = (m_volume / 2) + 56;

    //std::cerr << "rel vol = " << vol << std::endl;

    static int center = 56;
    if( vol > center ) {
      bitBlt( &buf, 0, 0, pixmapVolume( PixRight ), 0, 0, vol + 1 /* TODO: why + 1??? */ );
    }
    else if ( vol < center ) {
      bitBlt( &buf, vol, 0, pixmapVolume( PixLeft ), vol, 0, 56 );
    }
    else
    {}

    // Calculate actual volume string from pixelposition
    vol = volumeAtPosition( vol );
    QString vol_text; 
    if( vol > 0 )
      vol_text = "+";
    vol_text += QString::number( vol );
    vol_text += "%";

    // Draw relative volume number
    QPainter p_number(&buf);
    p_number.setPen( cg.buttonText() );
    QFont font;
    font.setPixelSize( 9 );
    p_number.setFont( font );
    const QRect rect( 40, 0, 34, 15 );
    p_number.drawText( rect, Qt::AlignRight | Qt::AlignVCenter, vol_text );
    p_number.end();
    //bitBlt( p_number.device(), 0, 0, &buf );

    p->drawPixmap( 0, 0, buf );
    return;
  }

  KListViewItem::paintCell(p, m_cg, column, width, align);
}
// --------------------------------------------------------------------------
//
// Function
//		Name:    HousekeepStoreAccount::ScanDirectory(int64_t)
//		Purpose: Private. Scan a directory for potentially deleteable
//			 items, and add them to the list. Returns true if the
//			 scan should continue.
//		Created: 11/12/03
//
// --------------------------------------------------------------------------
bool HousekeepStoreAccount::ScanDirectory(int64_t ObjectID,
	BackupStoreInfo& rBackupStoreInfo)
{
#ifndef WIN32
	if((--mCountUntilNextInterprocessMsgCheck) <= 0)
	{
		mCountUntilNextInterprocessMsgCheck =
			POLL_INTERPROCESS_MSG_CHECK_FREQUENCY;

		// Check for having to stop
		// Include account ID here as the specified account is locked
		if(mpHousekeepingCallback && mpHousekeepingCallback->CheckForInterProcessMsg(mAccountID))
		{
			// Need to abort now
			return false;
		}
	}
#endif

	// Get the filename
	std::string objectFilename;
	MakeObjectFilename(ObjectID, objectFilename);

	// Open it.
	std::auto_ptr<RaidFileRead> dirStream(RaidFileRead::Open(mStoreDiscSet,
		objectFilename));

	// Add the size of the directory on disc to the size being calculated
	int64_t originalDirSizeInBlocks = dirStream->GetDiscUsageInBlocks();
	mBlocksInDirectories += originalDirSizeInBlocks;
	mBlocksUsed += originalDirSizeInBlocks;

	// Read the directory in
	BackupStoreDirectory dir;
	BufferedStream buf(*dirStream);
	dir.ReadFromStream(buf, IOStream::TimeOutInfinite);
	dir.SetUserInfo1_SizeInBlocks(originalDirSizeInBlocks);
	dirStream->Close();

	// Is it empty?
	if(dir.GetNumberOfEntries() == 0)
	{
		// Add it to the list of directories to potentially delete
		mEmptyDirectories.push_back(dir.GetObjectID());
	}

	// Calculate reference counts first, before we start requesting
	// files to be deleted.
	// BLOCK
	{
		BackupStoreDirectory::Iterator i(dir);
		BackupStoreDirectory::Entry *en = 0;

		while((en = i.Next()) != 0)
		{
			// This directory references this object
			mapNewRefs->AddReference(en->GetObjectID());
		}
	}

	// BLOCK
	{
		// Remove any files which are marked for removal as soon
		// as they become old or deleted.
		bool deletedSomething = false;
		do
		{
			// Iterate through the directory
			deletedSomething = false;
			BackupStoreDirectory::Iterator i(dir);
			BackupStoreDirectory::Entry *en = 0;
			while((en = i.Next(BackupStoreDirectory::Entry::Flags_File)) != 0)
			{
				int16_t enFlags = en->GetFlags();
				if((enFlags & BackupStoreDirectory::Entry::Flags_RemoveASAP) != 0
					&& (en->IsDeleted() || en->IsOld()))
				{
					// Delete this immediately.
					DeleteFile(ObjectID, en->GetObjectID(), dir,
						objectFilename, rBackupStoreInfo);
					
					// flag as having done something
					deletedSomething = true;

					// Must start the loop from the beginning again, as iterator is now
					// probably invalid.
					break;
				}
			}
		} while(deletedSomething);
	}

	// BLOCK
	{
		// Add files to the list of potential deletions

		// map to count the distance from the mark
		typedef std::pair<std::string, int32_t> version_t;
		std::map<version_t, int32_t> markVersionAges;
			// map of pair (filename, mark number) -> version age

		// NOTE: use a reverse iterator to allow the distance from mark stuff to work
		BackupStoreDirectory::ReverseIterator i(dir);
		BackupStoreDirectory::Entry *en = 0;

		while((en = i.Next(BackupStoreDirectory::Entry::Flags_File)) != 0)
		{
			// Update recalculated usage sizes
			int16_t enFlags = en->GetFlags();
			int64_t enSizeInBlocks = en->GetSizeInBlocks();
			mBlocksUsed += enSizeInBlocks;
			if(en->IsOld()) mBlocksInOldFiles += enSizeInBlocks;
			if(en->IsDeleted()) mBlocksInDeletedFiles += enSizeInBlocks;

			// Work out ages of this version from the last mark
			int32_t enVersionAge = 0;
			std::map<version_t, int32_t>::iterator enVersionAgeI(
				markVersionAges.find(
					version_t(en->GetName().GetEncodedFilename(),
						en->GetMarkNumber())));
			if(enVersionAgeI != markVersionAges.end())
			{
				enVersionAge = enVersionAgeI->second + 1;
				enVersionAgeI->second = enVersionAge;
			}
			else
			{
				markVersionAges[version_t(en->GetName().GetEncodedFilename(), en->GetMarkNumber())] = enVersionAge;
			}
			// enVersionAge is now the age of this version.

			// Potentially add it to the list if it's deleted, if it's an old version or deleted
			if(en->IsOld() || en->IsDeleted())
			{
				// Is deleted / old version.
				DelEn d;
				d.mObjectID = en->GetObjectID();
				d.mInDirectory = ObjectID;
				d.mSizeInBlocks = en->GetSizeInBlocks();
				d.mMarkNumber = en->GetMarkNumber();
				d.mVersionAgeWithinMark = enVersionAge;
				d.mIsFlagDeleted = en->IsDeleted();

				// Add it to the list
				mPotentialDeletions.insert(d);

				// Update various counts
				mPotentialDeletionsTotalSize += d.mSizeInBlocks;
				if(d.mSizeInBlocks > mMaxSizeInPotentialDeletions) mMaxSizeInPotentialDeletions = d.mSizeInBlocks;

				// Too much in the list of potential deletions?
				// (check against the deletion target + the max size in deletions, so that we never delete things
				// and take the total size below the deletion size target)
				if(mPotentialDeletionsTotalSize > (mDeletionSizeTarget + mMaxSizeInPotentialDeletions))
				{
					int64_t sizeToRemove = mPotentialDeletionsTotalSize - (mDeletionSizeTarget + mMaxSizeInPotentialDeletions);
					bool recalcMaxSize = false;

					while(sizeToRemove > 0)
					{
						// Make iterator for the last element, while checking that there's something there in the first place.
						std::set<DelEn, DelEnCompare>::iterator i(mPotentialDeletions.end());
						if(i != mPotentialDeletions.begin())
						{
							// Nothing left in set
							break;
						}
						// Make this into an iterator pointing to the last element in the set
						--i;

						// Delete this one?
						if(sizeToRemove > i->mSizeInBlocks)
						{
							sizeToRemove -= i->mSizeInBlocks;
							if(i->mSizeInBlocks >= mMaxSizeInPotentialDeletions)
							{
								// Will need to recalculate the maximum size now, because we've just deleted that element
								recalcMaxSize = true;
							}
							mPotentialDeletions.erase(i);
						}
						else
						{
							// Over the size to remove, so stop now
							break;
						}
					}

					if(recalcMaxSize)
					{
						// Because an object which was the maximum size recorded was deleted from the set
						// it's necessary to recalculate this maximum.
						mMaxSizeInPotentialDeletions = 0;
						std::set<DelEn, DelEnCompare>::const_iterator i(mPotentialDeletions.begin());
						for(; i != mPotentialDeletions.end(); ++i)
						{
							if(i->mSizeInBlocks > mMaxSizeInPotentialDeletions)
							{
								mMaxSizeInPotentialDeletions = i->mSizeInBlocks;
							}
						}
					}
				}
			}
		}
	}

	// Recurse into subdirectories
	{
		BackupStoreDirectory::Iterator i(dir);
		BackupStoreDirectory::Entry *en = 0;
		while((en = i.Next(BackupStoreDirectory::Entry::Flags_Dir)) != 0)
		{
			ASSERT(en->IsDir());
			
			if(!ScanDirectory(en->GetObjectID(), rBackupStoreInfo))
			{
				// Halting operation
				return false;
			}
		}
	}

	return true;
}
Beispiel #10
0
	std::string LoadMtl(
		std::map<std::string, material_t>& material_map,
		const char* filename,
		const char* mtl_basepath)
	{
		material_map.clear();
		std::stringstream err;

		std::string filepath;

		if (mtl_basepath) {
			filepath = std::string(mtl_basepath) + std::string(filename);
		}
		else {
			filepath = std::string(filename);
		}

		std::ifstream ifs(filepath.c_str());
		if (!ifs) {
			err << "Cannot open file [" << filepath << "]" << std::endl;
			return err.str();
		}

		material_t material;

		int maxchars = 8192;  // Alloc enough size.
		std::vector<char> buf(maxchars);  // Alloc enough size.
		while (ifs.peek() != -1) {
			ifs.getline(&buf[0], maxchars);

			std::string linebuf(&buf[0]);

			// Trim newline '\r\n' or '\r'
			if (linebuf.size() > 0) {
				if (linebuf[linebuf.size() - 1] == '\n') linebuf.erase(linebuf.size() - 1);
			}
			if (linebuf.size() > 0) {
				if (linebuf[linebuf.size() - 1] == '\n') linebuf.erase(linebuf.size() - 1);
			}

			// Skip if empty line.
			if (linebuf.empty()) {
				continue;
			}

			// Skip leading space.
			const char* token = linebuf.c_str();
			token += strspn(token, " \t");

			assert(token);
			if (token[0] == '\0') continue; // empty line

			if (token[0] == '#') continue;  // comment line

			// new mtl
			if ((0 == strncmp(token, "newmtl", 6)) && isSpace((token[6]))) {
				// flush previous material.
				material_map.insert(std::pair<std::string, material_t>(material.name, material));

				// initial temporary material
				InitMaterial(material);

				// set new mtl name
				char namebuf[4096];
				token += 7;
				sscanf(token, "%s", namebuf);
				material.name = namebuf;
				continue;
			}

			// ambient
			if (token[0] == 'K' && token[1] == 'a' && isSpace((token[2]))) {
				token += 2;
				float r, g, b;
				parseFloat3(r, g, b, token);
				material.ambient[0] = r;
				material.ambient[1] = g;
				material.ambient[2] = b;
				continue;
			}

			// diffuse
			if (token[0] == 'K' && token[1] == 'd' && isSpace((token[2]))) {
				token += 2;
				float r, g, b;
				parseFloat3(r, g, b, token);
				material.diffuse[0] = r;
				material.diffuse[1] = g;
				material.diffuse[2] = b;
				continue;
			}

			// specular
			if (token[0] == 'K' && token[1] == 's' && isSpace((token[2]))) {
				token += 2;
				float r, g, b;
				parseFloat3(r, g, b, token);
				material.specular[0] = r;
				material.specular[1] = g;
				material.specular[2] = b;
				continue;
			}

			// transmittance
			if (token[0] == 'K' && token[1] == 't' && isSpace((token[2]))) {
				token += 2;
				float r, g, b;
				parseFloat3(r, g, b, token);
				material.transmittance[0] = r;
				material.transmittance[1] = g;
				material.transmittance[2] = b;
				continue;
			}

			// ior(index of refraction)
			if (token[0] == 'N' && token[1] == 'i' && isSpace((token[2]))) {
				token += 2;
				material.ior = parseFloat(token);
				continue;
			}

			// emission
			if (token[0] == 'K' && token[1] == 'e' && isSpace(token[2])) {
				token += 2;
				float r, g, b;
				parseFloat3(r, g, b, token);
				material.emission[0] = r;
				material.emission[1] = g;
				material.emission[2] = b;
				continue;
			}

			// shininess
			if (token[0] == 'N' && token[1] == 's' && isSpace(token[2])) {
				token += 2;
				material.shininess = parseFloat(token);
				continue;
			}

			// illum model
			if (0 == strncmp(token, "illum", 5) && isSpace(token[5])) {
				token += 6;
				material.illum = parseInt(token);
				continue;
			}

			// dissolve
			if ((token[0] == 'd' && isSpace(token[1]))) {
				token += 1;
				material.dissolve = parseFloat(token);
				continue;
			}
			if (token[0] == 'T' && token[1] == 'r' && isSpace(token[2])) {
				token += 2;
				material.dissolve = parseFloat(token);
				continue;
			}

			// ambient texture
			if ((0 == strncmp(token, "map_Ka", 6)) && isSpace(token[6])) {
				token += 7;
				material.ambient_texname = token;
				continue;
			}

			// diffuse texture
			if ((0 == strncmp(token, "map_Kd", 6)) && isSpace(token[6])) {
				token += 7;
				material.diffuse_texname = token;
				continue;
			}

			// specular texture
			if ((0 == strncmp(token, "map_Ks", 6)) && isSpace(token[6])) {
				token += 7;
				material.specular_texname = token;
				continue;
			}

			// normal texture
			if (IsBumpMap(token)) {
				while (*token == '-') {
					// parse bump map options...
					if ((0 == strncmp(token, "-bm", 3)) && isSpace(token[3])) {
						token += 4;
						material.bump_scale = parseFloat(token);
					}
					else {
						ParseUnsupportedBumpModifier(token, err);
					}
					// skip space
					token += strspn(token, " \t");
				}
				material.normal_texname = token;
				continue;
			}

			// unknown parameter
			const char* _space = strchr(token, ' ');
			if (!_space) {
				_space = strchr(token, '\t');
			}
			if (_space) {
				int len = _space - token;
				std::string key(token, len);
				std::string value = _space + 1;
				material.unknown_parameter.insert(std::pair<std::string, std::string>(key, value));
			}
		}
		// flush last material.
		material_map.insert(std::pair<std::string, material_t>(material.name, material));

		return err.str();
	}
Beispiel #11
0
	std::string
		LoadObj(
		std::vector<shape_t>& shapes,
		const char* filename,
		const char* mtl_basepath)
	{

		shapes.clear();

		std::stringstream err;

		std::ifstream ifs(filename);
		if (!ifs) {
			err << "Cannot open file [" << filename << "]" << std::endl;
			return err.str();
		}

		std::vector<float> v;
		std::vector<float> vn;
		std::vector<float> vt;
		std::vector<std::vector<vertex_index> > faceGroup;
		std::string name;

		// material
		std::map<std::string, material_t> material_map;
		material_t material;

		int maxchars = 8192;  // Alloc enough size.
		std::vector<char> buf(maxchars);  // Alloc enough size.
		while (ifs.peek() != -1) {
			ifs.getline(&buf[0], maxchars);

			std::string linebuf(&buf[0]);

			// Trim newline '\r\n' or '\r'
			if (linebuf.size() > 0) {
				if (linebuf[linebuf.size() - 1] == '\n') linebuf.erase(linebuf.size() - 1);
			}
			if (linebuf.size() > 0) {
				if (linebuf[linebuf.size() - 1] == '\n') linebuf.erase(linebuf.size() - 1);
			}

			// Skip if empty line.
			if (linebuf.empty()) {
				continue;
			}

			// Skip leading space.
			const char* token = linebuf.c_str();
			token += strspn(token, " \t");

			assert(token);
			if (token[0] == '\0') continue; // empty line

			if (token[0] == '#') continue;  // comment line

			// vertex
			if (token[0] == 'v' && isSpace((token[1]))) {
				token += 2;
				float x, y, z;
				parseFloat3(x, y, z, token);
				v.push_back(x);
				v.push_back(y);
				v.push_back(z);
				continue;
			}

			// normal
			if (token[0] == 'v' && token[1] == 'n' && isSpace((token[2]))) {
				token += 3;
				float x, y, z;
				parseFloat3(x, y, z, token);
				vn.push_back(x);
				vn.push_back(y);
				vn.push_back(z);
				continue;
			}

			// texcoord
			if (token[0] == 'v' && token[1] == 't' && isSpace((token[2]))) {
				token += 3;
				float x, y;
				parseFloat2(x, y, token);
				vt.push_back(x);
				vt.push_back(y);
				continue;
			}

			// face
			if (token[0] == 'f' && isSpace((token[1]))) {
				token += 2;
				token += strspn(token, " \t");

				std::vector<vertex_index> face;
				while (!isNewLine(token[0])) {
					vertex_index vi = parseTriple(token, v.size() / 3, vn.size() / 3, vt.size() / 2);
					face.push_back(vi);
					int n = strspn(token, " \t\r");
					token += n;
				}

				faceGroup.push_back(face);

				continue;
			}

			// use mtl
			if ((0 == strncmp(token, "usemtl", 6)) && isSpace((token[6]))) {

				char namebuf[4096];
				token += 7;
				sscanf(token, "%s", namebuf);

				if (material_map.find(namebuf) != material_map.end()) {
					material = material_map[namebuf];
				}
				else {
					// { error!! material not found }
					InitMaterial(material);
				}
				continue;

			}

			// load mtl
			if ((0 == strncmp(token, "mtllib", 6)) && isSpace((token[6]))) {
				char namebuf[4096];
				token += 7;
				sscanf(token, "%s", namebuf);

				std::string err_mtl = LoadMtl(material_map, namebuf, mtl_basepath);
				if (!err_mtl.empty()) {
					faceGroup.clear();  // for safety
					return err_mtl;
				}
				continue;
			}

			// group name
			if (token[0] == 'g' && isSpace((token[1]))) {

				// flush previous face group.
				shape_t shape;
				bool ret = exportFaceGroupToShape(shape, v, vn, vt, faceGroup, material, name);
				if (ret) {
					shapes.push_back(shape);
				}

				faceGroup.clear();

				std::vector<std::string> names;
				while (!isNewLine(token[0])) {
					std::string str = parseString(token);
					names.push_back(str);
					token += strspn(token, " \t\r"); // skip tag
				}

				assert(names.size() > 0);

				// names[0] must be 'g', so skipt 0th element.
				if (names.size() > 1) {
					name = names[1];
				}
				else {
					name = "";
				}

				continue;
			}

			// object name
			if (token[0] == 'o' && isSpace((token[1]))) {

				// flush previous face group.
				shape_t shape;
				bool ret = exportFaceGroupToShape(shape, v, vn, vt, faceGroup, material, name);
				if (ret) {
					shapes.push_back(shape);
				}

				faceGroup.clear();

				// @todo { multiple object name? }
				char namebuf[4096];
				token += 2;
				sscanf(token, "%s", namebuf);
				name = std::string(namebuf);


				continue;
			}

			// Ignore unknown command.
		}

		shape_t shape;
		bool ret = exportFaceGroupToShape(shape, v, vn, vt, faceGroup, material, name);
		if (ret) {
			shapes.push_back(shape);
		}
		faceGroup.clear();  // for safety

		return err.str();
	}
void grid_renderer<T>::process(line_symbolizer const& sym,
                               mapnik::feature_impl & feature,
                               proj_transform const& prj_trans)
{
    using pixfmt_type = typename grid_renderer_base_type::pixfmt_type;
    using color_type = typename grid_renderer_base_type::pixfmt_type::color_type;
    using renderer_type = agg::renderer_scanline_bin_solid<grid_renderer_base_type>;
    using conv_types = boost::mpl::vector<clip_line_tag, transform_tag,
                                          offset_transform_tag, affine_transform_tag,
                                          simplify_tag, smooth_tag, dash_tag, stroke_tag>;
    agg::scanline_bin sl;

    grid_rendering_buffer buf(pixmap_.raw_data(), common_.width_, common_.height_, common_.width_);
    pixfmt_type pixf(buf);

    grid_renderer_base_type renb(pixf);
    renderer_type ren(renb);

    ras_ptr->reset();

    agg::trans_affine tr;
    auto transform = get_optional<transform_type>(sym, keys::geometry_transform);
    if (transform)
    {
        evaluate_transform(tr, feature, common_.vars_, *transform, common_.scale_factor_);
    }

    box2d<double> clipping_extent = common_.query_extent_;

    bool clip = get<value_bool>(sym, keys::clip, feature, common_.vars_, true);
    double width = get<value_double>(sym, keys::stroke_width, feature, common_.vars_,1.0);
    double offset = get<value_double>(sym, keys::offset, feature, common_.vars_,0.0);
    double simplify_tolerance = get<value_double>(sym, keys::simplify_tolerance, feature, common_.vars_,0.0);
    double smooth = get<value_double>(sym, keys::smooth, feature, common_.vars_,false);
    bool has_dash = has_key<dash_array>(sym, keys::stroke_dasharray);

    if (clip)
    {
        double padding = (double)(common_.query_extent_.width()/pixmap_.width());
        double half_stroke = width/2.0;
        if (half_stroke > 1)
            padding *= half_stroke;
        if (std::fabs(offset) > 0)
            padding *= std::fabs(offset) * 1.2;
        padding *= common_.scale_factor_;
        clipping_extent.pad(padding);
    }

    vertex_converter<box2d<double>, grid_rasterizer, line_symbolizer,
                     CoordTransform, proj_transform, agg::trans_affine, conv_types, feature_impl>
        converter(clipping_extent,*ras_ptr,sym,common_.t_,prj_trans,tr,feature,common_.vars_,common_.scale_factor_);
    if (clip) converter.set<clip_line_tag>(); // optional clip (default: true)
    converter.set<transform_tag>(); // always transform
    if (std::fabs(offset) > 0.0) converter.set<offset_transform_tag>(); // parallel offset
    converter.set<affine_transform_tag>(); // optional affine transform
    if (simplify_tolerance > 0.0) converter.set<simplify_tag>(); // optional simplify converter
    if (smooth > 0.0) converter.set<smooth_tag>(); // optional smooth converter
    if (has_dash) converter.set<dash_tag>();
    converter.set<stroke_tag>(); //always stroke

    for ( geometry_type & geom : feature.paths())
    {
        if (geom.size() > 1)
        {
            converter.apply(geom);
        }
    }

    // render id
    ren.color(color_type(feature.id()));
    ras_ptr->filling_rule(agg::fill_non_zero);
    agg::render_scanlines(*ras_ptr, sl, ren);

    // add feature properties to grid cache
    pixmap_.add_feature(feature);

}
Beispiel #13
0
DWORD ServerHandler()
{

    bool running = true;

    // Socket handler
    SOCKET socketHnd;
    // Winsock data
    WSADATA wsaData;

    // Check for error
    if (WSAStartup(WSCK_V2, &wsaData))
    {
        errorFlags &= ERR_STARTUP;
        running = false;
    }

    // We want version 2
    if (wsaData.wVersion != WSCK_V2)
    {
        errorFlags &= ERR_WRONGVERSION;
        running = false;
    }

    // For TCP...
    SOCKADDR_IN sckAddr;
    sckAddr.sin_family = AF_INET;
    sckAddr.sin_port = htons(9009);	// Port 9009, will probably change to load from a config file later
    sckAddr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen from connections from ANY computer

    // Create the socket
    socketHnd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (socketHnd == INVALID_SOCKET)
    {
        errorFlags &= ERR_SOCKETERR;
        running = false;
    }

    if (bind(socketHnd, (LPSOCKADDR)&sckAddr, sizeof(sckAddr)) == SOCKET_ERROR)
    {
        errorFlags &= ERR_BINDERR;
        running = false;
    }

    if (running && errorFlags == 0)
    {
        MessageBoxA(NULL, "Server initialized, close this window to begin listening!", "Info", MB_ICONINFORMATION);
        // Listen with one backlog max
        listen(socketHnd, 1);
    }

    // player X and Y, 4 bytes at 0049E654 and 0049E658
    DWORD *playerX;
    DWORD *playerY;

    DWORD *playerMapID;

    PACKETHEADER PID;
    while (running)
    {
        playerX = (DWORD*)0x0049E654;
        playerY = (DWORD*)0x0049E658;

        playerMapID = (DWORD*)0x004A57F0;

        // Ctrl + F12 unloads the DLL
        if (GetKeyState(VK_CONTROL) && GetKeyState(VK_F12))
            running = false;

        // If polled to exit then stop running
        if (poll_exit)
            running = false;

        /*=========================================
        /* Data sending
        /*=========================================*/
        Buffer buf(13);	// 13 bytes in length
        buf.WriteByte(ID_LOC);
        buf.WriteInt((int)&playerX);
        buf.WriteInt((int)&playerY);
        buf.WriteInt((int)&playerMapID);

        send(socketHnd, (char*)buf.GetBytes(), buf.GetLength(), 0);
        //buf.Clear();

        Buffer buf2(80);
        // Pass the old buffer pointer
        recv(socketHnd, (char*)buf2.GetBytes(), sizeof(buf2.GetBytes())-1, 0);

        // Sleep to allow execution of other threads (and limit actions to about 30 FPS)
        Sleep(34);
    }

    //MessageBoxA(NULL, "test", "test", NULL);

    WSACleanup();

    //if (errorFlags > 0)
    //{
    //	char errMsg[1024];
    //	strcpy(errMsg, "There were error(s):\n");

    //	if (errorFlags & ERR_STARTUP)
    //		strcat(errMsg, "An error occurred while WinSock was initializing\n");
    //	if (errorFlags & ERR_WRONGVERSION)
    //		strcat(errMsg, "WinSock initialized the wrong version\n");
    //	if (errorFlags & ERR_SOCKETERR)
    //		strcat(errMsg, "Socket creation failed\n");
    //	if (errorFlags & ERR_BINDERR)
    //		strcat(errMsg, "Socket bind failed\n");
    //
    //	MessageBoxA(NULL, errMsg, "Error", MB_ICONERROR);
    //}

    return 0;
}
void
LoadVariablesThread::completeLoad()
{
#ifdef DEBUG_LOAD_VARIABLES
    log_debug("completeLoad called");
#endif


	// TODO: how to set _bytesTotal ?

	// this is going to override any previous setting,
	// better do this inside a subclass (in a separate thread)
	_bytesLoaded = 0;
	_bytesTotal = _stream->size();

	std::string toparse;

	const size_t chunkSize = 1024;
	boost::scoped_array<char> buf(new char[chunkSize]);
	unsigned int parsedLines = 0;
	// TODO: use read_string ?
	while ( size_t bytesRead = _stream->read(buf.get(), chunkSize) )
	{
#ifdef DEBUG_LOAD_VARIABLES
            log_debug("Read %u bytes", bytesRead);
#endif

		if ( _bytesLoaded )
		{
			std::string chunk(buf.get(), bytesRead);
			toparse += chunk;
		}
		else
		{
			size_t dataSize = bytesRead;
			utf8::TextEncoding encoding;
			char* ptr = utf8::stripBOM(buf.get(), dataSize,
					encoding);
			if ( encoding != utf8::encUTF8 &&
			     encoding != utf8::encUNSPECIFIED )
			{
                  log_unimpl(_("%s to UTF8 conversion in "
					    "MovieClip.loadVariables "
                                         "input parsing"),
					    utf8::textEncodingName(encoding));
			}
			std::string chunk(ptr, dataSize);
			toparse += chunk;
		}

#ifdef DEBUG_LOAD_VARIABLES
		log_debug("toparse: %s", toparse);
#endif

		// parse remainder
		size_t lastamp = toparse.rfind('&');
		if ( lastamp != std::string::npos )
		{
			std::string parseable = toparse.substr(0, lastamp);
#ifdef DEBUG_LOAD_VARIABLES
			log_debug("parseable: %s", parseable);
#endif
			parse(parseable);
			toparse = toparse.substr(lastamp+1);
#ifdef DEBUG_LOAD_VARIABLES
			log_debug("toparse nextline: %s", toparse);
#endif
			++parsedLines;
		}

		_bytesLoaded += bytesRead;

		// eof, get out !
		if ( _stream->eof() ) break;

		if ( cancelRequested() ) {
                    log_debug("Cancelling LoadVariables download thread...");
			_stream.reset();
			return;
		}
	}

	if ( ! toparse.empty() ) {
		parse(toparse);
	}

	try {
		_stream->go_to_end();
	}
        catch (IOException& ex) {
        log_error(_("Stream couldn't seek to end: %s"), ex.what());
	}
	
    _bytesLoaded = _stream->tell();
	if ( _bytesTotal !=  _bytesLoaded ) {
            log_error(_("Size of 'variables' stream advertised to be %d bytes,"
                          " but turned out to be %d bytes."),
			_bytesTotal, _bytesLoaded);
		_bytesTotal = _bytesLoaded;
	}

	_stream.reset(); // we don't need the IOChannel anymore

	//dispatchLoadEvent();
	setCompleted();
}
Beispiel #15
0
void Vector<T>::append(const T *v, unsigned vSize) {
    unsigned oldSize = size();
    setSize(oldSize + vSize);
    memcpy(buf() + oldSize, v, vSize * sizeof(T));
}
Beispiel #16
0
void RigidBody::SetNetAngularVelocityAttr(const PODVector<unsigned char>& value)
{
    float maxVelocity = physicsWorld_ ? physicsWorld_->GetMaxNetworkAngularVelocity() : DEFAULT_MAX_NETWORK_ANGULAR_VELOCITY;
    MemoryBuffer buf(value);
    SetAngularVelocity(buf.ReadPackedVector3(maxVelocity));
}
void sendReply ( void *state ) {

	StateStatsdb *st = (StateStatsdb *)state;

	if ( g_errno ) {
		g_httpServer.sendErrorReply(st->m_socket,
					    500,mstrerror(g_errno));
		return;
	}

	TcpSocket *s = st->m_socket;

	SafeBuf buf( 1024*32 , "tmpbuf0" );
	SafeBuf tmpBuf( 1024 , "tmpbuf1" );

	//
	// take these out until we need them!
	//
	/*
	// print the top of the page
	tmpBuf.safePrintf( 
			  //"<style type=\"text/css\">"
			  //"@import url(/styles/statsdb.css);</style>\n"
		"<script type=\"text/javascript\" "
		"src=\"/scripts/statsdb.js\"></script>\n"
		"<!-- DHTML Calendar -->"
		"<style type=\"text/css\">"
		"@import url(/jsc/calendar-win2k-1.css);"
		"</style>\n"
		"<script type=\"text/javascript\" "
		"src=\"/jsc/calendar.js\"></script>\n"
		"<script type=\"text/javascript\" "
		"src=\"/jsc/lang/calendar-en.js\"></script>\n"
		"<script type=\"text/javascript\" "
		"src=\"/jsc/calendar-setup.js\"></script>\n"
	);
	*/

	// make the query string
	char qs[1024];
	sprintf(qs,"&date_period=%li&date_units=%li&samples=%li",
		st->m_datePeriod,
		st->m_dateUnits,
		st->m_samples);

	// print standard header
	g_pages.printAdminTop ( &buf , st->m_socket , &st->m_request ,
				qs );

	buf.cat ( tmpBuf );

	//g_pages.printAdminTop2 ( &buf , st->m_socket , &st->m_request, NULL ,
	//			 tmpBuf.getBufStart(), tmpBuf.length() ); 

	// Debug print of CGI parameters and errors
	char startTimeStr[30];
	char endTimeStr[30];

	strncpy( startTimeStr, ctime( &st->m_startDate ), 30 );
	strncpy( endTimeStr, ctime( &st->m_endDate ), 30 );

	buf.safePrintf(
		       "<b>Graph of various query performance statistics.</b>"
		       "<br>"
		       "<br>"
		       );


	buf.safePrintf("<center>\n");

	if ( ! g_conf.m_useStatsdb ) 
		buf.safePrintf("<font color=red><b>Statsdb disabled. "
			       "Turn on in the master controls.</b>"
			       "</font>\n" );


	buf.safePrintf("<table %s>\n",TABLE_STYLE);

	buf.safePrintf("<tr><td bgcolor=#%s>"
		       "<center>",LIGHT_BLUE);

	/////////////////////////
	//
	// insert the div graph here
	//
	/////////////////////////
	buf.cat ( g_statsdb.m_gw );

	// purge it
	g_statsdb.m_gw.purge();
	g_statsdb.m_dupTable.reset();

	//"<img src=\"/stats%li.gif\" height=%li width=%li "
	//"border=\"0px\">"
	//st->m_hostId,
	//g_statsdb.getImgHeight(),
	//g_statsdb.getImgWidth());

	buf.safePrintf("</center>"
		       //"class=\"statsdb_image\">"
		       "</td></tr>\n");

	// the map key
	buf.safePrintf("<tr><td>");
	buf.cat ( st->m_sb2 );
	buf.safePrintf("</td></tr>\n");

	buf.safePrintf( "</table>\n" );

	buf.safePrintf("</center>");

	// write the controls section of the page
	writeControls( &buf, st );

	// print the bottom of the page
	g_pages.printAdminBottom2( &buf );
	
	g_errno = 0;
	mdelete ( st, sizeof(StateStatsdb), "PageStatsdb" );
	delete st;

	g_httpServer.sendDynamicPage ( s, buf.getBufStart(), buf.length() );
}
Beispiel #18
0
/*
 * This routine is called only during a Verify
 */
void get_attributes_and_compare_to_catalog(JCR *jcr, JobId_t JobId)
{
   BSOCK *fd;
   int n, len;
   FILE_DBR fdbr;
   struct stat statf;                 /* file stat */
   struct stat statc;                 /* catalog stat */
   POOL_MEM buf(PM_MESSAGE);
   POOLMEM *fname = get_pool_memory(PM_FNAME);
   int do_Digest = CRYPTO_DIGEST_NONE;
   int32_t file_index = 0;

   memset(&fdbr, 0, sizeof(FILE_DBR));
   fd = jcr->file_bsock;
   fdbr.JobId = JobId;
   jcr->FileIndex = 0;

   Dmsg0(20, "dir: waiting to receive file attributes\n");
   /*
    * Get Attributes and Signature from File daemon
    * We expect:
    *   FileIndex
    *   Stream
    *   Options or Digest (MD5/SHA1)
    *   Filename
    *   Attributes
    *   Link name  ???
    */
   while ((n=bget_dirmsg(fd)) >= 0 && !job_canceled(jcr)) {
      int stream;
      char *attr, *p, *fn;
      POOL_MEM Opts_Digest(PM_MESSAGE);   /* Verify Opts or MD5/SHA1 digest */

      if (job_canceled(jcr)) {
         goto bail_out;
      }
      fname = check_pool_memory_size(fname, fd->msglen);
      jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
      Dmsg1(200, "Atts+Digest=%s\n", fd->msg);
      if ((len = sscanf(fd->msg, "%ld %d %100s", &file_index, &stream,
            fname)) != 3) {
         Jmsg3(jcr, M_FATAL, 0, _("dird<filed: bad attributes, expected 3 fields got %d\n"
                                  " mslen=%d msg=%s\n"), len, fd->msglen, fd->msg);
         goto bail_out;
      }
      /*
       * We read the Options or Signature into fname
       *  to prevent overrun, now copy it to proper location.
       */
      pm_strcpy(Opts_Digest, fname);
      p = fd->msg;
      skip_nonspaces(&p);             /* skip FileIndex */
      skip_spaces(&p);
      skip_nonspaces(&p);             /* skip Stream */
      skip_spaces(&p);
      skip_nonspaces(&p);             /* skip Opts_Digest */
      p++;                            /* skip space */
      fn = fname;
      while (*p != 0) {
         *fn++ = *p++;                /* copy filename */
      }
      *fn = *p++;                     /* term filename and point to attribs */
      attr = p;

      /*
       * Got attributes stream, decode it
       */
      switch (stream) {
      case STREAM_UNIX_ATTRIBUTES:
      case STREAM_UNIX_ATTRIBUTES_EX:
         int32_t LinkFIf, LinkFIc;
         Dmsg2(400, "file_index=%d attr=%s\n", file_index, attr);
         jcr->JobFiles++;
         jcr->FileIndex = file_index;    /* remember attribute file_index */
         jcr->previous_jr.FileIndex = file_index;
         decode_stat(attr, &statf, sizeof(statf), &LinkFIf);  /* decode file stat packet */
         do_Digest = CRYPTO_DIGEST_NONE;
         jcr->fn_printed = false;
         pm_strcpy(jcr->fname, fname);  /* move filename into JCR */

         Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->fname);
         Dmsg1(020, "dird<filed: attr=%s\n", attr);

         /*
          * Find equivalent record in the database
          */
         fdbr.FileId = 0;
         if (!db_get_file_attributes_record(jcr, jcr->db, jcr->fname,
              &jcr->previous_jr, &fdbr)) {
            Jmsg(jcr, M_INFO, 0, _("New file: %s\n"), jcr->fname);
            Dmsg1(020, _("File not in catalog: %s\n"), jcr->fname);
            jcr->setJobStatus(JS_Differences);
            continue;
         } else {
            /*
             * mark file record as visited by stuffing the
             * current JobId, which is unique, into the MarkId field.
             */
            db_mark_file_record(jcr, jcr->db, fdbr.FileId, jcr->JobId);
         }

         Dmsg3(400, "Found %s in catalog. inx=%d Opts=%s\n",
               jcr->fname, file_index, Opts_Digest.c_str());
         decode_stat(fdbr.LStat, &statc, sizeof(statc), &LinkFIc); /* decode catalog stat */
         /*
          * Loop over options supplied by user and verify the
          * fields he requests.
          */
         for (p = Opts_Digest.c_str(); *p; p++) {
            char ed1[30], ed2[30];
            switch (*p) {
            case 'i':                /* compare INODEs */
               if (statc.st_ino != statf.st_ino) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      st_ino   differ. Cat: %s File: %s\n"),
                     edit_uint64((uint64_t)statc.st_ino, ed1),
                     edit_uint64((uint64_t)statf.st_ino, ed2));
                  jcr->setJobStatus(JS_Differences);
               }
               break;
            case 'p':                /* permissions bits */
               if (statc.st_mode != statf.st_mode) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      st_mode  differ. Cat: %x File: %x\n"),
                     (uint32_t)statc.st_mode, (uint32_t)statf.st_mode);
                  jcr->setJobStatus(JS_Differences);
               }
               break;
            case 'n':                /* number of links */
               if (statc.st_nlink != statf.st_nlink) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      st_nlink differ. Cat: %d File: %d\n"),
                     (uint32_t)statc.st_nlink, (uint32_t)statf.st_nlink);
                  jcr->setJobStatus(JS_Differences);
               }
               break;
            case 'u':                /* user id */
               if (statc.st_uid != statf.st_uid) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      st_uid   differ. Cat: %u File: %u\n"),
                     (uint32_t)statc.st_uid, (uint32_t)statf.st_uid);
                  jcr->setJobStatus(JS_Differences);
               }
               break;
            case 'g':                /* group id */
               if (statc.st_gid != statf.st_gid) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      st_gid   differ. Cat: %u File: %u\n"),
                     (uint32_t)statc.st_gid, (uint32_t)statf.st_gid);
                  jcr->setJobStatus(JS_Differences);
               }
               break;
            case 's':                /* size */
               if (statc.st_size != statf.st_size) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      st_size  differ. Cat: %s File: %s\n"),
                     edit_uint64((uint64_t)statc.st_size, ed1),
                     edit_uint64((uint64_t)statf.st_size, ed2));
                  jcr->setJobStatus(JS_Differences);
               }
               break;
            case 'a':                /* access time */
               if (statc.st_atime != statf.st_atime) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      st_atime differs\n"));
                  jcr->setJobStatus(JS_Differences);
               }
               break;
            case 'm':
               if (statc.st_mtime != statf.st_mtime) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      st_mtime differs\n"));
                  jcr->setJobStatus(JS_Differences);
               }
               break;
            case 'c':                /* ctime */
               if (statc.st_ctime != statf.st_ctime) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      st_ctime differs\n"));
                  jcr->setJobStatus(JS_Differences);
               }
               break;
            case 'd':                /* file size decrease */
               if (statc.st_size > statf.st_size) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      st_size  decrease. Cat: %s File: %s\n"),
                     edit_uint64((uint64_t)statc.st_size, ed1),
                     edit_uint64((uint64_t)statf.st_size, ed2));
                  jcr->setJobStatus(JS_Differences);
               }
               break;
            case '5':                /* compare MD5 */
               Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
               do_Digest = CRYPTO_DIGEST_MD5;
               break;
            case '1':                 /* compare SHA1 */
               do_Digest = CRYPTO_DIGEST_SHA1;
               break;
            case ':':
            case 'V':
            default:
               break;
            }
         }
         break;
      default:
         /*
          * Got Digest Signature from Storage daemon
          *  It came across in the Opts_Digest field.
          */
         if (crypto_digest_stream_type(stream) != CRYPTO_DIGEST_NONE) {
            Dmsg2(400, "stream=Digest inx=%d Digest=%s\n", file_index, Opts_Digest.c_str());
            /*
             * When ever we get a digest it MUST have been
             * preceded by an attributes record, which sets attr_file_index
             */
            if (jcr->FileIndex != (uint32_t)file_index) {
               Jmsg2(jcr, M_FATAL, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
                  file_index, jcr->FileIndex);
               goto bail_out;
            }
            if (do_Digest != CRYPTO_DIGEST_NONE) {
               db_escape_string(jcr, jcr->db, buf.c_str(), Opts_Digest.c_str(), strlen(Opts_Digest.c_str()));
               if (!bstrcmp(buf.c_str(), fdbr.Digest)) {
                  prt_fname(jcr);
                  Jmsg(jcr, M_INFO, 0, _("      %s differs. File=%s Cat=%s\n"),
                       stream_to_ascii(stream), buf.c_str(), fdbr.Digest);
                  jcr->setJobStatus(JS_Differences);
               }
               do_Digest = CRYPTO_DIGEST_NONE;
            }
         }
         break;
      }
      jcr->JobFiles = file_index;
   }

   if (is_bnet_error(fd)) {
      berrno be;
      Jmsg2(jcr, M_FATAL, 0, _("dir<filed: bad attributes from filed n=%d : %s\n"),
                        n, be.bstrerror());
      goto bail_out;
   }

   /* Now find all the files that are missing -- i.e. all files in
    *  the database where the MarkId != current JobId
    */
   jcr->fn_printed = false;
   Mmsg(buf,
      "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
      "WHERE File.JobId=%d AND File.FileIndex > 0 "
      "AND File.MarkId!=%d AND File.PathId=Path.PathId "
      "AND File.FilenameId=Filename.FilenameId",
         JobId, jcr->JobId);
   /* missing_handler is called for each file found */
   db_sql_query(jcr->db, buf.c_str(), missing_handler, (void *)jcr);
   if (jcr->fn_printed) {
      jcr->setJobStatus(JS_Differences);
   }

bail_out:
   free_pool_memory(fname);
}
	void StandardSensors::OnThreadExecute(void)
	{
		SetThreadName("CaptureSensors");

		// Pre-load what files we can... reduces open/close overhead (which is significant)

		// Setup CPU Clocks Support...
		static const UInt32 maxCpus = 8;
		static SensorLabelDesc cpuDescs[maxCpus];
		FileHandle cpuOnlineFiles[maxCpus];
		FileHandle cpuFreqFiles[maxCpus];
		for(UInt32 i=0; i<maxCpus; i++)
		{
			cpuOnlineFiles[i] = cpuFreqFiles[i] = NullFileHandle;
			SensorLabelDesc &desc = cpuDescs[i];
			FormatString(desc.name, sizeof(desc.name), "CPU%u Clocks", i);
			desc.label.ConditionalInit(desc.name);
		}
		if(CheckConnectionFlag(Enable_CPU_Zones))
		{
			int maxFreq = 0;
			for(UInt32 i=0; i<maxCpus; i++)
			{
				char onlinePath[64] = {0};
				FormatString(onlinePath, sizeof(onlinePath), "/sys/devices/system/cpu/cpu%u/online", i);
				cpuOnlineFiles[i] = OpenFile(onlinePath);
				cpuFreqFiles[i]   = NullFileHandle;
				if(cpuOnlineFiles[i] != NullFileHandle)
				{
					char maxFreqPath[64] = {0};
					FormatString(maxFreqPath, sizeof(maxFreqPath), "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq", i);
					maxFreq = std::max(maxFreq, ReadIntFile(maxFreqPath));
				}
			}
			for(UInt32 i=0; i<maxCpus; i++)
			{
				const SensorLabelDesc &desc = cpuDescs[i];
				SensorSetRange(desc.label, 0, (float)maxFreq, Sensor_Interp_Nearest, Sensor_Unit_KHz);
			}
		}

		// Setup GPU Clocks Support...
		FileHandle gpuFreqFile = NullFileHandle;
		if(CheckConnectionFlag(Enable_GPU_Clocks))
		{
			if(gpuFreqFile == NullFileHandle) // Adreno
			{
				gpuFreqFile = OpenFile("/sys/class/kgsl/kgsl-3d0/gpuclk");
				if(gpuFreqFile != NullFileHandle)
				{
					const int maxFreq = ReadIntFile("/sys/class/kgsl/kgsl-3d0/max_gpuclk");
					SensorSetRange(g_gpuLabel, 0, (float)maxFreq, Sensor_Interp_Nearest, Sensor_Unit_Hz);
				}
			}
			if(gpuFreqFile == NullFileHandle) // Mali
			{
				gpuFreqFile = OpenFile("/sys/class/misc/mali0/device/clock");
				if(gpuFreqFile != NullFileHandle)
				{
					std::string buf(256, '\0');
					ReadFileLine("/sys/class/misc/mali0/device/dvfs_table", &buf[0], (int)buf.size());

					// dvfs_table contains a space delimited list of all possible clock rates... so pick the greatest...
					int maxFreq = 0;
					std::stringstream bufstream(buf);
					std::istream_iterator<int> begin(bufstream);
					std::istream_iterator<int> end;
					for(std::istream_iterator<int> s=begin; s!=end; s++)
					{
						maxFreq = std::max(maxFreq, *s);
					}

					SensorSetRange(g_gpuLabel, 0, (float)maxFreq, Sensor_Interp_Nearest, Sensor_Unit_MHz);
				}
			}
		}

		// Setup Memory Clocks Support...
		FileHandle memFreqFile = NullFileHandle;
		//memFreqFile = OpenFile("/sys/class/devfreq/0.qcom,cpubw/cur_freq");
		if(memFreqFile != NullFileHandle)
		{
			const int maxFreq = ReadIntFile("/sys/class/devfreq/0.qcom,cpubw/max_freq");
			SensorSetRange(g_memLabel, 0, (float)maxFreq, Sensor_Interp_Nearest, Sensor_Unit_MByte_Second);
		}

		// Setup thermal sensors...
		static const UInt32 maxThermalSensors = 20;
		static SensorLabelDesc  thermalDescs[maxThermalSensors];
		FileHandle              thermalFiles[maxThermalSensors];
		for(UInt32 i=0; i<maxThermalSensors; i++)
		{
			thermalFiles[i] = NullFileHandle;
		}
		if(CheckConnectionFlag(Enable_Thermal_Sensors))
		{
			for(UInt32 i=0; i<maxThermalSensors; i++)
			{
				SensorLabelDesc &desc = thermalDescs[i];

				char typePath[64]      = {0};
				char tempPath[64]      = {0};
				char tripPointPath[64] = {0};
				FormatString(typePath,      sizeof(typePath),      "/sys/devices/virtual/thermal/thermal_zone%u/type", i);
				FormatString(tempPath,      sizeof(tempPath),      "/sys/devices/virtual/thermal/thermal_zone%u/temp", i);
				FormatString(tripPointPath, sizeof(tripPointPath), "/sys/devices/virtual/thermal/thermal_zone%u/trip_point_0_temp", i);

				// If either of these files don't exist, then we got to the end of the thermal zone list...
				if(!CheckFileExists(typePath) || !CheckFileExists(tempPath) || !CheckFileExists(tripPointPath))
					break;

				// Initialize the Label...
				if(ReadFileLine(typePath, desc.name, sizeof(desc.name)) <= 0)
					continue; // failed to read sensor name...
				desc.label.ConditionalInit(desc.name);

				char modePath[64] = {0};
				FormatString(modePath, sizeof(modePath), "/sys/devices/virtual/thermal/thermal_zone%d/mode", i);

				// check to see if the zone is disabled... its okay if there is no mode file...
				char mode[16] = {0};
				if(ReadFileLine(modePath, mode, sizeof(mode))>0 && !strcmp(mode, "disabled"))
					continue;

				// Finally... open the file.
				thermalFiles[i] = OpenFile(tempPath);

				// Check to see if the temperature file was found...
				if(thermalFiles[i] == NullFileHandle)
					continue;

				// Read in the critical temperature value.
				const int tripPoint = ReadIntFile(tripPointPath);
				if(tripPoint > 0)
				{
					SensorSetRange(desc.label, 0, (float)tripPoint, Sensor_Interp_Linear);
				}
			}
		}

		// For clocks, we store the last value and only send updates when it changes since we
		// use blocking chart rendering.
		int lastCpuFreq[maxCpus] = {0};
		int lastGpuFreq          = 0;
		int lastMemValue         = 0;

		UInt32 sampleCount = 0;

		while(!QuitSignaled() && IsConnected())
		{
			// Sample CPU Frequencies...
			for(UInt32 i=0; i<maxCpus; i++)
			{
				// If the 'online' file can't be found, then we just assume this CPU doesn't even exist
				if(cpuOnlineFiles[i] == NullFileHandle)
					continue;

				const SensorLabelDesc &desc   = cpuDescs[i];
				const bool             online = ReadIntFile(cpuOnlineFiles[i]) ? true : false;
				if(online && cpuFreqFiles[i]==NullFileHandle)
				{
					// Open the frequency file if we are online and its not already open...
					char freqPath[64] = {0};
					FormatString(freqPath, sizeof(freqPath), "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq", i);
					cpuFreqFiles[i] = OpenFile(freqPath);
				}
				else if(!online && cpuFreqFiles[i]!=NullFileHandle)
				{
					// close the frequency file if we are no longer online
					CloseFile(cpuFreqFiles[i]);
					cpuFreqFiles[i] = NullFileHandle;
				}
				const int freq = cpuFreqFiles[i]==NullFileHandle ? 0 : ReadIntFile(cpuFreqFiles[i]);
				if(freq != lastCpuFreq[i])
				{
					// Convert from KHz to Hz
					SensorSetValue(desc.label, (float)freq);
					lastCpuFreq[i] = freq;
				}
				ThreadYield();
			}

			// Sample GPU Frequency...
			if(gpuFreqFile != NullFileHandle)
			{
				const int freq = ReadIntFile(gpuFreqFile);
				if(freq != lastGpuFreq)
				{
					SensorSetValue(g_gpuLabel, (float)freq);
					lastGpuFreq = freq;
				}
			}

			// Sample Memory Bandwidth
			if(memFreqFile != NullFileHandle)
			{
				const int value = ReadIntFile(memFreqFile);
				if(value != lastMemValue)
				{
					SensorSetValue(g_memLabel, (float)value);
					lastMemValue = value;
				}
			}

			// Sample thermal sensors...
			if((sampleCount&15) == 0) // sample temperature at a much lower frequency as clocks... thermals don't change that fast.
			{
				for(UInt32 i=0; i<maxThermalSensors; i++)
				{
					FileHandle file = thermalFiles[i];
					if(file != NullFileHandle)
					{
						SensorSetValue(thermalDescs[i].label, (float)ReadIntFile(file));
					}
				}
				ThreadYield();
			}

			// Sleep 5ms between samples...
			ThreadSleepMicroseconds(5000);
			sampleCount++;
		}

		// Close down cached file handles...
		for(UInt32 i=0; i<maxCpus; i++)
		{
			if(cpuOnlineFiles[i] != NullFileHandle) CloseFile(cpuOnlineFiles[i]);
			if(cpuFreqFiles[i]   != NullFileHandle) CloseFile(cpuFreqFiles[i]);
		}
		if(gpuFreqFile != NullFileHandle) CloseFile(gpuFreqFile);
		if(memFreqFile != NullFileHandle) CloseFile(memFreqFile);
		for(UInt32 i=0; i<maxThermalSensors; i++)
		{
			if(thermalFiles[i] != NullFileHandle) CloseFile(thermalFiles[i]);
		}
	}
Beispiel #20
0
	HTTP_RESPONSE*
	resp_t::toHttpResponse(void)
	{
		//	std::lock_guard< std::mutex >	lck(m_mutex);
		HTTP_RESPONSE*					resp(new HTTP_RESPONSE);
		HTTP_RESPONSE_HEADERS*			hdrs(m_headers.getResponseHeaders());
		std::string						sc(getStatusString().toStdString());
		//QString							sc(getStatusString());
		BYTE*							buf(nullptr);
		HTTP_DATA_CHUNK*				chunk(new HTTP_DATA_CHUNK);
		std::size_t						len(m_body.size());

		::memset(resp, 0, sizeof(HTTP_RESPONSE));

		resp->StatusCode = m_status;
		resp->pReason = new CHAR[sc.length() + 1];
		::memset((void*)resp->pReason, 0, sc.length() + 1);
		::memcpy_s((void*)resp->pReason, sc.length() + 1, sc.c_str(), sc.length());

		resp->ReasonLength = ::strlen(resp->pReason);

		if (INT_MAX <= len)
			throw std::runtime_error("http::resp_t::toHttpResponse(): Overly large HTTP response encountered");

		buf = new BYTE[len + 1];

		::memset(buf, 0, len + 1);
		::memcpy_s(buf, len + 1, m_body.constData(), len);

		chunk->DataChunkType = HTTP_DATA_CHUNK_TYPE::HttpDataChunkFromMemory;
		chunk->FromMemory.pBuffer = buf;
		chunk->FromMemory.BufferLength = len;

		resp->pEntityChunks = chunk;
		resp->EntityChunkCount = 1;

		if (nullptr != hdrs) {
			::memcpy_s(resp->Headers.KnownHeaders, sizeof(resp->Headers.KnownHeaders), hdrs->KnownHeaders, sizeof(hdrs->KnownHeaders));

			resp->Headers.pTrailers = hdrs->pTrailers;
			resp->Headers.TrailerCount = hdrs->TrailerCount;
			resp->Headers.UnknownHeaderCount = hdrs->UnknownHeaderCount;

			if (0 != hdrs->UnknownHeaderCount) {
				//USHORT l = hdrs->UnknownHeaderCount;

				//if (USHRT_MAX-1 < l)
				//	throw std::runtime_error("http::resp_t::toHttpResponse(): Unusually large volume of unknown HTTP headers, aborting response.");


				//resp->Headers.pUnknownHeaders = new HTTP_UNKNOWN_HEADER[l+1];

				/*for (auto idx = 0; idx < l; idx++) {
					resp->Headers.pUnknownHeaders[idx].NameLength		= hdrs->pUnknownHeaders[idx].NameLength;
					resp->Headers.pUnknownHeaders[idx].RawValueLength	= hdrs->pUnknownHeaders[idx].RawValueLength;
					resp->Headers.pUnknownHeaders[idx].pName			= hdrs->pUnknownHeaders[idx].pName;
					*/

				resp->Headers.pUnknownHeaders = hdrs->pUnknownHeaders;

				//::memcpy_s(resp->Headers.pUnknownHeaders, (l+1)*sizeof(HTTP_UNKNOWN_HEADER), hdrs->pUnknownHeaders, l*sizeof(HTTP_UNKNOWN_HEADER));
			}

			//m_headers.destroyResponseHeaders(hdrs);

		}

		//m_log.info("unknown headers count: " + QString::number(resp->Headers.UnknownHeaderCount) + " name: " + QString(resp->Headers.pUnknownHeaders[0].pName) + " value: " + QString(resp->Headers.pUnknownHeaders[0].pRawValue));
		return resp;
	}
Beispiel #21
0
bool ossimCodecFactory::encodeJpeg( ossim_uint32 quality,
                                    const ossimRefPtr<ossimImageData>& in,
                                    std::vector<ossim_uint8>& out ) const
{
   bool result = false;

   if ( in.valid() && (in->getDataObjectStatus() != OSSIM_NULL) )
   {
      if ( in->getScalarType() == OSSIM_UINT8 )
      {
         // Open a memory stream up to put the jpeg image in memory:
         std::stringstream jpegStreamBuf;
         
         //---
         // Initialize JPEG compression library:
         // NOTE: JDIMENSION is an "unsigned int"
         //---
         struct jpeg_compress_struct cinfo;
         struct jpeg_error_mgr jerr;
         cinfo.err = jpeg_std_error( &jerr );
         jpeg_create_compress(&cinfo);
      
         // Define a custom stream destination manager for jpeglib to write compressed block:
         jpeg_cpp_stream_dest(&cinfo, jpegStreamBuf);
      
         /* Setting the parameters of the output file here */
         cinfo.image_width = in->getWidth();
         cinfo.image_height = in->getHeight();
   
         // Bands must be one or three for this writer.
         const ossim_uint32 INPUT_BANDS = in->getNumberOfBands();
         if ( (INPUT_BANDS == 1) || (INPUT_BANDS == 3) )
         {
            cinfo.input_components = INPUT_BANDS;
         }
         else
         {
            if ( INPUT_BANDS < 3 )
            {
               cinfo.input_components = 1; // Use first band.
            }
            else
            {
               cinfo.input_components = 3; // Use the first 3 bands.
            }
         }
      
         // colorspace of input image 
         if ( cinfo.input_components == 3)
         {
            cinfo.in_color_space = JCS_RGB;
         }
         else
         {
            cinfo.in_color_space = JCS_GRAYSCALE;
         }
      
         // Default compression parameters, we shouldn't be worried about these.
         jpeg_set_defaults( &cinfo );
      
         jpeg_set_quality(&cinfo, quality, TRUE); //limit to baseline-JPEG values
      
         // Now do the compression...
         jpeg_start_compress( &cinfo, TRUE );
      
         // Line buffer:
         ossim_uint32 buf_size = cinfo.input_components*cinfo.image_width;
         std::vector<ossim_uint8> buf(buf_size);
      
         // Compress the tile on line at a time:
      
         JSAMPROW row_pointer[1]; // Pointer to a single row.
         row_pointer[0] = (JSAMPLE*)&buf.front();

         // Get pointers to the input data:
         std::vector<const ossim_uint8*> inBuf(cinfo.input_components);
         for ( ossim_int32 band = 0; band < cinfo.input_components; ++band )
         {
            inBuf[band] = in->getUcharBuf(band);
         }

         ossim_uint32 inIdx = 0;
         for (ossim_uint32 line=0; line< cinfo.image_height; ++line)
         {
            // Convert from band sequential to band interleaved by pixel.
            ossim_uint32 outIdx = 0;
            for ( ossim_uint32 p = 0; p < cinfo.image_width; ++p )
            {
               for ( ossim_int32 band = 0; band < cinfo.input_components; ++band )
               {
                  buf[outIdx++] = inBuf[band][inIdx];
               }
               ++inIdx;
            }

            // Write it...
            jpeg_write_scanlines( &cinfo, row_pointer, 1 );
         }
      
         // Similar to read file, clean up after we're done compressing.
         jpeg_finish_compress( &cinfo );
         jpeg_destroy_compress( &cinfo );

         // Copy the memory stream to output vector.
         out.resize(jpegStreamBuf.str().size());
         jpegStreamBuf.seekg(0, std::ios_base::beg);
         jpegStreamBuf.read((char*)&out.front(), jpegStreamBuf.str().size());

         result = true;
      }
      else // Scalar type check...
      {
         ossimNotify(ossimNotifyLevel_WARN)
            << "ossimCodecFactory::encodeJpeg ERROR:"
            << "\nPassing non-eight bit data to eight bit encoder!" << std::endl;
      }
      
   } // Matches: if ( in.valid() ... )
   
   return result;
}
Beispiel #22
0
Vector<T>::~Vector() {
    free(buf());
    _buf  = 0;
    _size = 0;
}
Beispiel #23
0
Response http_sync(http_exch_fn http_transact,
                   const std::string& verb,
                   const std::string& url,
                   const std::string& content_type,
                   const std::vector<byte>& body,
                   size_t allowable_redirects)
{
    if(url.empty())
        throw HTTP_Error("URL empty");

    const auto protocol_host_sep = url.find("://");
    if(protocol_host_sep == std::string::npos)
        throw HTTP_Error("Invalid URL '" + url + "'");

    const auto host_loc_sep = url.find('/', protocol_host_sep + 3);

    std::string hostname, loc;

    if(host_loc_sep == std::string::npos)
    {
        hostname = url.substr(protocol_host_sep + 3, std::string::npos);
        loc = "/";
    }
    else
    {
        hostname = url.substr(protocol_host_sep + 3, host_loc_sep-protocol_host_sep-3);
        loc = url.substr(host_loc_sep, std::string::npos);
    }

    std::ostringstream outbuf;

    outbuf << verb << " " << loc << " HTTP/1.0\r\n";
    outbuf << "Host: " << hostname << "\r\n";

    if(verb == "GET")
    {
        outbuf << "Accept: */*\r\n";
        outbuf << "Cache-Control: no-cache\r\n";
    }
    else if(verb == "POST")
        outbuf << "Content-Length: " << body.size() << "\r\n";

    if(!content_type.empty())
        outbuf << "Content-Type: " << content_type << "\r\n";
    outbuf << "Connection: close\r\n\r\n";
    outbuf.write(reinterpret_cast<const char*>(body.data()), body.size());

    std::istringstream io(http_transact(hostname, outbuf.str()));

    std::string line1;
    std::getline(io, line1);
    if(!io || line1.empty())
        throw HTTP_Error("No response");

    std::stringstream response_stream(line1);
    std::string http_version;
    unsigned int status_code;
    std::string status_message;

    response_stream >> http_version >> status_code;

    std::getline(response_stream, status_message);

    if(!response_stream || http_version.substr(0,5) != "HTTP/")
        throw HTTP_Error("Not an HTTP response");

    std::map<std::string, std::string> headers;
    std::string header_line;
    while (std::getline(io, header_line) && header_line != "\r")
    {
        auto sep = header_line.find(": ");
        if(sep == std::string::npos || sep > header_line.size() - 2)
            throw HTTP_Error("Invalid HTTP header " + header_line);
        const std::string key = header_line.substr(0, sep);

        if(sep + 2 < header_line.size() - 1)
        {
            const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
            headers[key] = val;
        }
    }

    if(status_code == 301 && headers.count("Location"))
    {
        if(allowable_redirects == 0)
            throw HTTP_Error("HTTP redirection count exceeded");
        return GET_sync(headers["Location"], allowable_redirects - 1);
    }

    std::vector<byte> resp_body;
    std::vector<byte> buf(4096);
    while(io.good())
    {
        io.read(reinterpret_cast<char*>(buf.data()), buf.size());
        resp_body.insert(resp_body.end(), buf.data(), &buf[io.gcount()]);
    }

    const std::string header_size = search_map(headers, std::string("Content-Length"));

    if(!header_size.empty())
    {
        if(resp_body.size() != to_u32bit(header_size))
            throw HTTP_Error("Content-Length disagreement, header says " +
                             header_size + " got " + std::to_string(resp_body.size()));
    }

    return Response(status_code, status_message, resp_body, headers);
}
Beispiel #24
0
void Vector<T>::setDirect(int pos, T v) {
    assert(pos >= 0 && pos < size());
    buf()[pos] = v;
}
//=========================================================
//=========================================================
bool C_GameInstructor::WriteSaveData()
{
	if (engine->IsPlayingDemo())
		return false;

	if (!m_bDirtySaveData)
		return true;

#ifdef _X360
	float flPlatTime = Plat_FloatTime();

	static ConVarRef host_write_last_time("host_write_last_time");
	if (host_write_last_time.IsValid())
	{
		float flTimeSinceLastWrite = flPlatTime - host_write_last_time.GetFloat();
		if (flTimeSinceLastWrite < 3.5f)
		{
			// Prevent writing to the same storage device twice in less than 3 second succession for TCR success!
			// This happens after leaving a game in splitscreen.
			//DevMsg( "Waiting to write Game Instructor for splitscreen slot %d... (%.1f seconds remain)\n", m_nSplitScreenSlot, 3.5f - flTimeSinceLastWrite );
			return false;
		}
	}
#endif

	// Always mark as clean state to avoid re-entry on
	// subsequent frames when storage device might be
	// in a yet-unmounted state.
	m_bDirtySaveData = false;

#ifdef _X360
	DevMsg("Write Game Instructor for splitscreen slot %d at time: %.1f\n", m_nSplitScreenSlot, flPlatTime);

	if (m_nSplitScreenSlot < 0)
		return false;

	if (m_nSplitScreenSlot >= (int)XBX_GetNumGameUsers())
		return false;

	int iController = XBX_GetUserId(m_nSplitScreenSlot);

	if (iController < 0 || XBX_GetUserIsGuest(iController))
	{
		// Can't save data for guests
		return false;
	}

	DWORD nStorageDevice = XBX_GetStorageDeviceId(iController);
	if (!XBX_DescribeStorageDevice(nStorageDevice))
		return false;
#endif

	// Build key value data to save
	KeyValues *data = new KeyValues("Game Instructor Counts");
	KeyValues::AutoDelete autoDelete(data);

	for (int i = 0; i < m_Lessons.Count(); ++i)
	{
		CBaseLesson *pLesson = m_Lessons[i];

		int iDisplayCount = pLesson->GetDisplayCount();
		int iSuccessCount = pLesson->GetSuccessCount();

		if (iDisplayCount || iSuccessCount)
		{
			// We've got some data worth saving
			KeyValues *pKVData = new KeyValues(pLesson->GetName());

			if (iDisplayCount)
				pKVData->SetInt("display", iDisplayCount);

			if (iSuccessCount)
				pKVData->SetInt("success", iSuccessCount);

			data->AddSubKey(pKVData);
		}
	}

	// Save it!
	CUtlBuffer buf(0, 0, CUtlBuffer::TEXT_BUFFER);

	data->RecursiveSaveToFile(buf, 0);

	char	szFilename[_MAX_PATH];

#ifdef _X360
	if (IsX360())
	{
		XBX_MakeStorageContainerRoot(iController, XBX_USER_SETTINGS_CONTAINER_DRIVE, szFilename, sizeof(szFilename));
		int nLen = strlen(szFilename);
		Q_snprintf(szFilename + nLen, sizeof(szFilename) - nLen, ":\\game_instructor_counts.txt");
	}
	else
#endif
	{
		Q_snprintf(szFilename, sizeof(szFilename), "save/game_instructor_counts.txt");
		filesystem->CreateDirHierarchy("save", "MOD");
	}

	bool bWriteSuccess = filesystem->WriteFile(szFilename, MOD_DIR, buf);

#ifdef _X360
	if (xboxsystem)
	{
		xboxsystem->FinishContainerWrites(iController);
	}
#endif

	return bWriteSuccess;
}
Beispiel #26
0
T Vector<T>::get(int pos) {
    assert(pos >= 0 && pos < size());
    return buf()[pos];
}
void agg_renderer<T>::process(line_symbolizer const& sym,
                              Feature const& feature,
                              proj_transform const& prj_trans)
{
    typedef agg::renderer_base<agg::pixfmt_rgba32_plain> ren_base;
    typedef coord_transform2<CoordTransform,geometry_type> path_type;

    stroke const& stroke_ = sym.get_stroke();
    color const& col = stroke_.get_color();
    unsigned r=col.red();
    unsigned g=col.green();
    unsigned b=col.blue();
    unsigned a=col.alpha();
    
    agg::rendering_buffer buf(pixmap_.raw_data(),width_,height_, width_ * 4);
    agg::pixfmt_rgba32_plain pixf(buf);
    
    if (sym.get_rasterizer() == RASTERIZER_FAST)
    {
        typedef agg::renderer_outline_aa<ren_base> renderer_type;
        typedef agg::rasterizer_outline_aa<renderer_type> rasterizer_type;

        agg::line_profile_aa profile;
        //agg::line_profile_aa profile(stroke_.get_width() * scale_factor_, agg::gamma_none());
        profile.width(stroke_.get_width() * scale_factor_);
        ren_base base_ren(pixf);
        renderer_type ren(base_ren, profile);
        ren.color(agg::rgba8(r, g, b, int(a*stroke_.get_opacity())));
        //ren.clip_box(0,0,width_,height_);
        rasterizer_type ras(ren);
        ras.line_join(agg::outline_miter_accurate_join);
        ras.round_cap(true);
   
        for (unsigned i=0;i<feature.num_geometries();++i)
        {
            geometry_type const& geom = feature.get_geometry(i);
            if (geom.num_points() > 1)
            {
                path_type path(t_,geom,prj_trans);
                ras.add_path(path);
            }
        }
    }
    else
    {
        typedef agg::renderer_scanline_aa_solid<ren_base> renderer;

        agg::scanline_p8 sl;
    
        ren_base renb(pixf);
        renderer ren(renb);
        ras_ptr->reset();
        ras_ptr->gamma(agg::gamma_linear(0.0, stroke_.get_gamma()));
        
        metawriter_with_properties writer = sym.get_metawriter();
        for (unsigned i=0;i<feature.num_geometries();++i)
        {
            geometry_type const& geom = feature.get_geometry(i);
            if (geom.num_points() > 1)
            {
                path_type path(t_,geom,prj_trans);
    
                if (stroke_.has_dash())
                {
                    agg::conv_dash<path_type> dash(path);
                    dash_array const& d = stroke_.get_dash_array();
                    dash_array::const_iterator itr = d.begin();
                    dash_array::const_iterator end = d.end();
                    for (;itr != end;++itr)
                    {
                        dash.add_dash(itr->first * scale_factor_, 
                                      itr->second * scale_factor_);
                    }
    
                    agg::conv_stroke<agg::conv_dash<path_type > > stroke(dash);
    
                    line_join_e join=stroke_.get_line_join();
                    if ( join == MITER_JOIN)
                        stroke.generator().line_join(agg::miter_join);
                    else if( join == MITER_REVERT_JOIN)
                        stroke.generator().line_join(agg::miter_join);
                    else if( join == ROUND_JOIN)
                        stroke.generator().line_join(agg::round_join);
                    else
                        stroke.generator().line_join(agg::bevel_join);
    
                    line_cap_e cap=stroke_.get_line_cap();
                    if (cap == BUTT_CAP)
                        stroke.generator().line_cap(agg::butt_cap);
                    else if (cap == SQUARE_CAP)
                        stroke.generator().line_cap(agg::square_cap);
                    else
                        stroke.generator().line_cap(agg::round_cap);
    
                    stroke.generator().miter_limit(4.0);
                    stroke.generator().width(stroke_.get_width() * scale_factor_);
                    ras_ptr->add_path(stroke);
    
                }
                else
                {
                    agg::conv_stroke<path_type>  stroke(path);
                    line_join_e join=stroke_.get_line_join();
                    if ( join == MITER_JOIN)
                        stroke.generator().line_join(agg::miter_join);
                    else if( join == MITER_REVERT_JOIN)
                        stroke.generator().line_join(agg::miter_join);
                    else if( join == ROUND_JOIN)
                        stroke.generator().line_join(agg::round_join);
                    else
                        stroke.generator().line_join(agg::bevel_join);
    
                    line_cap_e cap=stroke_.get_line_cap();
                    if (cap == BUTT_CAP)
                        stroke.generator().line_cap(agg::butt_cap);
                    else if (cap == SQUARE_CAP)
                        stroke.generator().line_cap(agg::square_cap);
                    else
                        stroke.generator().line_cap(agg::round_cap);
    
                    stroke.generator().miter_limit(4.0);
                    stroke.generator().width(stroke_.get_width() * scale_factor_);
                    ras_ptr->add_path(stroke);
                    if (writer.first) writer.first->add_line(path, feature, t_, writer.second);
                }
            }
        }
        ren.color(agg::rgba8(r, g, b, int(a*stroke_.get_opacity())));
        agg::render_scanlines(*ras_ptr, sl, ren);    
    }
}
Beispiel #28
0
void Vector<T>::setSize(unsigned newSize) {
    if (!sameAllocSize(size(), newSize)) {
        _buf = (T *) realloc(buf(), getAllocSize(newSize) * sizeof(T));
    }
    _size = (newSize << 4) | (_size & 0xf);
}
ErrorStack execute(
  Engine* engine,
  storage::StorageId id,
  double* elapsed_ms,
  std::vector<std::string>* papi_results) {
  storage::PartitionerMetadata* metadata = storage::PartitionerMetadata::get_metadata(engine, id);
  make_dummy_partitions(engine, id, metadata);

  LOG(INFO) << "Allocating memories...";
  debugging::StopWatch alloc_watch;
  memory::AlignedMemory::AllocType kAlloc = memory::AlignedMemory::kNumaAllocOnnode;
  memory::AlignedMemory work_memory(kRecords * 32ULL, 1U << 21, kAlloc, 0);
  memory::AlignedMemory positions_memory(sizeof(BufferPosition) * kRecords, 1U << 12, kAlloc, 0);
  memory::AlignedMemory out_memory(sizeof(BufferPosition) * kRecords, 1U << 12, kAlloc, 0);
  memory::AlignedMemory partitions_memory(sizeof(uint8_t) * kRecords, 1U << 12, kAlloc, 0);
  memory::AlignedMemory log_memory(kRecords * kPayloadSize * 2ULL, 1U << 21, kAlloc, 0);
  alloc_watch.stop();
  LOG(INFO) << "Allocated memories in " << alloc_watch.elapsed_ms() << "ms";

  LOG(INFO) << "Populating logs to process...";
  debugging::StopWatch log_watch;
  char* log_buffer = reinterpret_cast<char*>(log_memory.get_block());
  uint64_t log_size = 0;
  BufferPosition* log_positions = reinterpret_cast<BufferPosition*>(positions_memory.get_block());
  populate_logs(id, log_buffer, log_positions, &log_size);
  log_watch.stop();
  LOG(INFO) << "Populated logs to process in " << log_watch.elapsed_ms() << "ms";

  if (FLAGS_profile) {
    COERCE_ERROR(engine->get_debug()->start_profile("partition_experiment.prof"));
    engine->get_debug()->start_papi_counters();
  }

  LOG(INFO) << "experiment's main part has started";
  debugging::StopWatch watch;

  storage::Partitioner partitioner_base(engine, id);
  ASSERT_ND(partitioner_base.is_valid());
  storage::hash::HashPartitioner partitioner(&partitioner_base);
  LogBuffer buf(log_buffer);
  if (FLAGS_run_partition) {
    LOG(INFO) << "running partitioning...";
    storage::Partitioner::PartitionBatchArguments partition_args = {
      0,
      buf,
      log_positions,
      kRecords,
      reinterpret_cast<uint8_t*>(partitions_memory.get_block())};
    partitioner.partition_batch(partition_args);
  }

  if (FLAGS_run_sort) {
    LOG(INFO) << "running sorting...";
    uint32_t written_count;
    storage::Partitioner::SortBatchArguments sort_args = {
      buf,
      log_positions,
      kRecords,
      8,
      8,
      &work_memory,
      Epoch(1),
      reinterpret_cast<BufferPosition*>(out_memory.get_block()),
      &written_count};
    partitioner.sort_batch(sort_args);
  }

  watch.stop();
  *elapsed_ms = watch.elapsed_ms();
  LOG(INFO) << "experiment's main part has ended. Took " << *elapsed_ms << "ms";

  if (FLAGS_profile) {
    engine->get_debug()->stop_profile();
    engine->get_debug()->stop_papi_counters();
    if (FLAGS_papi) {
      *papi_results = debugging::DebuggingSupports::describe_papi_counters(
        engine->get_debug()->get_papi_counters());
    }
  }

  return kRetOk;
}
/** Convert character vector to ASCII
 *
 * All charcodes > 127 are replaced with subst chars (0x1A)
 *
 * @param str character vector
 * @return character vector
 *
 * @version 0.1-?? (Marek Gagolewski)
 *
 * @version 0.1-?? (Marek Gagolewski, 2013-06-16)
 *          make StriException-friendly
 *
 * @version 0.2-1 (Marek Gagolewski, 2014-03-30)
 *          use single common buf;
 *          warn on invalid utf8 byte stream
 *
 * @version 0.3-1 (Marek Gagolewski, 2014-11-04)
 *    Issue #112: str_prepare_arg* retvals were not PROTECTed from gc
 */
SEXP stri_enc_toascii(SEXP str)
{
    PROTECT(str = stri_prepare_arg_string(str, "str"));
    R_len_t n = LENGTH(str);

    STRI__ERROR_HANDLER_BEGIN(1)

    // get buf size
    R_len_t bufsize = 0;
    for (R_len_t i=0; i<n; ++i) {
        SEXP curs = STRING_ELT(str, i);
        if (curs == NA_STRING)
            continue;

        R_len_t ni = LENGTH(curs);
        if (ni > bufsize) bufsize = ni;
    }
    String8buf buf(bufsize); // no more bytes than this needed
    char* bufdata = buf.data();

    SEXP ret;
    STRI__PROTECT(ret = Rf_allocVector(STRSXP, n));
    for (R_len_t i=0; i<n; ++i) {
        SEXP curs = STRING_ELT(str, i);
        if (curs == NA_STRING || IS_ASCII(curs)) {
            // nothing to do
            SET_STRING_ELT(ret, i, curs);
            continue;
        }

        R_len_t curn = LENGTH(curs);
        const char* curs_tab = CHAR(curs);

        if (IS_UTF8(curs)) {
            R_len_t k = 0, j = 0;
            UChar32 c;
            while (j<curn) {
                U8_NEXT(curs_tab, j, curn, c);
                if (c < 0) {
                    Rf_warning(MSG__INVALID_CODE_POINT_FIXING);
                    bufdata[k++] = ASCII_SUBSTITUTE;
                }
                else if (c > ASCII_MAXCHARCODE)
                    bufdata[k++] = ASCII_SUBSTITUTE;
                else
                    bufdata[k++] = (char)c;
            }
            SET_STRING_ELT(ret, i, Rf_mkCharLenCE(bufdata, k, CE_UTF8));
            // the string will be marked as ASCII anyway by mkCharLenCE
        }
        else { // some 8-bit encoding
            R_len_t k = 0;
            for (R_len_t j=0; j<curn; ++j) {
                if (U8_IS_SINGLE(curs_tab[j]))
                    bufdata[k++] = curs_tab[j];
                else {
                    bufdata[k++] = (char)ASCII_SUBSTITUTE; // subst char in ascii
                }
            }
            SET_STRING_ELT(ret, i, Rf_mkCharLenCE(bufdata, k, CE_UTF8));
            // the string will be marked as ASCII anyway by mkCharLenCE
        }
    }

    STRI__UNPROTECT_ALL
    return ret;
    STRI__ERROR_HANDLER_END(;/* nothing special to be done on error */)
}