1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/export.c
5 * PURPOSE: Save services to a file
6 * COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
7 *
8 */
9
10 #include "precomp.h"
11
12 #include <cderr.h>
13
14 static DWORD
GetTextFromListView(PMAIN_WND_INFO Info,LPWSTR Text,INT row,INT col)15 GetTextFromListView(PMAIN_WND_INFO Info,
16 LPWSTR Text,
17 INT row,
18 INT col)
19 {
20 LVITEM item;
21 DWORD NumChars;
22
23 ZeroMemory(&item, sizeof(item));
24 item.mask = LVIF_TEXT;
25 item.iSubItem = col;
26 item.pszText = Text;
27 item.cchTextMax = 500;
28 NumChars = (INT)SendMessageW(Info->hListView,
29 LVM_GETITEMTEXTW,
30 row,
31 (LPARAM)&item);
32 return NumChars;
33 }
34
35 static BOOL
SaveServicesToFile(PMAIN_WND_INFO Info,LPCWSTR pszFileName,DWORD nFormat)36 SaveServicesToFile(PMAIN_WND_INFO Info,
37 LPCWSTR pszFileName,
38 DWORD nFormat)
39 {
40 HANDLE hFile;
41 BOOL bSuccess = FALSE;
42
43 if (!nFormat || nFormat > 2)
44 {
45 return bSuccess;
46 }
47
48 hFile = CreateFileW(pszFileName,
49 GENERIC_WRITE,
50 0,
51 NULL,
52 CREATE_ALWAYS,
53 FILE_ATTRIBUTE_NORMAL,
54 NULL);
55
56 if(hFile != INVALID_HANDLE_VALUE)
57 {
58 WCHAR LVText[500];
59 WCHAR newl[2] = {L'\r', L'\n'};
60 WCHAR seps[2] = {L'\t', L','};
61 DWORD dwTextLength, dwWritten;
62 INT NumListedServ = 0;
63 INT i, k;
64
65 NumListedServ = ListView_GetItemCount(Info->hListView);
66
67 for (i=0; i < NumListedServ; i++)
68 {
69 for (k=0; k < LVMAX; k++)
70 {
71 dwTextLength = GetTextFromListView(Info,
72 LVText,
73 i,
74 k);
75 if (dwTextLength != 0)
76 {
77 WriteFile(hFile,
78 LVText,
79 sizeof(WCHAR) * dwTextLength,
80 &dwWritten,
81 NULL);
82 }
83
84 if (k < LVMAX - 1)
85 {
86 /* Do not add separator after the last table cell */
87 WriteFile(hFile,
88 &seps[nFormat-1],
89 sizeof(WCHAR),
90 &dwWritten,
91 NULL);
92 }
93 }
94 WriteFile(hFile,
95 newl,
96 sizeof(newl),
97 &dwWritten,
98 NULL);
99 }
100
101 CloseHandle(hFile);
102 bSuccess = TRUE;
103 }
104
105 return bSuccess;
106 }
107
ExportFile(PMAIN_WND_INFO Info)108 VOID ExportFile(PMAIN_WND_INFO Info)
109 {
110 OPENFILENAMEW ofn;
111 WCHAR szFileName[MAX_PATH];
112
113 ZeroMemory(&ofn, sizeof(ofn));
114 szFileName[0] = UNICODE_NULL;
115
116 ofn.lStructSize = sizeof(OPENFILENAME);
117 ofn.hwndOwner = Info->hMainWnd;
118 ofn.lpstrFilter = L"Text (Tab Delimited)(*.txt)\0*.txt\0Text (Comma Delimited)(*.csv)\0*.csv\0";
119 ofn.lpstrFile = szFileName;
120 ofn.nMaxFile = MAX_PATH;
121 ofn.lpstrDefExt = L"txt";
122 ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
123
124 if(GetSaveFileName(&ofn))
125 {
126 if (SaveServicesToFile(Info, szFileName, ofn.nFilterIndex))
127 return;
128 }
129
130 if (CommDlgExtendedError() != CDERR_GENERALCODES)
131 MessageBoxW(NULL, L"Export to file failed", NULL, 0);
132 }
133