fix clang build
[far2l.git] / multiarc / src / ArcProc.cpp
blob3281d6e5c619f8945fd54fb18613f415ca0dbc58
1 #include "MultiArc.hpp"
2 #include "marclng.hpp"
3 #include <errno.h>
5 BOOL PluginClass::GetFormatName(char *FormatName, char *DefExt)
7 *FormatName = 0;
8 if (DefExt)
9 *DefExt = 0;
10 char TempDefExt[NM];
11 return ArcPlugin->GetFormatName(ArcPluginNumber, ArcPluginType, FormatName, DefExt ? DefExt : TempDefExt);
14 void PluginClass::GetCommandFormat(int Command, char *Format, int FormatSize)
16 //*Format=0;
17 char ArcFormat[100] /*,DefExt[NM]*/;
18 /*if (!ArcPlugin->GetFormatName(ArcPluginNumber,ArcPluginType,ArcFormat,DefExt))
19 return;*/
20 if (!GetFormatName(ArcFormat))
21 return;
22 ArcPlugin->GetDefaultCommands(ArcPluginNumber, ArcPluginType, Command, Format);
23 KeyFileReadSection(INI_LOCATION, ArcFormat).GetChars(Format, FormatSize, CmdNames[Command], Format);
26 int PluginClass::DeleteFiles(struct PluginPanelItem *PanelItem, int ItemsNumber, int OpMode)
28 char Command[MA_MAX_SIZE_COMMAND_NAME], AllFilesMask[MA_MAX_SIZE_COMMAND_NAME];
29 if (ItemsNumber == 0)
30 return FALSE;
31 if ((OpMode & OPM_SILENT) == 0) {
32 const char *MsgItems[] = {GetMsg(MDeleteTitle), GetMsg(MDeleteFiles), GetMsg(MDeleteDelete),
33 GetMsg(MDeleteCancel)};
34 char Msg[512];
35 if (ItemsNumber == 1) {
36 char NameMsg[NM];
37 FSF.TruncPathStr(strncpy(NameMsg, PanelItem[0].FindData.cFileName, sizeof(NameMsg) - 1),
38 MAX_WIDTH_MESSAGE);
39 FSF.sprintf(Msg, GetMsg(MDeleteFile), NameMsg);
40 MsgItems[1] = Msg;
42 if (Info.Message(Info.ModuleNumber, 0, NULL, MsgItems, ARRAYSIZE(MsgItems), 2) != 0)
43 return FALSE;
44 if (ItemsNumber > 1) {
45 char Msg[100];
46 FSF.sprintf(Msg, GetMsg(MDeleteNumberOfFiles), ItemsNumber);
47 MsgItems[1] = Msg;
48 if (Info.Message(Info.ModuleNumber, FMSG_WARNING, NULL, MsgItems, ARRAYSIZE(MsgItems), 2) != 0)
49 return FALSE;
52 GetCommandFormat(CMD_DELETE, Command, sizeof(Command));
53 GetCommandFormat(CMD_ALLFILESMASK, AllFilesMask, sizeof(AllFilesMask));
54 int IgnoreErrors = (CurArcInfo.Flags & AF_IGNOREERRORS);
55 ArcCommand ArcCmd(PanelItem, ItemsNumber, Command, ArcName, CurDir, "", AllFilesMask, IgnoreErrors, CMD_DELETE, 0,
56 CurDir, ItemsInfo.Codepage);
57 if (!IgnoreErrors && ArcCmd.GetExecCode() != 0)
58 return FALSE;
59 if (Opt.UpdateDescriptions)
60 for (int I = 0; I < ItemsNumber; I++)
61 PanelItem[I].Flags|= PPIF_PROCESSDESCR;
62 return TRUE;
65 int PluginClass::ProcessHostFile(struct PluginPanelItem *PanelItem, int ItemsNumber, int OpMode)
67 struct ArcCmdMenuData
69 int Msg, Cmd;
71 static const ArcCmdMenuData MenuData[] = {
72 {MArcCmdTest, CMD_TEST },
73 {MArcCmdComment, CMD_COMMENT },
74 {MArcCmdCommentFiles, CMD_COMMENTFILES},
75 {MArcCmdSFX, CMD_SFX },
76 {MArcCmdRecover, CMD_RECOVER },
77 {MArcCmdProtect, CMD_PROTECT },
78 {MArcCmdLock, CMD_LOCK },
81 char Command[MA_MAX_SIZE_COMMAND_NAME], AllFilesMask[MA_MAX_SIZE_COMMAND_NAME];
82 int CommandType;
83 int ExitCode = 0;
85 while (1) {
86 struct FarMenuItemEx MenuItems[ARRAYSIZE(MenuData)];
88 ZeroFill(MenuItems);
89 MenuItems[ExitCode].Flags = MIF_SELECTED;
91 int Count = 0;
92 for (size_t i = 0; i < ARRAYSIZE(MenuData); i++) {
93 GetCommandFormat(MenuData[i].Cmd, Command, sizeof(Command));
94 if (*Command) {
95 MenuItems[Count].Text.TextPtr = GetMsg(MenuData[i].Msg);
96 MenuItems[Count].Flags|= MIF_USETEXTPTR;
97 MenuItems[Count++].UserData = MenuData[i].Cmd;
101 if (!Count) {
102 Count = 1;
103 MenuItems[0].UserData = 0xFFFFFFFF;
106 int BreakCode;
107 int BreakKeys[2] = {VK_F4, 0};
108 ExitCode = Info.Menu(Info.ModuleNumber, -1, -1, 0, FMENU_USEEXT | FMENU_WRAPMODE,
109 GetMsg(MArcCmdTitle), GetMsg(MSelectF4), "ArcCmd", BreakKeys, &BreakCode,
110 (FarMenuItem *)MenuItems, Count);
111 if (ExitCode >= 0) {
112 if (BreakCode == 0) // F4 pressed
114 MenuItems[0].Flags&= ~MIF_USETEXTPTR;
115 GetFormatName(MenuItems[0].Text.Text);
116 ConfigCommands(MenuItems[0].Text.Text, 2 + MenuData[ExitCode].Cmd * 2);
117 continue;
119 CommandType = (int)MenuItems[ExitCode].UserData;
120 if (MenuItems[ExitCode].UserData == 0xFFFFFFFF)
121 return FALSE;
122 } else
123 return FALSE;
124 break;
127 WINPORT(FlushConsoleInputBuffer)(NULL); // GetStdHandle(STD_INPUT_HANDLE));
129 GetCommandFormat(CommandType, Command, sizeof(Command));
130 GetCommandFormat(CMD_ALLFILESMASK, AllFilesMask, sizeof(AllFilesMask));
131 int IgnoreErrors = (CurArcInfo.Flags & AF_IGNOREERRORS);
132 char Password[512];
133 *Password = 0;
135 int AskVolume = (OpMode & (OPM_FIND | OPM_VIEW | OPM_EDIT | OPM_QUICKVIEW)) == 0 && CurArcInfo.Volume
136 && *CurDir == 0 && ExitCode == 0;
137 struct PluginPanelItem MaskPanelItem;
139 if (AskVolume) {
140 char VolMsg[300];
141 char NameMsg[NM];
142 FSF.TruncPathStr(strncpy(NameMsg, FSF.PointToName(ArcName), sizeof(NameMsg) - 1), MAX_WIDTH_MESSAGE);
143 FSF.sprintf(VolMsg, GetMsg(MExtrVolume), NameMsg);
144 const char *MsgItems[] = {"", VolMsg, GetMsg(MExtrVolumeAsk1), GetMsg(MExtrVolumeAsk2),
145 GetMsg(MExtrVolumeSelFiles), GetMsg(MExtrAllVolumes)};
146 int MsgCode = Info.Message(Info.ModuleNumber, 0, NULL, MsgItems, ARRAYSIZE(MsgItems), 2);
147 if (MsgCode < 0)
148 return -1;
149 if (MsgCode == 1) {
150 ZeroFill(MaskPanelItem);
151 strncpy(MaskPanelItem.FindData.cFileName, AllFilesMask,
152 ARRAYSIZE(MaskPanelItem.FindData.cFileName) - 1);
153 if (ItemsInfo.Encrypted)
154 MaskPanelItem.Flags = F_ENCRYPTED;
155 PanelItem = &MaskPanelItem;
156 ItemsNumber = 1;
160 if (strstr(Command, "%%P") != NULL)
161 for (int I = 0; I < ItemsNumber; I++)
162 if ((PanelItem[I].Flags & F_ENCRYPTED)
163 || (ItemsInfo.Encrypted
164 && (PanelItem[I].FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))) {
165 if (!GetPassword(Password, FSF.PointToName(ArcName)))
166 return FALSE;
167 break;
170 ArcCommand ArcCmd(PanelItem, ItemsNumber, Command, ArcName, CurDir, Password, AllFilesMask, IgnoreErrors,
171 CommandType, 0, CurDir,
172 ItemsInfo.Codepage);
173 return IgnoreErrors || ArcCmd.GetExecCode() == 0;
176 int __cdecl FormatSort(struct FarMenuItemEx *Item1, struct FarMenuItemEx *Item2)
178 #ifdef _NEW_ARC_SORT_
179 int Temp = (int)Item2->UserData - (int)Item1->UserData;
180 return Temp ? Temp : (int)Item1->UserData == -1 ? 0 : FSF.LStricmp(Item1->Text.Text, Item2->Text.Text);
181 #else
182 return strcasecmp(Item1->Text.Text, Item2->Text.Text);
183 #endif
186 int PluginClass::SelectFormat(char *ArcFormat, int AddOnly)
188 typedef int(__cdecl * FCmp)(const void *, const void *);
189 struct FarMenuItemEx *MenuItems = NULL, *NewMenuItems;
190 int MenuItemsNumber = 0;
191 char Format[100], DefExt[NM];
192 int BreakCode;
193 int BreakKeys[] = {VK_F4, VK_RETURN, 0};
194 int ExitCode;
196 #ifdef _NEW_ARC_SORT_
197 int SortModeIndex = GetPrivateProfileInt("MultiArc", "SortMode", 1, IniFile);
198 char *SortMode;
199 if (SortModeIndex <= 1 || SortModeIndex >= ARRAYSIZE(SortModes))
200 SortMode = NULL;
201 else
202 SortMode = SortModes[SortModeIndex];
203 #endif
205 BreakKeys[1] = (AddOnly) ? 0 : VK_RETURN;
207 while (1) {
208 for (int i = 0; i < ArcPlugin->FmtCount(); i++) {
209 for (int j = 0;; j++) {
210 if (!ArcPlugin->GetFormatName(i, j, Format, DefExt))
211 break;
213 if (AddOnly) // Only add to archive?
215 char Buffer[MA_MAX_SIZE_COMMAND_NAME];
216 ArcPlugin->GetDefaultCommands(i, j, CMD_ADD, Buffer);
217 KeyFileReadSection(INI_LOCATION, Format)
218 .GetChars(Buffer, sizeof(Buffer), CmdNames[CMD_ADD], Buffer);
219 if (*Buffer == 0)
220 continue;
223 NewMenuItems = (struct FarMenuItemEx *)realloc(MenuItems,
224 (MenuItemsNumber + 1) * sizeof(struct FarMenuItemEx));
225 if (NewMenuItems == NULL) {
226 free(MenuItems);
227 return FALSE;
229 MenuItems = NewMenuItems;
230 ZeroFill(MenuItems[MenuItemsNumber]);
231 MenuItems[MenuItemsNumber].UserData = MAKEWPARAM((WORD)i, (WORD)j);
232 strncpy(MenuItems[MenuItemsNumber].Text.Text, Format,
233 sizeof(MenuItems[MenuItemsNumber].Text.Text) - 1);
234 MenuItems[MenuItemsNumber].Flags =
235 ((MenuItemsNumber == 0 && *ArcFormat == 0) || !strcasecmp(ArcFormat, Format))
236 ? MIF_SELECTED
237 : 0;
238 #ifdef _NEW_ARC_SORT_
239 if (SortMode)
240 MenuItems[MenuItemsNumber].UserData = GetPrivateProfileInt(SortMode, Format, 0, IniFile);
241 else
242 MenuItems[MenuItemsNumber].UserData = SortModeIndex ? 0 : -1;
243 #endif
245 MenuItemsNumber++;
248 if (MenuItemsNumber == 0)
249 return FALSE;
251 FSF.qsort(MenuItems, MenuItemsNumber, sizeof(struct FarMenuItemEx), (FCmp)FormatSort);
253 DWORD Flags = FMENU_AUTOHIGHLIGHT | FMENU_USEEXT;
254 if (!Opt.AdvFlags.MenuWrapMode)
255 Flags|= FMENU_WRAPMODE;
256 else if (Opt.AdvFlags.MenuWrapMode == 2) {
257 CONSOLE_SCREEN_BUFFER_INFO csbi;
258 WINPORT(GetConsoleScreenBufferInfo)(NULL, &csbi); // GetStdHandle(STD_OUTPUT_HANDLE)
259 if (csbi.dwSize.Y - 6 >= MenuItemsNumber)
260 Flags|= FMENU_WRAPMODE;
262 ExitCode = Info.Menu(Info.ModuleNumber, -1, -1, 0, Flags, GetMsg(MSelectArchiver), GetMsg(MSelectF4),
263 NULL, BreakKeys, &BreakCode, (struct FarMenuItem *)MenuItems, MenuItemsNumber);
264 if (ExitCode >= 0) {
265 strcpy(ArcFormat, MenuItems[ExitCode].Text.Text);
266 if ((BreakCode >= 0 && BreakCode <= 1) || !AddOnly) // F4 or Enter pressed
267 ConfigCommands(ArcFormat, 2, TRUE, LOWORD(MenuItems[ExitCode].UserData),
268 HIWORD(MenuItems[ExitCode].UserData));
269 else
270 break;
271 } else
272 break;
273 free(MenuItems);
274 MenuItems = NULL;
275 MenuItemsNumber = 0;
277 if (MenuItems)
278 free(MenuItems);
279 return ExitCode >= 0;
282 int PluginClass::FormatToPlugin(char *Format, int &PluginNumber, int &PluginType)
284 char PluginFormat[100], DefExt[NM];
285 for (int i = 0; i < ArcPlugin->FmtCount(); i++) {
286 for (int j = 0;; j++) {
287 if (!ArcPlugin->GetFormatName(i, j, PluginFormat, DefExt))
288 break;
289 if (!strcasecmp(PluginFormat, Format)) {
290 PluginNumber = i;
291 PluginType = j;
292 return TRUE;
296 return FALSE;
299 SHAREDSYMBOL int WINAPI _export Configure(int ItemNumber);
301 int PluginClass::ProcessKey(int Key, unsigned int ControlState)
303 if ((ControlState & PKF_ALT) && Key == VK_F6) {
304 // HANDLE hScreen=Info.SaveScreen(0,0,-1,-1);
305 if (strstr(ArcName, /*"FarTmp"*/ "FTMP") == NULL) //$AA какая-то бяка баловалась
307 char CurDir[NM];
308 ArrayCpyZ(CurDir, ArcName);
309 char *Slash = strrchr(CurDir, GOOD_SLASH);
310 if (Slash != NULL) {
311 if (Slash != CurDir)
312 *Slash = 0;
313 // if (Slash!=CurDir && *(Slash-1)==':')
314 // Slash[1]=0;
315 // else
316 // *Slash=0;
317 if (sdc_chdir(CurDir))
318 fprintf(stderr, "sdc_chdir('%s') - %u\n", CurDir, errno);
321 struct PanelInfo PInfo;
322 Info.Control(this, FCTL_GETPANELINFO, &PInfo);
323 GetFiles(PInfo.SelectedItems, PInfo.SelectedItemsNumber, FALSE, PInfo.CurDir, OPM_SILENT);
324 // Info.RestoreScreen(hScreen);
325 Info.Control(this, FCTL_UPDATEPANEL, (void *)1);
326 Info.Control(this, FCTL_REDRAWPANEL, NULL);
327 Info.Control(this, FCTL_UPDATEANOTHERPANEL, (void *)1);
328 Info.Control(this, FCTL_REDRAWANOTHERPANEL, NULL);
329 return TRUE;
330 } else if (ControlState == (PKF_ALT | PKF_SHIFT) && Key == VK_F9) {
331 Configure(0);
332 return TRUE;
334 return FALSE;