Ejemplo n.º 1
0
    void Config::init ()
    {
      if (Path::is_file (MRTRIX_SYS_CONFIG_FILE)) {
        INFO ("reading config file \"" MRTRIX_SYS_CONFIG_FILE "\"...");
        try {
          KeyValue kv (MRTRIX_SYS_CONFIG_FILE);
          while (kv.next()) {
            config[kv.key()] = kv.value();
          }
        }
        catch (...) { }
      }

      std::string path = Path::join (Path::home(), MRTRIX_USER_CONFIG_FILE);
      if (Path::is_file (path)) {
        INFO ("reading config file \"" + path + "\"...");
        try {
          KeyValue kv (path);
          while (kv.next()) {
            config[kv.key()] = kv.value();
          }
        }
        catch (...) { }
      }
    }
Ejemplo n.º 2
0
bool
JetpackActorCommon::jsval_to_CompVariant(JSContext* cx, JSType type, jsval from,
                                         CompVariant* to, OpaqueSeenType* seen)
{
  if (type != JSTYPE_OBJECT)
    return false;

  js::LazilyConstructed<OpaqueSeenType> lost;
  if (!seen) {
    lost.construct();
    seen = lost.addr();
    if (!seen->ok())
      return false;
  }

  OpaqueSeenType::KeyType obj = JSVAL_TO_OBJECT(from);
  OpaqueSeenType::IdType id;
  if (!seen->add(obj, &id)) {
    if (OpaqueSeenType::kInvalidId == id)
      return false;
    *to = CompVariant(id);
    return true;
  }

  if (JS_IsArrayObject(cx, obj)) {
    nsTArray<Variant> elems;
    jsuint len;
    if (!JS_GetArrayLength(cx, obj, &len) ||
        !elems.SetCapacity(len))
      return false;
    for (jsuint i = 0; i < len; ++i) {
      jsval val;
      Variant* vp = elems.AppendElement();
      if (!JS_GetElement(cx, obj, i, &val) ||
          !jsval_to_Variant(cx, val, vp, seen))
        *vp = void_t();
    }
    *to = elems;
    return true;
  }

  js::AutoIdArray ida(cx, JS_Enumerate(cx, obj));
  if (!ida)
    return false;

  nsTArray<KeyValue> kvs;
  for (size_t i = 0; i < ida.length(); ++i) {
    jsval val; // reused for both key and value
    if (!JS_IdToValue(cx, ida[i], &val))
      return false;
    JSString* idStr = JS_ValueToString(cx, val);
    if (!idStr)
      return false;
    if (!JS_GetPropertyById(cx, obj, ida[i], &val))
      return false;
    KeyValue kv;
    // Silently drop properties that can't be converted.
    if (jsval_to_Variant(cx, val, &kv.value(), seen)) {
      kv.key() = nsDependentString((PRUnichar*)JS_GetStringChars(idStr),
                                   JS_GetStringLength(idStr));
      // If AppendElement fails, we lose this property, no big deal.
      kvs.AppendElement(kv);
    }
  }
  *to = kvs;

  return true;
}
Ejemplo n.º 3
0
void run ()
{

  const std::string path = argument[0];

  Tractography::Properties properties;
  Tractography::Reader<float> reader (path, properties);

  size_t init_count = 0;
  if (properties.find ("count") == properties.end()) {
    INFO ("\"count\" field not set in file " + path);
  } else {
    init_count = to<size_t>(properties["count"]);
    INFO ("Value of \"count\" in file " + path + " is " + str(init_count));
  }

  // Read the actual number of streamlines in the file
  Tractography::Streamline<float> tck;
  size_t count = 0;
  {
    ProgressBar progress ("evaluating actual streamline data count...");
    while (reader (tck)) {
      ++count;
      ++progress;
    }
  }
  reader.close();
  INFO ("Actual number of streamlines read is " + str(count));

  // Find the appropriate file offset in the header
  KeyValue kv (argument[0], "mrtrix tracks");
  std::string data_file;
  int64_t count_offset = 0;
  int64_t current_offset = 0;
  while (kv.next()) {
    std::string key = lowercase (kv.key());
    if (key == "count")
      count_offset = current_offset;
    current_offset = kv.tellg();
  }
  kv.close();

  if (!count_offset)
    throw Exception ("could not find location of \"count\" field in file \"" + path + "\"");
  DEBUG ("File offset for \"count\" field is " + str(count_offset));

  // Overwrite this data
  // Used C-style file access here as ofstream would either wipe the file contents, or
  //   append the data instead of inserting it
  FILE* fp;
  fp = fopen (path.c_str(), "r+");
  if (!fp)
    throw Exception ("could not open tracks file \"" + path + "\" for repair");
  if (fseek (fp, count_offset, SEEK_SET)) {
    fclose (fp);
    throw Exception ("unable to seek to the appropriate position in the file");
  }
  const std::string string = "count: " + str(count) + "\ntotal_count: " + str(count) + "\nEND\n";
  const int rvalue = fputs (string.c_str(), fp);
  fclose (fp);
  if (rvalue == EOF) {
    WARN ("\"count\" field may not have been properly updated");
  } else {
    INFO ("\"count\" field updated successfully");
  }

}