Sync with trunk.
[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 static BOOL
35 SaveServicesToFile(PMAIN_WND_INFO Info,
36 LPCTSTR pszFileName)
37 {
38 HANDLE hFile;
39 BOOL bSuccess = FALSE;
40
41 hFile = CreateFile(pszFileName,
42 GENERIC_WRITE,
43 0,
44 NULL,
45 CREATE_ALWAYS,
46 FILE_ATTRIBUTE_NORMAL,
47 NULL);
48
49 if(hFile != INVALID_HANDLE_VALUE)
50 {
51 TCHAR LVText[500];
52 TCHAR newl = _T('\n');
53 TCHAR tab = _T('\t');
54 DWORD dwTextLength, dwWritten;
55 INT NumListedServ = 0;
56 INT i, k;
57
58 NumListedServ = ListView_GetItemCount(Info->hListView);
59
60 for (i=0; i < NumListedServ; i++)
61 {
62 for (k=0; k<5; k++)
63 {
64 dwTextLength = GetTextFromListView(Info,
65 LVText,
66 i,
67 k);
68 if (LVText != NULL)
69 {
70 WriteFile(hFile,
71 LVText,
72 sizeof(TCHAR) * dwTextLength,
73 &dwWritten,
74 NULL);
75
76 WriteFile(hFile,
77 &tab,
78 sizeof(TCHAR),
79 &dwWritten,
80 NULL);
81 }
82 }
83 WriteFile(hFile,
84 &newl,
85 sizeof(TCHAR),
86 &dwWritten,
87 NULL);
88 }
89
90 CloseHandle(hFile);
91 bSuccess = TRUE;
92 }
93
94 return bSuccess;
95 }
96
97 VOID ExportFile(PMAIN_WND_INFO Info)
98 {
99 OPENFILENAME ofn;
100 TCHAR szFileName[MAX_PATH] = _T("");
101
102 ZeroMemory(&ofn, sizeof(ofn));
103
104 ofn.lStructSize = sizeof(OPENFILENAME);
105 ofn.hwndOwner = Info->hMainWnd;
106 ofn.lpstrFilter = _T("Text (Tab Delimited)(*.txt)\0*.txt\0Text (Comma Delimited)(*.csv)\0*.csv\0");
107 ofn.lpstrFile = szFileName;
108 ofn.nMaxFile = MAX_PATH;
109 ofn.lpstrDefExt = _T("txt");
110 ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
111
112 if(GetSaveFileName(&ofn))
113 {
114 if (SaveServicesToFile(Info, szFileName))
115 return;
116 }
117
118 if (CommDlgExtendedError() != CDERR_GENERALCODES)
119 MessageBox(NULL, _T("Export to file failed"), NULL, 0);
120 }