+ DocFileName result;
+ if (FileName::isAbsolute(fn) || !FileName::isAbsolute(params().origin))
+ result.set(fn, filePath());
+ else {
+ // filePath() ends with a path separator
+ FileName const test(filePath() + fn);
+ if (test.exists())
+ result.set(fn, filePath());
+ else
+ result.set(fn, params().origin);
+ }
+
+ return result;
+}
+
+
+string const Buffer::prepareFileNameForLaTeX(string const & name,
+ string const & ext, bool nice) const
+{
+ string const fname = makeAbsPath(name, filePath()).absFileName();
+ if (FileName::isAbsolute(name) || !FileName(fname + ext).isReadableFile())
+ return name;
+ if (!nice)
+ return fname;
+
+ // FIXME UNICODE
+ return to_utf8(makeRelPath(from_utf8(fname),
+ from_utf8(masterBuffer()->filePath())));
+}
+
+
+vector<pair<docstring, string>> const Buffer::prepareBibFilePaths(OutputParams const & runparams,
+ docstring_list const & bibfilelist,
+ bool const add_extension) const
+{
+ // If we are processing the LaTeX file in a temp directory then
+ // copy the .bib databases to this temp directory, mangling their
+ // names in the process. Store this mangled name in the list of
+ // all databases.
+ // (We need to do all this because BibTeX *really*, *really*
+ // can't handle "files with spaces" and Windows users tend to
+ // use such filenames.)
+ // Otherwise, store the (maybe absolute) path to the original,
+ // unmangled database name.
+
+ vector<pair<docstring, string>> res;
+
+ // determine the export format
+ string const tex_format = flavor2format(runparams.flavor);
+
+ // check for spaces in paths
+ bool found_space = false;
+
+ for (auto const & bit : bibfilelist) {
+ string utf8input = to_utf8(bit);
+ string database =
+ prepareFileNameForLaTeX(utf8input, ".bib", runparams.nice);
+ FileName try_in_file =
+ makeAbsPath(database + ".bib", filePath());
+ bool not_from_texmf = try_in_file.isReadableFile();
+ // If the file has not been found, try with the real file name
+ // (it might come from a child in a sub-directory)
+ if (!not_from_texmf) {
+ try_in_file = getBibfilePath(bit);
+ if (try_in_file.isReadableFile()) {
+ // Check if the file is in texmf
+ FileName kpsefile(findtexfile(changeExtension(utf8input, "bib"), "bib", true));
+ not_from_texmf = kpsefile.empty()
+ || kpsefile.absFileName() != try_in_file.absFileName();
+ if (not_from_texmf)
+ // If this exists, make path relative to the master
+ // FIXME Unicode
+ database =
+ removeExtension(prepareFileNameForLaTeX(
+ to_utf8(makeRelPath(from_utf8(try_in_file.absFileName()),
+ from_utf8(filePath()))),
+ ".bib", runparams.nice));
+ }
+ }