sync trunk HEAD (r50626)
[reactos.git] / base / applications / mscutils / servman / export.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/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 static DWORD
13 GetTextFromListView(PMAIN_WND_INFO Info,
14 TCHAR Text[500],
15 INT row,
16 INT col)
17 {
18 LVITEM item;
19 DWORD NumChars;
20
21 ZeroMemory(&item, sizeof(item));
22 item.mask = LVIF_TEXT;
23 item.iSubItem = col;
24 item.pszText = Text;
25 item.cchTextMax = 500;
26 NumChars = (INT)SendMessage(Info->hListView,
27 LVM_GETITEMTEXT,
28 row,
29 (LPARAM)&item);
30
31 return NumChars;
32 }
33
34
35 static BOOL
36 SaveServicesToFile(PMAIN_WND_INFO Info,
37 LPCTSTR pszFileName)
38 {
39 HANDLE hFile;
40 BOOL bSuccess = FALSE;
41
42 hFile = CreateFile(pszFileName,
43 GENERIC_WRITE,
44 0,
45 NULL,
46 CREATE_ALWAYS,
47 FILE_ATTRIBUTE_NORMAL,
48 NULL);
49
50 if(hFile != INVALID_HANDLE_VALUE)
51 {
52 TCHAR LVText[500];
53 TCHAR newl = _T('\n');
54 TCHAR tab = _T('\t');
55 DWORD dwTextLength, dwWritten;
56 INT NumListedServ = 0;
57 INT i, k;
58
59 NumListedServ = ListView_GetItemCount(Info->hListView);
60
61 for (i=0; i < NumListedServ; i++)
62 {
63 for (k=0; k<5; k++)
64 {
65 dwTextLength = GetTextFromListView(Info,
66 LVText,
67 i,
68 k);
69 if (LVText != NULL)
70 {
71 WriteFile(hFile,
72 LVText,
73 sizeof(TCHAR) * dwTextLength,
74 &dwWritten,
75 NULL);
76
77 WriteFile(hFile,
78 &tab,
79 sizeof(TCHAR),
80 &dwWritten,
81 NULL);
82 }
83 }
84 WriteFile(hFile,
85 &newl,
86 sizeof(TCHAR),
87 &dwWritten,
88 NULL);
89 }
90
91 CloseHandle(hFile);
92 bSuccess = TRUE;
93 }
94
95 return bSuccess;
96 }
97
98
99 VOID ExportFile(PMAIN_WND_INFO Info)
100 {
101 OPENFILENAME ofn;
102 TCHAR szFileName[MAX_PATH] = _T("");
103
104 ZeroMemory(&ofn, sizeof(ofn));
105
106 ofn.lStructSize = sizeof(OPENFILENAME);
107 ofn.hwndOwner = Info->hMainWnd;
108 ofn.lpstrFilter = _T("Text (Tab Delimited)(*.txt)\0*.txt\0Text (Comma Delimited)(*.csv)\0*.csv\0");
109 ofn.lpstrFile = szFileName;
110 ofn.nMaxFile = MAX_PATH;
111 ofn.lpstrDefExt = _T("txt");
112 ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
113
114 if(GetSaveFileName(&ofn))
115 {
116 if (SaveServicesToFile(Info, szFileName))
117 return;
118 }
119
120 if (CommDlgExtendedError() != CDERR_GENERALCODES)
121 MessageBox(NULL, _T("Export to file failed"), NULL, 0);
122 }