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