Small program to format text for copy/pasting into books

This forum is for anything that doesn't specifically have to do with Better Than Wolves
Post Reply
User avatar
agentwiggles
Posts: 188
Joined: Fri Apr 06, 2012 2:31 pm

Small program to format text for copy/pasting into books

Post by agentwiggles »

NOTE: The link to the program is in my next post (2 or 3 posts down), this one's just background and the question I asked before I'd written it.

//skip down if you don't want to read my rambling background post

Just today, I played with the new writeable books for the first time.

And HOLY SHIT, I am in love. I've wanted this functionality for ages, and I've already gone crazy in my world with the books. I've written books about my tunnel marking procedures for our communal mines, and, for each mining outpost, a small info packet which describes the areas that the mining outpost services, along with directions for returning home and other such tidbits.

This is cool. These books give a world history, and character, and I want to continue to make as many as I can. But what a drag, to type all that text manually, conforming to a 256 character limit for each page. And even copy-pasting is irritating, since you can't do it unless what's on your clipboard fits on one page.

But I'm a CS major, dammit, and I've taken enough programming to be able to build utilities for myself... so why should I work hard when a computer can do it for me? So I'm currently writing a simple program, which will take a text file for input, read in 256 characters at a time, and divide the original text into chunks for easy copy pasting, outputting the modified text to a file.

This program will also include a label above each page, so it'll be easy to know where you are, and also divide any text which is longer than one book into as many books as are necessary with a header indicating each new book. Lastly, the program will display the number of books required, and, based on that number, will calculate the necessary materials for making that many books, and how many chests will be necessary to contain them. Obviously, I doubt that most text that the program handles will require more than a couple of books, but I'm planning on doing Lovecraft's "The Dunwich Horror" as a test, and the first book in what will undoubtedly be an epic library, so it might take quite a few books.

//end of rambling background post.

The majority of the program is quite simple, and I don't need help with the looping or heading writing or anything. But there is one consideration I'd like to ask for some input on. I want to bring in 256 characters at a time of the text file I give the program, but I don't want to cut any words in half (or in any other fraction :p).

So what I need is a method to somehow ensure that the last word being brought in isn't cut off by the limit of 256 characters. If there is a word that will be cut off, I want to just stop at the last whole word, saving the word that would have been cut off for the beginning of the next page.

Currently, my idea is to use a cstring (an array of characters) with a size of 256 as a "holder" for each page. This will get loaded with 256 characters, then get written to the output file. One method I've thought of to check if a word has been cut off is to check the last space in the array to ensure that it's the space character. If not, then I would go backward through the array until I found the previous space (indicating the end of the last full word). Then I guess I'd run a loop to replace those last several characters with spaces.

Actually, having written this, I think this method will work well. Still, anyone with a good idea on how to prevent the aforementioned word cutoff issue should let me know. I know there's some good programmers on this forum, so I figured you guys would have some insight.

Also, when it's finished, which should be tonight after I finish up some homework, I'd be more than happy to post it here for anyone who would like to use such a utility (unless that violates the forum rules somehow, but I don't see why it would.)

Looking forward to any responses/insight!
Last edited by agentwiggles on Fri Nov 09, 2012 1:24 am, edited 2 times in total.
LiveInPain
Posts: 19
Joined: Sat Nov 03, 2012 5:07 pm
Location: Portugal

Re: Question for programmers - small C++ project I'm working

Post by LiveInPain »

I don't know much about C++ but i would do it very similar to you but i would write word by word into the file and use array of char to hold the words temporary and only write them if there it would not pass the 256 char limit, but this is just another way to solve the problem i really dont know if it would be more efficient or easy it's just the way i would do it. (i dont know if i made myself clear but english is not my main language).
Image
hawkcannon
Posts: 22
Joined: Mon Nov 14, 2011 10:04 pm

Re: Question for programmers - small C++ project I'm working

Post by hawkcannon »

There's one problem with your solution: it would cause an infinite loop at the end of the text file, assuming it doesn't end with a space. You could check whether the last character is a space or a punctuation mark to fix that, but LiveInPain's solution would be more flexible.
User avatar
agentwiggles
Posts: 188
Joined: Fri Apr 06, 2012 2:31 pm

Re: Question for programmers - small C++ project I'm working

Post by agentwiggles »

After a bit of messing with c-strings I realized that I dislike them, and decided that simply using c++ strings was the easiest option.

Program works like a charm. Here's a sample of output,
Spoiler
Show

Code: Select all

//*******************//
//     Book 1        //
//*******************//

*** Page 1 ***
I. The Horror In Clay The most merciful thing in the world, I think, is the inability of the human mind to correlate all its contents. We live on a placid island of ignorance in the midst of black seas of infinity, and it was not meant that we should 

*** Page 2 ***
voyage far. The sciences, each straining in its own direction, have hitherto harmed us little; but some day the piecing together of dissociated knowledge will open up such terrifying vistas of reality, and of our frightful position therein, that we shall 

*** Page 3 ***
either go mad from the revelation or flee from the light into the peace and safety of a new dark age. Theosophists have guessed at the awesome grandeur of the cosmic cycle wherein our world and human race form transient incidents. They have hinted at 
And a sample of the information provided at the end:
Spoiler
Show

Code: Select all

//*********************************//
//         Materials needed        //
//*********************************//
     Books        : 6
     Chests       : 1
     Leather      : 6
     Paper        : 18
     Ink Sacs     : 6
     Feathers     : 6
     Wood(Chests) : 8
Input for this run was the entire text of "Call of Cthulhu." I'm pretty pleased with the program.

Link for anyone interested is here
Last edited by agentwiggles on Tue Nov 13, 2012 5:01 am, edited 1 time in total.
User avatar
darahalian
Posts: 578
Joined: Mon Jul 04, 2011 9:57 pm

Re: Small program to format text for copy/pasting into books

Post by darahalian »

Nice job! Being a CS major myself, I totally understand the joy of making a cool little program like this.
FlowerChild wrote:Remain ever vigilant against the groth menace my friends. Early detection is crucial in avoiding a full-blown groth epidemic.
User avatar
Detritus
Posts: 398
Joined: Thu Aug 23, 2012 3:00 am
Location: Oz

Re: Small program to format text for copy/pasting into books

Post by Detritus »

Wow, seeing this inspired me. I found the first text file on my computer, which was a story I am writing, and copied the prologue in. It's 23 pages long (In Minecraft books), and fits into one book. Did you say how many pages fit into a book?
All parts should go together without forcing. You must remember that the parts you are reassembling were disassembled by you. Therefore, if you can't get them together again, there must be a reason. By all means, do not use a hammer.
User avatar
Azdoine
Posts: 270
Joined: Thu Apr 05, 2012 11:50 pm

Re: Small program to format text for copy/pasting into books

Post by Azdoine »

Wow. That's pretty fucking cool.
Thanks man :)

EDIT: Huh, this phrase won't copy/paste properly, but your program formatted it for me:

shall never know; for my slumber, though troubled and dream-infested, was continuous. When at last I awaked, it was to discover myself half sucked into a slimy expanse of hellish black mire which extended about me in monotonous undulations as far as I

Happily enough, the H.P.L. short story Dagon fits exactly into one book, which is pretty cool.
User avatar
StarsintheSky
Posts: 41
Joined: Tue Aug 28, 2012 3:45 pm

Re: Small program to format text for copy/pasting into books

Post by StarsintheSky »

Thanks for putting this together and offering it to the community!
IGN: Beschaulichkeit
User avatar
agentwiggles
Posts: 188
Joined: Fri Apr 06, 2012 2:31 pm

Re: Small program to format text for copy/pasting into books

Post by agentwiggles »

Detritus wrote:Wow, seeing this inspired me. I found the first text file on my computer, which was a story I am writing, and copied the prologue in. It's 23 pages long (In Minecraft books), and fits into one book. Did you say how many pages fit into a book?
Yeah, the output is divided into books and pages, so if there's more than one book's worth of pages (i.e. 50 pages) then the page counter will reset and it will start a new book.

So output looks like this:
Book 1
p.1
p.2
...
p.50
Book 2
p.1
... and so on.
Azdoine wrote:Huh, this phrase won't copy/paste properly, but your program formatted it for me:
/snip
I scratched my head over this for a while, but I think it has something to do with the way MC handles new lines in the books. It won't let you start a line with a space or a newline character iirc, so I'm guessing that problem can only really be solved by pasting that particular page in chunks. The phrase you posted is only 252 characters long so this one's not the program's fault as far as I can tell.


Also, thanks to everyone for the encouragement! This is one of the first programs I've built just for myself (as in not for a school assignment), and it's really very cool to see the stuff I've been learning coalesce into something that makes my life easier!
Ancilangeli
Posts: 80
Joined: Sun Jul 22, 2012 12:36 am

Re: Small program to format text for copy/pasting into books

Post by Ancilangeli »

thank you oh so fucking much you have no idea.
that is all
how to take a no like a sir
Spoiler
Show
a forum user after being told they couldnt do something wrote:Alright, that's all I wanted to know, thank you very much.

I think that this closes the issue. Thread can be locked.
User avatar
Andellmere
Posts: 174
Joined: Wed Sep 21, 2011 5:39 pm

Re: Small program to format text for copy/pasting into books

Post by Andellmere »

So, I downloaded this with no real intention to use it, because the only time I use books is on servers where I send 'reports' to whoever is admining so they know what I'm up to while they're off doing whatever as well as what I feel we need to complete next.

I figured I'd both stress test it for you and satisfy my own curiousity:

//*********************************//
// Materials needed //
//*********************************//
Books : 254
Chests : 9
Leather : 254
Paper : 762
Ink Sacs : 254
Feathers : 254
Wood(Chests) : 72

I give you the stats for War and Peace by Leo Tolstoy, if done in the style of minecraft.
May I just say, those are some scary numbers. And now that I know how it works(I thought I was too smart for README's and deleted the endtag...) I may convert a few of my favorite books/stories to minecraft books.
Do you perhaps know where the book info is stored? And if it can be easily accessed?
Need a combination door? I've got one you can use.
User avatar
agentwiggles
Posts: 188
Joined: Fri Apr 06, 2012 2:31 pm

Re: Small program to format text for copy/pasting into books

Post by agentwiggles »

Not too sure how the info is stored, but I don't think that it's easily accessible, so unfortunately I don't think you can add it to the game without actually putting into a book in-game.

Nice test, too - the primary reason I put the calculation feature in was to get numbers on big projects like that. But my chest calculations must be wrong (had to do some weirdness there), since 254 books would require 10 chests. I'll have to go over that code when I get a chance.

And yeah, that ENDFILE thing is a bit sketchy. File input is a little funky, and that was the easiest way I could come up with to make the whole deal work. That's another thing I'd like to improve on, but other methods of detecting the end of a file have some weird issues because of the way that the program divides text into books and pages. Without using some kind of tag like this, I couldn't figure out how to stop the program from filling the unused portion of the final book with repeated occurrences of the final word. The other nice thing about the tag is that it lets me put a little readme thing in the input file itself.
Dr. Kylstein
Posts: 67
Joined: Thu Feb 09, 2012 8:05 pm

Re: Small program to format text for copy/pasting into books

Post by Dr. Kylstein »

The book text is stored with the item's NBT entry. If it is in the SSP inventory, it shows up there in the level.dat. It looks like CNBT may be what you would want if you go that way.
User avatar
agentwiggles
Posts: 188
Joined: Fri Apr 06, 2012 2:31 pm

Re: Small program to format text for copy/pasting into books

Post by agentwiggles »

I may look into the NBT entry thing. Might be a little over my head at this point, though.

I just updated the Dropbox link with a small bugfix, the chest counting code has been simplified and corrected, so the chest count displayed at the end of the program should now work fine.

I've been thinking about the possibility of doing a small add-on mod for BTW implementing some features related to books and the construction of libraries. Not sure what it would do exactly, some ideas include a printing press system/block by which you could duplicate books and functionality for bookshelves to store books, which is something I'd REALLY like to have for my library project. (Why they don't already do this is beyond me, it just makes so much sense.) VERY VERY tentative right now, as I have little experience with Java and game programming. But it's an idea floating around in my head now so something might come of it.
User avatar
Azdoine
Posts: 270
Joined: Thu Apr 05, 2012 11:50 pm

Re: Small program to format text for copy/pasting into books

Post by Azdoine »

agentwiggles wrote: I've been thinking about the possibility of doing a small add-on mod for BTW implementing some features related to books and the construction of libraries. Not sure what it would do exactly, some ideas include a printing press system/block by which you could duplicate books and functionality for bookshelves to store books, which is something I'd REALLY like to have for my library project. (Why they don't already do this is beyond me, it just makes so much sense.) VERY VERY tentative right now, as I have little experience with Java and game programming. But it's an idea floating around in my head now so something might come of it.
That sounds hella cool. Good luck if you do take up the endeavor :)
User avatar
Detritus
Posts: 398
Joined: Thu Aug 23, 2012 3:00 am
Location: Oz

Re: Small program to format text for copy/pasting into books

Post by Detritus »

Both of those ideas have been kicking around for a while now. The bookshelf storage one in particular is, I think, currently being worked on by someone in the add-ons section.
That doesn't mean that you couldn't do it yourself, in a different way, or with a printing press and other library-style integrations.
All parts should go together without forcing. You must remember that the parts you are reassembling were disassembled by you. Therefore, if you can't get them together again, there must be a reason. By all means, do not use a hammer.
Post Reply