POV-Ray : Newsgroups : povray.programming : Tool for converting ASCII text to POV-Ray string array Server Time
3 Nov 2024 07:31:12 EST (-0500)
  Tool for converting ASCII text to POV-Ray string array (Message 1 to 1 of 1)  
From: Jörg 'Yadgar' Bleimann
Subject: Tool for converting ASCII text to POV-Ray string array
Date: 18 Dec 2011 19:45:15
Message: <4eee891b@news.povray.org>
Hi(gh)!

I'm not sure whether I'm right in this group... but as it is not 
possible to read in text line-wise from a file using #read (unless it is 
specially formatted) I wrote a small C++ program which generates a 
string array definition from any given ASCII text file... feel free to 
use it!

// beginning of code

// ascii2povarray
// A utility to produce PoV-Ray string array definition code from any 
ASCII text file

http://www.khyberspace.de)


#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;



int main()
{

   ifstream ascii;
   string ascii_name;
   string line;
   unsigned int i;

   cout << "ASCII file name: ";
   cin >> ascii_name;

   ascii.open(ascii_name.c_str());

   if (!ascii)
   {
     cerr << ascii_name << " cannot be opened!\n";
     exit (-1);
   }


   char ch;
   vector<char> buffer;
   vector<string> code;
   unsigned int lines=0;


   while (ascii.get(ch))
   {
     buffer.push_back(ch);
     if (ch == '\n')
     {
       i=0;
       while (i<buffer.size()-1)
       {	
	// cout << "Hallo!" << endl;
	if (buffer.at(i)=='"')
	  line.append("\\\"");
	else
	  line.push_back(buffer.at(i));
	i++;
       }
       code.push_back(line);	
       buffer.clear();
       lines++;
     }
     line.clear();
   };


   ascii.close();

   ofstream povcode;
   string povcode_name;

   cout << "PoV-Ray script? ";
   cin >> povcode_name;

   povcode.open(povcode_name.c_str());

   if (!povcode)
   {
     cerr << povcode_name << " cannot be opened!\n";
     exit (-1);
   }

   povcode << "// INCLUDE FILE GENERATED WITH ASCII2POVARRAY" << endl;
   povcode << endl;

   povcode_name = povcode_name.erase(povcode_name.size()-4, 
povcode_name.size());

   povcode << "#declare " << povcode_name << "_array = array[" << lines 
<< "]" << endl;
   povcode << "{" << endl;

   for (unsigned int i=0; i<lines; i++)
   {
     if (i==lines-1)
       povcode << "  \"" << code[i] << "\"" << endl;
     else
       povcode << "  \"" << code[i] << "\"," << endl;
   }

   povcode << "};" << endl;

   return 0;
}

// end of code

See you in Khyberspace!

Yadgar


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.