PageRenderTime 243ms CodeModel.GetById 141ms app.highlight 4ms RepoModel.GetById 97ms app.codeStats 0ms

/indra/newview/llexternaleditor.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 105 lines | 28 code | 16 blank | 61 comment | 0 complexity | 85ddd4a05f283e28b43a558b3688f352 MD5 | raw file
  1/** 
  2 * @file llexternaleditor.h
  3 * @brief A convenient class to run external editor.
  4 *
  5 * $LicenseInfo:firstyear=2010&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_LLEXTERNALEDITOR_H
 28#define LL_LLEXTERNALEDITOR_H
 29
 30#include <llprocesslauncher.h>
 31
 32/**
 33 * Usage:
 34 *  LLExternalEditor ed;
 35 *  ed.setCommand("MY_EXTERNAL_EDITOR_VAR");
 36 *  ed.run("/path/to/file1");
 37 *  ed.run("/other/path/to/file2");
 38 */
 39class LLExternalEditor
 40{
 41	typedef std::vector<std::string> string_vec_t;
 42
 43public:
 44
 45	typedef enum e_error_code {
 46		EC_SUCCESS,				/// No error.
 47		EC_NOT_SPECIFIED,		/// Editor path not specified.
 48		EC_PARSE_ERROR,			/// Editor command parsing error.
 49		EC_BINARY_NOT_FOUND,	/// Could find the editor binary (missing or not quoted).
 50		EC_FAILED_TO_RUN,		/// Could not execute the editor binary.
 51	} EErrorCode;
 52
 53	/**
 54	 * Set editor command.
 55	 *
 56	 * @param env_var			Environment variable of the same purpose.
 57	 * @param override			Optional override.
 58	 *
 59	 * First tries the override, then a predefined setting (sSetting),
 60	 * then the environment variable.
 61	 *
 62	 * @return EC_SUCCESS if command is valid and refers to an existing executable,
 63	 *         EC_NOT_SPECIFIED or EC_FAILED_TO_RUNan on error.
 64	 *
 65	 * @see sSetting
 66	 */
 67	EErrorCode setCommand(const std::string& env_var, const std::string& override = LLStringUtil::null);
 68
 69	/**
 70	 * Run the editor with the given file.
 71	 *
 72	 * @param file_path File to edit.
 73	 * @return EC_SUCCESS on success, error code on error.
 74	 */
 75	EErrorCode run(const std::string& file_path);
 76
 77	/**
 78	 * Get a meaningful error message for the given status code.
 79	 */
 80	static std::string getErrorMessage(EErrorCode code);
 81
 82private:
 83
 84	static std::string findCommand(
 85		const std::string& env_var,
 86		const std::string& override);
 87
 88	static size_t tokenize(string_vec_t& tokens, const std::string& str);
 89
 90	/**
 91	 * Filename placeholder that gets replaced with an actual file name.
 92	 */
 93	static const std::string sFilenameMarker;
 94
 95	/**
 96	 * Setting that can specify the editor command.
 97	 */
 98	static const std::string sSetting;
 99
100
101	std::string			mArgs;
102	LLProcessLauncher	mProcess;
103};
104
105#endif // LL_LLEXTERNALEDITOR_H