-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpg251-SampleTranscoder.cs
More file actions
147 lines (127 loc) · 4.85 KB
/
pg251-SampleTranscoder.cs
File metadata and controls
147 lines (127 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Sample Transcoder from Debugging with Fiddler pg 251
using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Collections.Generic;
using System.Windows.Forms;
using Fiddler;
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: Fiddler.RequiredVersion("2.3.9.5")]
// Note that this Transcoder only works when loaded by Fiddler itself; it will
// not work from a FiddlerCore-based application. The reason is that the output
// uses the columns shown in Fiddler’s Web Sessions list, and FiddlerCore has
// no such list.
// Ensure your class is public, or Fiddler won't see it!
[ProfferFormat("TAB-Separated Values", "Session List in Tab-Delimited Format")]
[ProfferFormat("Comma-Separated Values",
"Session List in Comma-Delimited Format; import into Excel or other tools")]
public class CSVTranscoder: ISessionExporter
{
public bool ExportSessions(string sFormat, Session[] oSessions,
Dictionary<string, object> dictOptions,
EventHandler<ProgressCallbackEventArgs> evtProgressNotifications)
{
bool bResult = false;
string chSplit;
// Determine if we already have a filename
// from the dictOptions collection
string sFilename = null;
if (null != dictOptions && dictOptions.ContainsKey("Filename"))
{
sFilename = dictOptions["Filename"] as string;
}
// If we don't yet have a filename, prompt the user
// with a File Save dialog, using the correct file extension
// for the export format they selected
if (sFormat == "Comma-Separated Values")
{
chSplit = ",";
if (string.IsNullOrEmpty(sFilename))
sFilename = Fiddler.Utilities.ObtainSaveFilename(
"Export As " + sFormat, "CSV Files (*.csv)|*.csv");
}
else
{
// Ensure caller asked for Tab-delimiting.
if (sFormat != "TAB-Separated Values") return false;
chSplit = "\t";
if (string.IsNullOrEmpty(sFilename))
sFilename = Fiddler.Utilities.ObtainSaveFilename(
"Export As " + sFormat, "TSV Files (*.tsv)|*.tsv");
}
// If we didn't get a filename, user cancelled. If so, bail out.
if (String.IsNullOrEmpty(sFilename)) return false;
try
{
StreamWriter swOutput = new StreamWriter(sFilename, false, Encoding.UTF8);
int iCount = 0;
int iMax = oSessions.Length;
#region WriteColHeaders
bool bFirstCol = true;
foreach (ColumnHeader oLVCol in FiddlerApplication.UI.lvSessions.Columns)
{
if (!bFirstCol)
{
swOutput.Write(chSplit);
}
else
{
bFirstCol = false;
}
// Remove any delimiter characters from the value
swOutput.Write(oLVCol.Text.Replace(chSplit, ""));
}
swOutput.WriteLine();
#endregion WriteColHeaders
#region WriteEachSession
foreach (Session oS in oSessions)
{
iCount++;
// The ViewItem object is the ListViewItem in the Web Sessions list
// Obviously, this doesn't exist in FiddlerCore-based applications
if (null != oS.ViewItem)
{
bFirstCol = true;
ListViewItem oLVI = (oS.ViewItem as ListViewItem);
if (null == oLVI) continue;
foreach (ListViewItem.ListViewSubItem oLVC in oLVI.SubItems)
{
if (!bFirstCol)
{
swOutput.Write(chSplit);
}
else
{
bFirstCol = false;
}
// Remove any delimiter characters from the value
swOutput.Write(oLVC.Text.Replace(chSplit, ""));
}
swOutput.WriteLine();
}
// Notify the caller of our progress
if (null != evtProgressNotifications)
{
ProgressCallbackEventArgs PCEA =
new ProgressCallbackEventArgs((iCount/(float)iMax),
"wrote " + iCount.ToString() + " records.");
evtProgressNotifications(null, PCEA);
// If the caller tells us to cancel, abort quickly
if (PCEA.Cancel) { swOutput.Close(); return false; }
}
}
#endregion WriteEachSession
swOutput.Close();
bResult = true;
}
catch (Exception eX)
{
// TODO: Replace alert with FiddlerApplication.Log.LogFormat(...
MessageBox.Show(eX.Message, "Failed to export");
bResult = false;
}
return bResult;
}
public void Dispose() { /*no-op*/ }
}