Logo Search packages:      
Sourcecode: yapet version File versions  Download package

void CSVImport::import ( const char *  pw  )  throw (std::exception)

Do the import.

Does the import.

Parameters:
pw the password set on the destination file.

Definition at line 145 of file csvimport.cc.

References YAPET::PasswordRecord::comment, YAPET::COMMENT_SIZE, countSeparator(), dstfile, getSeparatorPos(), YAPET::PasswordRecord::host, YAPET::HOST_SIZE, logError(), YAPET::PasswordRecord::name, YAPET::NAME_SIZE, NUM_SEPARATORS, YAPET::PasswordRecord::password, YAPET::PASSWORD_SIZE, YAPET::File::save(), srcfile, YAPET::PasswordRecord::username, YAPET::USERNAME_SIZE, and verbose.

                                                    {
    std::ifstream csvfile(srcfile.c_str());

    if (!csvfile)
      throw std::runtime_error("Cannot open " + srcfile);

    // the max line length. Computed from the field sizes of a YAPET password
    // record.
    const int max_len = YAPET::NAME_SIZE +
      YAPET::HOST_SIZE +
      YAPET::USERNAME_SIZE +
      YAPET::PASSWORD_SIZE +
      YAPET::COMMENT_SIZE +
      // for the separators
      NUM_SEPARATORS;

    // used for logging purpose
    unsigned int num_fields = NUM_SEPARATORS + 1;
    char num_fields_str[5];
    snprintf(num_fields_str, 5, "%u", num_fields);

    YAPET::Key key(pw);
    YAPET::File yapetfile(dstfile, key, true);
    std::list<YAPET::PartDec> list;

    char line[max_len];
    std::vector<std::string::size_type> seppos;
    unsigned long lineno = 0;
    while (csvfile.getline(line, max_len) ) {
      lineno++;
      std::string l(line);
      if (countSeparator(l) > NUM_SEPARATORS) {
          std::string tmp("Too many fields. Expected ");
          tmp += num_fields_str;
          tmp += " fields.";
          logError(lineno, tmp );
          continue;
      }
      if (countSeparator(l) < NUM_SEPARATORS) {
          std::string tmp("Too few fields. Expected ");
          tmp += num_fields_str;
          tmp += " fields.";
          logError(lineno, tmp );
          continue;
      }

      getSeparatorPos(l, seppos);
      if (seppos.size() == 0) {
          logError(lineno, "Unable to identify separators.");
          continue;
      }

      std::string name = l.substr(0, seppos[0]);
      std::string host = l.substr(seppos[0]+1, seppos[1] - seppos[0] - 1);
      std::string uname = l.substr(seppos[1]+1, seppos[2] - seppos[1] - 1);
      std::string passwd = l.substr(seppos[2]+1, seppos[3] - seppos[2] - 1 );
      std::string comment = l.substr(seppos[3]+1, l.length() - seppos[3]);
      YAPET::Record<YAPET::PasswordRecord> record;
      YAPET::PasswordRecord *ptr_rec = record;
      strncpy((char*)ptr_rec->name, name.c_str(), YAPET::NAME_SIZE);
      strncpy((char*)ptr_rec->host, host.c_str(), YAPET::HOST_SIZE);
      strncpy((char*)ptr_rec->username, uname.c_str(), YAPET::USERNAME_SIZE);
      strncpy((char*)ptr_rec->password, passwd.c_str(), YAPET::PASSWORD_SIZE);
      strncpy((char*)ptr_rec->comment, comment.c_str(), YAPET::COMMENT_SIZE);
      list.push_back(YAPET::PartDec(record, key));
      if (verbose) std::cout << ".";
    }
    if (verbose) std::cout << std::endl;
    yapetfile.save(list);
    csvfile.close();
}


Generated by  Doxygen 1.6.0   Back to index