Subversion Repositories NedoOS

Rev

Rev 1925 | Rev 1963 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <oscalls.h>
  6. #include <osfs.h>
  7. #include <intrz80.h>
  8. #include <ctype.h>
  9. #include <tcp.h>
  10. #include <graphic.h>
  11. #include <terminal.c>
  12. struct fileStruct
  13. {
  14.   long picId;
  15.   unsigned int picYear;
  16.   unsigned long totalAmount;
  17.   unsigned char picRating[8];
  18.   unsigned char picName[256];
  19.   unsigned char picType[64];
  20.   unsigned char authorIds[64];
  21.   unsigned char authorTitle[64];
  22.   unsigned char authorRealName[64];
  23.   unsigned char afn[128];
  24.   unsigned char pfn[128];
  25.   unsigned char fileName[128];
  26. } curFileStruct;
  27. unsigned char ver[] = "2.1";
  28. unsigned char netbuf[2048];
  29. unsigned char picture[16384];
  30. unsigned char crlf[2] = {13, 10};
  31. unsigned long bytecount;
  32. unsigned char status, keypress, verbose, randomPic, slideShow;
  33. struct sockaddr_in targetadr;
  34. struct readstructure readStruct;
  35. unsigned long contLen;
  36. unsigned long count;
  37. unsigned int slideShowTime = 0;
  38.  
  39. void emptyKeyBuf(void)
  40. {
  41.   do
  42.   {
  43.   } while (_low_level_get() != 0);
  44. }
  45.  
  46. void printHelp(void)
  47. {
  48.   ATRIB(95);
  49.   printf("   GETPIC [%s] zxart.ee picture viewer for nedoNET\n\r", ver);
  50.   ATRIB(33);
  51.   ATRIB(40);
  52.   printf("-------------------------------------------------------\n\r");
  53.   printf(" Управление:\n\r");
  54.   printf(" 'ESC' - выход из программы;\n\r");
  55.   printf(" '<-' или 'B' к последним картинкам;\n\r");
  56.   printf(" '->' или 'Пробел' к более старым картинкам\n\r");
  57.   printf(" 'J' Прыжок на  указанную по счету картинку\n\r");
  58.   printf(" 'I' Просмотр экрана информации о картинках\n\r");
  59.   printf(" 'S' Сохранить картинку на диск в текущую папку\n\r");
  60.   printf(" 'V' не выводить информацию об авторах\n\r");
  61.   printf(" 'R' переход в режим  случайная картинка с рейтингом 4+\n\r");
  62.   printf(" 'A' переход в режим  слайл-шоу\n\r");
  63.   printf(" 'H' Данная справочная информация\n\r");
  64.   printf("-----------------Нажмите любую кнопку------------------\n\r");
  65.   ATRIB(93);
  66.   do
  67.   {
  68.     YIELD();
  69.     keypress = _low_level_get();
  70.   } while (keypress == 0);
  71.   emptyKeyBuf();
  72. }
  73. void delay(unsigned long counter)
  74. {
  75.   unsigned long start, finish;
  76.   counter = counter / 20;
  77.   if (counter < 1)
  78.   {
  79.     counter = 1;
  80.   }
  81.   start = time();
  82.   finish = start + counter;
  83.  
  84.   while (start < finish)
  85.   {
  86.     start = time();
  87.   }
  88. }
  89.  
  90. void errorPrint(unsigned int error)
  91. {
  92.   switch (error)
  93.   {
  94.   case 2:
  95.     printf("02 SHUT_RDWR\n\r");
  96.     break;
  97.   case 4:
  98.     printf("04 ERR_INTR\n\r");
  99.     break;
  100.   case 23:
  101.     printf("23 ERR_NFILE\n\r");
  102.     break;
  103.   case 35:
  104.     printf("35 ERR_EAGAIN or ERR_EWOULDBLOCK\n\r");
  105.     break;
  106.   case 37:
  107.     printf("37 ERR_ALREADY\n\r");
  108.     break;
  109.   case 38:
  110.     printf("38 ERR_NOTSOCK\n\r");
  111.     break;
  112.   case 40:
  113.     printf("40 ERR_EMSGSIZE\n\r");
  114.     break;
  115.   case 41:
  116.     printf("41 ERR_PROTOTYPE\n\r");
  117.     break;
  118.   case 47:
  119.     printf("47 ERR_AFNOSUPPORT\n\r");
  120.     break;
  121.   case 53:
  122.     printf("53 ERR_ECONNABORTED\n\r");
  123.     break;
  124.   case 54:
  125.     printf("54 ERR_CONNRESET\n\r");
  126.     break;
  127.   case 57:
  128.     printf("57 ERR_NOTCONN\n\r");
  129.     break;
  130.   case 65:
  131.     printf("65 ERR_HOSTUNREACH\n\r");
  132.     break;
  133.   default:
  134.     printf("%u UNKNOWN ERROR\n\r", error);
  135.     break;
  136.   }
  137.   do
  138.   {
  139.     YIELD();
  140.   } while (_low_level_get() == 0);
  141.   emptyKeyBuf();
  142. }
  143.  
  144. unsigned char OpenSock(unsigned char family, unsigned char protocol)
  145. {
  146.   unsigned char socket;
  147.   unsigned int todo;
  148.   todo = OS_NETSOCKET((family << 8) + protocol);
  149.   if (todo > 32767)
  150.   {
  151.     printf("OS_NETSOCKET: ");
  152.     errorPrint(todo & 255);
  153.     exit(0);
  154.   }
  155.   else
  156.   {
  157.     socket = ((todo & 65280) >> 8);
  158.     // printf ("OS_NETSOCKET: Socket #%d created\n\r", socket);
  159.   }
  160.   return socket;
  161. }
  162.  
  163. unsigned int netShutDown(unsigned char socket)
  164. {
  165.   unsigned int todo;
  166.   todo = OS_NETSHUTDOWN(socket);
  167.   if (todo > 32767)
  168.   {
  169.     printf("OS_NETSHUTDOWN: ");
  170.     errorPrint(todo & 255);
  171.     return 255;
  172.   }
  173.   else
  174.   {
  175.     // printf ("Socket #%u closed.\n\r", socket);
  176.   }
  177.   return 0;
  178. }
  179.  
  180. unsigned char netConnect(unsigned char socket)
  181. {
  182.   unsigned int todo, retry = 10;
  183.  
  184.   targetadr.family = AF_INET;
  185.   targetadr.porth = 00;
  186.   targetadr.portl = 80;
  187.   targetadr.b1 = 217;
  188.   targetadr.b2 = 146;
  189.   targetadr.b3 = 69;
  190.   targetadr.b4 = 13;
  191.   while (retry > 0)
  192.   {
  193.     todo = OS_NETCONNECT(socket, &targetadr);
  194.  
  195.     if (todo > 32767)
  196.     {
  197.       netShutDown(socket);
  198.       socket = OpenSock(AF_INET, SOCK_STREAM);
  199.       printf("OS_NETCONNECT ERROR [Retry:%u] [Pic:%lu]\n\r", retry, count);
  200.       delay(1000);
  201.       retry--;
  202.     }
  203.     else
  204.     {
  205.       // printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  206.       return 0;
  207.     }
  208.   }
  209.   printf("OS_NETCONNECT: ");
  210.   errorPrint(todo & 255);
  211.   exit(0);
  212.   return 0;
  213. }
  214.  
  215. unsigned int tcpSend(unsigned char socket, unsigned int messageadr, unsigned int size)
  216. {
  217.   unsigned char retry = 50;
  218.   unsigned int todo;
  219.   readStruct.socket = socket;
  220.   readStruct.BufAdr = messageadr;
  221.   readStruct.bufsize = size;
  222.   readStruct.protocol = SOCK_STREAM;
  223.  
  224. wizwrite:
  225.   todo = OS_WIZNETWRITE(&readStruct);
  226.   if (todo > 32767)
  227.   {
  228.  
  229.     if (retry == 0)
  230.     {
  231.       printf("OS_WIZNETWRITE: ");
  232.       errorPrint(todo & 255);
  233.       exit(0);
  234.     }
  235.     retry--;
  236.     YIELD();
  237.     goto wizwrite;
  238.   }
  239.   else
  240.   {
  241.     //  printf("OS_WIZNETWRITE: %u bytes written. \n\r", todo);
  242.   }
  243.   return todo;
  244. }
  245.  
  246. unsigned int tcpRead(unsigned char socket)
  247. {
  248.   unsigned char retry = 75;
  249.   unsigned int err, todo;
  250.  
  251.   readStruct.socket = socket;
  252.   readStruct.BufAdr = (unsigned int)&netbuf;
  253.   readStruct.bufsize = sizeof(netbuf);
  254.   readStruct.protocol = SOCK_STREAM;
  255. wizread:
  256.   todo = OS_WIZNETREAD(&readStruct);
  257.   if (todo > 32767)
  258.   {
  259.     if (retry == 0)
  260.     {
  261.       err = todo & 255;
  262.       printf("OS_WIZNETREAD: ");
  263.       errorPrint(err);
  264.       if (err == 35)
  265.       {
  266.         retry = 75;
  267.         return 0;
  268.       }
  269.       exit(0);
  270.     }
  271.     retry--;
  272.     // printf("OS_WIZNETREAD: %u \n\r", retry);
  273.     YIELD();
  274.     delay(100);
  275.     goto wizread;
  276.   }
  277.   // printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  278.   return todo;
  279. }
  280.  
  281. unsigned int cutHeader(unsigned int todo)
  282. {
  283.   unsigned int q, headlng;
  284.   unsigned char *count;
  285.   count = strstr(netbuf, "Content-Length:");
  286.   if (count == NULL)
  287.   {
  288.     printf("contLen  not found \r\n");
  289.     contLen = 0;
  290.   }
  291.   else
  292.   {
  293.     contLen = atol(count + 15);
  294.     bytecount = contLen;
  295.     // printf ("Dlinna  soderzhimogo = %lu \n\r", bytecount);
  296.   }
  297.  
  298.   count = strstr(netbuf, "\r\n\r\n");
  299.   if (count == NULL)
  300.   {
  301.     printf("header not found\r\n");
  302.   }
  303.   else
  304.   {
  305.     headlng = ((unsigned int)count - (unsigned int)netbuf + 4);
  306.     q = todo - headlng;
  307.     memcpy(&netbuf, count + 4, q);
  308.     // printf ("header removed. %u bytes\r\n", headlng);
  309.   }
  310.  
  311.   return q;
  312. }
  313.  
  314.  
  315.  
  316. char *str_replace(char *dst, int num, const char *str,
  317.                   const char *orig, const char *rep)
  318. {
  319.   const char *ptr;
  320.   size_t len1 = strlen(orig);
  321.   size_t len2 = strlen(rep);
  322.   char *tmp = dst;
  323.  
  324.   num -= 1;
  325.   while ((ptr = strstr(str, orig)) != NULL)
  326.   {
  327.     num -= (ptr - str) + len2;
  328.     if (num < 1)
  329.       break;
  330.  
  331.     strncpy(dst, str, (size_t)(ptr - str));
  332.     dst += ptr - str;
  333.     strncpy(dst, rep, len2);
  334.     dst += len2;
  335.     str = ptr + len1;
  336.   }
  337.  
  338.   for (; (*dst = *str) && (num > 0); --num)
  339.   {
  340.     ++dst;
  341.     ++str;
  342.   }
  343.   return tmp;
  344. }
  345.  
  346. void fillPicture(unsigned char socket)
  347. {
  348.   unsigned int todo, w, pPos, headskip;
  349.  
  350.   headskip = 0;
  351.   pPos = 0;
  352.   bytecount = 255;
  353.   while (1)
  354.   {
  355.     todo = tcpRead(socket);
  356.     if (todo == 0)
  357.     {
  358.       break;
  359.     }
  360.     if (headskip == 0)
  361.     {
  362.       headskip = 1;
  363.       todo = cutHeader(todo);
  364.     }
  365.  
  366.     if (pPos + todo > sizeof(picture))
  367.     {
  368.       printf("dataBuffer overrun... %u reached \n\r", pPos + todo);
  369.       break;
  370.     }
  371.  
  372.     for (w = 0; w < todo; w++)
  373.     {
  374.       picture[w + pPos] = netbuf[w];
  375.     }
  376.     bytecount = bytecount - todo;
  377.     pPos = pPos + todo;
  378.     if (bytecount == 0)
  379.     {
  380.       break;
  381.     }
  382.   }
  383.   netShutDown(socket);
  384. }
  385. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  386. {
  387.  
  388.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  389.   str_replace(pfn, tfnSize, pfn, "/", "_");
  390.   str_replace(pfn, tfnSize, pfn, ":", "_");
  391.   str_replace(pfn, tfnSize, pfn, "*", "_");
  392.   str_replace(pfn, tfnSize, pfn, "?", "_");
  393.   str_replace(pfn, tfnSize, pfn, "<", "_");
  394.   str_replace(pfn, tfnSize, pfn, ">", "_");
  395.   str_replace(pfn, tfnSize, pfn, "|", "_");
  396.   str_replace(pfn, tfnSize, pfn, " ", "_");
  397.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  398.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  399.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  400.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  401.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  402.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  403. }
  404.  
  405. void stringRepair(unsigned char *pfn, unsigned int tSize)
  406. {
  407.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  408.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  409.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  410.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  411.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  412.   str_replace(pfn, tSize, pfn, "\\/", "/");
  413. }
  414.  
  415. unsigned char getPic(unsigned long fileId)
  416. {
  417.   unsigned int todo;
  418.   unsigned char buffer[] = "0000000000";
  419.   unsigned char socket;
  420.   socket = OpenSock(AF_INET, SOCK_STREAM);
  421.   todo = netConnect(socket);
  422.   netbuf[0] = '\0';
  423.   sprintf(buffer, "%lu", fileId);
  424.   strcat(netbuf, "GET /file/id:");
  425.   strcat(netbuf, buffer);
  426.   strcat(netbuf, " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0");
  427.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  428.   fillPicture(socket);
  429.   return 0;
  430. }
  431. unsigned char savePic(unsigned long fileId)
  432. {
  433.   FILE *fp2;
  434.   unsigned char afnSize, tfnSize;
  435.   unsigned char fileIdChar[10];
  436.  
  437.   afnSize = sizeof(curFileStruct.afn) - 1;
  438.   tfnSize = sizeof(curFileStruct.pfn) - 1;
  439.  
  440.   strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  441.   nameRepair(curFileStruct.afn, afnSize);
  442.  
  443.   strcpy(curFileStruct.pfn, curFileStruct.picName);
  444.   nameRepair(curFileStruct.pfn, tfnSize);
  445.  
  446.   sprintf(curFileStruct.fileName, "%s-%s-%ld.scr", curFileStruct.afn, curFileStruct.pfn, fileId);
  447.   if (strlen(curFileStruct.fileName) > 62)
  448.   {
  449.     sprintf(fileIdChar, "-%ld", fileId);
  450.     str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  451.     curFileStruct.fileName[50] = '\0';
  452.     strcat(curFileStruct.fileName, fileIdChar);
  453.     strcat(curFileStruct.fileName, ".scr");
  454.   }
  455.   OS_SETSYSDRV();
  456.   OS_MKDIR("../downloads");        // Create if not exist
  457.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  458.   OS_CHDIR("../downloads/getpic");
  459.   fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  460.   if (((int)fp2) & 0xff)
  461.   {
  462.     printf(curFileStruct.fileName);
  463.     printf(" creating error\r\n");
  464.     getchar();
  465.     exit(0);
  466.   }
  467.   OS_WRITEHANDLE(picture, fp2, 6912);
  468.   OS_CLOSEHANDLE(fp2);
  469.   return 0;
  470. }
  471.  
  472. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  473. {
  474.   unsigned int i, j;
  475.   unsigned int lenC, lenS;
  476.  
  477.   for (lenC = 0; c[lenC]; lenC++)
  478.     ;
  479.   for (lenS = 0; s[lenS]; lenS++)
  480.     ;
  481.  
  482.   for (i = startPos; i <= lenS - lenC; i++)
  483.   {
  484.     for (j = 0; s[i + j] == c[j]; j++)
  485.       ;
  486.  
  487.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  488.       return i;
  489.     if (j == lenC)
  490.       if (n - 1)
  491.         n--;
  492.       else
  493.         return i;
  494.   }
  495.   return -1;
  496. }
  497.  
  498. const char *parseJson(unsigned char *property)
  499. {
  500.   unsigned int w, lng, lngp1, findEnd, listPos;
  501.   unsigned char terminator;
  502.   int n;
  503.   n = -1;
  504.   netbuf[0] = '\0';
  505.   n = pos(picture, property, 1, 0);
  506.   if (n == -1)
  507.   {
  508.     strcpy(netbuf, "Not Found");
  509.     // printf("Property %s not found", property);
  510.     return netbuf;
  511.   }
  512.   lng = n - 1 + strlen(property);
  513.   if (picture[lng] == ':')
  514.   {
  515.     terminator = '\0';
  516.   }
  517.   if (picture[lng] == '\"')
  518.   {
  519.     terminator = '\"';
  520.   }
  521.   if (picture[lng] == '[')
  522.   {
  523.     terminator = ']';
  524.   }
  525.  
  526.   findEnd = 1;
  527.   lngp1 = lng + 1;
  528.  
  529.   while (42)
  530.   {
  531.  
  532.     if ((picture[lngp1 + findEnd] == ','))
  533.     {
  534.       if (terminator == '\0')
  535.       {
  536.         break;
  537.       }
  538.       if ((picture[lng + findEnd] == terminator))
  539.       {
  540.         findEnd--;
  541.         break;
  542.       }
  543.     }
  544.     findEnd++;
  545.   }
  546.   listPos = 0;
  547.   for (w = lngp1; w < findEnd + lngp1; w++)
  548.   {
  549.     netbuf[listPos] = picture[w];
  550.     listPos++;
  551.   }
  552.   netbuf[listPos] = '\0';
  553.   return netbuf;
  554. }
  555. void convert866(void)
  556. {
  557.   unsigned int lng, targetPos, w, q = 0;
  558.   unsigned char buffer[8], one, two;
  559.   unsigned int decVal;
  560.   lng = strlen(netbuf);
  561.   targetPos = lng + 1;
  562.  
  563.   while (q < lng)
  564.   {
  565.     one = netbuf[q];
  566.     two = netbuf[q + 1];
  567.     if (one == 92 && two == 117)
  568.     {
  569.       q = q + 2;
  570.       for (w = 0; w < 4; w++)
  571.       {
  572.         buffer[w] = netbuf[q + w];
  573.       }
  574.       q = q + 4;
  575.       buffer[4] = '\0';
  576.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  577.  
  578.       if (decVal < 1088)
  579.       {
  580.         decVal = decVal - 912;
  581.       }
  582.       if (decVal > 1087)
  583.       {
  584.         decVal = decVal - 864;
  585.       }
  586.       if (decVal == 1025)
  587.       {
  588.         decVal = 240;
  589.       }
  590.       if (decVal == 1105)
  591.       {
  592.         decVal = 241;
  593.       }
  594.       netbuf[targetPos] = decVal;
  595.     }
  596.     else
  597.     {
  598.       netbuf[targetPos] = netbuf[q];
  599.       q++;
  600.     }
  601.     targetPos++;
  602.   }
  603.   netbuf[targetPos] = '\0';
  604.  
  605.   for (w = lng + 1; w < targetPos + 1; w++)
  606.   {
  607.     netbuf[w - lng - 1] = netbuf[w];
  608.   }
  609. }
  610.  
  611. unsigned long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  612. {
  613.   unsigned int retry, tSize;
  614.   unsigned int todo, pPos, headskip;
  615.   unsigned char buffer[] = "000000000";
  616.   unsigned char *count, socket;
  617.   unsigned char userAgent[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; GetPic)\r\n\r\n\0";
  618.   retry = 10;
  619.   netbuf[0] = '\0';
  620.   switch (queryNum)
  621.   {
  622.   case 0:
  623.     strcat(netbuf, "GET /api/export:zxPicture/filter:zxPictureType=standard/limit:");
  624.     sprintf(buffer, "%u", limit);
  625.     strcat(netbuf, buffer);
  626.     strcat(netbuf, "/start:");
  627.     sprintf(buffer, "%lu", startPos);
  628.     strcat(netbuf, buffer);
  629.     strcat(netbuf, "/order:date,desc");
  630.     strcat(netbuf, userAgent);
  631.     break;
  632.  
  633.   case 1:
  634.     strcat(netbuf, "GET /api/types:zxPicture/export:zxPicture/language:eng/start:0/limit:1/order:rand/filter:zxPictureMinRating=4;zxPictureType=standard");
  635.     strcat(netbuf, userAgent);
  636.     break;
  637.  
  638.   case 3: // /api/export:author/filter:authorId=2202
  639.     strcat(netbuf, "GET /api/export:author/filter:authorId=");
  640.     sprintf(buffer, "%lu", startPos);
  641.     strcat(netbuf, buffer);
  642.     strcat(netbuf, userAgent);
  643.     break;
  644.  
  645.   case 99: // GET /jsonElementData/elementId:182798
  646.     strcpy(netbuf, "GET /jsonElementData/elementId:");
  647.     sprintf(buffer, "%lu", startPos);
  648.     strcat(netbuf, buffer);
  649.     strcat(netbuf, userAgent);
  650.     break;
  651.   }
  652.  
  653. rejson:
  654.   socket = OpenSock(AF_INET, SOCK_STREAM);
  655.   netConnect(socket);
  656.  
  657.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  658.  
  659.   headskip = 0;
  660.   pPos = 0;
  661.   fillPicture(socket);
  662.  
  663.   count = strstr(picture, "responseStatus\":\"success");
  664.   if (count == NULL)
  665.   {
  666.     ATRIB(91);
  667.     printf("BAD JSON, NO responseStatus: success. %u   \r\n", retry);
  668.     retry--;
  669.     YIELD();
  670.     if (retry > 0)
  671.       goto rejson;
  672.     return -1;
  673.   }
  674.  
  675.   count = strstr(picture, "\"id\":");
  676.   if (count == NULL)
  677.   {
  678.     ATRIB(91);
  679.     printf("BAD JSON: ID not found.\r\n");
  680.     return -2;
  681.   }
  682.  
  683.   netbuf[0] = '\0';
  684.   if (queryNum < 3)
  685.   {
  686.     parseJson("\"id\":");
  687.     curFileStruct.picId = atol(netbuf);
  688.     parseJson(",\"title\":\"");
  689.     convert866();
  690.     strcpy(curFileStruct.picName, netbuf);
  691.  
  692.     tSize = sizeof(curFileStruct.picName);
  693.     stringRepair(curFileStruct.picName, tSize);
  694.  
  695.     parseJson(",\"type\":\"");
  696.     strcpy(curFileStruct.picType, netbuf);
  697.     parseJson("\"rating\":\"");
  698.     strcpy(curFileStruct.picRating, netbuf);
  699.     parseJson("\"year\":\"");
  700.     curFileStruct.picYear = atoi(netbuf);
  701.     parseJson("\"totalAmount\":");
  702.     curFileStruct.totalAmount = atol(netbuf);
  703.     parseJson("\"authorIds\":[");
  704.     strcpy(curFileStruct.authorIds, netbuf);
  705.   }
  706.   if (queryNum == 99)
  707.   {
  708.     parseJson(",\"title\":\"");
  709.     convert866();
  710.     strcpy(curFileStruct.authorTitle, netbuf);
  711.     parseJson(",\"realName\":\"");
  712.     convert866();
  713.     strcpy(curFileStruct.authorRealName, netbuf);
  714.   }
  715.   return curFileStruct.picId;
  716. }
  717.  
  718. void printData(void)
  719. {
  720.   ATRIB(93);
  721.   printf(" #: ");
  722.   ATRIB(97);
  723.   printf("%lu", count);
  724.   ATRIB(93);
  725.   printf(" ID: ");
  726.   ATRIB(97);
  727.   printf("%lu ", curFileStruct.picId);
  728.   ATRIB(93);
  729.   printf(" Total Pics: ");
  730.   ATRIB(97);
  731.   printf("%lu \r\n", curFileStruct.totalAmount);
  732.   ATRIB(93);
  733.   printf(" Author: ");
  734.   ATRIB(96);
  735.   printf("%s\r\n", curFileStruct.authorTitle);
  736.   ATRIB(93);
  737.   printf(" TITLE: ");
  738.   ATRIB(95);
  739.   printf("%s\r\n", curFileStruct.picName);
  740.   ATRIB(93);
  741.   printf(" RATING: ");
  742.   ATRIB(97);
  743.   printf("%s", curFileStruct.picRating);
  744.   ATRIB(93);
  745.   printf(" YEAR: ");
  746.   ATRIB(97);
  747.   printf("%u", curFileStruct.picYear);
  748.   printf(" \r\n");
  749.   ATRIB(93);
  750.   printf(" AuthorsIDs ");
  751.   ATRIB(97);
  752.   printf("%s", curFileStruct.authorIds);
  753.   ATRIB(93);
  754.   printf(" Real name: ");
  755.   ATRIB(97);
  756.   printf("%s", curFileStruct.authorRealName);
  757.   printf(" \r\n");
  758.   ATRIB(96);
  759.   printf(" \r\n");
  760. }
  761. void safeKeys(unsigned char keypress)
  762. {
  763.   if (keypress == 27)
  764.   {
  765.     printf("Good bye...\r\n");
  766.     ATRIB(37);
  767.     ATRIB(40);
  768.     exit(0);
  769.   }
  770.  
  771.   if (keypress == 'j' || keypress == 'J')
  772.   {
  773.     printf("Jump to picture:");
  774.     scanf("%lu", &count);
  775.     if (count > curFileStruct.totalAmount - 1)
  776.     {
  777.       count = curFileStruct.totalAmount - 1;
  778.     }
  779.   }
  780.  
  781.   if (keypress == 'v' || keypress == 'V')
  782.   {
  783.     verbose = !verbose;
  784.  
  785.     if (verbose == 0)
  786.     {
  787.       BOX(1, 1, 80, 25, 40);
  788.       AT(1, 1);
  789.     }
  790.   }
  791.  
  792.   if (keypress == 'h' || keypress == 'H')
  793.   {
  794.     printHelp();
  795.   }
  796.  
  797.   if (keypress == 'r' || keypress == 'R')
  798.   {
  799.     randomPic = !randomPic;
  800.  
  801.     if (verbose == 1)
  802.     {
  803.       if (randomPic == 1)
  804.       {
  805.         printf("    Random mode enabled...\r\n");
  806.       }
  807.       else
  808.       {
  809.         printf("    Sequental mode enabled...\r\n");
  810.       }
  811.     }
  812.   }
  813.   if (keypress == 'a' || keypress == 'A')
  814.   {
  815.     slideShow = !slideShow;
  816.     if (slideShow == 1)
  817.     {
  818.       if (verbose == 1)
  819.         printf("    SlideShow mode enabled...\r\n\r\n");
  820.       slideShowTime = 250;
  821.     }
  822.     else
  823.     {
  824.       if (verbose == 1)
  825.         printf("    Manual mode enabled...\r\n\r\n");
  826.       slideShowTime = 0;
  827.     }
  828.   }
  829. }
  830.  
  831. C_task main(void)
  832. {
  833.   unsigned char errno;
  834.   unsigned long ipadress;
  835.   long iddqd, idkfa;
  836.   os_initstdio();
  837.  
  838.   count = 0;
  839.   verbose = 1;
  840.   randomPic = 0;
  841.   slideShow = 0;
  842.  
  843.   BOX(1, 1, 80, 25, 40);
  844.   AT(1, 1);
  845.   printHelp();
  846.   safeKeys(keypress);
  847.  
  848. start:
  849.   emptyKeyBuf();
  850.   switch (randomPic)
  851.   {
  852.   case 0:
  853.     iddqd = processJson(count, 1, 0);
  854.     break;
  855.   case 1:
  856.     iddqd = processJson(0, 1, 1);
  857.     break;
  858.   }
  859.  
  860.   if (iddqd < 0)
  861.   {
  862.     count++;
  863.     goto start;
  864.   }
  865.   idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  866.   if (idkfa < 0)
  867.   {
  868.     printf(" Cant parse curFileStruct.authorIds = %s \r\n\r\n", curFileStruct.authorIds);
  869.     count++;
  870.     goto start;
  871.   }
  872.  
  873.   if (verbose == 1)
  874.   {
  875.     printData();
  876.   }
  877.   else
  878.   {
  879.     // ATRIB(97);
  880.     // printf(" Getting picture...\r\n");
  881.   }
  882.   if (!strcmp(curFileStruct.picType, "standard"))
  883.  
  884.   {
  885.     errno = getPic(iddqd);
  886.  
  887.   review:
  888.     keypress = viewScreen6912((unsigned int)&picture, slideShowTime);
  889.     emptyKeyBuf();
  890.   }
  891.   else
  892.   {
  893.     printf("  >>Format %s not supported, skipped \n\r", curFileStruct.picType);
  894.     count++;
  895.     goto start;
  896.   }
  897.  
  898.   ///// Keys only for pictures
  899.   if (keypress == 's' || keypress == 'S')
  900.   {
  901.     savePic(iddqd);
  902.     if (verbose == 1)
  903.       printf("        ID:%lu    TITLE:%s  SAVED\r\n\r\n", curFileStruct.picId, curFileStruct.picName);
  904.     count++;
  905.   }
  906.  
  907.   if (keypress == 248 || keypress == 'b' || keypress == 'B')
  908.   {
  909.     if (count > 0)
  910.     {
  911.       count--;
  912.     }
  913.   }
  914.   if (keypress == 251 || keypress == 32)
  915.   {
  916.     count++;
  917.     goto start;
  918.   }
  919.   if (keypress == 'i' || keypress == 'I')
  920.   {
  921.     do
  922.     {
  923.       YIELD();
  924.     } while (_low_level_get() == 0);
  925.     emptyKeyBuf();
  926.     goto review;
  927.   }
  928.   safeKeys(keypress);
  929.   goto start;
  930. }
  931.