Есть примерно такой текст:
уточни задачу:
встречаются ли в "тексте" выражения типа А0..0, Б0..0, ...? какие лексемы там могут быть?
что служит разделителем записей?
я бы сделала подобную ***** на ANTLR. там очень просто сгенерить такой парсер. но насчёт скорости её работы - не знаю.
так на вскидку С++ stl и всё.
ну платформозависимо создавать дирректорию.
если чесно не пойму в чем интерес? - сравнить скорость результата на разных языках?
Можно написать на ruby
#!/bin/ruby
# -*- coding: utf-8 -*-
require 'fileutils'
require 'optparse'
require 'ostruct'
#параметры по умолчанию. Можно менять через параметры запуска. Справка вызывается стандартно --help
options = OpenStruct.new
options.file = "file.txt"
options.path = "/tmp/test_ruby/out"
OptionParser.new do |opts|
opts.banner = "Usage: parser.rb [options]"
opts.on("-f", "--file FILE", String, "path to file") do |t|
options.file = t
end
opts.on("-p", "--path DIR", String, "path to output dir") do |t|
options.path = t
end
end.parse!
def parseFile(file, path)
f = File.open(file)
f.each do |line|
dir, text = line.split(/\s+/)
next if dir.nil? or dir.empty?
fullPath = path + dir.gsub(//, '/')
FileUtils.makedirs(fullPath)
outFile = File.open(fullPath+"file", "w")
outFile.puts text
outFile.close
end
f.close
end
parseFile(options.file, options.path)
на перле гоняем цикл для прохода по строкам и регэкспом разделения строки на состовляющие.
Имеется ввиду не скорость работы, а скорость разработки...
С++ и STL
где-то 15 минут на написание, 5 минут на отладку, 5 минут на оформление поста ))
#include <map>
#include <algorithm>
#include <string>
void split(const char* pFile,const DWORD dwdFileLen)
{
const char* pCurr=pFile;
const char* pCurrEnd=pFile+dwdFileLen;
const char Sep[]="\r\n";
const char* pNext=0;
std::string currpath;
//1:путь , 2: текст файла
typedef std::map<std::string,std::string> td_dirslist;
td_dirslist _dirslist;
for(;pCurr<pCurrEnd;pCurr++)
{
pNext=std::find_first_of(pCurr,pCurrEnd,Sep,Sep+sizeof(Sep)-1);
currpath="./";
for(;pCurr<pNext;pCurr++)
{
if(*pCurr=='\r' || *pCurr=='\n')break;
if(*pCurr==' ')
{
_dirslist[currpath]=std::string(pCurr+1,pNext);
break;
}
currpath+=*pCurr;
currpath+="/";
}
pCurr=pNext;
while(pCurr<pCurrEnd && (*pCurr=='\r' || *pCurr=='\n'))
{
pCurr++;
}
pCurr--;
}
//сохранение
td_dirslist::iterator it=_dirslist.begin();
for(;it!=_dirslist.end();it++)
{
//создать каталог it->first
//...
//создать там файл file.txt с содержимым it->second
//...
}
}
const char* txt=
"А текст1\r\n"
"А0 текст2\r\n"
"А1 текст3\r\n"
"\r\n"
"А00 текст4\r\n"
"А01 текст5\r\n"
"\r\n"
"А000 текст6\r\n"
"А001 текст7\r\n"
"\r\n"
"Б текст8\r\n"
"Б0 текст9\r\n"
"\r\n"
"Б00 текстA\r\n"
"\r\n"
"Б000 текстB\r\n"
;
split(txt,strlen(txt)-1);
_dirslist ==
[0] ("./А/","текст1")
[1] ("./А/0/","текст2")
[2] ("./А/0/0/","текст4")
[3] ("./А/0/0/0/","текст6")
[4] ("./А/0/0/1/","текст7")
[5] ("./А/0/1/","текст5")
[6] ("./А/1/","текст3")
[7] ("./Б/","текст8")
[8] ("./Б/0/","текст9")
[9] ("./Б/0/0/","текстA")
[10] ("./Б/0/0/0/","текстB")
ViGOur,
требуются уточнения:
1. может ли быть последовательность в "перемешку" ?
# include <vector>
# include <string>
# include <algorithm>
# include <iostream>
# include <fstream>
# include <iterator>
typedef std::vector<char> chContainer;
typedef std::vector<char>::iterator chContainerIter;
typedef std::vector<char>::const_iterator chContainerConstIter;
void SSParser(const char* filename)
{
std::ifstream file(filename);
std::istreambuf_iterator<char> first(file.rdbuf());
std::istreambuf_iterator<char> eos;
chContainer chArray(first,eos);
std::string dir("");
const char space = ' ';
const char caret = '\n';
chContainerIter endChArray = chArray.end();
chContainerIter newBegin = chArray.begin();
chContainerIter spaceIter = std::find(newBegin, endChArray, space);
chContainerIter endlIter = std::find(newBegin, endChArray, caret);
while (spaceIter != endChArray) {
dir.assign(newBegin,spaceIter);
// создание директории dir ....
newBegin = endlIter+1;
std::ofstream ofile(...); // файл в только что созданной дирректории
std::copy(spaceIter+1,endlIter, ostreambuf_iterator(ofile));
spaceIter = std::find(newBegin,endChArray, space);
endlIter = std::find(newBegin,endChArray, caret);
}
}
Вот мой пример, на python'e:
import os
def workFunc(sz, text):
dir='./test'
for dr in sz:
print('Make dir:' + dr)
dir+='/'+dr
if os.path.exists(dir)!=True:os.mkdir(dir)
fl=open(dir+'/file', 'a')
fl.write(text)
fl.close()
file = open('d:/programming/test.txt')
lines=file.readlines()
file.close()
for n in lines:
line=n.split()
if len(line)==0:continue
workFunc(line[0], ' '.join(line[1:]))
Форум Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)