void RKVariable::writeData (int from_row, int to_row, RCommandChain *chain) {
	RK_TRACE (OBJECTS);
	if (from_row == -1) return;

	// TODO: try to sync in correct storage mode
	if (from_row == to_row) {
		RKGlobals::rInterface ()->issueCommand (getFullName () + '[' + QString::number (from_row+1) + "] <- " + getRText (from_row), RCommand::App | RCommand::Sync, QString::null, 0,0, chain);
		if (myData ()->cell_states[from_row] & RKVarEditData::UnsyncedInvalidState) writeInvalidField (from_row, chain);
	} else {
		QString data_string = "c (";
		for (int row = from_row; row <= to_row; ++row) {
			// TODO: use getCharacter and direct setting of vectors.
			data_string.append (getRText (row));
			if (row != to_row) {
				data_string.append (", ");
			}
			if (myData ()->cell_states[row] & RKVarEditData::UnsyncedInvalidState) writeInvalidField (row, chain);
		}
		data_string.append (")");
		RKGlobals::rInterface ()->issueCommand (getFullName () + '[' + QString::number (from_row + 1) + ':' + QString::number (to_row + 1) + "] <- " + data_string, RCommand::App | RCommand::Sync, QString::null, 0,0, chain);
	}

	ChangeSet *set = new ChangeSet;
	set->from_index = from_row;
	set->to_index = to_row;
	RKGlobals::tracker ()->objectDataChanged (this, set);
}
void RKVariable::setValueLabels (ValueLabels *labels) {
	RK_TRACE (OBJECTS);
	RK_ASSERT (myData ());
	
	if (labels != myData ()->value_labels) {
		delete (myData ()->value_labels);
		myData ()->value_labels = labels;
	}

	writeValueLabels (0);
	RKGlobals::tracker ()->objectMetaChanged (this);

	// find out which values got valid / invalid and change those
	for (int i=0; i < getLength (); ++i) {
		if (cellStatus (i) == ValueInvalid) {
			if (labels && labels->contains (getText (i))) {
				setText (i, getText (i));
			}
		} else {
			if (!(labels && labels->contains (getText (i)))) {
				setText (i, getText (i));
			}
		}
	}

	// also update display of all values:
	ChangeSet *set = new ChangeSet;
	set->from_index = 0;
	set->to_index = getLength () - 1;	
	RKGlobals::tracker ()->objectDataChanged (this, set);

	// TODO: find out whether the object is valid after the operation and update accordingly!
}
// virtual
void RKVariable::initializeEditDataToEmpty () {
	RK_TRACE (OBJECTS);
	RK_ASSERT (myData ());
	
	for (int row=0; row < getLength (); ++row) {
		myData ()->cell_states[row] = RKVarEditData::NA;
	}
}
void RKVariable::restore (RCommandChain *chain) {
	RK_TRACE (OBJECTS);
	RK_ASSERT (myData ());

	writeData (0, getLength () - 1, chain);
	delete myData ()->changes;
	writeMetaData (chain);
}
void RKVariable::syncDataToR () {
	RK_TRACE (OBJECTS);
	if (!(myData ()->changes)) return;
	
	// TODO
	writeData (myData ()->changes->from_index, myData ()->changes->to_index);
	myData ()->changes->from_index = -1;
	myData ()->changes->to_index = -1;
}
QString RKVariable::getLabeled (int row) {
	if (myData ()->value_labels) {
		QString otext = getText (row);
		if (myData ()->value_labels->contains (otext)) {
			return (*(myData ()->value_labels))[otext];
		}
	}
	return getText (row);
}
void RKVariable::writeInvalidField (int row, RCommandChain *chain) {
	RK_TRACE (OBJECTS);

	if (myData ()->invalid_fields[row]) {
		RKGlobals::rInterface ()->issueCommand (".rk.set.invalid.field (" + getFullName () + ", " + QString::number (row+1) + ", " + rQuote (*(myData ()->invalid_fields[row])) + ')', RCommand::App | RCommand::Sync, QString::null, 0,0, chain);
	} else {
		RKGlobals::rInterface ()->issueCommand (".rk.set.invalid.field (" + getFullName () + ", " + QString::number (row+1) + ", NULL)", RCommand::App | RCommand::Sync, QString::null, 0,0, chain);
	}
	myData ()->cell_states[row] -= (myData ()->cell_states[row] & RKVarEditData::UnsyncedInvalidState);
}
void RKVariable::setUnknown (int from_row, int to_row) {
	RK_TRACE (OBJECTS);
	RK_ASSERT (to_row < getLength ());

	if ((from_row < 0)) from_row = 0;
	if ((to_row < 0)) to_row = myData ()->allocated_length - 1;
		
	for (int row=from_row; row <= to_row; ++row) {
		myData ()->cell_strings[row] = RKVarEditData::Unknown;
	}
}
/** returns alignment to use for this variable */
RKVariable::CellAlign RKVariable::getAlignment () {
	RK_ASSERT (myData ());
	
	if (myData ()->formatting_options && (myData ()->formatting_options->alignment != FormattingOptions::AlignDefault)) {
		if (myData ()->formatting_options->alignment == FormattingOptions::AlignLeft) return AlignCellLeft;
		return AlignCellRight;
	} else {
	// TODO: use global (configurable) defaults, if not specified
		if ((getDataType () == DataCharacter) || (getDataType () == DataFactor)) {
			return AlignCellLeft;
		} else {
			return AlignCellRight;
		}
	}
}
// Begin serialization methods
QMap<IDLVar, QByteArray> DatabaseModel::getVariableList(const QByteArray &data) {
    QMap<IDLVar, QByteArray> variableList;
    if (instance()->hasStringEntries())
        return variableList;

    QByteArray myData(data);

    unsigned short numVariables = instance()->getUnsignedShort(&myData);

    while (numVariables > variableList.size()) {
        // first get name (4 bytes)
        unsigned int nameHash = instance()->getUnsignedInt(&myData);
        IDLVar varInfo = hashTable->value(nameHash);
        if (varInfo.hashCode == 0) {
            varInfo.varName = "0x" + QString::number(nameHash, 16).toUpper();
            varInfo.hashCode = nameHash;
        }

        // then get size (4 bytes)
        unsigned int size = instance()->getUnsignedInt(&myData);

        // then get the actual data
        QByteArray var(myData.left(size).data(), size);
        myData.remove(0, size);

        variableList[varInfo] = var;
    }

    return variableList;
}
Beispiel #11
0
/**
 * Obtains the investigations that the user can publish
 * to and saves related information to a workspace.
 * @return A workspace containing investigation information the user can publish
 * to.
 */
API::ITableWorkspace_sptr ICat4Catalog::getPublishInvestigations() {
  ICATPortBindingProxy icat;
  setICATProxySettings(icat);

  auto ws = API::WorkspaceFactory::Instance().createTable("TableWorkspace");
  // Populate the workspace with all the investigations that
  // the user is an investigator off and has READ access to.
  myData(ws);

  // Remove each investigation returned from `myData`
  // were the user does not have create/write access.
  for (int row = static_cast<int>(ws->rowCount()) - 1; row >= 0; --row) {
    ns1__dataset dataset;
    ns1__datafile datafile;

    // Verify if the user can CREATE datafiles in the "mantid" specific dataset.
    int64_t datasetID =
        getMantidDatasetId(ws->getRef<std::string>("InvestigationID", row));
    std::string datafileName = "tempName.nxs";

    dataset.id = &datasetID;
    datafile.name = &datafileName;
    datafile.dataset = &dataset;

    if (!isAccessAllowed(ns1__accessType__CREATE, datafile))
      ws->removeRow(row);
  }

  return ws;
}
QString RKVariable::getRText (int row) {
	RK_TRACE (OBJECTS);
	
	Status cell_state = cellStatus (row);
	
	if ((cell_state == ValueUnused) || (cell_state == ValueInvalid)) {
		return ("NA");
	} else if (getDataType () == DataFactor) {
		return (rQuote (getLabeled (row)));
	} else if (getDataType () == DataCharacter) {
		return (rQuote (getText (row)));
	} else {
		RK_ASSERT (myData ()->cell_doubles != 0);
		return (QString::number (myData ()->cell_doubles[row], 'g', MAX_PRECISION));
	}
}
void RKVariable::setVarType (RObject::RDataType new_type, bool sync) {
	RK_TRACE (OBJECTS);

	if (getDataType () == new_type) {
		return;
	}

	// if the variable is currently opened for editing, all values need to be rechecked / resynced
	if (myData ()) {
		bool internal_sync = myData ()->immediate_sync;
		setSyncing (false);
		// quick and dirty approach! TODO: make more efficient
		QStringList list;
		for (int i=0; i < getLength (); ++i) {
			list.append (getText (i));
		}

		setDataType (new_type);
		if (new_type == RObject::DataCharacter) {
			if (myData ()->cell_strings == 0) {
				delete [] (myData ()->cell_doubles);
				myData ()->cell_doubles = 0;
				myData ()->cell_strings = new QString[getLength ()];
			}
		} else {
			if (myData ()->cell_doubles == 0) {
				delete [] (myData ()->cell_strings);
				myData ()->cell_strings = 0;
				myData ()->cell_doubles = new double[getLength ()];
			}
		}

		int i = 0;
		for (QStringList::const_iterator it = list.constBegin (); it != list.constEnd (); ++it) {
			setText (i, *it);
			i++;
		}
		if (sync) {
			syncDataToR ();
		}
		setSyncing (internal_sync);
	} else {
		setDataType (new_type);
	}

	setMetaProperty ("type", QString ().setNum ((int) new_type), sync);
}
QString RKVariable::getValueLabelString () {
	RK_TRACE (OBJECTS);
	RK_ASSERT (myData ());

	if (myData ()->value_labels) {
		int i = 1;
		QString level_string;
		while (myData ()->value_labels->contains (QString::number (i))) {
			level_string.append ((*(myData ()->value_labels))[QString::number (i)]);
			if (myData ()->value_labels->contains (QString::number (++i))) {
				level_string.append ("#,#");
			}
		}
		
		return level_string;
	} else {
		return QString ();
	}
}
static jint NativeConverter_decode(JNIEnv* env, jclass, jlong address,
        jbyteArray source, jint sourceEnd, jcharArray target, jint targetEnd,
        jintArray data, jboolean flush) {

    UConverter* cnv = toUConverter(address);
    if (cnv == NULL) {
        maybeThrowIcuException(env, "toUConverter", U_ILLEGAL_ARGUMENT_ERROR);
        return U_ILLEGAL_ARGUMENT_ERROR;
    }
    ScopedByteArrayRO uSource(env, source);
    if (uSource.get() == NULL) {
        maybeThrowIcuException(env, "uSource", U_ILLEGAL_ARGUMENT_ERROR);
        return U_ILLEGAL_ARGUMENT_ERROR;
    }
    ScopedCharArrayRW uTarget(env, target);
    if (uTarget.get() == NULL) {
        maybeThrowIcuException(env, "uTarget", U_ILLEGAL_ARGUMENT_ERROR);
        return U_ILLEGAL_ARGUMENT_ERROR;
    }
    ScopedIntArrayRW myData(env, data);
    if (myData.get() == NULL) {
        maybeThrowIcuException(env, "myData", U_ILLEGAL_ARGUMENT_ERROR);
        return U_ILLEGAL_ARGUMENT_ERROR;
    }

    // Do the conversion.
    jint* sourceOffset = &myData[0];
    jint* targetOffset = &myData[1];
    const char* mySource = reinterpret_cast<const char*>(uSource.get() + *sourceOffset);
    const char* mySourceLimit = reinterpret_cast<const char*>(uSource.get() + sourceEnd);
    UChar* cTarget = uTarget.get() + *targetOffset;
    const UChar* cTargetLimit = uTarget.get() + targetEnd;
    UErrorCode errorCode = U_ZERO_ERROR;
    ucnv_toUnicode(cnv, &cTarget, cTargetLimit, &mySource, mySourceLimit, NULL, flush, &errorCode);
    *sourceOffset = mySource - reinterpret_cast<const char*>(uSource.get()) - *sourceOffset;
    *targetOffset = cTarget - uTarget.get() - *targetOffset;

    // If there was an error, count the problematic bytes.
    if (errorCode == U_ILLEGAL_CHAR_FOUND || errorCode == U_INVALID_CHAR_FOUND ||
        errorCode == U_TRUNCATED_CHAR_FOUND) {
        int8_t invalidByteCount = 32;
        char invalidBytes[32] = {'\0'};
        UErrorCode minorErrorCode = U_ZERO_ERROR;
        ucnv_getInvalidChars(cnv, invalidBytes, &invalidByteCount, &minorErrorCode);
        if (U_SUCCESS(minorErrorCode)) {
            myData[2] = invalidByteCount;
        }
    }

    // Managed code handles some cases; throw all other errors.
    if (shouldCodecThrow(flush, errorCode)) {
        maybeThrowIcuException(env, "ucnv_toUnicode", errorCode);
    }
    return errorCode;
}
void RKVariable::writeValueLabels (RCommandChain *chain) {
	RK_TRACE (OBJECTS);
	RK_ASSERT (myData ());
	
	if (myData ()->value_labels) {
		int i = 1;
		QString level_string = "c (";
		while (myData ()->value_labels->contains (QString::number (i))) {
			level_string.append (rQuote ((*(myData ()->value_labels))[QString::number (i)]));
			if (myData ()->value_labels->contains (QString::number (++i))) {
				level_string.append (", ");
			}
		}
		level_string.append (")");
		// using attr (..., "levels) instead of levels (...) in order to bypass checking
		RKGlobals::rInterface ()->issueCommand ("attr (" + getFullName () + ", \"levels\") <- " + level_string, RCommand::App | RCommand::Sync, QString::null, 0, 0, chain);
	} else {
		RKGlobals::rInterface ()->issueCommand ("attr (" + getFullName () + ", \"levels\") <- NULL", RCommand::App | RCommand::Sync, QString::null, 0, 0, chain);
	}
}
// virtual
void RKVariable::discardEditData () {
	RK_TRACE (OBJECTS);

	RK_ASSERT (myData ());

	if (getDataType () == RObject::DataCharacter) {
		delete [] myData ()->cell_strings;
		RK_ASSERT (myData ()->cell_doubles == 0);
	} else {
		delete [] myData ()->cell_doubles;
		RK_ASSERT (myData ()->cell_strings == 0);
	}
	delete [] myData ()->cell_states;

	RK_ASSERT (!(myData ()->changes));
	delete myData ()->value_labels;
	delete myData ()->formatting_options;
	delete myData ();
	data = 0;
}
double *RKVariable::getNumeric (int from_row, int to_row) {
	RK_TRACE (OBJECTS);
	if (to_row >= getLength ()) {
		RK_ASSERT (false);
		return 0;
	}
	RK_ASSERT (from_row <= to_row);

	// TODO: no, this is not good. Return a _copy_!
	// we simply return the whole array starting at the given offset for now. Change this, if the storage mechanism gets changed!
	return &(myData ()->cell_doubles[from_row]);
}
RKVariable::FormattingOptions *RKVariable::parseFormattingOptionsString (const QString &string) {
	RK_TRACE (OBJECTS);
	RK_ASSERT (myData ());

	FormattingOptions *formatting_options = new FormattingOptions;
	formatting_options->alignment = FormattingOptions::AlignDefault;
	formatting_options->precision_mode = FormattingOptions::PrecisionDefault;
	formatting_options->precision = 0;
	bool empty = true;

	QStringList list = QStringList::split ("#", string);
	QString option, parameter;
	for (QStringList::const_iterator it = list.constBegin (); it != list.constEnd (); ++it) {
		option = (*it).section (':', 0, 0);
		parameter = (*it).section (':', 1, 1);
		
		if (parameter.isEmpty ()) continue;
		
		if (option == "align") {
			int al = parameter.toInt ();
			if ((al >= (int) FormattingOptions::AlignDefault) && (al <= (int) FormattingOptions::AlignRight)) {
				empty = false;
				formatting_options->alignment = (FormattingOptions::Alignment) al;
			}
		} else if (option == "prec") {
			if (parameter == "d") {
				empty = false;
				formatting_options->precision_mode = FormattingOptions::PrecisionDefault;
			} else if (parameter == "v") {
				empty = false;
				formatting_options->precision_mode = FormattingOptions::PrecisionRequired;
			} else {
				int dig = parameter.toInt ();
				if ((dig >= 0) && (dig <= 15)) {
					empty = false;
					formatting_options->precision_mode = FormattingOptions::PrecisionFixed;
					formatting_options->precision = dig;
				}
			}
		} else {
			RK_ASSERT (false);
		}
	}
	
	if (empty) {
		delete formatting_options;
		return 0;
	} else {
		return formatting_options;
	}
}
void RKVariable::setFormattingOptions (FormattingOptions *formatting_options) {
	RK_TRACE (OBJECTS);
	RK_ASSERT (myData ());
	
	if (formatting_options != myData ()->formatting_options) {
		delete myData ()->formatting_options;
	}

	myData ()->formatting_options = formatting_options;

	if (!formatting_options) {
		setMetaProperty ("format", QString::null);
	} else {
		QString format_string;
		if (formatting_options->alignment != (int) FormattingOptions::AlignDefault) {
			format_string.append ("align:");
			format_string.append (QString::number (formatting_options->alignment));
		}
	
		if (formatting_options->precision_mode != (int) FormattingOptions::PrecisionDefault) {
			if (!format_string.isEmpty ()) format_string.append ("#");
			format_string.append ("prec:");
			if (formatting_options->precision_mode == (int) FormattingOptions::PrecisionRequired) {
				format_string.append ("v");
			} else {
				format_string.append (QString::number (formatting_options->precision));
			}
		}
	
		setMetaProperty ("format", format_string);
	}

	// also update display of all values:
	ChangeSet *set = new ChangeSet;
	set->from_index = 0;
	set->to_index = getLength () - 1;	
	RKGlobals::tracker ()->objectDataChanged (this, set);
}
bool RKVariable::updateType (RData *new_data) {
	RK_TRACE (OBJECTS);

	if (myData ()) {
		int old_type = type;
		bool ret = RObject::updateType (new_data);
		int new_type = type;
		type = old_type;		// needed to read out the old data
		setVarType (typeToDataType (new_type), false);
		type = new_type;
		return ret;
	}
	return RObject::updateType (new_data);
}
void RKVariable::setSyncing (bool immediate) {
	RK_TRACE (OBJECTS);
	RK_ASSERT (myData ());
	
	myData ()->immediate_sync = immediate;
	if (!immediate) {
		if (!myData ()->changes) {
			myData ()->changes = new ChangeSet;
			myData ()->changes->from_index = -1;
			myData ()->changes->to_index = -1;
		}
	} else {
		delete myData ()->changes;
		myData ()->changes = 0;
	}
}
void RKVariable::cellsChanged (int from_row, int to_row) {
	RK_TRACE (OBJECTS);
	if (myData ()->immediate_sync) {
		writeData (from_row, to_row);
	} else {
		RK_ASSERT (myData ()->changes);
		if ((myData ()->changes->from_index > from_row) || (myData ()->changes->from_index == -1)) myData ()->changes->from_index = from_row;
		if (myData ()->changes->to_index < to_row) myData ()->changes->to_index = to_row;
	}
}
void RKVariable::cellChanged (int row) {
	RK_TRACE (OBJECTS);
	if (myData ()->immediate_sync) {
		writeData (row, row);
	} else {
		RK_ASSERT (myData ()->changes);
		if ((myData ()->changes->from_index > row) || (myData ()->changes->from_index == -1)) myData ()->changes->from_index = row;
		if (myData ()->changes->to_index < row) myData ()->changes->to_index = row;
	}
}
Beispiel #25
0
int main(int argc, char* argv[]) {
  if (argc < 2) {
    std::cerr << "Example 11: Wrong number of arguments" << std::endl
              << "Example 11: Usage: example11 url [file]" << std::endl;
    return EXIT_FAILURE;
  }
  char* url = argv[1];
  char* filename = NULL;
  if (argc >= 3) {
    filename = argv[2];
  }

  try {
    curlpp::Cleanup cleaner;
    curlpp::Easy request;

    /// Set the writer callback to enable cURL to write result in a memory area
    curlpp::options::WriteFunctionCurlFunction myFunction(WriteCallback);

    FILE* file = stdout;
    if (filename != NULL) {
      file = fopen(filename, "wb");
      if (file == NULL) {
        fprintf(stderr, "%s/n", strerror(errno));
        return EXIT_FAILURE;
      }
    }

    curlpp::OptionTrait<void*, CURLOPT_WRITEDATA> myData(file);

    request.setOpt(myFunction);
    request.setOpt(myData);

    /// Setting the URL to retrive.
    request.setOpt(new curlpp::options::Url(url));
    request.setOpt(new curlpp::options::Verbose(true));
    request.perform();
  }

  catch (curlpp::LogicError& e) {
    std::cout << e.what() << std::endl;
  }

  catch (curlpp::RuntimeError& e) {
    std::cout << e.what() << std::endl;
  }
}
void RKVariable::downSize () {
	RK_TRACE (OBJECTS);

	// TODO: downsizing to values other than 0
	if (getLength () <= 0) {
		delete [] myData ()->cell_doubles;
		myData ()->cell_doubles = 0;
		delete [] myData ()->cell_strings;
		myData ()->cell_strings = 0;
		delete [] myData ()->cell_states;
		myData ()->cell_states = 0;
	}
}
void RKVariable::setValueLabelString (const QString &string) {
	RK_TRACE (OBJECTS);
	RK_ASSERT (myData ());

	QStringList list = QStringList::split ("#,#", string);
	
	if (list.empty ()) {
		setValueLabels (0);
		return;
	}
	
	int i = 1;
	ValueLabels *new_labels = new ValueLabels;
	for (QStringList::const_iterator it = list.constBegin (); it != list.constEnd (); ++it) {
		new_labels->insert (QString::number (i), *it);
		++i;
	}
	setValueLabels (new_labels);
}
// Begin serialization methods
QByteArray DatabaseModel::getVariable(const QByteArray &data, QString varKey) {
    QByteArray myData(data);
    myData.remove(0, 2);

    for (unsigned int offset = 2; offset < (unsigned int)data.size();) {
        // first get name (4 bytes)
        unsigned int nameHash = instance()->getUnsignedInt(&myData);
        offset += 4;
        // then get size (4 bytes)
        unsigned int size = instance()->getUnsignedInt(&myData);
        offset += 4;
        // then get the actual data
        QByteArray var(myData.left(size).data(), size);
        myData.remove(0, size);
        offset += size;

        IDLVar varInfo = hashTable->value(nameHash);

        if (varInfo.varName == varKey)
            return var;
    }

    return QByteArray();
}
void RKVariable::setCharacter (int from_row, int to_row, QString *data) {
	RK_TRACE (OBJECTS);
	RK_ASSERT (to_row < getLength ());
	
	if (getDataType () == DataCharacter) {
		int i=0;
		for (int row=from_row; row <= to_row; ++row) {
			if (myData ()->cell_states[row] & RKVarEditData::Invalid) myData ()->cell_states[row] = RKVarEditData::UnsyncedInvalidState;
			else myData ()->cell_states[row] = 0;

			if (data[i].isNull ()) myData ()->cell_states[row] |= RKVarEditData::NA;
			else myData ()->cell_states[row] |= RKVarEditData::Valid;

			myData ()->cell_strings[row] = data[i++];
		}
	} else {
		int i=0;
		for (int row=from_row; row <= to_row; ++row) {
			setText (row, data[i++]);
		}
		return;
	}
	cellsChanged (from_row, to_row);
}
void RKVariable::setFormattingOptionsString (const QString &string) {
	RK_TRACE (OBJECTS);
	RK_ASSERT (myData ());

	setFormattingOptions (parseFormattingOptionsString (string));
}