Example #1
0
File: dunits.c Project: xyuan/dohp
static dErr dUnitsAssignName(dUnits un,const char *(*namer)(dUnit),const char *proposed,dInt n,const dReal expon[],char **assigned)
{
  dErr err;
  char buf[1024],*p = buf;
  dInt left = 1024;
  dFunctionBegin;
  if (proposed) {
    err = PetscStrallocpy(proposed,assigned);dCHK(err);
    dFunctionReturn(0);
  }
  buf[0] = 0;
  for (dInt i=0; i<n; i++) {
    const char *s;
    dUnit base;
    dInt len;
    if (expon[i] == 0) continue;
    err = dUnitsGetBase(un,i,&base);dCHK(err);
    s = namer(base);
    if (expon[i] == 1)
      len = snprintf(p,left,"%s ",s);
    else if (round(expon[i]) == expon[i])
      len = snprintf(p,left,"%s^%1.0f ",s,expon[i]);
    else
      len = snprintf(p,left,"%s^%f ",s,expon[i]);
    left -= len;
    p += len;
  }
  p[-1] = 0; // Kill trailing space
  err = PetscStrallocpy(buf,assigned);dCHK(err);
  dFunctionReturn(0);
}
Example #2
0
/* Append md5sumed folder to path if path is a directory. */
static const char *
mutt_hcache_per_folder(const char *path, const char *folder,
                       hcache_namer_t namer)
{
  static char hcpath[_POSIX_PATH_MAX];
  struct stat sb;
  unsigned char md5sum[16];
  char* s;
  int ret, plen;
#ifndef HAVE_ICONV
  const char *chs = Charset && *Charset ? Charset : 
		    mutt_get_default_charset ();
#endif

  plen = mutt_strlen (path);

  ret = stat(path, &sb);
  if (ret < 0 && path[plen-1] != '/')
  {
#ifdef HAVE_ICONV
    return path;
#else
    snprintf (hcpath, _POSIX_PATH_MAX, "%s-%s", path, chs);
    return hcpath;
#endif
  }

  if (ret >= 0 && !S_ISDIR(sb.st_mode))
  {
#ifdef HAVE_ICONV
    return path;
#else
    snprintf (hcpath, _POSIX_PATH_MAX, "%s-%s", path, chs);
    return hcpath;
#endif
  }

  if (namer)
  {
    snprintf (hcpath, sizeof (hcpath), "%s%s", path,
              path[plen-1] == '/' ? "" : "/");
    if (path[plen-1] != '/')
      plen++;

    ret = namer (folder, hcpath + plen, sizeof (hcpath) - plen);
  }
  else
  {
    md5_buffer (folder, strlen (folder), &md5sum);

    /* On some systems (e.g. OS X), snprintf is defined as a macro.
     * Embedding directives inside macros is undefined, so we have to duplicate
     * the whole call:
     */
#ifndef HAVE_ICONV
    ret = snprintf(hcpath, _POSIX_PATH_MAX,
                   "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
                   "%02x%02x%02x%02x%02x%02x%02x%02x"
		   "-%s"
		   ,
		   path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
                   md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
                   md5sum[9], md5sum[10], md5sum[11], md5sum[12],
                   md5sum[13], md5sum[14], md5sum[15]
		   ,chs
		   );
#else
    ret = snprintf(hcpath, _POSIX_PATH_MAX,
                   "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
                   "%02x%02x%02x%02x%02x%02x%02x%02x"
		   ,
		   path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
                   md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
                   md5sum[9], md5sum[10], md5sum[11], md5sum[12],
                   md5sum[13], md5sum[14], md5sum[15]
		   );
#endif
  }
  
  if (ret <= 0)
    return path;

  if (stat (hcpath, &sb) >= 0)
    return hcpath;

  s = strchr (hcpath + 1, '/');
  while (s)
  {
    /* create missing path components */
    *s = '\0';
    if (stat (hcpath, &sb) < 0 && (errno != ENOENT || mkdir (hcpath, 0777) < 0))
      return path;
    *s = '/';
    s = strchr (s + 1, '/');
  }

  return hcpath;
}
Example #3
0
	istream& GenericKAnimFile::load(istream& in, int verbosity) {
		if(verbosity >= 1) {
			cout << "Loading anim file information..." << endl;
		}

		uint32_t magic;
		io.raw_read_integer(in, magic);
		if(!in || magic != MAGIC_NUMBER) {
			throw(KToolsError("Attempt to read a non-anim file as anim."));
		}

		io.raw_read_integer(in, version);

		if(version & 0xffff) {
			io.setNativeSource();
		}
		else {
			io.setInverseNativeSource();
			io.reorder(version);
		}

		if(verbosity >= 2) {
			cout << "Got anim version " << version << "." << endl;
		}

		versionRequire();


		uint32_t numelements;
		uint32_t numframes;
		uint32_t numevents;
		uint32_t numanims;

		io.read_integer(in, numelements);
		io.read_integer(in, numframes);
		io.read_integer(in, numevents);
		io.read_integer(in, numanims);

		setAnimCount(numanims);

		if(verbosity >= 1) {
			cout << "Loading " << numanims << " animations..." << endl;
		}

		if(!loadPre_all_anims(in, verbosity)) {
			throw(KToolsError("Failed to load animations."));
		}

		if(countElements() != numelements) {
			throw(KToolsError("Corrupt anim file (invalid element count)."));
		}
		if(countFrames() != numframes) {
			throw(KToolsError("Corrupt anim file (invalid frame count)."));
		}
		if(countEvents() != numevents) {
			throw(KToolsError("Corrupt anim file (invalid event count)."));
		}


		if(!shouldHaveHashTable() || !in || in.peek() == EOF) {
			if(shouldHaveHashTable()) {
				std::cerr << "WARNING: Missing hash table at the end of the anim file. Generating automatic names." << std::endl;
			}

			FallbackKAnimNamer namer;

			(void)loadPost_all_anims(in, namer, verbosity);
		}
		else {
			if(verbosity >= 1) {
				cout << "Loading anim hash table..." << endl;
			}

			hashtable_t ht;
			uint32_t htsize;
			io.read_integer(in, htsize);
			for(uint32_t i = 0; i < htsize; i++) {
				hash_t h;
				io.read_integer(in, h);

				string& str = ht[h];
				io.read_len_string<uint32_t>(in, str);

				if(verbosity >= 5) {
					cout << "\tGot 0x" << hex << h << dec << " => \"" << str << "\"" << endl;
				}
			}

			HashTableKAnimNamer namer(ht);

			(void)loadPost_all_anims(in, namer, verbosity);
		}


		if(in.peek() != EOF) {
			std::cerr << "Warning: There is leftover data in the input anim file." << std::endl;
		}

		return in;
	}