 |
|
 |
hey plz can anybody tell me how can i convert files(doc,pdf etc) to UDF(universal disk format) using .net libraries... if not. then tell me is there any other software third party?
|
|
|
|
 |
|
 |
The downvotes are because you are posting in the wrong place - try the
Q&A[^] forum
|
|
|
|
 |
|
 |
Hi all,
I think the following could also qualify as a coding horror but I intentionally wrote it this way (one giant linq statement) because of a discussion with a collegue:
Directory.EnumerateFiles(args[0], "*.cs", SearchOption.AllDirectories).AsParallel().Select(f => new { File = f, Bytes = File.ReadAllBytes(f) }).Select(f => new { File = f.File, Bytes = f.Bytes, Encoding = f.Bytes.Take(3).SequenceEqual(new byte[] { 239, 187, 191 }) ? Encoding.UTF8 : Encoding.Default }).ForAll(f => File.WriteAllText(f.File, f.Encoding.GetString(f.Bytes).Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Select(l => new { Line = l, Index = l.Select((c, i) => new { Char = c, Index = i }).FirstOrDefault(c => c.Char != ' ' && c.Char != '\t') }).Select(l => l.Index == null ? l.Line : l.Line.Select((c, i) => (i < l.Index.Index && c == '\t') ? " " : c.ToString()).Aggregate((s1, s2) => s1 + s2)).Aggregate((s1, s2) => s1 + Environment.NewLine + s2), f.Encoding));
Here another version where I tried to get a proper formatting:
static void Main(string[] args)
{
Directory.EnumerateFiles(args[0], "*.cs", SearchOption.AllDirectories).
AsParallel().
Select(f => new {
File = f,
Bytes = File.ReadAllBytes(f)
}).
Select(f => new {
File = f.File,
Bytes = f.Bytes,
Encoding = f.Bytes.Take(3).SequenceEqual(new byte[] { 239, 187, 191 })
? Encoding.UTF8 : Encoding.Default
}).
ForAll(f => File.WriteAllText(f.File,
f.Encoding.GetString(f.Bytes).Split(
new string[] { Environment.NewLine }, StringSplitOptions.None).
Select(l => new { Line = l, Index = l.
Select((c, i) => new { Char = c, Index = i }).
FirstOrDefault(c => c.Char != ' ' && c.Char != '\t') }).
Select(l => l.Index == null ? l.Line : l.Line.
Select((c, i) => (i < l.Index.Index && c == '\t')
? " " : c.ToString()).Aggregate((s1, s2) => s1 + s2)).
Aggregate((s1, s2) => s1 + Environment.NewLine + s2), f.Encoding));
}
So what does this do?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
args[0] should be a directory where C#-Files are located (*.cs). It opens all files, does a bit of Encoding-analysis (this might not work in every case but its fine for me), replaces Tabs at the beginning of each line with whitespaces (4 for each tab), removes alle whitespaces and tabs in otherwise totally empty lines and writes the file back with the proper encoding.
Interestingly this runs faster than I would have expected. For a codebase with about 8800 files it runs just a few seconds. I would even think (haven't measured it) that searching, reading and writing the files is more time consuming than the rest of the linq in between.
Robert
|
|
|
|
 |
|
 |
It's so bad, you can't even get it in the right forum...
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
 |
|
|
 |
|
 |
I should probably clarify why and how this code was created:
We have a relatively large codebase. Some code files are old (C# 1.0 times) and contain tabs instead of white spaces (this was the default setting in VS2003 if I remember correctly). Now everytime we edit one of those files Visual Studio (now 2010) starts replacing the tabs automatically. This is simply annoying when using diffs and can be a complete pain when doing merges in our SVN. So we just had the idea to replace all left tabs with one big update.
While discussing on how to do it was just an amusing idea to make it with Linq. The code was executed exactly one time (ignoring test runs) and will never be touched again. Its nothing that will get checked in in production code or anything like that.
Robert
|
|
|
|
 |
|
 |
(This is kind of off topic but I don't understand why they changed to defaulting to putting spaces at the front. If there are tabs (one tab per indent), it's easy for each developer to set his tab size to what's comfortable for him, and everyone gets to see the code with the indentation they like. As soon as it's spaces everyone's forced to view it the same way.
I get around this by using a non-fixed-pitch IDE font (so spaces are narrow) but it just seems like a big step backwards. Tabs are there for exactly this purpose.
|
|
|
|
 |
|
 |
I think the problem is when the two are mixed in the same line. Then it does not seem to matter how you set them lines don't align.
I also worked on a VMS system where the default was to use tabs - but it had stops at every 8 columns and if you set yours differently it was a mess.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
|
|
|
|
 |
|
 |
BobJanova wrote: each developer to set his tab size
How? In Notepad or another text editor? In a terminus emulator? How about if I include a code snippet in a Word document? Or in a CP post?
TABs are evil and must be abolished.
|
|
|
|
 |
|
 |
Any decent text editor (including IDEs) will let you set the display size of tabs. So will Word. Most web browsers will display tabs in a <pre> as 4 spaces or thereabouts which is what most people use for indentation anyway.
|
|
|
|
 |
|
 |
BobJanova wrote: Any decent text editor
I must not have any of those.
Plus, the worse problem is files with mixed TABs and SPACEs.
|
|
|
|
 |
|
 |
Notepad++ is very good. Shows tabs, spaces, does column deletes and highlights matching brackets, braces, parens etc. Also has modes for different languages C#, C++, VB, Lisp etc.
And it's free.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
|
|
|
|
 |
|
 |
Robert Rohde wrote: Its nothing that will get checked in in production code or anything like that.
Famous last words there...
Let's face it, after Monday and Tuesday, even the calendar says WTF!
Be careful which toes you step on today, they might be connected to the foot that kicks your butt tomorrow.
You can't scare me, I have children.
|
|
|
|
 |
|
 |
Robert Rohde wrote: Directory.EnumerateFiles(args[0], "*.cs", SearchOption.AllDirectories).AsParallel().Select(f => new { File = f, Bytes = File.ReadAllBytes(f) }).Select(f => new { File = f.File, Bytes = f.Bytes, Encoding = f.Bytes.Take(3).SequenceEqual(new byte[] { 239, 187, 191 }) ? Encoding.UTF8 : Encoding.Default }).ForAll(f => File.WriteAllText(f.File, f.Encoding.GetString(f.Bytes).Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Select(l => new { Line = l, Index = l.Select((c, i) => new { Char = c, Index = i }).FirstOrDefault(c => c.Char != ' ' && c.Char != '\t') }).Select(l => l.Index == null ? l.Line : l.Line.Select((c, i) => (i < l.Index.Index && c == '\t') ? " " : c.ToString()).Aggregate((s1, s2) => s1 + s2)).Aggregate((s1, s2) => s1 + Environment.NewLine + s2), f.Encoding));
did you honestly check in code like that ? if so WHY!?!
|
|
|
|
 |
|
 |
From my other post[^]:
Quote: The code was executed exactly one time (ignoring test runs) and will never be touched again. Its nothing that will get checked in in production code or anything like that.
|
|
|
|
 |
|
 |
but why ever have it in that "minified" unreadable format?
|
|
|
|
 |
|
 |
I've been working on about a dozen web pages that I updated. These pages have tables of courses that students can take, and the the page they link to changed. So, after a quick search, I used this regex in Vim (one of the best text editors):
:%s/<td>\(\w\+\)\s\(\d\+\)<\/td>/<td><a href="courses.php?course=\1%20\2">\1 \2<\/a><\/td>/i
|
|
|
|
 |
|
 |
create table trns.a(id int,name varchar(20))
insert into TRNS.a values(1,'A')
insert into TRNS.a values(2,'B')
insert into TRNS.a values(3,'C')
insert into TRNS.a values(4,'D')
insert into TRNS.a values(5,'E')
insert into TRNS.a values(6,'F')
select *from a
if you run select Query, you will get
id name
1 A
2 B
3 C
4 D
. .
. .
Q. I need to Update the above table in single update statement to get result like
select *from a
id name
1 B
2 A
3 D
4 C
. .
. .
Note: I know the solution, So I have put this question under "Clever Code".
|
|
|
|
 |
|
 |
UPDATE trns.a SET id=1+((id-1)^1)
|
|
|
|
 |
|
 |
You have updated the Id. What would you do if there may some more columns like Address and Mobile.... Only Update Name column. The result should be....
Id Address Name
---------------------------------------
1 Address1 B
2 Address2 A
3 Address3 D
4 Address4 C
. .... .
. .... .
|
|
|
|
 |
|
 |
you could do similar exor operations on the ASCII ordinal of a 1-character "name" field.
|
|
|
|
 |
|
 |
Thank you Sir! Today I have learned new things about Bitwise XOR. For updating name column, the query may be like ..
UPDATE trns.a SET Name=CHAR(1+((ASCII(Name)-1)^1)).
But if the Name column is not a single character eg ('Alexzendar','Bob','Caldle','Delle','Ellen','Fusion')??
|
|
|
|
 |
|
 |
Then you would need a different approach, and a better example.
BTW: this forum is for showing existing clever code, not for asking questions that may need clever code to get resolved. As such it is the antipode of the "Hall of Shame".
|
|
|
|
 |
|
 |
That was not part of the specification.
|
|
|
|
 |
|
 |
Surely anything to the power of 1 is itself? so (id-1)^1 = id-1
|
|
|
|
 |