1 /* vifm 2 * Copyright (C) 2012 xaizek. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 17 */ 18 19 #ifndef VIFM__ENGINE__VAR_H__ 20 #define VIFM__ENGINE__VAR_H__ 21 22 /* This unit provides all necessary to work with variables. */ 23 24 /* Enumeration of variable types. */ 25 typedef enum 26 { 27 VTYPE_ERROR, /* Signals about failure or uninitialized variable. */ 28 VTYPE_STRING, /* Regular string value. */ 29 VTYPE_INT, /* Integer, which is also used for boolean. */ 30 } 31 VarType; 32 33 /* Union of possible variable contents. */ 34 typedef union 35 { 36 char *string; /* String value for VTYPE_STRING, should be copied to use it. */ 37 int integer; /* VTYPE_INT value. */ 38 } 39 var_val_t; 40 41 /* Structure for script variable. */ 42 typedef struct 43 { 44 VarType type; /* Variable type. */ 45 var_val_t value; /* Value depending on type. */ 46 } 47 var_t; 48 49 /* Gets variable, which evaluates to true. Returns the variable. */ 50 var_t var_true(void); 51 52 /* Gets variable, which evaluates to false. Returns the variable. */ 53 var_t var_false(void); 54 55 /* Gets boolean variable for the boolean value. Returns the variable. */ 56 var_t var_from_bool(int bool_val); 57 58 /* Makes integer variable initialized with given integer. Returns the 59 * variable. */ 60 var_t var_from_int(int int_val); 61 62 /* Makes string variable initialized with given string. Returns the 63 * variable. */ 64 var_t var_from_str(const char str_val[]); 65 66 /* Returns variable, which signals about failed operation. */ 67 var_t var_error(void); 68 69 /* Convenience function that clones a variable. */ 70 var_t var_clone(var_t var); 71 72 /* Converts variable to a string. Returns new string, which should be freed by 73 * the caller. */ 74 char * var_to_str(const var_t var); 75 76 /* Converts variable to an integer. Returns integer value, parsing of a string 77 * is performed to get integer value. */ 78 int var_to_int(const var_t var); 79 80 /* Converts variable to a boolean. Returns non-zero if value is evaluated to 81 * true. */ 82 int var_to_bool(const var_t var); 83 84 /* Frees resources allocated for the var if any. */ 85 void var_free(const var_t var); 86 87 #endif /* VIFM__ENGINE__VAR_H__ */ 88 89 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */ 90 /* vim: set cinoptions+=t0 filetype=c : */ 91