| 1 |
/* |
| 2 |
* uservars.h |
| 3 |
* |
| 4 |
* This file is a part of NSIS. |
| 5 |
* |
| 6 |
* Copyright (C) 2003 Ramon |
| 7 |
* Copyright (C) 2003-2026 NSIS Contributors |
| 8 |
* |
| 9 |
* Licensed under the zlib/libpng license (the "License"); |
| 10 |
* you may not use this file except in compliance with the License. |
| 11 |
* |
| 12 |
* Licence details can be found in the file COPYING. |
| 13 |
* |
| 14 |
* This software is provided 'as-is', without any express or implied |
| 15 |
* warranty. |
| 16 |
* |
| 17 |
* Doxygen comments by Jim Park -- 07/25/2007 |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef ___USERVARS___H_____ |
| 21 |
#define ___USERVARS___H_____ |
| 22 |
|
| 23 |
#include "lang.h" |
| 24 |
|
| 25 |
struct uservarstring { |
| 26 |
int name; |
| 27 |
int index; |
| 28 |
int pos; |
| 29 |
int reference; |
| 30 |
}; |
| 31 |
|
| 32 |
class UserVarsStringList : public SortedStringListND<struct uservarstring> |
| 33 |
{ |
| 34 |
public: |
| 35 |
/* Default constructor */ |
| 36 |
UserVarsStringList() : m_index(0) {} |
| 37 |
|
| 38 |
/* Destructor */ |
| 39 |
virtual ~UserVarsStringList() {} |
| 40 |
|
| 41 |
/** |
| 42 |
* Adds a name to the UserVarsStringList. Sets reference count to |
| 43 |
* ref_count. |
| 44 |
* |
| 45 |
* @param name The User variable string to store. |
| 46 |
* @param ref_count The reference count to store. Default is 0. |
| 47 |
* @return The index of the added variable string. |
| 48 |
*/ |
| 49 |
int add(const TCHAR *name, int ref_count = 0 ) |
| 50 |
{ |
| 51 |
int pos=SortedStringListND<struct uservarstring>::add(name); |
| 52 |
if (pos == -1) return -1; |
| 53 |
|
| 54 |
uservarstring* ustr = ((uservarstring*) m_gr.get()) + pos; |
| 55 |
ustr->index = m_index; |
| 56 |
ustr->pos = pos; |
| 57 |
ustr->reference = ref_count; |
| 58 |
|
| 59 |
int temp = m_index; |
| 60 |
m_index++; |
| 61 |
|
| 62 |
return temp; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get the index of the string that matches 'name.' |
| 67 |
* |
| 68 |
* @param name The name of the string to search for. |
| 69 |
* @param n_chars If -1, match entire string, otherwise compare only |
| 70 |
* n_chars worth of characters. |
| 71 |
* @return The index position of the structure where structure.name == |
| 72 |
* name. |
| 73 |
*/ |
| 74 |
int get(const TCHAR *name, int n_chars = -1) const |
| 75 |
{ |
| 76 |
int v = SortedStringListND<struct uservarstring>::find(name, n_chars); |
| 77 |
if (v == -1) return -1; |
| 78 |
return (((struct uservarstring*) m_gr.get())[v].index); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get count of strings. |
| 83 |
* |
| 84 |
* @return The count of strings. |
| 85 |
*/ |
| 86 |
int getnum() const |
| 87 |
{ |
| 88 |
return m_index; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Given the index of the structure, return the reference count. |
| 93 |
* |
| 94 |
* @return The reference count of the nth uservarstring structure. |
| 95 |
* If not found, returns -1. |
| 96 |
*/ |
| 97 |
int get_reference(int idx) const |
| 98 |
{ |
| 99 |
int pos=get_internal_idx(idx); |
| 100 |
if (pos==-1) return -1; |
| 101 |
return (((struct uservarstring*) m_gr.get())[pos].reference); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Given the index of the structure, increment the reference count. |
| 106 |
* |
| 107 |
* @return The previous reference count (before the increment). |
| 108 |
* If not found, returns -1. |
| 109 |
*/ |
| 110 |
int inc_reference(int idx) |
| 111 |
{ |
| 112 |
int pos=get_internal_idx(idx); |
| 113 |
if (pos==-1) return -1; |
| 114 |
return ((struct uservarstring*) m_gr.get())[pos].reference++; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Given the index of the structure, return the string value |
| 119 |
* of the name. |
| 120 |
* |
| 121 |
* @return String value of the name as TCHAR*. |
| 122 |
* If not found, returns NULL. |
| 123 |
*/ |
| 124 |
TCHAR *idx2name(int idx) |
| 125 |
{ |
| 126 |
int pos=get_internal_idx(idx); |
| 127 |
if (pos==-1) return NULL; |
| 128 |
struct uservarstring *data=(struct uservarstring *) m_gr.get(); |
| 129 |
return ((TCHAR*) m_strings.get() + data[pos].name); |
| 130 |
} |
| 131 |
|
| 132 |
private: |
| 133 |
int m_index; |
| 134 |
|
| 135 |
int get_internal_idx(int idx) const |
| 136 |
{ |
| 137 |
struct uservarstring *data=(struct uservarstring *) m_gr.get(); |
| 138 |
for (int i = 0; i < m_index; i++) |
| 139 |
{ |
| 140 |
if (data[i].index == idx) |
| 141 |
{ |
| 142 |
return i; |
| 143 |
} |
| 144 |
} |
| 145 |
return -1; |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
#endif |