/* * This file is a part of CMSLU -- Content Management System like Unix * and is not publicly distributed * * Copyright (c) 2009, Tomasz Sowa * All rights reserved. * */ #include "mountparser.h" #include "data.h" #include "log.h" bool MountParser::IsWhite(int c) { if( c==' ' || c=='\t' || c==13 ) return true; return false; } void MountParser::SkipWhite() { while( IsWhite(*pinput) ) ++pinput; } void MountParser::SkipLine() { while( *pinput && *pinput != 10 ) ++pinput; if( *pinput == 10 ) ++pinput; } void MountParser::ReadWord(std::string & res) { res.clear(); while( *pinput && *pinput!=10 && !IsWhite(*pinput) ) { res += *pinput; ++pinput; } } bool MountParser::ReadParamArg(int & out) { SkipWhite(); char * new_pos; long temp = strtol(pinput, &new_pos, 10); if( pinput == new_pos ) return false; pinput = new_pos; SkipWhite(); // here can we make a test whether the temp is greater from 'int' type out = (int)temp; return true; } void MountParser::ReadParamArgs(std::vector & args) { int arg; while( ReadParamArg(arg) ) { args.push_back(arg); log << log3 << "MP: mount param arg: " << arg << logend; if( *pinput == ',' ) ++pinput; } } void MountParser::ReadParam(std::string & res, std::vector & args) { SkipWhite(); res.clear(); args.clear(); while( *pinput && *pinput!=10 && *pinput!=',' && *pinput!='(' && !IsWhite(*pinput) ) { res += *pinput; ++pinput; } if( res.empty() ) return; // reading arguments SkipWhite(); if( *pinput == '(' ) { ++pinput; ReadParamArgs(args); if( *pinput != ')' ) { // there should be ')' at the end // temporarily we do nothing } else { ++pinput; } } if( *pinput == ',' ) ++pinput; } void MountParser::ReadMountType() { SkipWhite(); ReadWord(temp); if( temp.empty() ) { // an empty line (some white characters only) err = Error::empty; } else if( temp == "cms" ) { mount.type = Mount::cms; log << log3 << "MP: mount type: cms" << logend; } else if( temp == "thread" ) { mount.type = Mount::thread; log << log3 << "MP: mount type: thread" << logend; } else { err = Error::mount_unknown; log << log1 << "MP: unknown mount type: " << temp << logend; } } void MountParser::ReadMountPoint() { SkipWhite(); // !! narazie bez cudzyslowow ReadWord(temp); pdir = data.dirs.GetDir(temp); if( pdir ) { mount.dir_id = pdir->id; log << log3 << "MP: mount point: " << temp << logend; } else { err = Error::no_mountpoint; log << log1 << "MP: there is no such a mount point: " << temp << logend; } } void MountParser::ReadMountParams() { mount.ClearParams(); for( ReadParam(temp, param_args) ; !temp.empty() ; ReadParam(temp, param_args) ) { if( !mount.ParseStrParam(temp, param_args) ) { log << log1 << "MP: unknown mount param: " << temp << logend; err = Error::mount_no_param; return; } else { log << log3 << "MP: mount param: " << temp << logend; } } } void MountParser::ReadRow(std::map & output) { ReadMountType(); if( err == Error::empty ) { err = Error::ok; SkipLine(); return; } if( err == Error::ok ) ReadMountPoint(); if( err == Error::ok ) ReadMountParams(); if( err == Error::ok ) output.insert( std::make_pair(mount.dir_id, mount) ); SkipLine(); } Error MountParser::Parse(const std::string & input, std::map & output) { pinput = input.c_str(); err = Error::ok; output.clear(); while( *pinput && err == Error::ok ) ReadRow(output); return err; }