Example #1
0
int main() {
	char *buf1 = malloc(sizeof(char) * SIZE);
	char *buf2 = malloc(sizeof(char) * SIZE);

	int count1;
	int count2;


	count1 = input_number(SIZE, buf1);
	printf("count = %i\n", count1);
	count2 = input_number(SIZE, buf2);
	printf("count = %i\n", count2);


	output_number(count1, buf1);
	output_number(count2, buf2);

	char *buf1_reversed = reverse_number(count1, buf1);
	char *buf2_reversed = reverse_number(count2, buf2);
	char *res_diff = diff_number(count1, buf1_reversed, buf2_reversed);
	char *res = reverse_number(count1, res_diff);

	output_number(count1, res);

	free(buf1);
	free(buf2);
	free(buf1_reversed);
	free(buf2_reversed);
	free(res);
	free(res_diff);

	return 0;
}
Example #2
0
File: scr.c Project: anylonen/omega
int cinema_getnum_line (int line, char * prompt)
{
  int num;

  assert(0 == line || 1 == line);

  cinema_blank_line(line);
  cinema_print_line(line, prompt);
  num = (int)input_number(Cinema);

  return num;
}
Example #3
0
int incbin_file(char *name, int *id, int *swap, int *skip, int *read, struct macro_static **macro) {

    struct incbin_file_data *ifd;
    char *in_tmp, *n, *tmp_c;
    int file_size, q;
    FILE *f;


    /* create the full output file name */
    if (use_incdir == YES)
        tmp_c = ext_incdir;
    else
        tmp_c = include_dir;

    if (create_full_name(tmp_c, name) == FAILED)
        return FAILED;

    f = fopen(full_name, "rb");
    q = 0;

    if (f == NULL && (tmp_c == NULL || tmp_c[0] == 0)) {
        sprintf(emsg, "Error opening file \"%s\".\n", name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    /* if not found in ext_incdir silently try the include directory */
    if (f == NULL && use_incdir == YES) {
        if (create_full_name(include_dir, name) == FAILED)
            return FAILED;

        f = fopen(full_name, "rb");
        q = 0;

        if (f == NULL && (include_dir == NULL || include_dir[0] == 0)) {
            sprintf(emsg, "Error opening file \"%s\".\n", name);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* if failed try to find the file in the current directory */
    if (f == NULL) {
        fprintf(stderr, "%s:%d: ", get_file_name(active_file_info_last->filename_id), active_file_info_last->line_current);
        fprintf(stderr, "INCBIN_FILE: Could not open \"%s\", trying \"%s\"... ", full_name, name);
        f = fopen(name, "rb");
        q = 1;
    }

    if (f == NULL) {
        fprintf(stderr, "not found.\n");
        sprintf(emsg, "Error opening file \"%s\".\n", full_name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    if (q == 1) {
        fprintf(stderr, "found.\n");
        strcpy(full_name, name);
    }

    fseek(f, 0, SEEK_END);
    file_size = ftell(f);
    fseek(f, 0, SEEK_SET);

    ifd = (struct incbin_file_data *)malloc(sizeof(struct incbin_file_data));
    n = malloc(sizeof(char) * (strlen(full_name)+1));
    in_tmp = (char *)malloc(sizeof(char) * file_size);
    if (ifd == NULL || n == NULL || in_tmp == NULL) {
        if (ifd != NULL)
            free(ifd);
        if (n != NULL)
            free(n);
        if (in_tmp != NULL)
            free(in_tmp);
        sprintf(emsg, "Out of memory while allocating data structure for \"%s\".\n", full_name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    /* read the whole file into a buffer */
    fread(in_tmp, 1, file_size, f);
    fclose(f);

    /* complete the structure */
    ifd->next = NULL;
    ifd->size = file_size;
    strcpy(n, full_name);
    ifd->name = n;
    ifd->data = in_tmp;

    /* find the index */
    q = 0;
    if (incbin_file_data_first != NULL) {
        ifd_tmp = incbin_file_data_first;
        while (ifd_tmp->next != NULL && strcmp(ifd_tmp->name, full_name) != 0) {
            ifd_tmp = ifd_tmp->next;
            q++;
        }
        if (ifd_tmp->next == NULL && strcmp(ifd_tmp->name, full_name) != 0) {
            ifd_tmp->next = ifd;
            q++;
        }
    }
    else
        incbin_file_data_first = ifd;

    *id = q;

    /* SKIP bytes? */
    if (compare_next_token("SKIP", 4) == FAILED)
        *skip = 0;
    else {
        skip_next_token();
        inz = input_number();
        if (inz != SUCCEEDED) {
            print_error(".INCBIN needs the amount of skipped bytes.\n", ERROR_DIR);
            return FAILED;
        }

        *skip = d;

        if (d >= file_size) {
            sprintf(emsg, "SKIP value (%d) is more than the size (%d) of file \"%s\".\n", d, file_size, full_name);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* READ bytes? */
    if (compare_next_token("READ", 4) == FAILED)
        *read = file_size - *skip;
    else {
        skip_next_token();
        inz = input_number();
        if (inz != SUCCEEDED) {
            print_error(".INCBIN needs the amount of bytes for reading.\n", ERROR_DIR);
            return FAILED;
        }

        *read = d;

        if (*skip + *read > file_size) {
            sprintf(emsg, "Overreading file \"%s\" by %d bytes.\n", full_name, *skip + *read - file_size);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* SWAP bytes? */
    if (compare_next_token("SWAP", 4) == FAILED)
        *swap = 0;
    else {
        if ((*read & 1) == 1) {
            sprintf(emsg, "The read size of file \"%s\" is odd (%d)! Cannot perform SWAP.\n", full_name, *read);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
        *swap = 1;
        skip_next_token();
    }

    /* FSIZE? */
    if (compare_next_token("FSIZE", 5) == SUCCEEDED) {
        skip_next_token();

        /* get the definition label */
        if (get_next_token() == FAILED)
            return FAILED;

        add_a_new_definition(tmp, (double)file_size, NULL, DEFINITION_TYPE_VALUE, 0);
    }

    /* FILTER? */
    if (compare_next_token("FILTER", 6) == SUCCEEDED) {
        skip_next_token();

        /* get the filter macro name */
        if (get_next_token() == FAILED)
            return FAILED;

        *macro = macro_get(tmp);

        if (*macro == NULL) {
            sprintf(emsg, "No MACRO \"%s\" defined.\n", tmp);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }
    else
        *macro = NULL;

    return SUCCEEDED;
}
Example #4
0
int main()
{
	char ascii_buffer[17];
   char fbyte, inchar;
   int i, j, k, pagenum, value, start, end;
   char linebuf[80], *p, *buf, ch;

   brdInit();
   sfspi_init();
	if (sf_init())
   {
   	printf("Flash init failed\n");
      exit(-1);
   }
   else
   {
   	printf("Flash init OK\n");
      printf("# of blocks: %d\n", sf_blocks);
      printf("size of block: %d\n\n", sf_blocksize);
   }
   print_command();

   while(1)
   {
   	inchar = getchar();
      if(inchar == 'c')
      {
         printf("page number to clear?");
         pagenum = input_number();
 			if(pagenum >= 0 && pagenum < sf_blocks)
         {
         	printf("\nClearing page %d\n", pagenum);
	         memset (flash_buf, 0, sf_blocksize);
	         sf_writeRAM(flash_buf, 0, sf_blocksize);
         	sf_RAMToPage(pagenum);
         }
         else
         {
         	printf("ERROR: invalid page number\n");
         }
      }  //end if(inchar =='c')
      else if((inchar == 'p') || (inchar == 'r'))
      {
         if (inchar == 'p') {
            // Use start for page to print
            printf("Page number to print out?");
         	start = input_number();
            // Check that it is a valid page
 		   	if(start < 0 || start >= sf_blocks) {
               printf("Invalid page number.\n\n");
               continue;
            }
            // Set single page range for 'p' command
            end = start;
         }
         else {
            printf("Starting page number to print out?");
         	start = input_number();
            // Check that it is a valid page
 		   	if(start < 0 || start >= sf_blocks) {
               printf("Invalid page number.\n\n");
               continue;
            }
            printf("\nEnding page number to print out?");
            end = input_number();
 		   	if(end < start || end >= sf_blocks) {
               printf("Invalid page number.\n\n");
               continue;
            }
         }
         // Loop through range of pages (range of 1 page for 'p' command)
         for (pagenum = start; pagenum <= end; pagenum++)
         {
	     		printf("\nPage %d", pagenum);
	  	    	sf_pageToRAM(pagenum);
	         sf_readRAM(flash_buf, 0, sf_blocksize);

            // Test if entire buffer filled with a single value
            buf = flash_buf;
            for (j = k = 0, ch = *buf; j < 512; j++) {
               if(ch != *buf++) {
                  k = 1;
                  break;
               }
            }
            // See if page is all the same value
            if (k) {
               printf("\n");  // No, drop through to print data
            }
            else {  // Yes, print out message instead
               printf("   ALL = 0x%02x\n", ch);
               continue;
            }
				k = (sf_blocksize & 0xFFF0) + ((sf_blocksize & 0x000F) ? 16 : 0);
				ascii_buffer[16] = 0;
	         for (j = 0, buf = flash_buf; j < k; j++)
            {
	         	if (j % 16 == 0)
               {
	         		p = linebuf;
	         		p += sprintf (p, "%04x: ", j);
	         	}
	         	fbyte = *buf++;

					if (j >= sf_blocksize)
               {
						p += sprintf (p, "   ");
						ascii_buffer[j % 16] = ' ';
					}
               else
               {
	               p += sprintf (p, "%02x ", fbyte);
	               ascii_buffer[j % 16] = isprint (fbyte) ? fbyte : '.';
					}
	         	if (j % 16 == 15)
               {
	         		printf ("%s   %s\n", linebuf, ascii_buffer);
	         	}
	         }
         }
      }  // end if((inchar =='p') || (inchar == 'r'))
      else if(inchar == 'f')
      {
         printf("page number to fill with specified value?  ");
         pagenum = input_number();
 			if(pagenum >= 0 && pagenum < sf_blocks)
         {
         	printf("\nPage %d\n", pagenum);
         	printf("enter fill value  "  );
         	value = input_number();
            printf("\nValue is %d dec is %02x hex", value,value);
         	printf("\nFilling page %d with value %02x hex\n", pagenum,value);
	         memset (flash_buf, value, sf_blocksize);
	         sf_writeRAM(flash_buf, 0, sf_blocksize);
         	sf_RAMToPage(pagenum);
         }
         else
         {
         	printf("ERROR: invalid page number\n");
         }
 		}	// end of if(inchar == 'f')
       else if(inchar == 't')
      {
         printf("page number in which to write text? ");
         pagenum = input_number();
 			if(pagenum >= 0 && pagenum < sf_blocks)
         {
         	printf("\nPage %d\n", pagenum);
         	printf("enter character string followed by RETURN  \n"  );
				gets (flash_buf);
            printf("Storing the following text ==> %s \n",flash_buf);
	         sf_writeRAM(flash_buf, 0, sf_blocksize);
         	sf_RAMToPage(pagenum);
         }
         else
         {
         	printf("ERROR: invalid page number\n");
         }
 		}	// end of if(inchar == 't')
      print_command();
   }	// end of while
}
Example #5
0
void edit_languages() {
  char s1[81];
  int i, i1, i2;

  bool done = false;
  do {
    out->Cls();
    nlx();
    for (i = 0; i < initinfo.num_languages; i++) {
      if (i && ((i % 23) == 0)) {
        pausescr();
      }
      Printf("%-2d. %-20s    %-50s\n", i + 1, languages[i].name, languages[i].dir);
    }
    nlx();
    out->SetColor(Scheme::PROMPT);
    Puts("Languages: M:odify, D:elete, I:nsert, Q:uit : ");
    out->SetColor(Scheme::NORMAL);
    char ch = onek("Q\033MID");
    switch (ch) {
    case 'Q':
    case '\033':
      done = true;
      break;
    case 'M':
      nlx();
      out->SetColor(Scheme::PROMPT);
      sprintf(s1, "Edit which (1-%d) ? ", initinfo.num_languages);
      Puts(s1);
      out->SetColor(Scheme::NORMAL);
      i = input_number(2);
      if ((i > 0) && (i <= initinfo.num_languages)) {
        edit_lang(i - 1);
      }
      break;
    case 'D':
      if (initinfo.num_languages > 1) {
        nlx();
        sprintf(s1, "Delete which (1-%d) ? ", initinfo.num_languages);
        out->SetColor(Scheme::PROMPT);
        Puts(s1);
        out->SetColor(Scheme::NORMAL);
        i = input_number(2);
        if ((i > 0) && (i <= initinfo.num_languages)) {
          nlx();
          out->SetColor(Scheme::ERROR_TEXT);
          Puts("Are you sure? ");
          out->SetColor(Scheme::NORMAL);
          ch = onek("YN\r");
          if (ch == 'Y') {
            initinfo.num_languages--;
            for (i1 = i - 1; i1 < initinfo.num_languages; i1++) {
              languages[i1] = languages[i1 + 1];
            }
            if (initinfo.num_languages == 1) {
              languages[0].num = 0;
            }
          }
        }
      } else {
        nlx();
        out->SetColor(Scheme::ERROR_TEXT);
        Printf("You must leave at least one language.\n");
        out->SetColor(Scheme::NORMAL);
        nlx();
        out->GetChar();
      }
      break;
    case 'I':
      if (initinfo.num_languages >= MAX_LANGUAGES) {
        out->SetColor(Scheme::ERROR_TEXT);
        Printf("Too many languages.\n");
        out->SetColor(Scheme::NORMAL);
        nlx();
        out->GetChar();
        break;
      }
      nlx();
      out->SetColor(Scheme::PROMPT);
      sprintf(s1, "Insert before which (1-%d) ? ", initinfo.num_languages + 1);
      Puts(s1);
      out->SetColor(Scheme::NORMAL);
      i = input_number(2);
      if ((i > 0) && (i <= initinfo.num_languages + 1)) {
        out->SetColor(Scheme::ERROR_TEXT);
        Puts("Are you sure? ");
        out->SetColor(Scheme::NORMAL);
        ch = onek("YN\r");
        if (ch == 'Y') {
          --i;
          for (i1 = initinfo.num_languages; i1 > i; i1--) {
            languages[i1] = languages[i1 - 1];
          }
          initinfo.num_languages++;
          strcpy(languages[i].name, "English");
          strncpy(languages[i].dir, syscfg.gfilesdir, sizeof(languages[i1].dir) - 1);
          strncpy(languages[i].mdir, syscfg.gfilesdir, sizeof(languages[i1].mdir) - 1);
          i2 = 0;
          for (i1 = 0; i1 < initinfo.num_languages; i1++) {
            if ((i != i1) && (languages[i1].num >= i2)) {
              i2 = languages[i1].num + 1;
            }
          }
          if (i2 >= 250) {
            for (i2 = 0; i2 < 255; i2++) {
              for (i1 = 0; i1 < initinfo.num_languages; i1++) {
                if ((i != i1) && (languages[i1].num == i2)) {
                  break;
                }
              }
              if (i1 >= initinfo.num_languages) {
                break;
              }
            }
          }
          languages[i].num = i2;
          edit_lang(i);
        }
      }
      break;
    }
  } while (!done);

  char szFileName[ MAX_PATH ];
  sprintf(szFileName, "%slanguage.dat", syscfg.datadir);
  unlink(szFileName);
  int hFile = open(szFileName, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  write(hFile, (void *)languages, initinfo.num_languages * sizeof(languagerec));
  close(hFile);
}
Example #6
0
 bool input() {
   return input_number() && input_tokens();
 }