コード例 #1
0
ファイル: restore.c プロジェクト: abgood/backup
int printBackupInfo(const char* bckpInfoPath) {
    
    int numberOfFiles = getNumOfLines(bckpInfoPath);
    char* infoLine;
    
    int i;
    for(i = 1; i <= numberOfFiles; i++) {
        
        printf("%d - ", i);
        
        if( ( infoLine = getLineAt(i, bckpInfoPath) ) == NULL) {
            printf("Error reading line from %s\n", BACKUPINFO);
            free(infoLine);
            return -1;
        }
        
        char* fileName = getFileNameFromInfoLine(infoLine);
        char* backupDate = getBackupPathFromInfoLine(infoLine);
        char* datestr = backupDateToReadableDate(backupDate);
        
        printf("%s - backed up at %s\n", fileName, datestr);
        
        free(infoLine);
        free(fileName);
        free(backupDate);
        free(datestr);
    }
    
    printf("%d - Restore the whole folder\n", i);
    
    return 0;
}
コード例 #2
0
ファイル: gcode.cpp プロジェクト: Funny-DK/repsnapper
void GCode::updateWhereAtCursor(const vector<char> &E_letters)
{
  int line = buffer->get_insert()->get_iter().get_line();
  // Glib::RefPtr<Gtk::TextBuffer> buf = iter.get_buffer();
  if (line == 0) return;
  string text = getLineAt(buffer, line-1);
  Command commandbefore(text, Vector3d::ZERO, E_letters);
  Vector3d where = commandbefore.where;
  // complete position of previous line
  int l = line;
  while (l>0 && where.x()==0) {
    l--;
    text = getLineAt(buffer, l);
    where.x() = Command(text, Vector3d::ZERO, E_letters).where.x();
  }
  l = line;
  while (l>0 && where.y()==0) {
    l--;
    text = getLineAt(buffer, l);
    where.y() = Command(text, Vector3d::ZERO, E_letters).where.y();
  }
  l = line;
  // find last z pos fast
  if (buffer_zpos_lines.size()>0)
    for (uint i = buffer_zpos_lines.size()-1; i>0 ;i--)
      {
	if (int(buffer_zpos_lines[i]) <= l) {
	  text = getLineAt(buffer, buffer_zpos_lines[i]);
	  //cerr << text << endl;
	  Command c(text, Vector3d::ZERO, E_letters);
	  where.z() = c.where.z();
	  if (where.z()!=0) break;
	}
      }
  while (l>0 && where.z()==0) {
    l--;
    text = getLineAt(buffer, l);
    Command c(text, Vector3d::ZERO, E_letters);
    where.z() = c.where.z();
  }
  // current move:
  text = getLineAt(buffer, line);
  Command command(text, where, E_letters);
  Vector3d dwhere = command.where - where;
  where.z() -= 0.0000001;
  currentCursorWhere = where+dwhere;
  currentCursorCommand = command;
  currentCursorFrom = where;
}
コード例 #3
0
ファイル: gcode.cpp プロジェクト: Funny-DK/repsnapper
void GCode::MakeText(string &GcodeTxt,
		     const Settings &settings,
		     ViewProgress * progress)
{
  string GcodeStart = settings.GCode.getStartText();
  string GcodeLayer = settings.GCode.getLayerText();
  string GcodeEnd   = settings.GCode.getEndText();

	double lastE = -10;
	double lastF = 0; // last Feedrate (can be omitted when same)
	Vector3d pos(0,0,0);
	Vector3d LastPos(-10,-10,-10);
	std::stringstream oss;

	Glib::Date date;
	date.set_time_current();
	Glib::TimeVal time;
	time.assign_current_time();
	GcodeTxt += "; GCode by Repsnapper, "+
	  date.format_string("%a, %x") +
	  //time.as_iso8601() +
	  "\n";

	GcodeTxt += "\n; Startcode\n"+GcodeStart + "; End Startcode\n\n";

	layerchanges.clear();
	if (progress) progress->restart(_("Collecting GCode"), commands.size());
	int progress_steps=(int)(commands.size()/100);
	if (progress_steps==0) progress_steps=1;

	for (uint i = 0; i < commands.size(); i++) {
	  char E_letter;
	  if (settings.Slicing.UseTCommand) // use first extruder's code for all extuders
	    E_letter = settings.Extruders[0].GCLetter[0];
	  else
	    E_letter = settings.Extruders[commands[i].extruder_no].GCLetter[0];
	  if (progress && i%progress_steps==0 && !progress->update(i)) break;

	  if ( commands[i].Code == LAYERCHANGE ) {
	    layerchanges.push_back(i);
	    if (GcodeLayer.length()>0)
	      GcodeTxt += "\n; Layerchange GCode\n" + GcodeLayer +
		"; End Layerchange GCode\n\n";
	  }

	  if ( commands[i].where.z() < 0 )  {
	    cerr << i << " Z < 0 "  << commands[i].info() << endl;
	  }
	  else {
	    GcodeTxt += commands[i].GetGCodeText(LastPos, lastE, lastF,
						 settings.Slicing.RelativeEcode,
						 E_letter,
						 settings.Hardware.SpeedAlways) + "\n";
	  }
	}

	GcodeTxt += "\n; End GCode\n" + GcodeEnd + "\n";

	buffer->set_text (GcodeTxt);

	// save zpos line numbers for faster finding
	buffer_zpos_lines.clear();
	uint blines = buffer->get_line_count();
	for (uint i = 0; i < blines; i++) {
	  const string line = getLineAt(buffer, i);
	  if (line.find("Z") != string::npos ||
	      line.find("z") != string::npos)
	    buffer_zpos_lines.push_back(i);
	}

	if (progress) progress->stop();

}
コード例 #4
0
void GCode::MakeText(string &GcodeTxt, const string &GcodeStart, 
		     const string &GcodeLayer, const string &GcodeEnd,
		     bool RelativeEcode, 
		     ViewProgress * progress)
{
	double lastE = -10;
	double lastF = 0; // last Feedrate (can be omitted when same)
	Vector3d pos(0,0,0);
	Vector3d LastPos(-10,-10,-10);
	std::stringstream oss;

	Glib::Date date;
	date.set_time_current();
	Glib::TimeVal time;
	time.assign_current_time();
	GcodeTxt += "; GCode by Repsnapper, "+
	  date.format_string("%a, %x") +
	  //time.as_iso8601() +
	  "\n";

	GcodeTxt += "\n; Startcode\n"+GcodeStart + "; End Startcode\n\n";

	layerchanges.clear();

	progress->restart(_("Collecting GCode"),commands.size());
	int progress_steps=(int)(commands.size()/100);
	if (progress_steps==0) progress_steps=1;

	for (uint i = 0; i < commands.size(); i++) {
	  if (i%progress_steps==0) if (!progress->update(i)) break;

	  if ( commands[i].Code == LAYERCHANGE ) {
	    layerchanges.push_back(i);
	    if (GcodeLayer.length()>0)
	      GcodeTxt += "\n; Layerchange GCode\n" + GcodeLayer + 
		"; End Layerchange GCode\n\n"; 
	  }
	  
	  if ( commands[i].where.z() < 0 )  {
	    cerr << i << " Z < 0 "  << commands[i].info() << endl;
	  }
	  else {
	    GcodeTxt += commands[i].GetGCodeText(LastPos, lastE, lastF, RelativeEcode) + "\n";
	  }
	}

	GcodeTxt += "\n; End GCode\n" + GcodeEnd + "\n";

	buffer->set_text (GcodeTxt);

	// save zpos line numbers for faster finding
	buffer_zpos_lines.clear();
	uint blines = buffer->get_line_count();
	for (uint i = 0; i < blines; i++) {
	  const string line = getLineAt(buffer, i);
	  if (line.find("Z") != string::npos ||
	      line.find("z") != string::npos)
	    buffer_zpos_lines.push_back(i);
	}


	  // 	oss.str( "" );
	// 	switch(commands[i].Code)
	// 	{
	// 	case SELECTEXTRUDER:
	// 		oss  << "T0\n";
	// 		add_text_filter_nan(oss.str(), GcodeTxt);
	// 		//GcodeTxt += oss.str();
	// 		break;
	// 	case SETSPEED:
	// 		commands[i].where.z() = LastPos.z();
	// 		commands[i].e = lastE;
	// 	case ZMOVE:
	// 		commands[i].where.x() = LastPos.x();
	// 		commands[i].where.y() = LastPos.y();
	// 	case COORDINATEDMOTION:
	// 		if ((commands[i].where.x() != LastPos.x()) + 
	// 		    (commands[i].where.y() != LastPos.y()) +
	// 		    (commands[i].where.z() != LastPos.z()) != 0 &&
	// 		    AntioozeDistance != 0 && commands[i].e == lastE &&
	// 		    !Use3DGcode && AntioozeDistance != 0)
	// 		{
	// 			if (UseIncrementalEcode)
	// 			{
	// 				oss << "G1 E" << (lastE - AntioozeDistance) << "  F" << AntioozeSpeed << " ;antiooze retract\n";
	// 			}
	// 			else
	// 			{
	// 				oss << "G1 E" << -(AntioozeDistance) << "  F" << AntioozeSpeed << " ;antiooze retract\n";
	// 			}
	// 		}
	// 		oss  << "G1 ";
	// 		if(commands[i].where.x() != LastPos.x())
	// 			oss << "X" << commands[i].where.x() << " ";
	// 		if(commands[i].where.y() != LastPos.y())
	// 			oss << "Y" << commands[i].where.y() << " ";
	// 		if(commands[i].where.z() != LastPos.z())
	// 			oss << "Z" << commands[i].where.z() << " ";
	// 		if(commands[i].e != lastE)
	// 		{
	// 			if(UseIncrementalEcode)	// in incremental mode, the same is nothing
	// 				{
	// 				if(commands[i].e != lastE)
	// 					oss << "E" << commands[i].e << " ";
	// 				}
	// 			else
	// 				{
	// 				if(commands[i].e >= 0.0)
	// 					oss << "E" << commands[i].e << " ";
	// 				}
	// 		}
	// 		oss << "F" << commands[i].f;
	// 		if(commands[i].comment.length() != 0)
	// 			oss << " ;" << commands[i].comment << "\n";
	// 		else
	// 			oss <<  "\n";
	// 		if ((commands[i].where.x() != LastPos.x()) + 
	// 		    (commands[i].where.y() != LastPos.y()) +
	// 		    (commands[i].where.z() != LastPos.z()) != 0 &&
	// 		    AntioozeDistance != 0 &&
	// 		    commands[i].e == lastE  && 
	// 		    !Use3DGcode && AntioozeDistance != 0)
	// 		{
	// 			if (UseIncrementalEcode)
	// 			{
	// 				oss << "G1 E" << lastE << "  F" << AntioozeSpeed << " ;antiooze return\n";
	// 			}
	// 			else
	// 			{
	// 				oss << "G1 E" << AntioozeDistance << "  F" << AntioozeSpeed << " ;antiooze return\n";
	// 			}
	// 		}
	// 		add_text_filter_nan(oss.str(), GcodeTxt);
	// 		//GcodeTxt += oss.str();
	// 		if(commands[i].Code == ZMOVE && commands[i].where.z() != LastPos.z())
	// 		  add_text_filter_nan(GcodeLayer + "\n", GcodeTxt);
	// 		  //GcodeTxt += GcodeLayer + "\n";

	// 		LastPos = commands[i].where;
	// 		if( commands[i].e >= 0.0)
	// 			lastE = commands[i].e;
	// 		break;
	// 	case EXTRUDERON:
	// 	  // Dont switch extruder on/off right after eachother
	// 		if(i != 0 && commands[i-1].Code == EXTRUDEROFF) continue;
	// 		oss  << "M101\n";
	// 		add_text_filter_nan(oss.str(), GcodeTxt);
	// 		//GcodeTxt += oss.str();
	// 		break;
	// 	case EXTRUDEROFF:
	// 	  // Dont switch extruder on/off right after eachother
	// 		if(i != 0 && (i+1) < commands.size() && 
	// 		   commands[i+1].Code == EXTRUDERON) continue;	
	// 		// don't switch extruder off twize
	// 		if(i != 0 && (i+1) < commands.size() && 
	// 		   commands[i+1].Code == EXTRUDEROFF) continue;	
	// 		oss  << "M103\n";
	// 		add_text_filter_nan(oss.str(), GcodeTxt);
	// 		//GcodeTxt += oss.str();
	// 		break;
	// 	case COORDINATEDMOTION3D:
	// 		oss  << "G1 X" << commands[i].where.x() << " Y" << commands[i].where.y() << " Z" << commands[i].where.z();
	// 		oss << " F" << commands[i].f;
	// 		if(commands[i].comment.length() != 0)
	// 			oss << " ;" << commands[i].comment << "\n";
	// 		else
	// 			oss <<  "\n";
	// 		add_text_filter_nan(oss.str(), GcodeTxt);
	// 		//GcodeTxt += oss.str();
	// 		LastPos = commands[i].where;
	// 		break;
	// 	case RAPIDMOTION:
	// 		oss  << "G0 X" << commands[i].where.x() << " Y" << commands[i].where.y() << " Z" << commands[i].where.z()  << "\n";
	// 		add_text_filter_nan(oss.str(), GcodeTxt);
	// 		//GcodeTxt += oss.str();
	// 		LastPos = commands[i].where;
	// 		break;
	// 	case GOTO:
	// 		oss  << "G92";
	// 		if(commands[i].where.x() != LastPos.x() && commands[i].where.x() >= 0)
	// 		{
	// 			LastPos.x() = commands[i].where.x();
	// 			oss << " X" << commands[i].where.x();
	// 		}
	// 		if(commands[i].where.y() != LastPos.y() && commands[i].where.y() >= 0)
	// 		{
	// 			LastPos.y() = commands[i].where.y();
	// 			oss << " Y" << commands[i].where.y();
	// 		}
	// 		if(commands[i].where.z() != LastPos.z() && commands[i].where.z() >= 0)
	// 		{
	// 			LastPos.z() = commands[i].where.z();
	// 			oss << " Z" << commands[i].where.z();
	// 		}
	// 		if(commands[i].e != lastE && commands[i].e >= 0.0)
	// 		{
	// 			lastE = commands[i].e;
	// 			oss << " E" << commands[i].e;
	// 		}
	// 		oss <<  "\n";
	// 		add_text_filter_nan(oss.str(), GcodeTxt);
	// 		//GcodeTxt += oss.str();
	// 		break;
	// 	default:
	// 		break; // ignored CGCode
	// 	}
	// 	pos = commands[i].where;
	// cerr<< oss.str()<< endl;
	//}

}
コード例 #5
0
ファイル: restore.c プロジェクト: abgood/backup
int main (int argc, const char * argv[], char* envp[])
{
    
    // usage: rstr dir2 dir3
    if ( argc != 3 ) {
        fprintf ( stderr, "Usage: %s dir_backup dir_restore\n", argv[0] );
        exit(1);
    }
    
    char * pwd = getenv("PWD");
    DIR *backupDir;
    DIR *restoreDir;
    
    if ( ( backupDir = opendir(argv[1]) ) == NULL ) {
        free(backupDir);
        perror(argv[1]);
        exit(2);
    }
    
    if ( ( restoreDir = opendir(argv[2]) ) == NULL ) {
        // if restore dir doesnt exist, create it
        if(mkdir(argv[2], S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
            perror("Problem creating restore destination");
            exit(5);
        }
        if ( ( restoreDir = opendir(argv[2]) ) == NULL ) {
            perror(argv[2]);
            closedir(backupDir);
            free(backupDir);
            free(restoreDir);
            exit(3);
        }
    }
    
    printf("The following restore points are available:\n");
    // moving into backupDir will allow directory reading
    if(chdir(argv[1]) != 0) {
        perror(argv[1]);
        exit(8);
    }
    
    char** backups = getAndPrintFolders(backupDir);
    int numberOfBackups = getNumOfBackups(backupDir);
    
    char prompt[MAX_LEN];
    sprintf(prompt,"Which restore point? (0 to cancel)\n%s", PROMPT);
    int lineNumber = getChoice(prompt, numberOfBackups);
    //User sees numbers from 1 to numberOfBackups, but we want it from 0 to numberOfBackups-1 due to array access
    if(lineNumber == 0) {
        printf("Restore program ended.\n");
        freeStrArray(backups, numberOfBackups);
        return 0;
    }
    
    lineNumber = lineNumber-1;
    
    char* selectedBckpPath = getBackupFullPath(argv[1], backups[lineNumber]);
    char* fullBckpInfoPath = getBackupInfo(selectedBckpPath);
    
    // moving out of the backupDir
    if(chdir(pwd) != 0) {
        perror(pwd);
        exit(8);
    }
    
    DIR *selectedBackup;
    if ( ( selectedBackup = opendir(selectedBckpPath) ) == NULL ) {
        freeStrArray(backups, numberOfBackups);
        perror(selectedBckpPath);
        exit(2);
    }
    
    char * datestr = backupDateToReadableDate(backups[lineNumber]);
    printf("\n%s restore point chosen.\n", datestr);
    free(datestr);
    freeStrArray(backups, numberOfBackups);
    printf("This backup contains the following files:\n\n");
    printBackupInfo(fullBckpInfoPath);
    
    int numberOfFiles = getNumOfLines(fullBckpInfoPath);
    sprintf(prompt,"\nSelect a file to restore (0 to cancel):\n%s", PROMPT);
    lineNumber = getChoice(prompt, numberOfFiles  + 1);
    
    printf("\n");
    if ( lineNumber == numberOfFiles + 1 ) {
        
        printf("Doing full restore!\n");
        char* fileRestorePath;
        char* destFilePath;
        char* originFilePath;
        
        int i;
        for (i = 1 ; i <= getNumOfLines(fullBckpInfoPath); i++ ) {
            
            fileRestorePath = getLineAt(i, fullBckpInfoPath);
            char* fileName = getFileNameFromInfoLine(fileRestorePath);
            destFilePath = getFileFullPath(argv[2], fileName);
            originFilePath = getFileFullPath(argv[1], fileRestorePath);
            
            if(copyFile(originFilePath, destFilePath) == 0)
                printf("Restored %s successfully!\n", fileName);
            else
                printf("Error copying %s!\n", fileName);
            
            free(fileRestorePath);
            free(fileName);
            free(destFilePath);
            free(originFilePath);
        }
        
    } else if ( lineNumber > 0 && lineNumber <= numberOfFiles ) {
        char* fileRestorePath = getLineAt(lineNumber, fullBckpInfoPath);
        char* fileName = getFileNameFromInfoLine(fileRestorePath);
        char* destFilePath = getFileFullPath( argv[2], fileName );
        char* originFilePath = getFileFullPath( argv[1], fileRestorePath );
        if(copyFile(originFilePath, destFilePath) == 0)
            printf("Restored %s successfully!\n", fileName);
        else
            printf("Error copying %s!\n", fileName);

        free(fileRestorePath);
        free(fileName);
        free(destFilePath);
        free(originFilePath);
    }
        
    // clean up the strings
    free(selectedBckpPath);
    free(fullBckpInfoPath);    
    
    closedir(backupDir);
    closedir(restoreDir);
    closedir(selectedBackup);
    
    printf("\nRestore program ended.\n");
    return 0;
}